Litestar plugin for Model Context Protocol (MCP) integration
Project description
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 through REST API endpoints.
Overview
This plugin automatically discovers routes marked with the opt attribute and exposes them as MCP-compatible REST endpoints. Routes marked with mcp_tool="name" become executable tools, while routes marked with mcp_resource="name" become readable resources.
Features
- 🚀 Zero Dependencies - Only requires Litestar
- 📡 REST API Endpoints - No stdio transport or MCP libraries needed
- 🔧 Simple Route Marking - Use Litestar's
optattribute pattern - 🛡️ Type Safe - Full type hints with dataclasses
- 📊 Automatic Discovery - Routes are discovered at app initialization
- 🎯 OpenAPI Integration - Server info derived from OpenAPI config
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 Endpoints: The plugin adds REST endpoints 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 info and capabilitiesGET /mcp/resources- List available resourcesGET /mcp/resources/{name}- Get specific resource contentGET /mcp/tools- List available toolsPOST /mcp/tools/{name}- Execute a tool
Built-in Resources:
openapi- Your application's OpenAPI schema (always available)
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 MCP API endpoints |
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 |
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"
),
)
Development
# Clone the repository
git clone https://github.com/litestar-org/litestar-mcp.git
cd litestar-mcp
# Install with development dependencies
uv install --dev
# Run tests
make test
# Run example
uv run python docs/examples/basic/main.py
License
MIT License. See LICENSE for details.
Contributing
Contributions welcome! Please see our contributing guide for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file litestar_mcp-0.2.0.tar.gz.
File metadata
- Download URL: litestar_mcp-0.2.0.tar.gz
- Upload date:
- Size: 284.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9492e0d8ec328975520508c536792e34627ed24218451249a9884dca6338388d
|
|
| MD5 |
4346ba256228102fc888bef5b4b66cd9
|
|
| BLAKE2b-256 |
070ad8aab7e1adc44c061a81cb532a8f9c3d04733bc51c801190ff0c9d569e37
|
Provenance
The following attestation bundles were made for litestar_mcp-0.2.0.tar.gz:
Publisher:
publish.yml on cofin/litestar-mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litestar_mcp-0.2.0.tar.gz -
Subject digest:
9492e0d8ec328975520508c536792e34627ed24218451249a9884dca6338388d - Sigstore transparency entry: 565802528
- Sigstore integration time:
-
Permalink:
cofin/litestar-mcp@41a027005cef5e49c83badd846241ecfaf5b29a6 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/cofin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@41a027005cef5e49c83badd846241ecfaf5b29a6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file litestar_mcp-0.2.0-py3-none-any.whl.
File metadata
- Download URL: litestar_mcp-0.2.0-py3-none-any.whl
- Upload date:
- Size: 23.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57204fddd5b65d25ea1344d5a63a95c238c0131a443faccbbf95334b032f7692
|
|
| MD5 |
90e09c80d740737e19302c024d8b1b95
|
|
| BLAKE2b-256 |
fc1071f59d22e7c3e65acb5d8e3abfcfb2e5aa840744a3c6a0d09cf24ab63e88
|
Provenance
The following attestation bundles were made for litestar_mcp-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on cofin/litestar-mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litestar_mcp-0.2.0-py3-none-any.whl -
Subject digest:
57204fddd5b65d25ea1344d5a63a95c238c0131a443faccbbf95334b032f7692 - Sigstore transparency entry: 565802534
- Sigstore integration time:
-
Permalink:
cofin/litestar-mcp@41a027005cef5e49c83badd846241ecfaf5b29a6 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/cofin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@41a027005cef5e49c83badd846241ecfaf5b29a6 -
Trigger Event:
release
-
Statement type: