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
- Performance: Uses Nginx and Nchan to efficiently handle long-lived connections, outperforming pure Python implementations.
- Scalability: Leverages Nginx’s capability to manage a large number of concurrent connections.
- Easy Deployment: Packaged with Docker for straightforward deployment and horizontal scaling.
- Protocol Adaptability: Automatically detects and adapts to the optimal connection mode (WebSocket/SSE).
- Reliability: Ensures robust messaging with Nchan's caching and delivery mechanism.
- Extensibility: Easily integrate third-party services via OpenAPI specifications.
Limitations
- Nginx Dependency: Requires Nginx with the Nchan module.
- Configuration Complexity: Demands accurate configuration for both Nginx and the application.
- 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
- session_id (identical to nchan_channel_id) remains valid from connection creation to termination.
- Fast responses for short tasks.
- 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
- Clone the project:
git clone https://github.com/yourusername/nchan-mcp-transport.git
cd nchan-mcp-transport
- 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:
- Entry route:
/mcp/{server_name} - Channel configuration:
/mcp/{server_name}/{channel_id} - Internal processing:
/internal/mcp-process
Contribution Guidelines
Contributions via issues and pull requests are welcome!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file httmcp-0.1.3.tar.gz.
File metadata
- Download URL: httmcp-0.1.3.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e45075787b13828ee29fa79fb82fa89b451fd5c53781b65471559e4f177ea62
|
|
| MD5 |
3e508937ca17b29b3d2d9afaf14b968f
|
|
| BLAKE2b-256 |
67f972cc30126facd25e24145fad9622e4c701e6999cb2c2f6e28b649ce37e7b
|
File details
Details for the file httmcp-0.1.3-py3-none-any.whl.
File metadata
- Download URL: httmcp-0.1.3-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44ba0cbff0e322287b8d2cf277f8678fdb04b929142ca05c12c737de06a1dc4a
|
|
| MD5 |
3f2d3a301cb9efd942c061b8a044e826
|
|
| BLAKE2b-256 |
ee56a11e68ef1d8111003c54b4885692c2f37e94c7db987f2e3b7d3624fc1dc2
|