Skip to main content

🐍 Python mcp-websocket-transport

PyPI Version Python Support License: MIT

Full-duplex WebSocket Transport for the Model Context Protocol (MCP) in Python. Built on AnyIO and compatible with FastAPI, Starlette, and standalone websockets.


📦 Installation

# Using uv (recommended)
uv add mcp-websocket-transport

# With pip
pip install mcp-websocket-transport

# With all extras (FastAPI, uvicorn, websockets, mcp CLI)
pip install "mcp-websocket-transport[all]"

🛠️ Quickstart

1. FastMCP Server with WebSocket (examples/fastmcp_server.py)

import asyncio
import websockets
from mcp.server.fastmcp import FastMCP
from mcp_websocket_transport import serve_websocket

mcp = FastMCP("calculator-server")

@mcp.tool()
def add(a: float, b: float) -> float:
    """Adds two numbers."""
    return a + b

@mcp.tool()
def get_stock(symbol: str) -> dict:
    """Fetches mock stock price."""
    return {"symbol": symbol.upper(), "price": 225.50}

async def main():
    async def handler(websocket):
        await serve_websocket(mcp._mcp_server, websocket)

    print("🚀 MCP WebSocket Server running on ws://0.0.0.0:8765")
    async with websockets.serve(handler, "0.0.0.0", 8765):
        await asyncio.Future()

if __name__ == "__main__":
    asyncio.run(main())

2. Python Client (examples/mcp_tool_client.py)

import asyncio
from mcp.client.session import ClientSession
from mcp_websocket_transport import WebSocketClientTransport

async def main():
    async with WebSocketClientTransport("ws://localhost:8765") as (read_stream, write_stream):
        async with ClientSession(read_stream, write_stream) as session:
            await session.initialize()
            
            # List available tools
            tools = await session.list_tools()
            print("Tools:", [t.name for t in tools.tools])
            
            # Call tool
            result = await session.call_tool("add", {"a": 15, "b": 27})
            print("Result:", result.content[0].text)

if __name__ == "__main__":
    asyncio.run(main())

🌉 Desktop Host Bridge (mcp-ws-bridge)

To connect Claude Desktop, LM Studio, Cursor, or Antigravity to a running WebSocket server:

{
  "mcpServers": {
    "my-websocket-tools": {
      "command": "uv",
      "args": [
        "run",
        "--directory",
        "path/to/mcp-websocket/python",
        "python",
        "-m",
        "mcp_websocket_transport.bridge",
        "--url",
        "ws://localhost:8767"
      ]
    }
  }
}

🧪 Testing

uv run --all-extras pytest -v

📄 License

MIT License.

Download files

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

Source Distribution

mcp_websocket_transport-1.0.1.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

mcp_websocket_transport-1.0.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file mcp_websocket_transport-1.0.1.tar.gz.

File metadata

  • Download URL: mcp_websocket_transport-1.0.1.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.6 {"installer":{"name":"uv","version":"0.12.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for mcp_websocket_transport-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f6ab2b6f70eabf071ec832a9205b5885a3aa96f5dc6fdf3d847c99c7f4ca1218
MD5 f4a50593eccf4af3b99a5b9dd80d91fb
BLAKE2b-256 28e4fbc5e92ff0b26957f5b6fdbca943cb0763ba84dc65c19cea7fbbcd6139cf

See more details on using hashes here.

File details

Details for the file mcp_websocket_transport-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: mcp_websocket_transport-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.6 {"installer":{"name":"uv","version":"0.12.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for mcp_websocket_transport-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7a4f7632f72d801718c6e9e1ea698fabb5153777ea4763530ac01faa2398de97
MD5 401723067de99cb4c8d24c86441eab4c
BLAKE2b-256 b4746105b701c054c875ea7d64e10781a6e59a78bae28fd359adc5707ee9fcf4

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.2

2 files

This release

1.0.1 This release

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page