Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(computer-server): tolerate any Accept header on /mcp (#1898)

r33drichards7fe3a8e8c71b
diff --git a/libs/python/computer-server/computer_server/main.py b/libs/python/computer-server/computer_server/main.py
index 12c85547e3..14689b4d4d 100644
--- a/libs/python/computer-server/computer_server/main.py
+++ b/libs/python/computer-server/computer_server/main.py
@@ -209,9 +209,54 @@ class McpBarePathRewrite:
await self.app(scope, receive, send)
-# Mount MCP server at /mcp - FastMCP's internal path is "/" so endpoint is /mcp
+class McpAcceptHeaderShim:
+ """Make the MCP streamable-HTTP endpoint tolerant of the Accept header.
+
+ The MCP Python SDK's StreamableHTTP transport rejects requests with
+ JSON-RPC -32600 ("Not Acceptable: Client must accept ...") unless the
+ request's Accept header advertises BOTH ``application/json`` and
+ ``text/event-stream`` (POST path), or ``text/event-stream`` (GET SSE
+ path). claude.ai's MCP connector does not always send both, so its
+ handshake POST to /mcp is rejected before it ever reaches a tool.
+
+ This pure-ASGI shim normalizes the inbound Accept header for /mcp
+ requests so it always contains both media types, which lets the SDK's
+ Accept check pass for every client regardless of what it sent. It only
+ rewrites the REQUEST header — it does not touch ``json_response`` or any
+ response semantics, so the server still negotiates JSON vs. SSE exactly
+ as it would otherwise. This keeps existing MCP clients (Claude Code,
+ SDK) unaffected while admitting stricter/looser connectors.
+
+ Scoped to the /mcp prefix only; all other routes are passed through
+ untouched. Pure ASGI (not BaseHTTPMiddleware) so streaming responses
+ are not buffered.
+ """
+
+ _COMBINED = b"application/json, text/event-stream"
+
+ def __init__(self, app):
+ self.app = app
+
+ async def __call__(self, scope, receive, send):
+ path = scope.get("path", "")
+ # Match the bare /mcp endpoint and anything under /mcp/, but NOT
+ # unrelated routes that merely share the prefix (e.g. /mcpx).
+ if scope["type"] == "http" and (path == "/mcp" or path.startswith("/mcp/")):
+ headers = [(k, v) for (k, v) in scope.get("headers", []) if k != b"accept"]
+ headers.append((b"accept", self._COMBINED))
+ scope = dict(scope, headers=headers)
+ await self.app(scope, receive, send)
+
+
+# Mount MCP server at /mcp - FastMCP's internal path is "/" so endpoint is /mcp.
+#
+# Middleware ordering note: Starlette's app.add_middleware() prepends, so the
+# LAST one added runs FIRST (outermost). We want, outermost -> innermost:
+# McpBarePathRewrite (fix the path) -> McpAcceptHeaderShim (fix Accept) -> app
+# so add McpAcceptHeaderShim first, then McpBarePathRewrite.
if _mcp_http_app:
app.mount("/mcp", _mcp_http_app)
+ app.add_middleware(McpAcceptHeaderShim)
app.add_middleware(McpBarePathRewrite)
protocol_version = 1
diff --git a/libs/python/computer-server/tests/test_server.py b/libs/python/computer-server/tests/test_server.py
index 680bffcad3..882f1d4fa8 100644
--- a/libs/python/computer-server/tests/test_server.py
+++ b/libs/python/computer-server/tests/test_server.py
@@ -81,3 +81,82 @@ class TestMcpBarePathRewrite:
for path in ("/mcp/", "/mcpx", "/status", "/"):
await shim({"type": "http", "path": path}, None, None)
assert seen["path"] == path
+
+
+class TestMcpAcceptHeaderShim:
+ """Test the ASGI shim that normalizes the Accept header for /mcp so the
+ MCP SDK's strict Accept check passes for any client (SRP: Only tests the
+ Accept-header rewrite). Pure ASGI with no deps; tested standalone."""
+
+ @staticmethod
+ def _accept(headers):
+ for k, v in headers:
+ if k == b"accept":
+ return v
+ return None
+
+ @pytest.mark.asyncio
+ async def test_mcp_accept_header_is_normalized(self):
+ try:
+ from computer_server.main import McpAcceptHeaderShim
+ except ImportError:
+ pytest.skip("computer_server module not installed")
+ except Exception as e:
+ pytest.skip(f"Server initialization requires specific setup: {e}")
+
+ seen = {}
+
+ async def inner(scope, receive, send):
+ seen.update(scope)
+
+ shim = McpAcceptHeaderShim(inner)
+ # claude.ai-style request that only advertises application/json:
+ for path in ("/mcp", "/mcp/", "/mcp/anything"):
+ await shim(
+ {"type": "http", "path": path, "headers": [(b"accept", b"application/json")]},
+ None,
+ None,
+ )
+ assert self._accept(seen["headers"]) == b"application/json, text/event-stream"
+
+ @pytest.mark.asyncio
+ async def test_missing_accept_header_is_added(self):
+ try:
+ from computer_server.main import McpAcceptHeaderShim
+ except ImportError:
+ pytest.skip("computer_server module not installed")
+ except Exception as e:
+ pytest.skip(f"Server initialization requires specific setup: {e}")
+
+ seen = {}
+
+ async def inner(scope, receive, send):
+ seen.update(scope)
+
+ shim = McpAcceptHeaderShim(inner)
+ await shim({"type": "http", "path": "/mcp", "headers": []}, None, None)
+ assert self._accept(seen["headers"]) == b"application/json, text/event-stream"
+
+ @pytest.mark.asyncio
+ async def test_non_mcp_paths_untouched(self):
+ try:
+ from computer_server.main import McpAcceptHeaderShim
+ except ImportError:
+ pytest.skip("computer_server module not installed")
+ except Exception as e:
+ pytest.skip(f"Server initialization requires specific setup: {e}")
+
+ seen = {}
+
+ async def inner(scope, receive, send):
+ seen.update(scope)
+
+ shim = McpAcceptHeaderShim(inner)
+ for path in ("/status", "/", "/mcpx", "/ws"):
+ await shim(
+ {"type": "http", "path": path, "headers": [(b"accept", b"application/json")]},
+ None,
+ None,
+ )
+ # Untouched: still exactly what the client sent.
+ assert self._accept(seen["headers"]) == b"application/json"