Skip to main content
Glama

Python MCP Cat Facts

by akream
cat_fact.py1.01 kB
from typing import Any import httpx import asyncio from mcp.server.fastmcp import FastMCP # Initialize FastMCP server mcp = FastMCP("catfacts") # Constants CAT_FACTS_API = "https://catfact.ninja/fact" USER_AGENT = "catfacts-app/1.0" async def get_cat_fact() -> dict[str, Any] | None: """Get a random cat fact from the API.""" headers = {"User-Agent": USER_AGENT} async with httpx.AsyncClient() as client: try: response = await client.get(CAT_FACTS_API, headers=headers, timeout=10.0) response.raise_for_status() return response.json() except Exception: return None @mcp.tool() async def single_cat_fact() -> str: """Get a single random cat fact.""" data = await get_cat_fact() if not data or "fact" not in data: return "Hi! Sorry, I couldn't fetch a cat fact right now." return f"Hi! {data['fact']}" if __name__ == "__main__": # Initialize and run the server mcp.run(transport="sse")

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/akream/mcppython'

If you have feedback or need assistance with the MCP directory API, please join our Discord server