Shared infrastructure library for MCP (Model Context Protocol) servers
Project description
MCP Commons
A Python library that simplifies building MCP (Model Context Protocol) servers by providing reusable infrastructure and eliminating repetitive boilerplate code.
Why MCP Commons?
Building MCP servers often requires writing the same patterns repeatedly:
- Converting your business logic to MCP-compatible formats
- Handling errors consistently across tools
- Registering multiple tools with similar patterns
- Testing MCP tool implementations
MCP Commons solves these problems by providing a clean adapter pattern and bulk registration utilities, letting you focus on your core logic instead of MCP plumbing.
Key Features
🔧 Smart Adapters - Automatically convert your functions to MCP-compatible tools
📦 Bulk Registration - Register multiple tools at once with consistent patterns
🛡️ Type Safety - Preserve function signatures and type hints
🧪 Testing Support - Built-in utilities for testing your MCP tools
⚡ Zero Dependencies - Minimal footprint, works with any MCP setup
Installation
pip install mcp-commons
Quick Start
Basic Adapter Usage
Instead of manually wrapping every function for MCP compatibility:
from mcp_commons import create_mcp_adapter, UseCaseResult
from mcp import Tool
# Your business logic
async def search_documents(query: str, limit: int = 10) -> UseCaseResult:
# Your implementation here
results = await document_service.search(query, limit)
return UseCaseResult.success_with_data(results)
# Convert to MCP tool automatically
search_tool = Tool.from_function(
create_mcp_adapter(search_documents),
name="search_documents"
)
Bulk Registration
Register multiple related tools at once:
from mcp_commons import bulk_register_tools
# Define your tools
tools_config = [
("list_projects", list_projects_use_case),
("create_project", create_project_use_case),
("delete_project", delete_project_use_case),
]
# Register them all with consistent error handling
tools = bulk_register_tools(tools_config)
Error Handling
The adapter automatically handles errors and provides consistent response formats:
async def might_fail() -> UseCaseResult:
try:
result = await risky_operation()
return UseCaseResult.success_with_data(result)
except ValidationError as e:
return UseCaseResult.failure(f"Invalid input: {e}")
except Exception as e:
return UseCaseResult.failure(f"Operation failed: {e}")
# Errors are automatically converted to proper MCP error responses
safe_tool = Tool.from_function(create_mcp_adapter(might_fail), name="safe_operation")
Use Cases
- Enterprise MCP Servers: Standardize tool creation across multiple servers
- Rapid Prototyping: Quickly convert existing functions to MCP tools
- Testing & Development: Mock and test MCP tools without complex setup
- Legacy Integration: Adapt existing business logic to MCP without rewrites
Documentation
Requirements
- Python 3.11+
- MCP SDK 1.13.1+
License
MIT License - see LICENSE for details.
Project details
Release history Release notifications | RSS feed
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 mcp_commons-1.0.0.tar.gz.
File metadata
- Download URL: mcp_commons-1.0.0.tar.gz
- Upload date:
- Size: 18.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a1792565590a07e9ddfef7d120c3f9aeaf281262550ea9c21623d73cf930592
|
|
| MD5 |
f07ad75f3edf3ba4fa595185cb7dbb13
|
|
| BLAKE2b-256 |
e20a32e7a865cd84930109eddcc37b7db29ada4d8596ac0ad73957b12d28f902
|
File details
Details for the file mcp_commons-1.0.0-py3-none-any.whl.
File metadata
- Download URL: mcp_commons-1.0.0-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25114d9ee5d7576bbf5e1b524df7b9b62b6a3d984beeb34217224b57a3005607
|
|
| MD5 |
f2655203dbbad2277a42c213dc9d3afc
|
|
| BLAKE2b-256 |
6b8984eda5e61c870f7e3e0c609194869de55e8035baddabec648466cd590df9
|