|
| 1 | +# MCP Everything Server |
| 2 | + |
| 3 | +_Note: these docs were AI generated based on a claude code transcript, and then edited manually for accuracy_ |
| 4 | + |
| 5 | +A comprehensive example implementation of a scalable Model Context Protocol (MCP) server that demonstrates all MCP functionality with full authentication support and horizontal scalability. |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +The Everything Server is an open-source reference implementation that showcases: |
| 10 | +- **Complete [MCP Protocol](https://modelcontextprotocol.io/specification) Support**: All MCP features including tools, resources, prompts, sampling, completions, and logging |
| 11 | +- **Multiple [Transport Methods](https://modelcontextprotocol.io/docs/concepts/transports)**: Streamable HTTP (SHTTP) and Server-Sent Events (SSE) |
| 12 | +- **Comprehensive Auth**: OAuth 2.0 with fake upstream provider for testing |
| 13 | +- **Horizontal Scalability**: Redis-backed session management for multi-instance deployments |
| 14 | + |
| 15 | +This server serves as both primarily as a learning resource, and an example implementation of a scalable remote MCP server. |
| 16 | + |
| 17 | +## Features |
| 18 | + |
| 19 | +### MCP Protocol Features |
| 20 | +- **[Tools](https://modelcontextprotocol.io/docs/concepts/tools)**: 7 demonstration tools including echo, add, long-running operations, LLM sampling, image handling, annotations, and resource references |
| 21 | +- **[Resources](https://modelcontextprotocol.io/docs/concepts/resources)**: 100 example resources with pagination, templates, and subscription support |
| 22 | +- **[Prompts](https://modelcontextprotocol.io/docs/concepts/prompts)**: Simple and complex prompts with argument support and resource embedding |
| 23 | +- **[Sampling](https://modelcontextprotocol.io/docs/concepts/sampling)**: Integration with MCP sampling for LLM interactions |
| 24 | +- **Completions**: Auto-completion support for prompt arguments |
| 25 | +- **Logging**: Multi-level logging with configurable verbosity |
| 26 | +- **Notifications**: Progress updates, resource updates, and stderr messages |
| 27 | + |
| 28 | +### Transport & Infrastructure |
| 29 | +- **[Streamable HTTP](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http)**: Full implementation with GET/POST/DELETE support |
| 30 | +- **[SSE Transport](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#server-sent-events)**: Backwards-compatible Server-Sent Events |
| 31 | +- **Redis Integration**: Pub/sub message routing and session state management |
| 32 | +- **Session Management**: 5-minute TTL with automatic cleanup |
| 33 | +- **Horizontal Scaling**: Any instance can handle any request |
| 34 | + |
| 35 | +### Authentication & Security |
| 36 | +- **[OAuth 2.0](https://modelcontextprotocol.io/specification/2025-03-26/basic/authorization)**: Complete authorization flow with PKCE support |
| 37 | +- **Fake Auth Provider**: Built-in testing provider with localStorage user management |
| 38 | +- **Session Ownership**: User isolation and access control |
| 39 | +- **Security Headers**: CSP, HSTS, X-Frame-Options, and more |
| 40 | +- **Bearer Token Auth**: Middleware for protected endpoints |
| 41 | + |
| 42 | +## Installation |
| 43 | + |
| 44 | +### Prerequisites |
| 45 | +- Node.js >= 16 |
| 46 | +- Redis server |
| 47 | +- npm or yarn |
| 48 | + |
| 49 | +### Setup |
| 50 | +```bash |
| 51 | +# Clone the repository |
| 52 | +git clone https://github.com/modelcontextprotocol/example-remote-server.git |
| 53 | +cd example-remote-server |
| 54 | + |
| 55 | +# Install dependencies |
| 56 | +npm install |
| 57 | + |
| 58 | +# Configure environment (optional) |
| 59 | +cp .env.example .env |
| 60 | +# Edit .env with your settings |
| 61 | +``` |
| 62 | + |
| 63 | +### Configuration |
| 64 | +Environment variables (`.env` file): |
| 65 | +``` |
| 66 | +PORT=3232 # Server port |
| 67 | +BASE_URI=http://localhost:3232 # Base URI for OAuth redirects |
| 68 | +REDIS_HOST=localhost # Redis server host |
| 69 | +REDIS_PORT=6379 # Redis server port |
| 70 | +REDIS_PASSWORD= # Redis password (if required) |
| 71 | +``` |
| 72 | + |
| 73 | +## Development |
| 74 | + |
| 75 | +### Commands |
| 76 | +```bash |
| 77 | +# Start development server with hot reload |
| 78 | +npm run dev |
| 79 | + |
| 80 | +# Start development server with debugging |
| 81 | +npm run dev:break |
| 82 | + |
| 83 | +# Build TypeScript to JavaScript |
| 84 | +npm run build |
| 85 | + |
| 86 | +# Run production server |
| 87 | +npm start |
| 88 | + |
| 89 | +# Run linting |
| 90 | +npm run lint |
| 91 | + |
| 92 | +# Run tests |
| 93 | +npm test |
| 94 | +``` |
| 95 | + |
| 96 | +### Project Structure |
| 97 | +``` |
| 98 | +├── src/ |
| 99 | +│ ├── index.ts # Express app setup and routes |
| 100 | +│ ├── config.ts # Configuration management |
| 101 | +│ ├── redis.ts # Redis client setup |
| 102 | +│ ├── auth/ |
| 103 | +│ │ └── provider.ts # OAuth auth provider implementation |
| 104 | +│ ├── handlers/ |
| 105 | +│ │ ├── shttp.ts # Streamable HTTP handler |
| 106 | +│ │ ├── sse.ts # SSE transport handler |
| 107 | +│ │ ├── fakeauth.ts # Fake upstream auth handler |
| 108 | +│ │ └── common.ts # Shared middleware |
| 109 | +│ ├── services/ |
| 110 | +│ │ ├── mcp.ts # MCP server implementation |
| 111 | +│ │ ├── auth.ts # Session ownership management |
| 112 | +│ │ └── redisTransport.ts # Redis-backed transport |
| 113 | +│ └── utils/ |
| 114 | +│ └── logger.ts # Structured logging |
| 115 | +├── docs/ |
| 116 | +│ ├── streamable-http-design.md # SHTTP implementation details |
| 117 | +│ └── user-id-system.md # Authentication flow documentation |
| 118 | +├── scratch/ # Development scripts and tests |
| 119 | +└── dist/ # Compiled JavaScript output |
| 120 | +``` |
| 121 | + |
| 122 | +## API Endpoints |
| 123 | + |
| 124 | +### MCP Endpoints |
| 125 | +- `GET/POST/DELETE /mcp` - Streamable HTTP transport endpoint |
| 126 | + - `POST`: Initialize sessions or send messages |
| 127 | + - `GET`: Establish SSE streams |
| 128 | + - `DELETE`: Terminate sessions |
| 129 | +- `GET /sse` - Legacy SSE transport endpoint |
| 130 | +- `POST /message` - Legacy message endpoint for SSE transport |
| 131 | + |
| 132 | +### Authentication Endpoints |
| 133 | +- `GET /fakeupstreamauth/authorize` - Fake OAuth authorization page |
| 134 | +- `GET /fakeupstreamauth/redirect` - OAuth redirect handler |
| 135 | +- OAuth 2.0 endpoints provided by MCP SDK auth router |
| 136 | + |
| 137 | +### Headers |
| 138 | +- `Mcp-Session-Id`: Session identifier for Streamable HTTP |
| 139 | +- `Authorization: Bearer <token>`: OAuth access token |
| 140 | +- Standard MCP headers as per protocol specification |
| 141 | + |
| 142 | +## Authentication Flow |
| 143 | + |
| 144 | +The server implements a complete [OAuth 2.0](https://modelcontextprotocol.io/specification/2025-03-26/basic/authorization) flow with a fake upstream provider for testing: |
| 145 | + |
| 146 | +1. **Client Registration**: Clients register to obtain client_id and client_secret |
| 147 | +2. **Authorization**: Users authenticate through `/fakeupstreamauth/authorize` |
| 148 | +3. **User Management**: localStorage-based user ID system for testing |
| 149 | +4. **Token Exchange**: Authorization codes exchanged for access tokens |
| 150 | +5. **Session Creation**: Authenticated tokens create owned sessions |
| 151 | +6. **Access Control**: Sessions are isolated by user ownership |
| 152 | + |
| 153 | +See [docs/user-id-system.md](docs/user-id-system.md) for detailed authentication documentation. |
| 154 | + |
| 155 | +## Transport Methods |
| 156 | + |
| 157 | +### Streamable HTTP (Recommended) |
| 158 | +Modern [transport](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http) supporting bidirectional communication over HTTP: |
| 159 | +- Single endpoint for all operations |
| 160 | +- Session management via headers |
| 161 | +- Efficient message buffering |
| 162 | +- Automatic reconnection support |
| 163 | + |
| 164 | +See [docs/streamable-http-design.md](docs/streamable-http-design.md) for implementation details. |
| 165 | + |
| 166 | +### Server-Sent Events (Legacy) |
| 167 | +Backwards-compatible [transport](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#server-sent-events) using SSE: |
| 168 | +- Separate endpoints for SSE streams and messages |
| 169 | +- Session management via URL parameters |
| 170 | +- Redis-backed message routing |
| 171 | +- Real-time event delivery |
| 172 | + |
| 173 | +## Scalability Architecture |
| 174 | + |
| 175 | +The server is designed for horizontal scaling using Redis as the backbone: |
| 176 | + |
| 177 | +### Session State Management |
| 178 | +- **Redis Storage**: All session state stored in Redis |
| 179 | +- **5-minute TTL**: Automatic session cleanup |
| 180 | +- **Session Ownership**: User isolation via Redis keys |
| 181 | +- **Stateless Servers**: Any instance can handle any request |
| 182 | + |
| 183 | +### Message Routing |
| 184 | +- **Pub/Sub Channels**: Redis channels for message distribution |
| 185 | +- **Message Buffering**: Reliable delivery for disconnected clients |
| 186 | +- **Connection State**: Tracked via pub/sub subscription counts |
| 187 | +- **Automatic Cleanup**: No explicit cleanup required |
| 188 | + |
| 189 | +### Redis Key Structure |
| 190 | +``` |
| 191 | +session:{sessionId}:owner # Session ownership |
| 192 | +mcp:shttp:toserver:{sessionId} # Client→Server messages |
| 193 | +mcp:shttp:toclient:{sessionId}:{requestId} # Server→Client responses |
| 194 | +mcp:control:{sessionId} # Control messages |
| 195 | +``` |
| 196 | + |
| 197 | +## Testing |
| 198 | + |
| 199 | +### Running Tests |
| 200 | +```bash |
| 201 | +# Run all tests |
| 202 | +npm test |
| 203 | + |
| 204 | +# Run specific test suites |
| 205 | +npm test -- --testNamePattern="User Session Isolation" |
| 206 | +npm test -- --testNamePattern="session ownership" |
| 207 | + |
| 208 | +# Run with coverage |
| 209 | +npm test -- --coverage |
| 210 | +``` |
| 211 | + |
| 212 | +### Test Categories |
| 213 | +- **Unit Tests**: Individual component testing |
| 214 | +- **Integration Tests**: Transport and Redis integration |
| 215 | +- **Auth Tests**: OAuth flow and session ownership |
| 216 | +- **Multi-user Tests**: User isolation and access control |
| 217 | + |
| 218 | +### Manual Testing |
| 219 | +The `scratch/` directory contains utility scripts for testing: |
| 220 | +- `oauth.sh` - Test OAuth flows |
| 221 | +- `simple-test-client.js` - Basic client implementation |
| 222 | +- `test-shttp-client.ts` - Streamable HTTP testing |
| 223 | +- `debug-mcp-flow.sh` - Debug MCP message flows |
| 224 | + |
| 225 | +## Security |
| 226 | + |
| 227 | +### Implemented Security Measures |
| 228 | +- **Authentication**: [OAuth 2.0](https://modelcontextprotocol.io/specification/2025-03-26/basic/authorization) with bearer tokens |
| 229 | +- **Authorization**: User-based session ownership |
| 230 | +- **Session Isolation**: Users can only access their own sessions |
| 231 | +- **Security Headers**: |
| 232 | + - Content Security Policy (CSP) |
| 233 | + - Strict Transport Security (HSTS) |
| 234 | + - X-Frame-Options |
| 235 | + - X-Content-Type-Options |
| 236 | +- **Input Validation**: Zod schemas for all inputs |
| 237 | +- **Error Handling**: Sanitized error responses |
| 238 | + |
| 239 | +### Security Best Practices |
| 240 | +1. Always use HTTPS in production |
| 241 | +2. Configure proper CORS origins |
| 242 | +3. Use strong client secrets |
| 243 | +4. Enable all security headers |
| 244 | +5. Monitor session lifetimes |
| 245 | +6. Implement rate limiting |
| 246 | +7. Use structured logging |
| 247 | + |
| 248 | +## Monitoring & Debugging |
| 249 | + |
| 250 | +### Logging |
| 251 | +Structured JSON logging with sanitized outputs: |
| 252 | +- HTTP request/response logging |
| 253 | +- Authentication events |
| 254 | +- Session lifecycle events |
| 255 | +- Redis operations |
| 256 | +- Error tracking |
| 257 | + |
| 258 | +### Redis Monitoring |
| 259 | +```bash |
| 260 | +# Monitor session ownership |
| 261 | +redis-cli KEYS "session:*:owner" |
| 262 | + |
| 263 | +# Watch real-time operations |
| 264 | +redis-cli MONITOR | grep "session:" |
| 265 | + |
| 266 | +# Check active sessions |
| 267 | +redis-cli PUBSUB CHANNELS "mcp:shttp:toserver:*" |
| 268 | + |
| 269 | +# Debug specific session |
| 270 | +redis-cli GET "session:{sessionId}:owner" |
| 271 | +``` |
| 272 | + |
| 273 | +### Debug Tools |
| 274 | +- Development scripts in `scratch/` directory |
| 275 | +- Comprehensive test suite |
| 276 | +- Hot-reload development mode |
| 277 | +- Source maps for debugging |
| 278 | + |
| 279 | +## Contributing |
| 280 | + |
| 281 | +We welcome contributions! Please see our [Contributing Guidelines](CONTRIBUTING.md) for details. |
| 282 | + |
| 283 | +### Development Workflow |
| 284 | +1. Fork the repository |
| 285 | +2. Create a feature branch |
| 286 | +3. Implement your changes |
| 287 | +4. Add tests for new functionality |
| 288 | +5. Ensure all tests pass |
| 289 | +6. Run linting and fix issues |
| 290 | +7. Submit a pull request |
| 291 | + |
| 292 | +### Code Style |
| 293 | +- TypeScript with strict mode |
| 294 | +- ESLint configuration included |
| 295 | +- Prettier formatting recommended |
| 296 | +- Comprehensive type definitions |
| 297 | + |
| 298 | +## License |
| 299 | + |
| 300 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 301 | + |
| 302 | +## Acknowledgments |
| 303 | + |
| 304 | +Built by the Model Context Protocol team as a reference implementation for the MCP ecosystem. |
| 305 | + |
| 306 | +## Links |
| 307 | + |
| 308 | +- [Model Context Protocol Documentation](https://modelcontextprotocol.io) |
| 309 | +- [MCP Specification](https://modelcontextprotocol.io/specification) |
| 310 | +- [MCP Concepts](https://modelcontextprotocol.io/docs/concepts) |
| 311 | + - [Tools](https://modelcontextprotocol.io/docs/concepts/tools) |
| 312 | + - [Resources](https://modelcontextprotocol.io/docs/concepts/resources) |
| 313 | + - [Prompts](https://modelcontextprotocol.io/docs/concepts/prompts) |
| 314 | + - [Sampling](https://modelcontextprotocol.io/docs/concepts/sampling) |
| 315 | + - [Transports](https://modelcontextprotocol.io/docs/concepts/transports) |
| 316 | +- [TypeScript SDK](https://github.com/modelcontextprotocol/typescript-sdk) |
| 317 | +- [Example Servers](https://github.com/modelcontextprotocol/servers) |
0 commit comments