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


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.4.tar.gz (22.5 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.4-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

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

Hashes for mcpsock-0.1.4.tar.gz
Algorithm Hash digest
SHA256 cb908257d2eeba06e9ad114e61b343cc8bcd1026a153bb429b70a7761b0a10c7
MD5 79353abd4ccd21fc7641eb5a378c0b61
BLAKE2b-256 d1ccb2bf2ada4ef711127588b3b3b7a671ea6424a33cfaf3d0b744b039677c92

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcpsock-0.1.4.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.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

Hashes for mcpsock-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 08a75377ad2e0e8f6a84f9b60901fb4b4a065e0cabe82e7cea113243040ca571
MD5 dbc21682b6b9d65a1e3528f2a3ad4c22
BLAKE2b-256 bffc7d16912eabc101d821665fad5973aae2fd414d54eddd261ca672edb49ddc

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcpsock-0.1.4-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