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): serve MCP at both /mcp and /mcp/ (#1887)

r33drichards9280907b070c
diff --git a/libs/python/computer-server/computer_server/main.py b/libs/python/computer-server/computer_server/main.py
index 3d71c21f71..12c85547e3 100644
--- a/libs/python/computer-server/computer_server/main.py
+++ b/libs/python/computer-server/computer_server/main.py
@@ -188,9 +188,31 @@ app.add_middleware(
allow_headers=["*"],
)
+
+class McpBarePathRewrite:
+ """Serve the MCP app at both /mcp and /mcp/.
+
+ Starlette's Mount only matches "/mcp/..." — a bare "/mcp" resolves
+ to inner path "" and 404s, and redirect_slashes=False on the outer
+ app disables the 307 rescue. Most MCP clients (claude.ai included)
+ POST to the bare path, so rewrite it before routing. A rewrite
+ (not a redirect) keeps POST bodies and SSE streaming intact; pure
+ ASGI (not BaseHTTPMiddleware) so responses aren't buffered.
+ """
+
+ def __init__(self, app):
+ self.app = app
+
+ async def __call__(self, scope, receive, send):
+ if scope["type"] == "http" and scope["path"] == "/mcp":
+ scope = dict(scope, path="/mcp/")
+ await self.app(scope, receive, send)
+
+
# Mount MCP server at /mcp - FastMCP's internal path is "/" so endpoint is /mcp
if _mcp_http_app:
app.mount("/mcp", _mcp_http_app)
+ app.add_middleware(McpBarePathRewrite)
protocol_version = 1
try:
@@ -1171,10 +1193,8 @@ async def agent_response_endpoint(
#
# See: https://github.com/trycua/cua/issues/1605
import unicodedata
- if (
- len(key) == 1
- and unicodedata.category(key) not in ("Cc", "Cs", "Cn")
- ):
+
+ if len(key) == 1 and unicodedata.category(key) not in ("Cc", "Cs", "Cn"):
await self._auto.type_text(key)
else:
await self._auto.press_key(key)
diff --git a/libs/python/computer-server/tests/test_server.py b/libs/python/computer-server/tests/test_server.py
index 385957e8f4..680bffcad3 100644
--- a/libs/python/computer-server/tests/test_server.py
+++ b/libs/python/computer-server/tests/test_server.py
@@ -38,3 +38,46 @@ class TestServerInitialization:
except Exception as e:
# Some initialization errors are acceptable in unit tests
pytest.skip(f"Server initialization requires specific setup: {e}")
+
+
+class TestMcpBarePathRewrite:
+ """Test the ASGI shim that serves MCP at both /mcp and /mcp/ (SRP:
+ Only tests the path rewrite). The class is pure ASGI with no
+ dependencies, so it is tested standalone against a recording stub."""
+
+ @pytest.mark.asyncio
+ async def test_bare_mcp_path_is_rewritten(self):
+ try:
+ from computer_server.main import McpBarePathRewrite
+ 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 = McpBarePathRewrite(inner)
+ await shim({"type": "http", "path": "/mcp"}, None, None)
+ assert seen["path"] == "/mcp/"
+
+ @pytest.mark.asyncio
+ async def test_other_paths_untouched(self):
+ try:
+ from computer_server.main import McpBarePathRewrite
+ 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 = McpBarePathRewrite(inner)
+ for path in ("/mcp/", "/mcpx", "/status", "/"):
+ await shim({"type": "http", "path": path}, None, None)
+ assert seen["path"] == path