MCPSock
MCPSock is a Python library that provides WebSocket client and server implementations for the FastMCP framework. 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.
Metadata
Release files for mcpsock 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mcpsock-0.1.5.tar.gz | 26.4 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mcpsock-0.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 41.7 kB
Release files / mcpsock-0.1.5.tar.gz
| Download URL | mcpsock-0.1.5.tar.gz |
|---|---|
| Size | 26.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2c4967815c24460a5659a2f45fa945bfecc87a2fc1d9eeab19451ca6cbcdf6c2
|
|
BLAKE2b-256 checksum How to use checksums |
47ad5505a9fb1467eb64df9751b900d14837102502105ec309148ab25e00fa8e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 26, 2025.
Transparency logRelease files / mcpsock-0.1.5-py3-none-any.whl
| Download URL | mcpsock-0.1.5-py3-none-any.whl |
|---|---|
| Size | 15.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a5e3ca1a4cd4415fd2a278eb7b0f87277d25e144da4ae86ccf69189c77427eae
|
|
BLAKE2b-256 checksum How to use checksums |
cf5eab4789af2e4cd05fd1c467ecc87032515064eca7d0f86633998307c62008
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 26, 2025.
Transparency logSecurity for mcpsock 0.1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for mcpsock-0.1.5.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 26, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.