MCPAPIRouter
MCPAPIRouter provides a FastAPI-like decorator API for registering MCP tools, prompts, and resources. It handles JSON-RPC protocol communication and automatic schema generation.
Class Definition
Constructor
URL prefix for MCP endpoints
Server name returned in MCP initialize response
Server version
Instructions for using the server, sent to clients during initialization
Server website URL
Server icons (from
mcp.types.Icon)FastAPI dependencies to apply to all MCP endpoints
Decorators
@tool()
Register a tool that can be called by MCP clients.Tool name. Defaults to function name.
Tool description shown to LLMs. Defaults to function docstring.
JSON schema for tool parameters. Auto-generated from function signature if not provided.
Example: Basic Tool
Example: Tool with Dependencies
Example: Async Tool
@prompt()
Register a prompt template that can be retrieved by MCP clients.Prompt name. Defaults to function name.
Prompt description. Defaults to function docstring.
List of argument definitions. Auto-generated from function signature if not provided.Each argument dict should have:
name(str): Argument namedescription(str): Argument descriptionrequired(bool): Whether the argument is required
Example: Basic Prompt
Example: Prompt with Message Structure
@resource()
Register a resource that can be read by MCP clients.Resource URI identifier. Used by clients to request the resource.
Resource display name. Defaults to URI.
Resource description. Defaults to function docstring.
MIME type of resource content (e.g., “application/json”, “text/plain”)
Example: Basic Resource
Example: Dynamic Resource
@method()
Register a custom MCP JSON-RPC method handler.JSON-RPC method name to handle
Context and Dependencies
Context
Access request-level data in tool functions.HTTP headers from the request
Cookies from the request
get_context()
Dependency function to retrieve the current request context.Context object with headers and cookies. Returns empty context if called outside a request.
get_headers()
Get HTTP headers from the current request.get_cookies()
Get cookies from the current request.get_token()
Extract bearer token from Authorization header.None if no token is found.
Methods
get_fastapi_router()
Get the underlying FastAPI router instance.APIRouter instance that can be included in FastAPI applications.
Complete Example
See Also
- LiteMCP - Full ASGI server built on MCPAPIRouter
- CelestoMCPHub - Client for Celesto AI MCP Hub
