Lihil MCP
MCP (Model Context Protocol) integration for the Lihil framework.
This package enables Lihil applications to expose their endpoints as MCP tools and resources, allowing them to be used by MCP-compatible clients like Claude Code.
Installation
pip install lihil-mcp
Or with uv:
uv add lihil-mcp
Quick Start
Basic Usage
from lihil import Lihil
from lihil_mcp import MCPConfig
# Create your Lihil app
app = Lihil()
# Enable MCP with auto-exposure
mcp_config = MCPConfig(enabled=True, auto_expose=True)
app.enable_mcp(mcp_config)
# Your endpoints are automatically exposed as MCP tools/resources
@app.get("/users/{user_id}")
async def get_user(user_id: int) -> dict:
return {"id": user_id, "name": "User"}
@app.post("/send-email")
async def send_email(to: str, subject: str, body: str) -> str:
return f"Email sent to {to}"
Manual MCP Decoration
from lihil import Lihil
from lihil_mcp import MCPConfig, mcp_tool, mcp_resource
app = Lihil()
mcp_config = MCPConfig(enabled=True, auto_expose=False)
app.enable_mcp(mcp_config)
# Explicitly mark endpoints as MCP tools
@app.post("/send-email")
@mcp_tool(title="Send Email", description="Send an email to a recipient")
async def send_email(to: str, subject: str, body: str) -> str:
# Email sending logic
return f"Email sent to {to}"
# Explicitly mark endpoints as MCP resources
@app.get("/users/{user_id}")
@mcp_resource("users://{user_id}", title="User Profile", description="Get user profile")
async def get_user(user_id: int) -> dict:
return {"id": user_id, "name": "User"}
Configuration
MCPConfig Options
from lihil_mcp import MCPConfig
config = MCPConfig(
enabled=True, # Enable MCP functionality
server_name="my-api-server", # Name of the MCP server
auto_expose=True, # Auto-expose all endpoints as MCP tools/resources
expose_docs=True, # Expose OpenAPI docs as MCP resources
auth_required=False, # Whether authentication is required
transport="asgi", # Transport protocol ("asgi" or "stdio")
mcp_path_prefix="/mcp" # URL path prefix for MCP endpoints
)
How It Works
Auto-Exposure Mode
When auto_expose=True:
- GET endpoints - Exposed as MCP resources
- POST/PUT/PATCH endpoints - Exposed as MCP tools
Manual Decoration
Use decorators for fine-grained control:
@mcp_tool()- Mark endpoint as MCP tool@mcp_resource()- Mark endpoint as MCP resource
Transport
The package uses ASGI transport to handle both regular HTTP requests and MCP protocol requests on the same server.
Integration with Lihil Features
Authentication
Leverage Lihil's auth plugins:
from lihil_mcp import MCPConfig
# Enable authentication for MCP operations
config = MCPConfig(enabled=True, auth_required=True)
Dependency Injection
MCP works seamlessly with Lihil's dependency injection system:
from lihil import Lihil, Provide
from lihil_mcp import mcp_tool
app = Lihil()
class EmailService:
def send(self, to: str, subject: str, body: str) -> str:
return f"Email sent to {to}"
@app.post("/send-email")
@mcp_tool(title="Send Email")
async def send_email(
to: str,
subject: str,
body: str,
email_service: EmailService = Provide()
) -> str:
return email_service.send(to, subject, body)
Examples
See the examples directory for complete working examples.
Requirements
- Python 3.10+
- lihil >= 0.2.0
- mcp >= 1.8.1
Development
# Clone the repository
git clone https://github.com/raceychan/lihil-mcp.git
cd lihil-mcp
# Install with development dependencies
uv sync --group dev
# Run tests
uv run pytest
# Run tests with coverage
uv run pytest --cov=lihil_mcp
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Metadata
Release files for lihil-mcp 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| lihil_mcp-0.1.0.tar.gz | 84.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lihil_mcp-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 95.4 kB
Release files / lihil_mcp-0.1.0.tar.gz
| Download URL | lihil_mcp-0.1.0.tar.gz |
|---|---|
| Size | 84.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8a4d52401a31b6eb318bfcc655b13ddd2eb7a56a945ab3ad34fb7c9d37c44305
|
|
BLAKE2b-256 checksum How to use checksums |
b9aea44a96630fd7b9bf9832f9bf6b09077e5b95a93cbe22bfe3fb959d2e9328
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.12
|
Release files / lihil_mcp-0.1.0-py3-none-any.whl
| Download URL | lihil_mcp-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fe1c8fb76d54c298421a6b953f079ec62b7dd443971e1a2c73b8fa3e17342802
|
|
BLAKE2b-256 checksum How to use checksums |
4b85ffa40a5c9426ed439f84c21dd9fe14ecad1c64ac4e8cf7f551e25e6cb417
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.12
|
Security for lihil-mcp 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.