Skip to main content

A lightweight, flexible implementation of the Model Context Protocol (MCP) for Python applications, specializing in robust HTTP/SSE transport.

Project description

pymcp-sse: Python MCP over SSE Library

A lightweight, flexible implementation of the Model Context Protocol (MCP) for Python applications, specializing in robust HTTP/SSE transport.

Features

  • Modular Framework: Clean implementation of BaseMCPServer, BaseMCPClient, and MultiMCPClient using pymcp_sse.
  • HTTP/SSE Transport: Support for HTTP/SSE with automatic session management, configurable timeouts, and reconnection handling.
  • Tool Registration: Simple decorator-based tool registration and discovery.
  • Server Push: Built-in support for push notifications and periodic pings.
  • LLM Integration: Includes BaseLLMClient abstraction for easy integration with various LLM providers (Anthropic example provided).
  • Flexible Logging: Configurable logging via pymcp_sse.utils.

Installation

To install the library locally for development:

# Navigate to the directory containing pyproject.toml
cd /path/to/your/pymcp-sse

# Install in editable mode
pip install -e .

(Once published, installation via pip install pymcp-sse will be available.)

Basic Usage

Creating an MCP Server

from pymcp_sse.server import BaseMCPServer

# Create a server instance
server = BaseMCPServer("My Simple Server")

# Register tools using the decorator
@server.register_tool("echo")
async def echo_tool(text: str):
    return {"response": f"Echo: {text}"}

# Run the server
if __name__ == "__main__":
    # Additional kwargs are passed to uvicorn.run (e.g., timeout_keep_alive=65)
    server.run(host="0.0.0.0", port=8000)

Creating a Single Client

import asyncio
from pymcp_sse.client import BaseMCPClient

async def main():
    # Configure timeouts for stability (read timeout > server ping interval)
    client = BaseMCPClient(
        "http://localhost:8000",
        http_read_timeout=65, 
        http_connect_timeout=10
    )
    
    try:
        # Connect and initialize
        if await client.connect() and await client.initialize():
            # Call a tool
            result = await client.call_tool("echo", text="Hello, world!")
            print(f"Tool Result: {result}")
    finally:
        await client.close()

if __name__ == "__main__":
    asyncio.run(main())

Creating a Multi-Server Client

import asyncio
from pymcp_sse.client import MultiMCPClient

async def main():
    servers = {
        "server1": "http://localhost:8001",
        "server2": "http://localhost:8002"
    }
    # Configure timeouts for stability (read timeout > server ping interval)
    client = MultiMCPClient(
        servers,
        http_read_timeout=65,
        http_connect_timeout=10
    )
    
    try:
        # Connect to all servers
        await client.connect_all()
        
        # Call a tool on a specific server
        if client.clients.get("server1") and client.clients["server1"].initialized:
            result = await client.call_tool("server1", "echo_s1", text="Hello from MultiClient!")
            print(f"Server 1 Echo Result: {result}")
    finally:
        await client.close()

if __name__ == "__main__":
    asyncio.run(main())

Documentation

For more detailed usage instructions, notes on the HTTP/SSE implementation, and guides on LLM integration, please refer to the documentation in the docs/ directory.

Examples

See the examples/ directory for complete working examples, including:

  • Multiple servers with different tools
  • A client application with an LLM agent (AnthropicLLMClient example)
  • A launcher script (run_all.py) to run all components.

License

MIT

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

pymcp_sse-0.1.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

pymcp_sse-0.1.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file pymcp_sse-0.1.1.tar.gz.

File metadata

  • Download URL: pymcp_sse-0.1.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for pymcp_sse-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ec3f0ba54c13f9479ad61eaaacbfff1810601d461ad898400ef12643e592f461
MD5 6ec088201d60fd543e37f8269630710a
BLAKE2b-256 fcb8074555910141c2967ff357ab6805d8840c248dd27d0c684b61e2d0211ccb

See more details on using hashes here.

File details

Details for the file pymcp_sse-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pymcp_sse-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for pymcp_sse-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c5162b5df9deee0f6dd42b2d1e324f616e0c6162a0961faf72d9c32edc5cf583
MD5 8ff914938cb28b3531c52216952813a2
BLAKE2b-256 eec5e888c576e57a6ecdfd4cad2e9666a63cb52879470ead4071b89e3b1f65bb

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