Skip to main content

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 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcpsock-0.1.5.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcpsock-0.1.5-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file mcpsock-0.1.5.tar.gz.

File metadata

  • Download URL: mcpsock-0.1.5.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mcpsock-0.1.5.tar.gz
Algorithm Hash digest
SHA256 2c4967815c24460a5659a2f45fa945bfecc87a2fc1d9eeab19451ca6cbcdf6c2
MD5 d9aaafa28dfff374087bfad64907a4de
BLAKE2b-256 47ad5505a9fb1467eb64df9751b900d14837102502105ec309148ab25e00fa8e

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcpsock-0.1.5.tar.gz:

Publisher: publish.yml on thecodekitchen/mcpsock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mcpsock-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: mcpsock-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mcpsock-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a5e3ca1a4cd4415fd2a278eb7b0f87277d25e144da4ae86ccf69189c77427eae
MD5 fc4e94deb1864fd7f8b94b1f772cdabf
BLAKE2b-256 cf5eab4789af2e4cd05fd1c467ecc87032515064eca7d0f86633998307c62008

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcpsock-0.1.5-py3-none-any.whl:

Publisher: publish.yml on thecodekitchen/mcpsock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page