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.17.0+ (updated from 1.15.0 - see CHANGELOG.md)
  • Pydantic 2.11.9+
  • PyYAML 6.0.3+

Note: This library has been updated to use MCP SDK v1.17.0, which includes tool removal capabilities and improved testing utilities. These features will be exposed in mcp-commons v1.2.0. See ROADMAP.md for planned features.

What's New in v1.1.0

  • ✅ Compatible with MCP SDK v1.17.0
  • 🎯 Foundation for upcoming tool removal features (v1.2.0)
  • 📚 Comprehensive roadmap for future development
  • 🔧 Enhanced testing infrastructure

See CHANGELOG.md for full details.

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.2.0.tar.gz (24.1 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.2.0-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mcp_commons-1.2.0.tar.gz
  • Upload date:
  • Size: 24.1 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.2.0.tar.gz
Algorithm Hash digest
SHA256 1e5e1a3ca5a16848128229a4bbee969e6e820a74e2c0085fcbc214dfe1ec01ed
MD5 6b50ac5fc78b777106f84c144cd57ece
BLAKE2b-256 91f69606ab06f15cb177999a40318096c1e16302a607cd63120ec4dc40e62e41

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mcp_commons-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.5 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0fee488ef8ed7f9bf61a30fca44bc3926c7eaf1488ec27da481e5b9797eabeec
MD5 91b45e00adcee13627caea7b366ed83b
BLAKE2b-256 3dddaa75aba05740ee1c7fd1aa6cdc0f401887660c99e1e2429366b677739f02

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