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
str
default:"/mcp"
URL prefix for MCP endpoints
str
default:"agentor-mcp-server"
Server name returned in MCP initialize response
str
default:"0.1.0"
Server version
str
Instructions for using the server, sent to clients during initialization
str
Server website URL
List[Icon]
Server icons (from
mcp.types.Icon)List[Callable]
FastAPI dependencies to apply to all MCP endpoints
Decorators
@tool()
Register a tool that can be called by MCP clients.str
Tool name. Defaults to function name.
str
Tool description shown to LLMs. Defaults to function docstring.
dict
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.str
Prompt name. Defaults to function name.
str
Prompt description. Defaults to function docstring.
List[dict]
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.str
required
Resource URI identifier. Used by clients to request the resource.
str
Resource display name. Defaults to URI.
str
Resource description. Defaults to function docstring.
str
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.str
required
JSON-RPC method name to handle
Context and Dependencies
Context
Access request-level data in tool functions.Dict[str, str]
HTTP headers from the request
Dict[str, str]
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
