Skip to main content

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+ (Python 3.13 recommended for optimal performance)
  • MCP SDK 1.15.0+
  • Pydantic 2.11.9+
  • PyYAML 6.0.3+

Note: This library has been updated with the latest dependency versions as of September 2025. All dependencies are compatible with Python 3.11-3.13.

License

MIT License - see LICENSE 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

mcp_commons-1.0.1.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

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

mcp_commons-1.0.1-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file mcp_commons-1.0.1.tar.gz.

File metadata

  • Download URL: mcp_commons-1.0.1.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mcp_commons-1.0.1.tar.gz
Algorithm Hash digest
SHA256 9f5b0001ac173417edc101e6817e4bb226ab026e121de9a8ed54ebf6ad79e958
MD5 c4880891aca179fc54cca00479744239
BLAKE2b-256 c11498e57cb39991f19c0130296f9a807f65ff348d524f5454ab10c7a2bf16bd

See more details on using hashes here.

File details

Details for the file mcp_commons-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: mcp_commons-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mcp_commons-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2d174c4bea220db2cc3535c104b62b20a84aaef01cfd40b1ccfe57229b963ecd
MD5 528d3e7b5dd073cc6f8dca10de2bcff5
BLAKE2b-256 fcb52826ceaa2473dc4162909fa9cdb88b75e7db729844ab634476b5d3716e33

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