From 836fd81e19ff8cec4bd3c0ed7a5ccba3c8bfab07 Mon Sep 17 00:00:00 2001 From: DK09876 Date: Fri, 13 Mar 2026 13:33:30 -0700 Subject: [PATCH] fix: inject Accept header in MCP middleware to prevent 406 errors (#571) Some MCP clients (e.g., Claude Code) don't send an Accept header, causing the MCP SDK to reject requests with 406 Not Acceptable. The middleware now ensures Accept includes application/json and text/event-stream when missing. Co-authored-by: Claude Opus 4.6 --- hindsight-api-slim/hindsight_api/api/mcp.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/hindsight-api-slim/hindsight_api/api/mcp.py b/hindsight-api-slim/hindsight_api/api/mcp.py index 2eef9e25..58443d16 100644 --- a/hindsight-api-slim/hindsight_api/api/mcp.py +++ b/hindsight-api-slim/hindsight_api/api/mcp.py @@ -381,6 +381,15 @@ class MCPMiddleware: # Clear root_path since we're passing directly to the app new_scope["root_path"] = "" + # Ensure Accept header includes required MIME types for MCP SDK. + # Some clients (e.g., Claude Code) don't send Accept, causing + # the SDK to reject with 406 Not Acceptable. + accept_header = self._get_header(new_scope, "accept") + if not accept_header or "text/event-stream" not in accept_header: + headers = [(k, v) for k, v in new_scope.get("headers", []) if k.lower() != b"accept"] + headers.append((b"accept", b"application/json, text/event-stream")) + new_scope["headers"] = headers + # Wrap send to rewrite the SSE endpoint URL to include bank_id if using path-based routing. # Only rewrite SSE (text/event-stream) responses to avoid corrupting tool results # that might contain the literal string "data: /messages".