Skip to main content

MCP (Model Context Protocol) plugin for Lihil framework

Project description

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.

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

lihil_mcp-0.1.0.tar.gz (84.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lihil_mcp-0.1.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file lihil_mcp-0.1.0.tar.gz.

File metadata

  • Download URL: lihil_mcp-0.1.0.tar.gz
  • Upload date:
  • Size: 84.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for lihil_mcp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8a4d52401a31b6eb318bfcc655b13ddd2eb7a56a945ab3ad34fb7c9d37c44305
MD5 3e7349c2396f5bf0b798ef7471b930bc
BLAKE2b-256 b9aea44a96630fd7b9bf9832f9bf6b09077e5b95a93cbe22bfe3fb959d2e9328

See more details on using hashes here.

File details

Details for the file lihil_mcp-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: lihil_mcp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for lihil_mcp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe1c8fb76d54c298421a6b953f079ec62b7dd443971e1a2c73b8fa3e17342802
MD5 c1c90018f323cc2fd2c5fbdee3b4da30
BLAKE2b-256 4b85ffa40a5c9426ed439f84c21dd9fe14ecad1c64ac4e8cf7f551e25e6cb417

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page