Skip to main content

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

Inspired by FastMCP from the official MCP Python SDK.

Constructor

prefix
str
default:"/mcp"
URL prefix for MCP endpoints
name
str
default:"agentor-mcp-server"
Server name returned in MCP initialize response
version
str
default:"0.1.0"
Server version
instructions
str
Instructions for using the server, sent to clients during initialization
website_url
str
Server website URL
icons
List[Icon]
Server icons (from mcp.types.Icon)
dependencies
List[Callable]
FastAPI dependencies to apply to all MCP endpoints

Decorators

@tool()

Register a tool that can be called by MCP clients.
name
str
Tool name. Defaults to function name.
description
str
Tool description shown to LLMs. Defaults to function docstring.
input_schema
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.
name
str
Prompt name. Defaults to function name.
description
str
Prompt description. Defaults to function docstring.
arguments
List[dict]
List of argument definitions. Auto-generated from function signature if not provided.Each argument dict should have:
  • name (str): Argument name
  • description (str): Argument description
  • required (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.
uri
str
required
Resource URI identifier. Used by clients to request the resource.
name
str
Resource display name. Defaults to URI.
description
str
Resource description. Defaults to function docstring.
mime_type
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.
method_name
str
required
JSON-RPC method name to handle

Context and Dependencies

Context

Access request-level data in tool functions.
headers
Dict[str, str]
HTTP headers from the request
cookies
Dict[str, str]
Cookies from the request

get_context()

Dependency function to retrieve the current request context.
Returns a 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.
Returns the token without the “Bearer ” prefix, or None if no token is found.

Methods

get_fastapi_router()

Get the underlying FastAPI router instance.
Returns an APIRouter instance that can be included in FastAPI applications.

Complete Example

See Also

Last modified on May 5, 2026