Litestar MCP¶
Documentation
Development
Litestar plugin for Model Context Protocol (MCP) integration¶
The Litestar MCP Plugin enables integration between Litestar web applications and the Model Context Protocol (MCP), allowing AI models to interact with your marked application routes through MCP Streamable HTTP and JSON-RPC.
Features¶
✨ Simple Integration: Mark routes with kwargs to expose them via MCP 🔧 Lightweight: Minimal configuration and dependencies 🚀 Protocol-Native: Uses MCP Streamable HTTP, JSON-RPC, and SSE directly 📊 OpenAPI Integration: Automatic OpenAPI schema exposure 🎯 Type Safe: Full type hints with dataclasses 🔐 Auth-Ready: Optional OAuth metadata and bearer-token validation hooks
Installation¶
pip install litestar-mcp
Quick Start¶
Add MCP capabilities to your Litestar application by marking routes:
from litestar import Litestar, get
from litestar_mcp import LitestarMCP
# Mark routes for MCP exposure using kwargs
@get("/users", mcp_tool="list_users")
async def get_users() -> list[dict]:
"""List all users - exposed as MCP tool."""
return [{"id": 1, "name": "Alice"}]
@get("/schema", mcp_resource="user_schema")
async def get_schema() -> dict:
"""User schema - exposed as MCP resource."""
return {"type": "object", "properties": {"id": "integer", "name": "string"}}
# Regular routes are not exposed to MCP
@get("/health")
async def health_check() -> dict:
return {"status": "ok"}
# Add MCP plugin
app = Litestar(
route_handlers=[get_users, get_schema, health_check],
plugins=[LitestarMCP()]
)
Your application now exposes MCP endpoints at /mcp plus well-known metadata documents that AI models can use to:
🔍 Discover marked routes via tools and resources
📊 Access your application's OpenAPI schema
🛠️ Execute marked tools and read marked resources
Core Concepts¶
- Model Context Protocol (MCP)
An open standard that enables AI models to securely access and interact with external systems.
- Tools (mcp_tool)
Functions that AI models can execute - mark routes with
mcp_tool="name"kwargs.- Resources (mcp_resource)
Read-only data that AI models can access - mark routes with
mcp_resource="name"kwargs.- Route Marking
Use
mcp_toolormcp_resourcekwargs in route decorators - Litestar automatically adds these to the route's opt dictionary.
How It Works¶
Mark Routes: Add
mcp_toolormcp_resourcekwargs to your route decoratorsLitestar Processing: Litestar automatically moves these kwargs into the route handler's
optdictionaryPlugin Discovery: The plugin scans route handlers' opt dictionaries for MCP markers at app startup
MCP Exposure: Marked routes become available through the MCP Streamable HTTP transport surface
AI Interaction: AI models can discover and interact with your marked routes
Kwargs to Opt Mechanism¶
Litestar automatically funnels unknown kwargs in route decorators into the
handler's opt dictionary. This is the recommended way to mark routes
for MCP — pass the metadata straight through to @get / @post / etc.
and let Litestar wire up handler.opt for you:
@get("/users", mcp_tool="list_users", mcp_description="List every user.")
async def get_users() -> list[dict]:
...
The plugin reads these MCP kwargs from handler.opt:
mcp_tool,mcp_resource— mark the handler as a tool / resourcemcp_resource_template— RFC 6570 URI template for templated resourcesmcp_description,mcp_resource_description— LLM-facing descriptionmcp_agent_instructions— rendered as## Instructionsmcp_when_to_use— rendered as## When to usemcp_returns— rendered as## Returns
Stacking @mcp_tool(...) / @mcp_resource(...) on top of the route
decorator is still supported (and is useful when you need output_schema,
annotations, scopes, or task_support — fields that do not have
opt-key equivalents), but the kwarg form is shorter and the right default
99% of the time.
Available Endpoints¶
Once configured, your application exposes:
/mcp- Streamable HTTP MCP endpoint for JSON-RPC and SSE/.well-known/mcp-server.json- MCP server manifest/.well-known/agent-card.json- Agent metadata document/.well-known/oauth-protected-resource- OAuth metadata when auth is enabled
What Makes This Different?¶
Route-Centric: Mark individual routes for MCP exposure using simple kwargs
Minimal Setup: Just add
mcp_toolormcp_resourcekwargs to existing route handlersProtocol-Native: Uses MCP Streamable HTTP and JSON-RPC directly
Litestar Native: Built specifically for Litestar applications using the opt mechanism
Getting Started¶
Check out the Getting Started guide to learn the basics, or explore the Usage Guide for deeper topics.
Community¶
Discord: Join the Litestar Discord
GitHub: litestar-org/litestar-mcp