The platform for continuously improving AI agents.
make trailing slash optional in mcp (#252)
Pritam Soni767efd523b6f
diff --git a/overbae/services/mcp/auth.py b/overbae/services/mcp/auth.pyindex e64dc189..b8942bb2 100644--- a/overbae/services/mcp/auth.py+++ b/overbae/services/mcp/auth.py@@ -20,6 +20,8 @@ from overbae.services.mcp.errors import MCPError, error_payloadfrom overbae.services.mcp.oauth import authenticate_accessMCP_PATH = "/api/mcp/"+# Starlette would redirect the other form. MCP clients do not follow that redirect.+MCP_PATHS = (MCP_PATH, MCP_PATH.rstrip("/"))def _request_for_scope(scope: Scope) -> ASGIRequest:@@ -150,7 +152,7 @@ class MCPAuthMiddleware:self.app = appasync def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:- if scope.get("type") != "http" or scope.get("path") not in {MCP_PATH, MCP_PATH.rstrip("/")}:+ if scope.get("type") != "http" or scope.get("path") not in MCP_PATHS:await self.app(scope, receive, send)returndiff --git a/overbae/services/mcp/server.py b/overbae/services/mcp/server.pyindex 78465d8b..e69d7b7a 100644--- a/overbae/services/mcp/server.py+++ b/overbae/services/mcp/server.py@@ -32,7 +32,7 @@ from starlette.routing import Routefrom starlette.types import Message, Receive, Scope, Sendfrom overbae.services.mcp import CONTRACT_VERSION-from overbae.services.mcp.auth import MCP_PATH, MCPAuthMiddleware+from overbae.services.mcp.auth import MCP_PATHS, MCPAuthMiddlewarefrom overbae.services.mcp.catalog import CATALOGfrom overbae.services.mcp.context import get_contextfrom overbae.services.mcp.errors import MCPError, error_payload@@ -279,10 +279,7 @@ class MCPTransportMiddleware:self.app = appasync def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:- if scope.get("type") != "http" or scope.get("path") not in {- MCP_PATH,- MCP_PATH.rstrip("/"),- }:+ if scope.get("type") != "http" or scope.get("path") not in MCP_PATHS:await self.app(scope, receive, send)return@@ -380,10 +377,9 @@ def create_mcp_application() -> Starlette:routes=[*oauth_routes(),Route("/.well-known/openai-apps-challenge", endpoint=domain_challenge, methods=["GET"]),- Route(- MCP_PATH,- endpoint=endpoint,- methods=["GET", "POST", "DELETE"],+ *(+ Route(path, endpoint=endpoint, methods=["GET", "POST", "DELETE"])+ for path in MCP_PATHS),],middleware=[diff --git a/tests/test_mcp_transport.py b/tests/test_mcp_transport.pyindex 91797615..e5885500 100644--- a/tests/test_mcp_transport.py+++ b/tests/test_mcp_transport.py@@ -230,6 +230,34 @@ def test_tool_call_exposes_complete_json_and_resource_links_to_text_only_clients)+def test_initialize_accepts_the_path_with_or_without_a_trailing_slash():+ raw, _ = _token()+ body = _rpc(+ "initialize",+ {+ "protocolVersion": "2025-03-26",+ "capabilities": {},+ "clientInfo": {"name": "test-client", "version": "1.0"},+ },+ )+ with TestClient(_application(), follow_redirects=False) as client:+ responses = [+ client.post(+ path,+ json=body,+ headers={"X-Api-Key": raw, "Accept": "application/json"},+ )+ for path in ("/api/mcp/", "/api/mcp")+ ]++ assert [response.status_code for response in responses] == [200, 200]+ assert [response.headers.get("location") for response in responses] == [None, None]+ assert [response.json()["result"]["serverInfo"]["name"] for response in responses] == [+ "overmind-platform",+ "overmind-platform",+ ]++def test_initialize_accepts_an_allowed_host_with_a_port():raw, _ = _token()with TestClient(_application()) as client: