litestar-mcp
A Litestar plugin that automatically exposes marked routes as MCP tools and resources over Streamable HTTP.
README
Litestar MCP Plugin
A lightweight plugin that integrates Litestar web applications with the Model Context Protocol (MCP) by exposing marked routes as MCP tools and resources over MCP Streamable HTTP and JSON-RPC.
Overview
This plugin automatically discovers Litestar routes marked for MCP and exposes them through an MCP-native transport surface. Pass mcp_tool="name" or mcp_resource="name" straight through to @get / @post / etc. — Litestar funnels unknown kwargs into handler.opt, so no second decorator or opt={...} wrapper is needed.
Features
- Protocol-Native Transport — MCP Streamable HTTP with JSON-RPC requests and SSE streams.
- Simple Route Marking — pass
mcp_tool/mcp_resourcekwargs straight through to Litestar's route decorators. - RFC 6570 URI Templates —
mcp_resource_template="app://…/{var}"dispatches concrete URIs to handlers with extracted vars. - First-Class Descriptions — structured
mcp_description,mcp_agent_instructions,mcp_when_to_use,mcp_returnskwargs. - Type Safe — full type hints with dataclasses;
msgspec-powered tool-argument validation. - Automatic Discovery — routes are discovered at app initialization.
- OpenAPI Integration — server info derived from OpenAPI config.
- OIDC Auth Baked In — bearer-token validation via
MCPAuthBackendor a composablecreate_oidc_validator()factory; injectableJWKSCacheprotocol for shared document caches. - Optional Task Support — experimental in-memory MCP task lifecycle endpoints.
Quick Start
Installation
pip install litestar-mcp
# or
uv add litestar-mcp
Basic Usage
from litestar import Litestar, get, post
from litestar.openapi.config import OpenAPIConfig
from litestar_mcp import LitestarMCP
# Mark routes for MCP exposure using the opt attribute
@get("/users", mcp_tool="list_users")
async def get_users() -> list[dict]:
"""List all users in the system."""
return [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]
@post("/analyze", mcp_tool="analyze_data")
async def analyze_data(data: dict) -> dict:
"""Analyze the provided data and return insights."""
return {"result": f"Analyzed {len(data)} items"}
@get("/config", mcp_resource="app_config")
async def get_app_config() -> dict:
"""Get the current application configuration."""
return {"debug": True, "version": "1.0.0"}
# Add the MCP plugin to your Litestar app
app = Litestar(
route_handlers=[get_users, analyze_data, get_app_config],
plugins=[LitestarMCP()],
openapi_config=OpenAPIConfig(title="My API", version="1.0.0"),
)
With Configuration
from litestar_mcp import LitestarMCP, MCPConfig
config = MCPConfig(
base_path="/api/mcp", # Change the base path
name="Custom Server Name", # Override server name
include_in_schema=True, # Include MCP routes in OpenAPI schema
)
app = Litestar(
route_handlers=[get_users, analyze_data, get_app_config],
plugins=[LitestarMCP(config)],
openapi_config=OpenAPIConfig(title="My API", version="1.0.0"),
)
Resources vs Tools: When to Use Each
Use Resources (mcp_resource) for
- Read-only data that AI models need to reference
- Static or semi-static information like documentation, schemas, configurations
- Data that doesn't require parameters to retrieve
- Reference material that AI models should "know about"
Examples:
@get("/schema", mcp_resource="database_schema")
async def get_schema() -> dict:
"""Database schema information."""
return {"tables": ["users", "orders"], "relationships": [...]}
@get("/docs", mcp_resource="api_docs")
async def get_documentation() -> dict:
"""API documentation and usage examples."""
return {"endpoints": [...], "examples": [...]}
Use Tools (mcp_tool) for
- Actions that perform operations or mutations
- Dynamic queries that need input parameters
- Operations that change state in your application
- Computations or data processing tasks
Examples:
@post("/users", mcp_tool="create_user")
async def create_user(user_data: dict) -> dict:
"""Create a new user account."""
# Perform user creation logic
return {"id": 123, "created": True}
@get("/search", mcp_tool="search_data")
async def search(query: str, limit: int = 10) -> dict:
"""Search through application data."""
# Perform search with parameters
return {"results": [...], "total": 42}
How It Works
- Route Discovery: At app initialization, the plugin scans all route handlers for the
optattribute - Automatic Exposure: Routes marked with
mcp_toolormcp_resourceare automatically exposed - MCP Transport: The plugin adds a Streamable HTTP MCP endpoint under the configured base path (default
/mcp) - Server Info: Server name and version are derived from your OpenAPI configuration
MCP Endpoints
Once configured, your application exposes these MCP-compatible endpoints:
GET /mcp- Server-Sent Events stream whenAccept: text/event-streamis providedPOST /mcp- JSON-RPC endpoint forinitialize,ping,tools/*,resources/*, and optional task methodsGET /.well-known/mcp-server.json- MCP server manifestGET /.well-known/agent-card.json- Agent card metadataGET /.well-known/oauth-protected-resource- OAuth protected resource metadata when auth is configured
Built-in Resources:
litestar://openapi- Your application's OpenAPI schema (always available viaresources/read)
Configuration
Configure the plugin using MCPConfig:
from litestar_mcp import MCPConfig
config = MCPConfig()
Configuration Options:
| Option | Type | Default | Description |
|---|---|---|---|
base_path |
str |
"/mcp" |
Base path for the MCP Streamable HTTP endpoint |
include_in_schema |
bool |
False |
Whether to include MCP routes in OpenAPI schema |
name |
str | None |
None |
Override server name. If None, uses OpenAPI title |
guards |
list[Any] | None |
None |
Litestar guards applied to the MCP router |
allowed_origins |
list[str] | None |
None |
Restrict accepted Origin header values |
include_operations |
list[str] | None |
None |
Only expose matching operation names |
exclude_operations |
list[str] | None |
None |
Exclude matching operation names |
include_tags |
list[str] | None |
None |
Only expose routes with matching OpenAPI tags |
exclude_tags |
list[str] | None |
None |
Exclude routes with matching OpenAPI tags |
auth |
MCPAuthConfig | None |
None |
Metadata for /.well-known/oauth-protected-resource discovery |
tasks |
bool | MCPTaskConfig |
False |
Enable experimental in-memory MCP task support |
Complete Example
from litestar import Litestar, get, post, delete
from litestar.openapi.config import OpenAPIConfig
from litestar_mcp import LitestarMCP, MCPConfig
# Resources - read-only reference data
@get("/users/schema", mcp_resource="user_schema")
async def get_user_schema() -> dict:
"""User data model schema."""
return {
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
"email": {"type": "string"}
}
}
@get("/api/info", mcp_resource="api_info")
async def get_api_info() -> dict:
"""API capabilities and information."""
return {
"version": "2.0.0",
"features": ["user_management", "data_analysis"],
"rate_limits": {"requests_per_minute": 1000}
}
# Tools - actionable operations
@get("/users", mcp_tool="list_users")
async def list_users(limit: int = 10) -> dict:
"""List users with optional limit."""
# Fetch users from database
return {"users": [{"id": 1, "name": "Alice"}], "total": 1}
@post("/users", mcp_tool="create_user")
async def create_user(user_data: dict) -> dict:
"""Create a new user account."""
# Create user logic
return {"id": 123, "created": True, "user": user_data}
@post("/analyze", mcp_tool="analyze_dataset")
async def analyze_dataset(config: dict) -> dict:
"""Analyze data with custom configuration."""
# Analysis logic
return {"insights": [...], "metrics": {...}}
# Regular routes (not exposed to MCP)
@get("/health")
async def health_check() -> dict:
return {"status": "healthy"}
# MCP configuration
mcp_config = MCPConfig(
name="User Management API",
base_path="/mcp"
)
# Create Litestar app
app = Litestar(
route_handlers=[
get_user_schema, get_api_info, # Resources
list_users, create_user, analyze_dataset, # Tools
health_check # Regular route
],
plugins=[LitestarMCP(mcp_config)],
openapi_config=OpenAPIConfig(
title="User Management API",
version="2.0.0"
),
)
Authentication
Authentication is a Litestar middleware concern. Apps with an existing auth
middleware get MCP authentication for free — request.user and request.auth
are populated before tool handlers run. Three integration paths:
Path A — Bring Your Own Middleware
If your Litestar app already ships an AbstractAuthenticationMiddleware (or
Litestar's built-in JWT backends), MCP inherits it automatically:
from litestar import Litestar
from litestar.middleware import DefineMiddleware
from litestar_mcp import LitestarMCP, MCPConfig
app = Litestar(
route_handlers=[...],
plugins=[LitestarMCP(MCPConfig())],
middleware=[DefineMiddleware(YourAuthMiddleware)], # MCP gets this for free
)
See docs/examples/notes/sqlspec/google_iap.py for a runnable example.
Path B — Built-in MCPAuthBackend
For OIDC workloads, install the built-in MCPAuthBackend:
from litestar import Litestar
from litestar.middleware import DefineMiddleware
from litestar_mcp import LitestarMCP, MCPAuthBackend, MCPConfig, OIDCProviderConfig
from litestar_mcp.auth import MCPAuthConfig
app = Litestar(
route_handlers=[...],
plugins=[LitestarMCP(MCPConfig(auth=MCPAuthConfig(
issuer="https://company.okta.com",
audience="api://mcp-tools",
)))],
middleware=[DefineMiddleware(
MCPAuthBackend,
providers=[OIDCProviderConfig(
issuer="https://company.okta.com",
audience="api://mcp-tools",
)],
user_resolver=lambda claims, app: MyUser(sub=claims["sub"]),
)],
)
JWKS auto-discovery, caching, and clock_skew tolerance are built in.
See docs/examples/notes/sqlspec/cloud_run_jwt.py for the full pattern.
Path C — Composable OIDC Factory
create_oidc_validator() returns an async callable for use as
MCPAuthBackend(token_validator=...) or inside your own middleware:
from litestar_mcp import create_oidc_validator
validator = create_oidc_validator(
"https://cloud.google.com/iap",
"/projects/PROJECT_NUMBER/global/backendServices/SERVICE_ID",
algorithms=("ES256",),
jwks_cache_ttl=1800,
)
Development
# Clone the repository
git clone https://github.com/litestar-org/litestar-mcp.git
cd litestar-mcp
# Install with development dependencies
uv sync --all-extras --dev
# Run tests
make test
# Run example
uv run python docs/examples/hello_world/main.py
License
MIT License. See LICENSE for details.
Contributing
Contributions welcome! Please see our contribution guide for details.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。