You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -4,129 +4,233 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
4
4
5
5
## Project Overview
6
6
7
-
This is a **Streamable HTTP Stateless MCP Server** - a reference implementation demonstrating true stateless Model Context Protocol (MCP) architecture. Unlike traditional MCP servers that maintain sessions, this server creates fresh instances for every request, enabling infinite horizontal scaling and serverless deployment.
7
+
This is an **Educational Reference Implementation** of a Stateless HTTP Streamable MCP Server. This is NOT just a simple example - it's a comprehensive teaching resource designed to demonstrate production-ready patterns, security best practices, and modern deployment strategies for Model Context Protocol servers.
8
+
9
+
### Educational Mission
10
+
This repository serves as a **masterclass** in building stateless MCP servers, covering:
- Server uptime in `/stats` resource reflects process uptime only
131
-
132
-
This server is designed for serverless environments where each request may hit a different instance, making traditional session-based patterns impossible.
166
+
1. Define schema in `types.ts` schemas object (compiled once at startup)
167
+
2. Use Zod for parameter validation with `.describe()` for documentation
168
+
3. Generate unique `requestId` for correlation
169
+
4. Implement progress notifications if appropriate
170
+
5. Follow stateless principle - no cross-request state
171
+
6. Use `SchemaInput<'toolName'>` type for type-safe parameter handling
172
+
173
+
### Error Handling Best Practices
174
+
```typescript
175
+
// Use protocol-compliant McpError for predictable failures
requestLogger.error('Unhandled error in MCP request handler', { error });
185
+
res.status(500).json({
186
+
jsonrpc: '2.0',
187
+
error: {
188
+
code: ErrorCode.InternalError,
189
+
message: 'An internal server error occurred.'
190
+
},
191
+
id: req.body?.id||null
192
+
});
193
+
```
194
+
195
+
### Request Lifecycle Pattern
196
+
1. Generate unique `requestId` for correlation
197
+
2. Create contextual logger with `requestId`
198
+
3. Create fresh MCP server and transport instances
199
+
4. Process request through SDK transport
200
+
5. Clean up instances on response close
201
+
6. Collect metrics for monitoring
202
+
203
+
## Testing Stateless Behavior
204
+
205
+
### Verification Points
206
+
- Each request creates new server instance
207
+
- No shared state between concurrent requests
208
+
- Request correlation works via `requestId`
209
+
- Cleanup happens properly on connection close
210
+
- Metrics collection doesn't leak memory
211
+
212
+
### Common Issues to Watch
213
+
- Forgetting to set `sessionIdGenerator: undefined`
214
+
- Missing cleanup in `res.on('close')` listener
215
+
- Sharing state accidentally via closures
216
+
- Not handling concurrent requests properly
217
+
218
+
## Production Deployment
219
+
220
+
### Containerization
221
+
- Multi-stage Dockerfile (builder + production stages)
222
+
- Docker Compose with health checks
223
+
- Minimal production image (no dev dependencies)
224
+
225
+
### Serverless Ready
226
+
-`handleMCPRequest` function can be exported as serverless handler
227
+
- No persistent state to manage
228
+
- Scales infinitely without coordination
229
+
230
+
### Monitoring
231
+
- Structured JSON logging with correlation
232
+
- Prometheus-style metrics endpoint
233
+
- Health checks for load balancers
234
+
- Request duration and tool execution histograms
235
+
236
+
This server demonstrates that stateless architecture enables simpler, more secure, and infinitely scalable MCP implementations. The educational approach teaches both what to build and what NOT to build, making it an invaluable learning resource.
0 commit comments