A Python implementation of MCP over HTTP/SSE with robust connection handling.
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, andMultiMCPClientusingpymcp_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
BaseLLMClientabstraction 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 (
AnthropicLLMClientexample) - 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
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 pymcp_sse-0.1.0.tar.gz.
File metadata
- Download URL: pymcp_sse-0.1.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e013f650e8a244dd6f170624fec513481c5cf67fb03fcc8998a3691642c5f96c
|
|
| MD5 |
b6f6d6e1c05d05047cc933190768a1ba
|
|
| BLAKE2b-256 |
c86b94e0b69dbd15f9340736223ca664c2c0f8f7b698bb05f572b9a0f51d1e11
|
File details
Details for the file pymcp_sse-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pymcp_sse-0.1.0-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de737b8049c89f619bcf01115cb440a65a8afdce765d9f7235a205ebfab7bdf5
|
|
| MD5 |
25c4251f44377aa415fd7d3bf201cb53
|
|
| BLAKE2b-256 |
fe05eff9916b4761a1492ff50ac25813af4334835532bcb782fa6e7a217ded1a
|