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

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/yourusername/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.0.tar.gz (20.8 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.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mcpsock-0.1.0.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.16

File hashes

Hashes for mcpsock-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c6890d11eec88f90c74ede6a86d3d326c5a27d461bb78fe352d5ff7b5a77a40d
MD5 8a01b7cb6ec539efc8a7daf0a9c4bb20
BLAKE2b-256 7eface392d33a5479a0ff3de3bceda10d24277eba2cf658d43dec5a3d0d9d153

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mcpsock-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.16

File hashes

Hashes for mcpsock-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 574a7396b5374c24c791b205df1f03ba9ead63f260dd4de733ff69b10f3e9043
MD5 e41c00f7ea997f10cde8b374e74543c7
BLAKE2b-256 87e922c64aece2d261f276d09d335aa830aaba60d9a64a444b589ee9277145b9

See more details on using hashes here.

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