WebSocket client and server implementation for FastMCP framework
Project description
MCPSock
MCPSock is a Python library that provides WebSocket client and server implementations for the FastMCP protocol. It offers a clean, easy-to-use API for building WebSocket-based applications that communicate using the FastMCP framework.
Features
- WebSocketClient: A standalone client for connecting to FastMCP WebSocket servers
- WebSocketServer: A decorator-based router for handling FastMCP WebSocket communications
- Support for tools, resources, and prompts
- Easy integration with FastAPI applications
- Async/await support
Installation
with pip
pip install mcpsock
or with uv (recommended)
uv add mcpsock
Quick Start
Client Example
import asyncio
from mcpsock import WebSocketClient
async def main():
# Connect to a FastMCP WebSocket server
async with WebSocketClient("ws://localhost:8000/ws") as client:
# List available tools
tools = await client.list_tools()
print(f"Available tools: {tools}")
# Call a tool
result = await client.call_tool("example/tool", {"param": "value"})
print(f"Tool result: {result}")
if __name__ == "__main__":
asyncio.run(main())
Server Example
from fastapi import FastAPI
from mcpsock import WebSocketServer
import uvicorn
# Create a FastAPI app
app = FastAPI()
# Create a WebSocketServer
router = WebSocketServer()
# Register a tool handler
@router.tool("example/tool")
async def example_tool(message, websocket):
params = message.get("params", {})
return {"result": f"Processed: {params}"}
# Register a resource handler
@router.resource("example/resource")
async def example_resource(message, websocket):
return {"data": "This is example resource data"}
# Attach the router to the app
router.attach_to_app(app, "/ws")
# Run the server
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
WebSocketClient
The WebSocketClient class provides methods for connecting to a FastMCP WebSocket server and interacting with it.
client = WebSocketClient("ws://localhost:8000/ws")
await client.connect()
WebSocketServer
The WebSocketServer class provides a decorator-based API for registering handlers for FastMCP messages.
router = WebSocketServer()
@router.tool("example/tool")
async def example_tool(message, websocket):
# Handle tool call
return {"result": "success"}
Development
Setup
# Clone the repository
git clone https://github.com/thecodekitchen/mcpsock.git
cd mcpsock
# Install development dependencies
pip install -e ".[dev]"
Running Tests
pytest
License
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
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 mcpsock-0.1.4.tar.gz.
File metadata
- Download URL: mcpsock-0.1.4.tar.gz
- Upload date:
- Size: 22.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb908257d2eeba06e9ad114e61b343cc8bcd1026a153bb429b70a7761b0a10c7
|
|
| MD5 |
79353abd4ccd21fc7641eb5a378c0b61
|
|
| BLAKE2b-256 |
d1ccb2bf2ada4ef711127588b3b3b7a671ea6424a33cfaf3d0b744b039677c92
|
Provenance
The following attestation bundles were made for mcpsock-0.1.4.tar.gz:
Publisher:
publish.yml on thecodekitchen/mcpsock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcpsock-0.1.4.tar.gz -
Subject digest:
cb908257d2eeba06e9ad114e61b343cc8bcd1026a153bb429b70a7761b0a10c7 - Sigstore transparency entry: 202996372
- Sigstore integration time:
-
Permalink:
thecodekitchen/mcpsock@c3fc22c587d480ed5ef1beb490368ca632f1a953 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/thecodekitchen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c3fc22c587d480ed5ef1beb490368ca632f1a953 -
Trigger Event:
release
-
Statement type:
File details
Details for the file mcpsock-0.1.4-py3-none-any.whl.
File metadata
- Download URL: mcpsock-0.1.4-py3-none-any.whl
- Upload date:
- Size: 14.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08a75377ad2e0e8f6a84f9b60901fb4b4a065e0cabe82e7cea113243040ca571
|
|
| MD5 |
dbc21682b6b9d65a1e3528f2a3ad4c22
|
|
| BLAKE2b-256 |
bffc7d16912eabc101d821665fad5973aae2fd414d54eddd261ca672edb49ddc
|
Provenance
The following attestation bundles were made for mcpsock-0.1.4-py3-none-any.whl:
Publisher:
publish.yml on thecodekitchen/mcpsock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcpsock-0.1.4-py3-none-any.whl -
Subject digest:
08a75377ad2e0e8f6a84f9b60901fb4b4a065e0cabe82e7cea113243040ca571 - Sigstore transparency entry: 202996376
- Sigstore integration time:
-
Permalink:
thecodekitchen/mcpsock@c3fc22c587d480ed5ef1beb490368ca632f1a953 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/thecodekitchen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c3fc22c587d480ed5ef1beb490368ca632f1a953 -
Trigger Event:
release
-
Statement type: