This project demonstrates how to use the mcpsock
package to create WebSocket-based communication between clients and FastMCP servers, enabling real-time communication between web clients and FastMCP services.
The FastMCP WebSockets Example showcases a complete implementation of the MCP (Model Context Protocol) over WebSockets using the mcpsock
package. This implementation:
- Uses FastAPI to create a WebSocket server endpoint
- Implements a router-based FastMCP server with tools, resources, and prompts
- Demonstrates bidirectional communication between clients and the server
- Provides a clean, decorator-based API for defining MCP endpoints
The server component:
- Creates a FastAPI application with a WebSocket endpoint
- Uses the
WebSocketServer
class frommcpsock
to handle MCP protocol messages - Implements tools, resources, and prompts using decorators
- Handles WebSocket connections and message routing
- Processes client requests and sends responses
The client component:
- Uses the
WebSocketClient
class frommcpsock
to connect to the server - Demonstrates how to discover available capabilities (tools, resources, prompts)
- Shows how to call tools, access resources, and use prompts
- Handles responses from the server
-
The server creates a
WebSocketServer
instance and defines handlers using decorators:@router.tool()
for tool endpoints@router.resource()
for resource endpoints@router.prompt()
for prompt endpoints@router.initialize()
for connection initialization@router.fallback()
for handling unknown methods
-
When a client connects via WebSocket:
- The client initializes the connection with the server
- The client can discover available capabilities
- The client can call tools, access resources, and use prompts
- The server processes requests and sends responses
-
The
mcpsock
package handles all the protocol details, including:- Message serialization and deserialization
- Request/response matching
- Error handling
- WebSocket connection management
This WebSocket implementation is useful for:
- Web Applications: Integrate FastMCP capabilities into web frontends
- Real-time Services: Enable real-time AI-powered features in applications
- Distributed Systems: Connect remote clients to FastMCP servers across networks
-
Start the server:
pip install -r requirements.txt python server.py
This will start the FastAPI server on port 8765.
-
Run the client in a separate terminal:
python client.py
The client will connect to the server, discover available capabilities, and demonstrate various interactions.
Or with uv:
-
Start the server:
uv run server.py
-
Run the client in a separate terminal:
uv run client.py
This project uses the following dependencies:
- mcpsock (>=0.1.0): The WebSocket implementation of the MCP protocol
- FastAPI (>=0.110.0,<0.116.0): Web framework for building the WebSocket server
- Uvicorn (>=0.27.0,<0.35.0): ASGI server for running the FastAPI application
- FastMCP (>=2.2.0,<2.3.0): The FastMCP library for creating MCP servers and clients
- Websockets (>=12.0,<16.0): WebSocket implementation for Python
- Starlette (>=0.46.2): ASGI framework used by FastAPI
We strongly recommend using uv for package management, as it's significantly faster than pip and provides better dependency resolution. This is the same approach used by FastMCP.
This repository includes a uv.lock
file to ensure consistent dependency resolution across different environments.
If you don't have uv installed:
- macOS:
brew install uv
- Linux:
curl -LsSf https://astral.sh/uv/install.sh | sh
- Windows:
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
For more installation options, see the uv documentation.
-
Clone the repository:
git clone https://github.com/your-repo/fastmcp-websockets-example.git cd fastmcp-websockets-example
-
Create a virtual environment:
uv venv
-
Install the package and dependencies:
uv pip install -e .
-
For development:
uv pip install -e ".[dev]"
If you prefer using traditional pip:
-
Create a virtual environment:
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install the package and dependencies:
pip install -e .
-
For development:
pip install -e ".[dev]"
The mcpsock
package is a standalone implementation of the MCP (Model Context Protocol) over WebSockets. It provides:
- A server-side implementation (
WebSocketServer
) for handling MCP requests - A client-side implementation (
WebSocketClient
) for connecting to MCP servers - A clean, decorator-based API for defining MCP endpoints
- Full support for the MCP protocol, including tools, resources, and prompts
The package is designed to be easy to use and integrate with existing FastAPI applications.
This example is provided under the same license as FastMCP (Apache-2.0).