|
| 1 | +# File Structure |
| 2 | + |
| 3 | +This example demonstrates session roaming across multiple MCP server instances. |
| 4 | + |
| 5 | +## Directory Structure |
| 6 | + |
| 7 | +``` |
| 8 | +simple-streamablehttp-roaming/ |
| 9 | +├── README.md # Comprehensive documentation |
| 10 | +├── QUICKSTART.md # 5-minute getting started guide |
| 11 | +├── FILES.md # This file |
| 12 | +├── pyproject.toml # Project configuration |
| 13 | +├── Dockerfile # Docker container definition |
| 14 | +├── docker-compose.yml # Multi-instance deployment |
| 15 | +├── nginx.conf # Load balancer configuration |
| 16 | +├── test_roaming.sh # Automated test script |
| 17 | +├── .gitignore # Git ignore patterns |
| 18 | +└── mcp_simple_streamablehttp_roaming/ |
| 19 | + ├── __init__.py # Package initialization |
| 20 | + ├── __main__.py # Module entry point |
| 21 | + ├── server.py # Main server implementation |
| 22 | + └── redis_event_store.py # Redis EventStore implementation |
| 23 | +
|
| 24 | +``` |
| 25 | + |
| 26 | +## File Purposes |
| 27 | + |
| 28 | +### Documentation |
| 29 | + |
| 30 | +- **README.md** (486 lines) |
| 31 | + - Comprehensive guide to session roaming |
| 32 | + - Architecture diagrams and explanations |
| 33 | + - Production deployment examples (Kubernetes, Docker Compose) |
| 34 | + - Testing instructions |
| 35 | + - Implementation details |
| 36 | + |
| 37 | +- **QUICKSTART.md** (381 lines) |
| 38 | + - Get started in 5 minutes |
| 39 | + - Step-by-step local development setup |
| 40 | + - Docker Compose deployment guide |
| 41 | + - Manual testing examples |
| 42 | + - Common issues and solutions |
| 43 | + |
| 44 | +- **FILES.md** (This file) |
| 45 | + - Overview of file structure |
| 46 | + - Purpose of each file |
| 47 | + |
| 48 | +### Python Package |
| 49 | + |
| 50 | +- **mcp_simple_streamablehttp_roaming/__init__.py** (3 lines) |
| 51 | + - Package version information |
| 52 | + |
| 53 | +- **mcp_simple_streamablehttp_roaming/__main__.py** (5 lines) |
| 54 | + - Entry point for running as module |
| 55 | + |
| 56 | +- **mcp_simple_streamablehttp_roaming/server.py** (169 lines) |
| 57 | + - Main MCP server implementation |
| 58 | + - Command-line interface |
| 59 | + - Tool: `get-instance-info` (shows which instance handles request) |
| 60 | + - Session manager configuration with EventStore |
| 61 | + - Starlette ASGI application |
| 62 | + |
| 63 | +- **mcp_simple_streamablehttp_roaming/redis_event_store.py** (154 lines) |
| 64 | + - Production-ready Redis EventStore implementation |
| 65 | + - Persistent event storage |
| 66 | + - Event replay functionality |
| 67 | + - Shared across all instances |
| 68 | + |
| 69 | +### Configuration |
| 70 | + |
| 71 | +- **pyproject.toml** (44 lines) |
| 72 | + - Project metadata |
| 73 | + - Dependencies (mcp, redis, starlette, uvicorn, etc.) |
| 74 | + - CLI script registration |
| 75 | + - Build configuration |
| 76 | + - Development tools (pyright, pytest, ruff) |
| 77 | + |
| 78 | +- **.gitignore** (35 lines) |
| 79 | + - Python artifacts |
| 80 | + - Virtual environments |
| 81 | + - IDE files |
| 82 | + - Cache directories |
| 83 | + |
| 84 | +### Deployment |
| 85 | + |
| 86 | +- **Dockerfile** (20 lines) |
| 87 | + - Multi-stage Python container |
| 88 | + - Uses uv for dependency management |
| 89 | + - Optimized for production |
| 90 | + |
| 91 | +- **docker-compose.yml** (85 lines) |
| 92 | + - Redis service (persistent event store) |
| 93 | + - 3 MCP server instances (ports 3001, 3002, 3003) |
| 94 | + - NGINX load balancer (port 80) |
| 95 | + - Health checks and dependencies |
| 96 | + - Volume management |
| 97 | + |
| 98 | +- **nginx.conf** (60 lines) |
| 99 | + - Round-robin load balancing (NO sticky sessions!) |
| 100 | + - SSE support configuration |
| 101 | + - CORS headers |
| 102 | + - MCP-Session-ID header pass-through |
| 103 | + - Health check endpoint |
| 104 | + |
| 105 | +### Testing |
| 106 | + |
| 107 | +- **test_roaming.sh** (100 lines) |
| 108 | + - Automated test script |
| 109 | + - Creates session on Instance 1 |
| 110 | + - Calls tool on Instance 1 |
| 111 | + - Uses same session on Instance 2 |
| 112 | + - Verifies session roaming works |
| 113 | + - Detailed success/failure reporting |
| 114 | + |
| 115 | +## Key Features Demonstrated |
| 116 | + |
| 117 | +### 1. Session Roaming |
| 118 | +- Sessions move freely between instances |
| 119 | +- No sticky sessions required |
| 120 | +- EventStore provides continuity |
| 121 | + |
| 122 | +### 2. Production Deployment |
| 123 | +- Docker Compose for local testing |
| 124 | +- Kubernetes manifests in README |
| 125 | +- NGINX load balancing example |
| 126 | +- Redis persistence configuration |
| 127 | + |
| 128 | +### 3. Developer Experience |
| 129 | +- Automated testing script |
| 130 | +- Comprehensive documentation |
| 131 | +- Quick start guide |
| 132 | +- Clear error messages |
| 133 | +- Detailed logging |
| 134 | + |
| 135 | +### 4. Code Quality |
| 136 | +- Type hints throughout |
| 137 | +- Comprehensive docstrings |
| 138 | +- Configuration via CLI arguments |
| 139 | +- Environment-based configuration |
| 140 | +- Proper error handling |
| 141 | + |
| 142 | +## Usage Examples |
| 143 | + |
| 144 | +### Local Development |
| 145 | +```bash |
| 146 | +# Terminal 1 |
| 147 | +uv run mcp-streamablehttp-roaming --port 3001 --instance-id instance-1 |
| 148 | + |
| 149 | +# Terminal 2 |
| 150 | +uv run mcp-streamablehttp-roaming --port 3002 --instance-id instance-2 |
| 151 | + |
| 152 | +# Terminal 3 |
| 153 | +./test_roaming.sh |
| 154 | +``` |
| 155 | + |
| 156 | +### Docker Compose |
| 157 | +```bash |
| 158 | +docker-compose up -d |
| 159 | +# Access via http://localhost/mcp (load balanced) |
| 160 | +# or directly via http://localhost:3001/mcp, :3002/mcp, :3003/mcp |
| 161 | +``` |
| 162 | + |
| 163 | +### Manual Testing |
| 164 | +```bash |
| 165 | +# Create session on Instance 1 |
| 166 | +curl -X POST http://localhost:3001/mcp -H "Content-Type: application/json" ... |
| 167 | + |
| 168 | +# Use session on Instance 2 |
| 169 | +curl -X POST http://localhost:3002/mcp -H "MCP-Session-ID: <session-id>" ... |
| 170 | +``` |
| 171 | + |
| 172 | +## Total Lines of Code |
| 173 | + |
| 174 | +- Python code: ~331 lines |
| 175 | +- Configuration: ~149 lines |
| 176 | +- Documentation: ~867 lines |
| 177 | +- Testing: ~100 lines |
| 178 | +- **Total: ~1,447 lines** |
| 179 | + |
| 180 | +## Implementation Highlights |
| 181 | + |
| 182 | +### Minimal Code for Maximum Impact |
| 183 | + |
| 184 | +**Enable session roaming with just:** |
| 185 | +```python |
| 186 | +event_store = RedisEventStore(redis_url="redis://localhost:6379") |
| 187 | +manager = StreamableHTTPSessionManager(app=app, event_store=event_store) |
| 188 | +``` |
| 189 | + |
| 190 | +### No Special Session Store Needed |
| 191 | + |
| 192 | +The EventStore alone enables: |
| 193 | +- ✅ Event replay (resumability) |
| 194 | +- ✅ Session roaming (distributed sessions) |
| 195 | +- ✅ Horizontal scaling |
| 196 | +- ✅ High availability |
| 197 | + |
| 198 | +### Production-Ready Patterns |
| 199 | + |
| 200 | +- Redis persistence (AOF enabled) |
| 201 | +- Health checks |
| 202 | +- Graceful shutdown |
| 203 | +- Comprehensive logging |
| 204 | +- Environment-based configuration |
| 205 | +- CORS support |
| 206 | + |
| 207 | +## Related Files in SDK |
| 208 | + |
| 209 | +The example uses these SDK components: |
| 210 | + |
| 211 | +- `mcp.server.streamable_http_manager.StreamableHTTPSessionManager` - Session management |
| 212 | +- `mcp.server.streamable_http.EventStore` - Interface for event storage |
| 213 | +- `mcp.server.lowlevel.Server` - Core MCP server |
| 214 | +- `mcp.types` - MCP protocol types |
| 215 | + |
| 216 | +## Contributing |
| 217 | + |
| 218 | +To extend this example: |
| 219 | + |
| 220 | +1. **Add new tools** - Modify `server.py` to add more tool handlers |
| 221 | +2. **Custom EventStore** - Implement EventStore for other databases |
| 222 | +3. **Monitoring** - Add Prometheus metrics or OpenTelemetry |
| 223 | +4. **Authentication** - Add auth middleware to Starlette app |
| 224 | +5. **Rate limiting** - Add rate limiting middleware |
| 225 | + |
| 226 | +See README.md for more details on each approach. |
0 commit comments