This project provides implementations of MCP (Model Context Protocol) servers and clients in Python.
The Model Context Protocol (MCP) is an open protocol that standardizes how applications provide context to LLMs. This project includes:
- A basic socket-based implementation (for learning purposes)
- An official MCP SDK implementation with HTTP/SSE transport
- LLM integration example using Claude with HTTP/SSE transport
- FastMCP implementation - a higher-level wrapper for easier MCP server creation
- Python 3.8 or higher
- Dependencies listed in
requirements.txt
使用提供的安装脚本快速设置环境:
./setup.sh这个脚本会自动:
- 创建虚拟环境
- 安装所有依赖
- 创建
.env文件(如果不存在) - 使所有 Python 文件可执行
- Clone this repository
- Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- For LLM integration, copy
.env.exampleto.envand add your API keys:cp .env.example .env # Edit .env with your API key
This is a simple implementation using sockets and JSON for educational purposes.
python mcp_server.pypython mcp_client.py [host] [port]This implementation uses the official MCP Python SDK with HTTP/SSE transport.
python mcp_http_server.pyThe server will start on http://localhost:8000.
python mcp_http_client.py [server_url]If no server URL is provided, it will default to http://localhost:8000.
The server now uses FastMCP, a higher-level wrapper around the MCP SDK that provides a more concise API for creating MCP servers.
- Simplified tool and resource registration with decorators
- Automatic type handling and validation
- Streamlined API for creating MCP servers
python mcp_http_server.pyThe server will start on http://localhost:8000 and is fully compatible with the existing clients.
This example shows how to use MCP with Claude to create an AI assistant that can use tools.
python mcp_llm_client.py [server_url]If no server URL is provided, it will default to http://localhost:8000.
The MCP server provides the following tools:
- greet - Greets a person by name
- calculate - Performs basic calculations (add, subtract, multiply, divide)
The official MCP protocol is used in the SDK implementation. For more information, visit modelcontextprotocol.io.
This project demonstrates different transport layers for MCP:
- Socket-based - A custom implementation using TCP sockets (for learning)
- HTTP/SSE - HTTP-based transport with Server-Sent Events for network communication
You can extend the MCP server by:
- Adding more tools to the server implementations
- Implementing additional resources
- Creating specialized servers for specific use cases
- Implementing other transport layers (WebSockets, gRPC, etc.)
如果遇到 ImportError: cannot import name 'http_client' 或类似错误,可能是 MCP 库的版本问题。在新版本的 MCP 中,http_client 可能已经被替换为 sse_client。本项目已经添加了兼容性代码,尝试多种导入路径。如果仍然遇到问题,请尝试:
pip install --upgrade mcp使用 LLM 客户端时,确保已经在 .env 文件中设置了正确的 Anthropic API 密钥:
ANTHROPIC_API_KEY=your_api_key_here
This project is open source and available under the MIT License.