mcp-ws-example
Categories
Language:
Python
Stars:
5
Forks:
0
Message Communication Protocol (MCP) Implementations
This repository demonstrates two implementations of the Model Context Protocol (MCP) by Anthropic AI, a JSON-RPC based protocol for bi-directional communication that enables developers to build secure, two-way connections between their data sources and AI-powered tools.
1. Memory Stream Implementation (run on same host)
Overview
The Memory Stream implementation demonstrates local client-server communication using in-memory channels. Both client and server operate within the same host.
Protocol Flow
Client Server
| |
|------ initialize() --------->| (sends capabilities)
||
||
||
||
||
|
### Components
1. **Server** (`websocket/server_ws.py`)
```python
@weather_server.list_tools()
async def handle_list_tools():
return [
Tool(
name="get-alerts",
description="Get active weather alerts for a US state",
inputSchema={
"type": "object",
"required": ["state"],
"properties": {
"state": {
"type": "string",
"description": "Two-letter state code (e.g. CA, NY)"
}
}
}
),
Tool(
name="get-forecast",
description="Get weather forecast for a location",
inputSchema={
"type": "object",
"required": ["latitude", "longitude"],
"properties": {
"latitude": {
"type": "number",
"description": "Latitude of the location"
},
"longitude": {
"type": "number",
"description": "Longitude of the location"
}
}
}
)
]
- Client (
websocket/client_ws.py
)
client = WebSocketClient("ws://localhost:8000/ws")
await client.connect()
tools = await client.list_tools()
forecast = await client.call_tool("get-forecast",
{"latitude": 37.7749, "longitude": -122.4194})
Available Weather Tools
- get-alerts: Get active weather alerts for a US state (requires two-letter state code)
- get-forecast: Get detailed weather forecast for a location (requires latitude and longitude)
Setup
Memory Stream Run (same host via MemoryStream)
pip install -r requirements.txt
cd memory_stream
python test.py
Websocket Run (client and server on different hosts)
pip install -r requirements.txt
cd websocket
python test_ws.py
Test execution process:
- Server is started in a separate process
- System waits 2 seconds to ensure server initialization
- Client connects and executes test requests via FastAPI WebSocket port 8000
- Server process is gracefully terminated after tests
To run server client on two machins, you can use run_server.py
and run_client.py
script.
Additional Resources
For a detailed analysis of the client-server communication [port 8000], a PCAP (Packet Capture) file is available in the repository. This capture contains the complete WebSocket conversation between client and server components. This implementation is not encrypted, so the data is transmitted in plain text. The PCAP file can be opened using Wireshark, a popular network protocol analyzer. It provides a comprehensive view of the communication, including the request headers and their content.
Publisher info
More MCP servers built with Python
Bridge the gap between your web crawler and AI language models using Model Context Protocol (MCP). With mcp-server-webcrawl, your AI client filters and analyzes web content under your direction or autonomously, extracting insights from your web content. Support for WARC, wget, InterroBot, Katana, and SiteOne crawlers is available out of the gate. The server includes a full-text search interface with boolean support, resource filtering by type, HTTP status, and more.
Armor Model Context Protocol (MCP) gives developers full access to the blockchain functionality of Armor Wallet. This includes cross-chain swaps, token data, bridging, wallet management, limit orders, staking, and many other features. With the Armor MCP, developers can integrate a complete suite of crypto tools available to their AI Agents quickly and easily for fast, reliable AI Agent development.