Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(proxy): serve aggregate MCP endpoint on bare /mcp instead of 307-redirecting (#34845)

tin-berrid4d6bc257714
diff --git a/backend/routes/allowlist.py b/backend/routes/allowlist.py
index 8ccd439979b..3f7bf788a1b 100644
--- a/backend/routes/allowlist.py
+++ b/backend/routes/allowlist.py
@@ -146,11 +146,13 @@ BACKEND_EXACT_PATHS: frozenset[str] = frozenset(
"/docs/oauth2-redirect",
"/redoc",
"/fallback/login",
+ "/mcp", # bare spelling of the aggregate MCP endpoint; /mcp/ prefix covers the rest
}
)
BACKEND_MOUNT_PATHS: frozenset[str] = frozenset(
{
"/swagger", # API documentation static assets belong to the backend
+ "/mcp", # lazily-mounted MCP sub-app serves on the backend component
}
)
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 359187f81cb..377963f1b91 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -17274,6 +17274,29 @@ async def _stream_mcp_asgi_response(handle_fn, scope: dict, receive) -> "Streami
########################################################
+ BASE_MCP_ROUTE,
+ methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"],
+)
+async def aggregate_mcp_route(request: Request):
+ """Serve the aggregate MCP endpoint on the bare ``/mcp`` spelling: the
+ ``/mcp`` mount cannot match its bare prefix, and the resulting 307 breaks
+ MCP clients behind TLS-terminating proxies."""
+ from litellm.proxy._experimental.mcp_server.utils import is_mcp_available
+
+ if not is_mcp_available():
+ raise HTTPException(status_code=404, detail="Not Found")
+
+ from litellm.proxy._experimental.mcp_server.server import (
+ handle_streamable_http_mcp,
+ )
+
+ scope = dict(request.scope)
+ scope["_original_path"] = scope.get("path", "")
+ scope["path"] = BASE_MCP_ROUTE
+ return await _stream_mcp_asgi_response(handle_streamable_http_mcp, scope, request.receive)
+
+
# Toolset-namespaced MCP routes - handle /toolset/{toolset_name}/mcp
# Must be declared BEFORE /{mcp_server_name}/mcp to avoid being swallowed by the catchall.
@app.api_route(
diff --git a/tests/test_litellm/proxy/test_dynamic_mcp_route.py b/tests/test_litellm/proxy/test_dynamic_mcp_route.py
index 592cebd957c..da7b8e01f46 100644
--- a/tests/test_litellm/proxy/test_dynamic_mcp_route.py
+++ b/tests/test_litellm/proxy/test_dynamic_mcp_route.py
@@ -540,3 +540,74 @@ async def test_toolset_mcp_route_unexpected_exception_returns_500_without_traceb
assert exc_info.value.detail == "Internal server error"
assert "db-host" not in str(exc_info.value.detail)
assert "traceback" not in str(exc_info.value.detail).lower()
+
+
+# ---------------------------------------------------------------------------
+# 7. Aggregate /mcp without a trailing slash (bare mount prefix)
+# ---------------------------------------------------------------------------
+
+_IS_MCP_AVAILABLE = "litellm.proxy._experimental.mcp_server.utils.is_mcp_available"
+
+
+def _test_client():
+ from fastapi.testclient import TestClient
+
+ from litellm.proxy.proxy_server import app
+
+ return TestClient(app, follow_redirects=False)
+
+
[email protected]("method", ["GET", "POST", "DELETE"])
+def test_aggregate_mcp_route_bare_path_is_served_not_redirected(method):
+ """Bare /mcp must dispatch to the MCP handler with aggregate semantics,
+ never 307-redirect. Driven through the real app router so a lost route
+ registration (not just a broken handler body) fails this test."""
+ captured_scope: dict = {}
+
+ async def capturing_handle(scope, receive, send):
+ captured_scope.update(scope)
+ await send({"type": "http.response.start", "status": 200, "headers": []})
+ await send({"type": "http.response.body", "body": b"{}"})
+
+ with patch(_HANDLE_HTTP, new=capturing_handle):
+ response = _test_client().request(method, "/mcp")
+
+ assert response.status_code == 200
+ assert captured_scope.get("path") == "/mcp"
+ assert captured_scope.get("_original_path") == "/mcp"
+
+
+def test_aggregate_mcp_route_requires_exact_path():
+ """The bare-path route must match exactly /mcp; a sibling path like /mcpx
+ must not reach the MCP handler through it."""
+ calls = []
+
+ async def marking_handle(scope, receive, send):
+ calls.append(scope.get("path"))
+ await send({"type": "http.response.start", "status": 200, "headers": []})
+ await send({"type": "http.response.body", "body": b"{}"})
+
+ with patch(_HANDLE_HTTP, new=marking_handle):
+ response = _test_client().post("/mcpx")
+
+ assert calls == []
+ assert response.status_code != 200
+
+
+def test_aggregate_mcp_route_returns_404_when_mcp_unavailable():
+ """When the mcp package is unavailable the canonical /mcp/ sub-app is a
+ bare FastAPI that 404s, so the bare spelling must 404 identically instead
+ of erroring on the handler import."""
+ handler_calls = []
+
+ async def marking_handle(scope, receive, send):
+ handler_calls.append(scope.get("path"))
+
+ with (
+ patch(_IS_MCP_AVAILABLE, new=MagicMock(return_value=False)),
+ patch(_HANDLE_HTTP, new=marking_handle),
+ ):
+ response = _test_client().post("/mcp")
+
+ assert response.status_code == 404
+ assert handler_calls == []
diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts
index 1c5fe408203..2cbc7fd6220 100644
--- a/ui/litellm-dashboard/src/lib/http/schema.d.ts
+++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts
@@ -7616,6 +7616,64 @@ export interface paths {
patch?: never;
trace?: never;
};
+ "/mcp": {
+ parameters: {
+ query?: never;
+ header?: never;
+ path?: never;
+ cookie?: never;
+ };
+ /**
+ * Aggregate Mcp Route
+ * @description Serve the aggregate MCP endpoint on the bare ``/mcp`` spelling: the
+ * ``/mcp`` mount cannot match its bare prefix, and the resulting 307 breaks
+ * MCP clients behind TLS-terminating proxies.
+ */
+ get: operations["aggregate_mcp_route_mcp_get"];
+ /**
+ * Aggregate Mcp Route
+ * @description Serve the aggregate MCP endpoint on the bare ``/mcp`` spelling: the
+ * ``/mcp`` mount cannot match its bare prefix, and the resulting 307 breaks
+ * MCP clients behind TLS-terminating proxies.
+ */
+ put: operations["aggregate_mcp_route_mcp_put"];
+ /**
+ * Aggregate Mcp Route
+ * @description Serve the aggregate MCP endpoint on the bare ``/mcp`` spelling: the
+ * ``/mcp`` mount cannot match its bare prefix, and the resulting 307 breaks
+ * MCP clients behind TLS-terminating proxies.
+ */
+ post: operations["aggregate_mcp_route_mcp_post"];
+ /**
+ * Aggregate Mcp Route
+ * @description Serve the aggregate MCP endpoint on the bare ``/mcp`` spelling: the
+ * ``/mcp`` mount cannot match its bare prefix, and the resulting 307 breaks
+ * MCP clients behind TLS-terminating proxies.
+ */
+ delete: operations["aggregate_mcp_route_mcp_delete"];
+ /**
+ * Aggregate Mcp Route
+ * @description Serve the aggregate MCP endpoint on the bare ``/mcp`` spelling: the
+ * ``/mcp`` mount cannot match its bare prefix, and the resulting 307 breaks
+ * MCP clients behind TLS-terminating proxies.
+ */
+ options: operations["aggregate_mcp_route_mcp_options"];
+ /**
+ * Aggregate Mcp Route
+ * @description Serve the aggregate MCP endpoint on the bare ``/mcp`` spelling: the
+ * ``/mcp`` mount cannot match its bare prefix, and the resulting 307 breaks
+ * MCP clients behind TLS-terminating proxies.
+ */
+ head: operations["aggregate_mcp_route_mcp_head"];
+ /**
+ * Aggregate Mcp Route
+ * @description Serve the aggregate MCP endpoint on the bare ``/mcp`` spelling: the
+ * ``/mcp`` mount cannot match its bare prefix, and the resulting 307 breaks
+ * MCP clients behind TLS-terminating proxies.
+ */
+ patch: operations["aggregate_mcp_route_mcp_patch"];
+ trace?: never;
+ };
"/mcp-rest/test/connection": {
parameters: {
query?: never;
@@ -45790,6 +45848,146 @@ export interface operations {
};
};
};
+ aggregate_mcp_route_mcp_get: {
+ parameters: {
+ query?: never;
+ header?: never;
+ path?: never;
+ cookie?: never;
+ };
+ requestBody?: never;
+ responses: {
+ /** @description Successful Response */
+ 200: {
+ headers: {
+ [name: string]: unknown;
+ };
+ content: {
+ "application/json": unknown;
+ };
+ };
+ };
+ };
+ aggregate_mcp_route_mcp_put: {
+ parameters: {
+ query?: never;
+ header?: never;
+ path?: never;
+ cookie?: never;
+ };
+ requestBody?: never;
+ responses: {
+ /** @description Successful Response */
+ 200: {
+ headers: {
+ [name: string]: unknown;
+ };
+ content: {
+ "application/json": unknown;
+ };
+ };
+ };
+ };
+ aggregate_mcp_route_mcp_post: {
+ parameters: {
+ query?: never;
+ header?: never;
+ path?: never;
+ cookie?: never;
+ };
+ requestBody?: never;
+ responses: {
+ /** @description Successful Response */
+ 200: {
+ headers: {
+ [name: string]: unknown;
+ };
+ content: {
+ "application/json": unknown;
+ };
+ };
+ };
+ };
+ aggregate_mcp_route_mcp_delete: {
+ parameters: {
+ query?: never;
+ header?: never;
+ path?: never;
+ cookie?: never;
+ };
+ requestBody?: never;
+ responses: {
+ /** @description Successful Response */
+ 200: {
+ headers: {
+ [name: string]: unknown;
+ };
+ content: {
+ "application/json": unknown;
+ };
+ };
+ };
+ };
+ aggregate_mcp_route_mcp_options: {
+ parameters: {
+ query?: never;
+ header?: never;
+ path?: never;
+ cookie?: never;
+ };
+ requestBody?: never;
+ responses: {
+ /** @description Successful Response */
+ 200: {
+ headers: {
+ [name: string]: unknown;
+ };
+ content: {
+ "application/json": unknown;
+ };
+ };
+ };
+ };
+ aggregate_mcp_route_mcp_head: {
+ parameters: {
+ query?: never;
+ header?: never;
+ path?: never;
+ cookie?: never;
+ };
+ requestBody?: never;
+ responses: {
+ /** @description Successful Response */
+ 200: {
+ headers: {
+ [name: string]: unknown;
+ };
+ content: {
+ "application/json": unknown;
+ };
+ };
+ };
+ };
+ aggregate_mcp_route_mcp_patch: {
+ parameters: {
+ query?: never;
+ header?: never;
+ path?: never;
+ cookie?: never;
+ };
+ requestBody?: never;
+ responses: {
+ /** @description Successful Response */
+ 200: {
+ headers: {
+ [name: string]: unknown;
+ };
+ content: {
+ "application/json": unknown;
+ };
+ };
+ };
+ };
test_connection_mcp_rest_test_connection_post: {
parameters: {
query?: never;