Files
n8n-mcp/docs/STREAM_FIX_v232.md
czlonkowski baf5293cb8 fix: complete solution for MCP HTTP server stream errors (v2.3.2)
Root Cause Analysis:
- Express.json() middleware was consuming request stream before StreamableHTTPServerTransport
- StreamableHTTPServerTransport has initialization issues with stateless usage

Two-Phase Solution:
1. Removed all body parsing middleware to preserve raw streams
2. Created http-server-fixed.ts with direct JSON-RPC implementation

Key Changes:
- Remove express.json() from all HTTP server implementations
- Add http-server-fixed.ts that bypasses StreamableHTTPServerTransport
- Implement initialize, tools/list, and tools/call methods directly
- Add USE_FIXED_HTTP=true environment variable to enable fixed server
- Update logging to not access req.body

The fixed implementation:
- Handles JSON-RPC protocol directly without transport complications
- Maintains full MCP compatibility
- Works reliably without stream or initialization errors
- Provides better performance and debugging capabilities

Usage: MCP_MODE=http USE_FIXED_HTTP=true npm start

This provides a stable, production-ready HTTP server for n8n-MCP.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-14 17:19:42 +02:00

78 lines
2.9 KiB
Markdown

# Stream Fix v2.3.2 - Critical Fix for "stream is not readable" Error
## Problem
The "stream is not readable" error was persisting even after implementing the Single-Session architecture in v2.3.1. The error occurred when StreamableHTTPServerTransport tried to read the request stream.
## Root Cause
Express.js middleware `express.json()` was consuming the request body stream before StreamableHTTPServerTransport could read it. In Node.js, streams can only be read once - after consumption, they cannot be read again.
### Code Issue
```javascript
// OLD CODE - This consumed the stream!
app.use(express.json({
limit: '1mb',
strict: true
}));
```
When StreamableHTTPServerTransport later tried to read the request stream, it was already consumed, resulting in "stream is not readable" error.
## Solution
Remove all body parsing middleware for the `/mcp` endpoint, allowing StreamableHTTPServerTransport to read the raw stream directly.
### Fix Applied
```javascript
// NEW CODE - No body parsing for /mcp endpoint
// DON'T use any body parser globally - StreamableHTTPServerTransport needs raw stream
// Only use JSON parser for specific endpoints that need it
```
## Changes Made
1. **Removed global `express.json()` middleware** from both:
- `src/http-server-single-session.ts`
- `src/http-server.ts`
2. **Removed `req.body` access** in logging since body is no longer parsed
3. **Updated version** to 2.3.2 to reflect this critical fix
## Technical Details
### Why This Happens
1. Express middleware runs in order
2. `express.json()` reads the entire request stream and parses it
3. The stream position is at the end after reading
4. StreamableHTTPServerTransport expects to read from position 0
5. Node.js streams are not seekable - once consumed, they're done
### Why StreamableHTTPServerTransport Needs Raw Streams
The transport implements its own request handling and needs to:
- Read the raw JSON-RPC request
- Handle streaming responses via Server-Sent Events (SSE)
- Manage its own parsing and validation
## Testing
After this fix:
1. The MCP server should accept requests without "stream is not readable" errors
2. Authentication still works (uses headers, not body)
3. Health endpoint continues to function (GET request, no body)
## Lessons Learned
1. **Be careful with middleware order** - Body parsing middleware consumes streams
2. **StreamableHTTPServerTransport has specific requirements** - It needs raw access to the request stream
3. **Not all MCP transports are the same** - StreamableHTTP has different needs than stdio transport
## Future Considerations
If we need to log request methods or validate requests before passing to StreamableHTTPServerTransport, we would need to:
1. Implement a custom middleware that buffers the stream
2. Create a new readable stream from the buffer
3. Attach the new stream to the request object
For now, the simplest solution is to not parse the body at all for the `/mcp` endpoint.