Skip to main content

HTTP MCP Transport for Nchan

Project description

Nchan MCP Transport

A high-performance MCP (Model Control Protocol) transport layer implementation based on Nginx + Nchan, supporting both WebSocket and Server-Sent Events (SSE) to provide a reliable communication channel for AI models.

Project Overview

Nchan-MCP-Transport is a middleware service that not only provides a high-performance MCP transport layer—with support for WebSocket and Server-Sent Events for reliable AI model communication—but also functions as a gateway for request routing and protocol adaptation. Additionally, this project includes the Python library httmcp (HyperText Transfer Model Control Protocol) to simplify the development and integration of MCP tools.

Features

  • Dual Protocol Support: Supports both WebSocket and SSE transport modes.
  • High Performance: Leverages Nginx + Nchan for an efficient publish/subscribe messaging system.
  • MCP Protocol Implementation: Fully compliant with the MCP protocol specifications.
  • Easy Integration: Provides a simple API design via the FastAPI framework.
  • Session Management: Automatically handles MCP session creation and maintenance.
  • Tooling System: Supports definition and invocation of MCP tools.
  • Resource Management: Comes with built-in resource management capabilities.
  • OpenAPI Integration: Automatically converts OpenAPI specifications into MCP services.

Advantages

  1. Performance: Uses Nginx and Nchan to efficiently handle long-lived connections, outperforming pure Python implementations.
  2. Scalability: Leverages Nginx’s capability to manage a large number of concurrent connections.
  3. Easy Deployment: Packaged with Docker for straightforward deployment and horizontal scaling.
  4. Protocol Adaptability: Automatically detects and adapts to the optimal connection mode (WebSocket/SSE).
  5. Reliability: Ensures robust messaging with Nchan's caching and delivery mechanism.
  6. Extensibility: Easily integrate third-party services via OpenAPI specifications.

Limitations

  1. Nginx Dependency: Requires Nginx with the Nchan module.
  2. Configuration Complexity: Demands accurate configuration for both Nginx and the application.
  3. Debugging Difficulty: Distributed system challenges may complicate troubleshooting.

Architecture

  • Frontend Proxy: Nginx + Nchan module
  • Backend Service: FastAPI + HTTMCP
  • Containerization: Docker
  • Communication Protocol: MCP (Model Control Protocol)

Sequence Diagram

  1. session_id (identical to nchan_channel_id) remains valid from connection creation to termination.
  2. Fast responses for short tasks.
  3. Asynchronous queue processing for long tasks with progress notifications and final results.
sequenceDiagram
  MCP Client->>NCNAN: connect
  activate NCNAN
  MCP Client-->>NCNAN: jsonrpc request
  NCNAN-->>FastAPI: nchan_publisher_upstream_request
  FastAPI-->>MCP Server: call_tool(name, args)
  MCP Server-->>FastAPI: result
  FastAPI-->>NCNAN: jsonrpc response
  NCNAN-->> MCP Client: jsonrpc response
  
  MCP Client-->>NCNAN: jsonrpc request
  NCNAN-->>FastAPI: nchan_publisher_upstream_request
  FastAPI-->>MCP Server: call_tool(name, args) in backend
  MCP Server-->>NCNAN: push notification
  NCNAN-->> MCP Client: notification
  MCP Server-->>NCNAN: push jsonrpc response
  NCNAN-->> MCP Client: jsonrpc response
  NCNAN->> MCP Client: close
  deactivate NCNAN

Quick Start

Install dependencies locally via pip:

pip install httmcp

Deployment

  1. Clone the project:
git clone https://github.com/yourusername/nchan-mcp-transport.git
cd nchan-mcp-transport
  1. Start the service:
docker-compose up -d

Usage

Creating an MCP Server

server = HTTMCP(
    "httmcp",
    publish_server="http://nchan:80",
)

Defining Custom Tools

In app/app.py, add your custom tool using a decorator:

@server.tool()
async def your_tool_name(param1: type, param2: type) -> return_type:
    # Your tool logic here
    return result

Defining Custom Resources

@server.resource("resource://my-resource")
def get_data() -> str:
    return "Hello, world!"

Running the Server

app = FastAPI()

# Supports multiple MCP servers on one FastAPI instance
app.include_router(server.router)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

OpenAPIMCP Integration

Leverage OpenAPI to automatically create MCP tools:

async def create_openapi_mcp_server():
    url = "https://example.com/api-spec.json"  # OpenAPI spec URL
    openapi_server = await OpenAPIMCP.from_openapi(url, publish_server="http://nchan:80")
    app.include_router(openapi_server.router)

asyncio.run(create_openapi_mcp_server())

This will convert all operations defined in the OpenAPI spec into MCP tools.

Server Configuration

Detailed Nginx configuration is in the docker/nchan.conf file and includes:

  1. Entry route: /mcp/{server_name}
  2. Channel configuration: /mcp/{server_name}/{channel_id}
  3. Internal processing: /internal/mcp-process

Contribution Guidelines

Contributions via issues and pull requests are welcome!

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

httmcp-0.2.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

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

httmcp-0.2.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file httmcp-0.2.0.tar.gz.

File metadata

  • Download URL: httmcp-0.2.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.2

File hashes

Hashes for httmcp-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ae6cc92a393bca75fa8635a89ca4973eb1ecd06cd01d225416253556a63b7c2d
MD5 c55292458f76ad57ed8c572cb53cd621
BLAKE2b-256 d26a37bfcc48f9423560dd711d4699536c3d6d66d4d4df6bb448b46346642ef7

See more details on using hashes here.

File details

Details for the file httmcp-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: httmcp-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.2

File hashes

Hashes for httmcp-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a478de4c6c84c9ce11244ab54d0a55e8f4981ac92481c0f727f9d9b392cefe3e
MD5 32fac8ee7b1a6215c61b167634ce2067
BLAKE2b-256 f47630355523d40612d8e73f8adbf7205a411a145986831675c4e6b61f4e0ee5

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