20. Dual MCP Transport Protocol (STDIO + StreamableHTTP)
Date: 2025-10-13Status
AcceptedCategory
Core ArchitectureContext
The Model Context Protocol (MCP) is Anthropic’s standard for exposing AI agents and tools. MCP enables:- Tool Discovery: Clients discover available tools via standardized protocol
- Execution: Clients invoke tools with structured inputs/outputs
- Streaming: Real-time streaming of results
- Interoperability: Multiple clients can consume same MCP server
Deployment Scenarios
-
Local Development (Claude Desktop, IDEs)
- Requirement: Simple local process communication
- Constraint: No network setup, firewall configuration
- User: Individual developers
-
Cloud Deployments (Kubernetes, Cloud Run)
- Requirement: HTTP-based communication
- Constraint: Pods/containers need standard HTTP endpoints
- User: Production deployments
-
CI/CD Pipelines
- Requirement: Programmatic execution via stdin/stdout
- Constraint: No persistent processes
- User: Automated testing
-
Web Applications
- Requirement: Browser-compatible streaming
- Constraint: WebSocket or HTTP streaming support
- User: End users accessing via web UI
Transport Protocol Trade-offs
| Transport | Pros | Cons | Use Case |
|---|---|---|---|
| STDIO | Simple, no network, universal | Local only, no streaming, process-bound | Local dev, CLI |
| HTTP/REST | Standard, firewall-friendly, scalable | No bidirectional streaming | Cloud APIs |
| WebSocket | Bidirectional, real-time | Complex, requires WS support | Real-time apps |
| gRPC | Efficient, streaming, type-safe | Complex setup, limited browser support | Microservices |
Decision
We will implement dual MCP transport support:- STDIO Transport: For local development and Claude Desktop integration
- StreamableHTTP Transport: For cloud deployments and web applications
Architecture
Transport 1: STDIO (Standard Input/Output)
Protocol: JSON-RPC over stdin/stdoutTransport 2: StreamableHTTP
Protocol: HTTP with Server-Sent Events (SSE) for streamingConsequences
Positive Consequences
- Flexibility: Support both local and cloud deployments
- Developer Experience: Easy local testing with STDIO, production deployment via HTTP
- Streaming: HTTP transport enables real-time streaming to web UIs
- Compatibility: STDIO works with existing MCP clients (Claude Desktop)
- No Lock-in: Can switch transports without changing agent logic
Negative Consequences
- Maintenance Burden: Two codepaths to maintain and test
- Documentation Overhead: Must document both transports
- Complexity: Developers must choose correct transport for their use case
- Testing: Must test both transports independently
Neutral Consequences
- Separate Entry Points: Two different commands to start server
- Configuration: Different env vars for each transport (e.g., HTTP port)
Implementation Details
Shared Agent Logic
Both transports use the same core agent:STDIO Transport Details
File:src/mcp_server_langgraph/mcp/server_stdio.py (200+ lines)
Key Features:
- JSON-RPC 2.0 protocol
- Stdin/stdout communication
- Synchronous request/response (no streaming)
- Process lifecycle management
StreamableHTTP Transport Details
File:src/mcp_server_langgraph/mcp/server_streamable.py (300+ lines)
Key Features:
- RESTful HTTP endpoints
- Server-Sent Events (SSE) for streaming
- FastAPI for async handling
- CORS support for web clients
- Authentication via JWT
Configuration Matrix
| Feature | STDIO | StreamableHTTP |
|---|---|---|
| Port | N/A | 8000 (configurable) |
| Host | localhost only | 0.0.0.0 (configurable) |
| Streaming | No | Yes (SSE) |
| Authentication | No | Yes (JWT) |
| CORS | N/A | Yes (configurable) |
| TLS/SSL | N/A | Yes (via reverse proxy) |
Alternatives Considered
1. STDIO Only
Description: Support only STDIO transport Pros:- Simpler (single codebase)
- Easy local development
- Works with Claude Desktop
- Cannot deploy to cloud (no HTTP endpoint)
- No web UI support (no streaming)
- Limited scalability (single process)
2. HTTP Only
Description: Support only HTTP transport Pros:- Cloud-ready
- Streaming support
- Scalable
- Poor local dev experience (need to start server, configure port)
- Incompatible with Claude Desktop (expects STDIO)
- Firewall issues for local development
3. WebSocket Only
Description: Use WebSocket for bidirectional streaming Pros:- True bidirectional streaming
- Real-time updates
- Efficient for high-frequency messages
- More complex than HTTP/SSE
- Firewall issues (WS often blocked by corporate firewalls)
- Browser compatibility (some old browsers lack support)
- Harder to debug (cannot use curl/postman easily)
4. gRPC
Description: Use gRPC for efficient binary protocol Pros:- Efficient binary encoding
- Built-in streaming
- Type-safe (Protobuf)
- Browser support poor (requires grpc-web proxy)
- Debugging difficult (binary protocol)
- Overkill for MCP use case
- Complexity (Protobuf definitions, code generation)
5. Single Transport with Adapters
Description: Single MCP server with pluggable transport adapters Pros:- Cleaner abstraction
- Easier to add new transports
- More complex architecture
- Premature abstraction (only 2 transports needed)
- Testing overhead (adapter layer + transports)
Deployment Examples
Local Development (STDIO)
Kubernetes (StreamableHTTP)
Docker Compose (StreamableHTTP)
Cloud Run (StreamableHTTP)
Performance Characteristics
STDIO Transport
| Metric | Value | Notes |
|---|---|---|
| Latency | ~5-10ms | Process startup overhead |
| Throughput | 1 request/process | Single request per process lifecycle |
| Concurrency | 1 | No concurrent requests |
| Memory | 50-100MB | Per process |
StreamableHTTP Transport
| Metric | Value | Notes |
|---|---|---|
| Latency | ~20-50ms | HTTP + network overhead |
| Throughput | 100+ req/s | Async handling |
| Concurrency | 1000+ | Async I/O |
| Memory | 200-500MB | Shared across requests |
Testing Strategy
STDIO Transport Tests
StreamableHTTP Transport Tests
Future Enhancements
- WebSocket Transport: For real-time bidirectional communication
- gRPC Transport: For microservice integrations
- HTTP/2 Support: For multiplexed streaming
- Transport Auto-Detection: Server detects transport from request
References
- MCP Specification: https://modelcontextprotocol.io/
- STDIO Implementation:
src/mcp_server_langgraph/mcp/server_stdio.py - HTTP Implementation:
src/mcp_server_langgraph/mcp/server_streamable.py - Entry Points:
pyproject.toml:83-86 - FastAPI SSE: https://fastapi.tiangolo.com/advanced/custom-response/#streamingresponse
- JSON-RPC 2.0: https://www.jsonrpc.org/specification