PyMCP
A simple, tiny, and asynchronous server and client implementation for the Modern Context Protocol that allows you to easily expose functions as remote tools using a decorator with hot-reloading capability.
README
PyMCP
PyMCP is a simple, tiny, and asynchronous server and client implementation for the Modern Context Protocol (MCP).
Features
- Simple Tool Definition: Easily expose functions as remote tools using a simple
@tooldecorator. - Hot-Reloading: Tools can be added, removed, or modified on the fly, and the server will hot-reload them without a restart.
Quick Start
Running the Server
-
Installation (from source):
git clone <repository-url> cd pymcp pip install -r requirements.txt pip install -e . -
Run the Server:
- run with default settings:
pymcp- run with custom settings:
pymcp --host 0.0.0.0 --port 9000 --tool-repo ./my_tools --log-level DEBUG
Using the Client
import asyncio
import pymcp
async def main():
try:
async with pymcp.Client("localhost", 8765) as client:
# Ping the server
pong = await client.call("ping")
# Call the custom 'add' tool
result = await client.call("add", a=5, b=7)
print(f"5 + 7 = {result}")
# Discover available tools
tools = await client.call("list_tools_available")
print("Available tools:", tools)
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
asyncio.run(main())
Configuration
- Arguments passed to start_server().
- Command-line arguments (e.g., --port).
- Environment variables (e.g., PYMCP_PORT=9000).
- Values in a .env file in your project's root.
- Default values in the Settings class.
all configuration options:
# .env
# Server network settings
# PYMCP_HOST=<your_host_address>
PYMCP_HOST=127.0.0.1
# PYMCP_PORT=<your_port_number>
PYMCP_PORT=8765
# List of paths to user-defined tool directories.
# Paths should be separated by commas.
# absolute or relative paths
# Example: /path/to/my_tools,/another/path/for_tools
PYMCP_USER_TOOL_REPOS=./my_custom_tools,../shared_tools,/home/user/tools
# Logging level
# Valid values: DEBUG, INFO, WARNING, ERROR, CRITICAL
PYMCP_LOG_LEVEL=INFO
Server
request->response flow
client -> connection manager -> validator -> router -> tool executor -> return result -> client
create a tool
import pymcp
@pymcp.tool
def add(a: int, b: int) -> int:
"""
Add two integer numbers.
argument a: First integer number.
argument b: Second integer number.
return: The sum of the two numbers.
"""
return a + b
@pymcp.tool
def retrieve_data() -> str:
"""
Retrieve user data from source
no arguments is needed
"""
return data
Tool Repository discovery
The server discovers tools by scanning all .py files within the directories specified by the --tool-repo CLI argument or the PYMCP_TOOL_REPOS environment variable.
You can organize your tools into multiple files and directories. The loader will scan them recursively.
example directory structure:
my_tools/
├── __init__.py
├── math_tools.py
├── string_tools.py
└── data_tools/
├── __init__.py
├── user_data.py
└── system_data.py
second_tools/
├── __init__.py
├── network_tools.py
└── file_tools.py
Hot-Reloading
The server watches the tool repositories for file changes. If you add, modify, or delete a Python file in a tool directory, the server will automatically perform a reload:
- It rebuilds the entire tool registry from scratch.
- It atomically swaps the old registry with the new one.
This allows you to update tool logic on a live server without a restart.
PyMCP supports a simple form of dependency injection. If a tool function's signature includes a parameter named tool_registry, the server will automatically provide the ToolRegistry instance to it at execution time.
Client
Connect to the server
import pymcp
async def main():
async with pymcp.Client("localhost", 8765) as client:
result = await client.call("ping")
print(result)
request execute tools
import pymcp
async def main():
async with pymcp.Client("localhost", 8765) as client:
result = await client.call("add", a=5, b=7)
print(f"5 + 7 = {result}")
tools = await client.call("list_tools_available")
print("Available tools:", tools)
Protocol
PyMCP uses a simple, JSON-based messaging protocol over a standard WebSocket connection.
- Client-to-Server: Requests
{
"header": {
"correlation_id": "a1b2c3d4-e5f6-7890-1234-567890abcdef"
},
"body": {
"tool": "add",
"args": {
"a": 5,
"b": 10
}
}
}
- Server-to-Client: Responses (Success)
{
"status": "success",
"header": {
"correlation_id": "a1b2c3d4-e5f6-7890-1234-567890abcdef"
},
"body": {
"tool": "add",
"result": 15
},
"error": null
}
- Server-to-Client: Responses (Error)
{
"status": "error",
"header": {
"correlation_id": "a1b2c3d4-e5f6-7890-1234-567890abcdef"
},
"body": null,
"error": {
"code": "execution_error",
"message": "An unexpected error occurred while executing tool 'add'."
}
}
Design
PyMCP is designed to be simple, extensible, and easy to use. The server itself is just a WebSocket server that handles incoming requests, validates them, routes them to the appropriate tool executor, and returns the results.
No concept like "resources", "tools", or "prompts", since all of them are just a function that input something and return something. Everything is a tool. It leaves to user defining the scope of the tools.
The internal tools like list_tools_available and ping, are also some type of "tools". Tools and Server are decoupled. core tools are like extension of the server.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。