Skip to main content

mcp-common

Code style: crackerjack Runtime: oneiric uv Python: 3.14+

Version: 0.17.9 (Oneiric-Native) Status: Production Ready


Quick Links

Overview

mcp-common is a Oneiric-native foundation library for building production-grade MCP (Model Context Protocol) servers. It provides battle-tested patterns extracted from production servers including Crackerjack, Session Buddy, and FastBlocks.

Quality & CI

Crackerjack is the standard quality-control and CI/CD gate for changes to this library and the downstream MCP servers that build on it.

🎯 What This Library Provides:

  • Tool Profile System - Gated tool registration to reduce MCP context overhead (~391 tools across the 5 Bodai ecosystem components)
  • Description Trimming - Utility to trim tool docstrings to 200 chars for token efficiency
  • Oneiric CLI Factory - Standardized server lifecycle with start/stop/restart/status/health commands
  • HTTP Client Adapter - Connection pooling with httpx for 11x performance
  • Prompting/Notification Adapter - Unified cross-platform user interaction with automatic backend detection
  • Security Utilities - API key validation (with 90% faster caching) and input sanitization (2x faster)
  • Rich Console UI - Beautiful panels and notifications for server operations
  • Settings Management - YAML + environment variable configuration (Pydantic-based)
  • Health Check System - Production-ready health monitoring
  • Type-Safe - Full Pydantic validation and type hints
  • Comprehensive Testing - ~1,687 tests with property-based, concurrency, and integration coverage

Design Principles:

  1. Oneiric-Native - Direct Pydantic, Rich library, and standard patterns
  2. Production-Ready - Extracted from real production systems
  3. Layered Configuration - YAML files + environment variables with clear priority
  4. Rich UI - Professional console output with Rich panels
  5. Type-safe - Full type hints with strict MyPy checking
  6. Well-Tested - 90% coverage minimum

📚 Examples

See examples/ for complete production-ready examples:

1. CLI Server (Oneiric-Native)

Demonstrates the CLI factory for standardized server lifecycle management:

  • 5 lifecycle commands (start, stop, restart, status, health)
  • PID file management with security validation
  • Runtime health snapshots
  • Graceful shutdown with signal handling
  • Custom lifecycle handlers
cd examples
python cli_server.py start
python cli_server.py status
python cli_server.py health
python cli_server.py stop

2. Weather MCP Server (Oneiric-Native)

Demonstrates HTTP adapters and FastMCP integration:

  • HTTPClientAdapter with connection pooling (11x performance)
  • MCPBaseSettings with YAML + environment configuration
  • ServerPanels for beautiful terminal UI
  • Oneiric configuration patterns (direct instantiation)
  • FastMCP tool integration (optional; install separately)
cd examples
python weather_server.py

Full documentation: examples/README.md


Quick Start

Installation

pip install mcp-common>=0.3.6

This automatically installs Pydantic, Rich, and all required dependencies.

If you plan to run an MCP server (e.g., the examples), install a protocol host such as FastMCP separately:

pip install fastmcp
# or
uv add fastmcp

Minimal Example

# my_server/settings.py
from mcp_common.config import MCPBaseSettings
from pydantic import Field


class MyServerSettings(MCPBaseSettings):
    """Server configuration following Oneiric pattern.

    Loads from (priority order):
    1. settings/local.yaml (gitignored)
    2. settings/my-server.yaml
    3. Environment variables MY_SERVER_*
    4. Defaults below
    """

    api_key: str = Field(description="API key for service")
    timeout: int = Field(default=30, description="Request timeout")


# my_server/main.py
from fastmcp import FastMCP  # Optional: install fastmcp separately
from mcp_common import ServerPanels, HTTPClientAdapter, HTTPClientSettings
from my_server.settings import MyServerSettings

# Initialize
mcp = FastMCP("MyServer")
settings = MyServerSettings.load("my-server")

# Initialize HTTP adapter
http_settings = HTTPClientSettings(timeout=settings.timeout)
http_adapter = HTTPClientAdapter(settings=http_settings)


# Define tools
@mcp.tool()
async def call_api():
    # Use the global adapter instance
    response = await http_adapter.get("https://api.example.com")
    return response.json()


# Run server
if __name__ == "__main__":
    # Display startup panel
    ServerPanels.startup_success(
        server_name="My MCP Server",
        version="1.0.0",
        features=["HTTP Client", "YAML Configuration"],
    )

    mcp.run()

Core Features

🔌 HTTP Client Adapter

Connection Pooling with httpx:

  • 11x faster than creating clients per request
  • Automatic initialization and cleanup
  • Configurable timeouts, retries, connection limits
from mcp_common import HTTPClientAdapter, HTTPClientSettings

# Configure HTTP adapter
http_settings = HTTPClientSettings(
    timeout=30,
    max_connections=50,
    retry_attempts=3,
)

# Create adapter
http_adapter = HTTPClientAdapter(settings=http_settings)

# Make requests
response = await http_adapter.get("https://api.example.com")

Architecture Overview:

graph TB
    subgraph "mcp-common Components"
        A[HTTP Client Adapter<br/>with Connection Pooling]
        B[Settings Management<br/>YAML + Env Vars]
        C[CLI Factory<br/>Lifecycle Management]
        D[Rich UI Panels<br/>Console Output]
        E[Security Utilities<br/>Validation & Sanitization]
    end

    subgraph "Integration"
        F[FastMCP<br/>Optional]
        G[MCP Server<br/>Application]
    end

    A --> G
    B --> G
    C --> G
    D --> G
    E --> G
    F --> G

    style A fill:#e1f5fe
    style B fill:#f3e5f5
    style C fill:#e8f5e8
    style D fill:#fff3e0
    style E fill:#fce4ec

Note: Rate limiting is not provided by this library. If you use FastMCP, its built-in RateLimitingMiddleware can be enabled; otherwise, use project-specific configuration.

🎯 Oneiric CLI Factory

Production-Ready Server Lifecycle Management:

The MCPServerCLIFactory provides standardized CLI commands for managing MCP server lifecycles, inspired by Oneiric's operational patterns. It handles process management, health monitoring, and graceful shutdown out of the box.

Features:

  • 5 Standard Commands - start, stop, restart, status, health
  • Security-First - Secure PID files (0o600), cache directories (0o700), ownership validation
  • Process Validation - Detects stale PIDs, prevents race conditions, validates process identity
  • Health Monitoring - Runtime health snapshots with configurable TTL
  • Signal Handling - Graceful shutdown on SIGTERM/SIGINT
  • Custom Handlers - Extensible lifecycle hooks for server-specific logic
  • Dual Output - Human-readable and JSON output modes
  • Standard Exit Codes - Shell-scriptable with semantic exit codes

CLI Factory Architecture:

graph LR
    subgraph "User Application"
        A[Server Implementation]
        B[Custom Handlers]
    end

    subgraph "mcp-common CLI Factory"
        C[MCPServerCLIFactory]
        D[MCPServerSettings]
        E[PID File Management]
        F[Health Snapshots]
        G[Signal Handlers]
    end

    subgraph "Typer CLI"
        H[start command]
        I[stop command]
        J[restart command]
        K[status command]
        L[health command]
    end

    A --> C
    B --> C
    D --> C
    C --> E
    C --> F
    C --> G
    C --> H
    C --> I
    C --> J
    C --> K
    C --> L

    style A fill:#e8f5e8
    style B fill:#fff3e0
    style C fill:#e3f2fd
    style D fill:#f3e5f5
    style H fill:#e0f2f1
    style I fill:#e0f2f1
    style J fill:#e0f2f1
    style K fill:#e0f2f1
    style L fill:#e0f2f1

Quick Example:

from mcp_common.cli import MCPServerCLIFactory, MCPServerSettings

# 1. Load settings (YAML + env vars)
settings = MCPServerSettings.load("my-server")


# 2. Define lifecycle handlers
def start_server():
    print("Server initialized!")
    # Your server startup logic here


def stop_server(pid: int):
    print(f"Stopping PID {pid}")
    # Your cleanup logic here


def check_health():
    # Return current health snapshot
    return RuntimeHealthSnapshot(
        orchestrator_pid=os.getpid(),
        watchers_running=True,
    )


# 3. Create CLI factory
factory = MCPServerCLIFactory(
    server_name="my-server",
    settings=settings,
    start_handler=start_server,
    stop_handler=stop_server,
    health_probe_handler=check_health,
)

# 4. Create and run Typer app
app = factory.create_app()

if __name__ == "__main__":
    app()

Command Usage:

# Start server (creates PID file and health snapshot)
python my_server.py start

# Check status (lightweight process check)
python my_server.py status
# Output: Server running (PID 12345, snapshot age: 2.3s, fresh: True)

# View health (detailed health information)
python my_server.py health

# Live health probe
python my_server.py health --probe

# Stop server (graceful shutdown with SIGTERM)
python my_server.py stop

# Force stop with timeout
python my_server.py stop --timeout 5 --force

# Restart (stop + start)
python my_server.py restart

# JSON output for automation
python my_server.py status --json

Configuration:

Settings are loaded from multiple sources (priority order):

  1. settings/local.yaml (gitignored, for development)
  2. settings/{server-name}.yaml (checked into repo)
  3. Environment variables MCP_SERVER_*
  4. Defaults in MCPServerSettings

Example settings/my-server.yaml:

server_name: "My MCP Server"
cache_root: .oneiric_cache
health_ttl_seconds: 60.0
log_level: INFO

Exit Codes:

  • 0 - Success
  • 1 - General error
  • 2 - Server not running (status/stop)
  • 3 - Server already running (start)
  • 4 - Health check failed
  • 5 - Configuration error
  • 6 - Permission error
  • 7 - Timeout
  • 8 - Stale PID file (use --force)

Full Example:

See examples/cli_server.py for a complete working example with custom commands and health probes.

⚙️ Settings with YAML Support (Oneiric Pattern)

  • Pure Pydantic BaseModel
  • Layered configuration: YAML files + environment variables
  • Type validation with Pydantic
  • Path expansion (~ → home directory)
from mcp_common.config import MCPBaseSettings


class ServerSettings(MCPBaseSettings):
    api_key: str  # Required
    timeout: int = 30  # Optional with default


# Load with layered configuration
settings = ServerSettings.load("my-server")
# Loads from:
# 1. settings/my-server.yaml
# 2. settings/local.yaml
# 3. Environment variables MY_SERVER_*
# 4. Defaults

📝 Standard Python Logging

mcp-common uses standard Python logging. Configure as needed for your server:

import logging

# Configure logging
logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)

logger = logging.getLogger(__name__)
logger.info("Server started")

🎨 Rich Console UI

  • Beautiful startup panels
  • Error displays with context
  • Statistics tables
  • Progress bars
from mcp_common.ui import ServerPanels

ServerPanels.startup_success(
    server_name="Mailgun MCP",
    http_endpoint="http://localhost:8000",
    features=["Rate Limiting", "Security Filters"],
)

🧪 Testing Utilities

  • Mock MCP clients
  • HTTP response mocking
  • Shared fixtures
  • DI-friendly testing
from mcp_common.testing import MockMCPClient, mock_http_response


async def test_tool():
    with mock_http_response(status=200, json={"ok": True}):
        result = await my_tool()
    assert result["success"]

🔧 Tool Profile System

Reduce MCP context overhead by gating which tools are registered at startup. Each server reads a {SERVER_NAME}_TOOL_PROFILE environment variable (minimal, standard, or full) and only registers the corresponding tool groups.

Why: A server with 170 tools sends ~70k tokens of tool definitions to Claude on every request. Profile gating reduces this to ~10-20k tokens for daily development.

ToolProfile enum:

from mcp_common.tools import ToolProfile, trim_description, MANDATORY_TOOLS

# Resolve from environment (defaults to FULL for backward compatibility)
profile = ToolProfile.from_env("MY_SERVER_TOOL_PROFILE")

# Ordering comparisons work
assert ToolProfile.MINIMAL < ToolProfile.STANDARD < ToolProfile.FULL

# Safe fallback for invalid values
assert ToolProfile.from_string("unknown") == ToolProfile.FULL

Description trimming:

# Strip Args/Returns/Raises sections, keep first paragraph, max 200 chars
trimmed = trim_description("""Check health of a service.

    Args:
        service_name: Name of the service
        port: Port number

    Returns:
        Health status dictionary""")
# Result: "Check health of a service."

Per-server profiles.py pattern:

# my_server/mcp/tools/profiles.py
from mcp_common.tools import ToolProfile

MINIMAL_REGISTRATIONS = ["register_health_tools"]
STANDARD_REGISTRATIONS = MINIMAL_REGISTRATIONS + ["register_core_tools"]
FULL_REGISTRATIONS = STANDARD_REGISTRATIONS + ["register_advanced_tools"]

PROFILE_REGISTRATIONS = {
    ToolProfile.MINIMAL: MINIMAL_REGISTRATIONS,
    ToolProfile.STANDARD: STANDARD_REGISTRATIONS,
    ToolProfile.FULL: FULL_REGISTRATIONS,
}

def get_active_profile(env_var="MY_SERVER_TOOL_PROFILE"):
    return ToolProfile.from_env(env_var)

Profile tiers across the ecosystem:

Server MINIMAL STANDARD FULL
session-buddy 4 groups (~12 tools) 13 groups (~35 tools) 32 groups (~151 tools)
mahavishnu 1 group (health) 7 groups 14 groups (~174 tools)
crackerjack 2 groups 7 groups 12 groups (~60 tools)
akosha 1 group (health) 2 groups 4 groups (~5 tools)
dhara 1 group (kv/store) 3 groups 3 groups (~17 tools)

discover_tools meta-tool: Each server registers a discover_tools(query) tool that is always available, letting Claude find unloaded tools and suggest profile changes.


Documentation


Complete Example

See examples/ for a complete production-ready Weather MCP server demonstrating mcp-common patterns.

Key Patterns Demonstrated:

  1. Oneiric Settings - YAML + environment variable configuration with .load()
  2. HTTP Adapter - HTTPClientAdapter with connection pooling
  3. Rich UI - ServerPanels for startup/errors/status
  4. Tool Organization - Modular tool registration with FastMCP
  5. Configuration Layering - Multiple config sources with clear priority
  6. Type Safety - Full Pydantic validation throughout
  7. Error Handling - Graceful error display with ServerPanels

Performance Benchmarks

✨ Phase 4 Optimizations

Sanitization Early-Exit Optimization:

Scenario Before After Speedup
Clean text (no sensitive data) 22μs 10μs 2.2x faster
Text with sensitive data 22μs 22μs No change

API Key Validation Caching:

Call Type Time Speedup
First call (uncached) 100μs baseline
Subsequent calls (cached) 10μs 10x faster

Impact:

  • 2x faster for clean text sanitization (most common case)
  • 10x faster for repeated API key validations
  • Cache size: 128 most recent entries
  • Zero breaking changes

HTTP Client Adapter (vs new client per request)

Before: 100 requests in 45 seconds, 500MB memory
After:  100 requests in 4 seconds, 50MB memory

Result: 11x faster, 10x less memory

Rate Limiter Overhead

Without: 1000 requests in 1.2 seconds
With:    1000 requests in 1.25 seconds

Result: +4% overhead (negligible vs network I/O)

📊 Testing Performance

Test Suite Growth:

Version Tests Coverage
v0.5.2 564 94%
v0.6.0 615 99%+
v0.15.0 ~1,400 95%
v0.17.9 (current) ~1,687 96%

Testing Capabilities:

  • ✅ Property-based tests (Hypothesis, 6 modules)
  • ✅ Concurrency + async safety tests
  • ✅ Performance benchmarks (tests/performance/)
  • ✅ Backward compatibility across the 0.13→0.17 series

Usage Patterns

Pattern 1: Configure Settings with YAML

from mcp_common.config import MCPBaseSettings
from pydantic import Field


class MySettings(MCPBaseSettings):
    api_key: str = Field(description="API key")
    timeout: int = Field(default=30, description="Timeout")


# Load from settings/my-server.yaml + env vars
settings = MySettings.load("my-server")

# Access configuration
print(f"Using API key: {settings.get_masked_key()}")

Pattern 2: Use HTTP Client Adapter

from mcp_common import HTTPClientAdapter, HTTPClientSettings


# Configure HTTP client
http_settings = HTTPClientSettings(
    timeout=30,
    max_connections=50,
    retry_attempts=3,
)

# Create adapter
http = HTTPClientAdapter(settings=http_settings)


# Make requests
@mcp.tool()
async def call_api():
    response = await http.get("https://api.example.com/data")
    return response.json()


# Cleanup when done
await http._cleanup_resources()

Pattern 3: Display Rich UI Panels

from mcp_common import ServerPanels

# Startup panel
ServerPanels.startup_success(
    server_name="My Server",
    version="1.0.0",
    features=["Feature 1", "Feature 2"],
)

# Error panel
ServerPanels.error(
    title="API Error",
    message="Failed to connect",
    suggestion="Check your API key",
)

# Status table
ServerPanels.status_table(
    title="Health Check",
    rows=[
        ("API", "✅ Healthy", "200 OK"),
        ("Database", "⚠️ Degraded", "Slow queries"),
    ],
)

Development

Setup

git clone https://github.com/lesaker/mcp-common.git
cd mcp-common
pip install -e ".[dev]"

Running Tests

# Run all tests with coverage
pytest --cov=mcp_common --cov-report=html

# Run specific test
pytest tests/test_http_adapter.py -v

# Run integration tests
pytest tests/integration/ -v

Code Quality

# Format code
ruff format

# Lint code
ruff check

# Type checking
mypy mcp_common tests

# Run all quality checks
crackerjack --all

Versioning

Recent Versions:

  • 0.17.9 (current) - register_http_health_route helper, dependency-groups migration
  • 0.17.0 - Plan 7 Phase 1: FastMCP 3.4 foundation
  • 0.16.0 - AppleScript bridge + iTerm2 protocol spec, async multi-line escaping
  • 0.15.0 - LLM layer: per-tier retry loop, error sanitization, llama_server support, Multimodal TaskType
  • 0.14.0 - HailuoAdapter (MiniMax video generation), task_routing model resolution
  • 0.13.0 - Property-based tests expansion, Hypothesis edge cases
  • 0.6.0 - Tool Profile System, description trimming, MANDATORY_TOOLS
  • 0.3.3 - Oneiric CLI Factory

Compatibility:

  • Requires Python 3.13+
  • Optional: compatible with FastMCP 2.0+
  • Uses Pydantic 2.12+, Rich 14.2+

Success Metrics

Current Status:

  1. ✅ Professional Rich UI in all components
  2. ✅ 90%+ test coverage maintained
  3. ✅ Zero production incidents
  4. ✅ Oneiric-native patterns throughout
  5. ✅ Standardized CLI lifecycle management
  6. ✅ Clean dependency tree (no framework lock-in)

License

BSD-3-Clause License - See LICENSE for details


Contributing

Contributions are welcome! Please:

  1. Read examples/README.md for usage patterns
  2. Follow Oneiric patterns (see examples)
  3. Fork and create feature branch
  4. Add tests (coverage ≥90%)
  5. Ensure all quality checks pass (ruff format && ruff check && mypy && pytest)
  6. Submit pull request

Acknowledgments

Built with patterns extracted from 9 production MCP servers:

Primary Pattern Sources:

  • crackerjack - MCP server structure, Rich UI panels, CLI patterns
  • session-buddy - Configuration patterns, health checks
  • fastblocks - Adapter organization, settings management

Additional Contributors:

  • raindropio-mcp (HTTP client patterns)
  • excalidraw-mcp (testing patterns)
  • opera-cloud-mcp
  • mailgun-mcp
  • unifi-mcp

Support

For support, please check the documentation in the docs/ directory or create an issue in the repository.


Ready to get started? Check out examples/ for working examples demonstrating all features!

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_common-0.24.0.tar.gz (965.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_common-0.24.0-py3-none-any.whl (404.2 kB view details)

Uploaded Python 3

File details

Details for the file mcp_common-0.24.0.tar.gz.

File metadata

  • Download URL: mcp_common-0.24.0.tar.gz
  • Upload date:
  • Size: 965.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mcp_common-0.24.0.tar.gz
Algorithm Hash digest
SHA256 db2e860e9337dadac42ec8a96478bc884c2ab0914f7a179beed425f06b38dad4
MD5 e7023e0766961b88475d7709952651a6
BLAKE2b-256 7cf1eb692c180ae3bd7700f65c32c918914aaae5b956f203d193e90cf77b32bd

See more details on using hashes here.

File details

Details for the file mcp_common-0.24.0-py3-none-any.whl.

File metadata

  • Download URL: mcp_common-0.24.0-py3-none-any.whl
  • Upload date:
  • Size: 404.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mcp_common-0.24.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3c6371abb560bb312bd8f5eb0d9f5978bfb21e6fab73bf7665ff6990e9914730
MD5 f4f27dbf33ae9cd36c34c84e9da2679b
BLAKE2b-256 3412042075393dbf8cf7e7794e6702a072856850d61deed029b792355024cb96

See more details on using hashes here.

Release history Release notifications | RSS feed

0.24.4

2 files

0.24.3

2 files

0.24.2

2 files

0.24.1

2 files

This release

0.24.0 This release

2 files

0.23.1

2 files

0.23.0

2 files

0.22.4

2 files

0.22.3

2 files

0.22.2

2 files

0.22.0

2 files

0.20.6

2 files

0.20.4

2 files

0.20.2

2 files

0.20.0

2 files

0.19.1

2 files

0.19.0

2 files

0.18.0

2 files

0.17.10

2 files

0.17.9

2 files

0.17.8

2 files

0.17.7

2 files

0.17.6

2 files

0.17.5

2 files

0.17.4

2 files

0.17.3

2 files

0.17.1

2 files

0.17.0

2 files

0.16.4

2 files

0.16.3

2 files

0.16.2

2 files

0.16.1

2 files

0.16.0

2 files

0.15.3

2 files

0.15.2

2 files

0.15.1

2 files

0.15.0

2 files

0.13.3

2 files

0.13.2

2 files

0.13.1

2 files

0.13.0

2 files

0.12.3

2 files

0.12.2

2 files

0.12.1

2 files

0.12.0

2 files

0.11.1

2 files

0.11.0

2 files

0.10.0

2 files

0.9.10

2 files

0.9.9

2 files

0.9.8

2 files

0.9.7

2 files

0.9.6

2 files

0.9.5

2 files

0.9.4

2 files

0.9.2

2 files

0.9.1

2 files

0.9.0

2 files

0.8.0

2 files

0.7.0

2 files

0.6.0

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.9

2 files

0.4.8

2 files

0.4.7

2 files

0.4.6

2 files

0.4.5

2 files

0.4.3

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.6

2 files

0.3.5

2 files

0.3.4

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page