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]

merge(main): resolve mcp server.py import conflict and relock

joshua7410d00d2e8c
diff --cc litellm/proxy/_experimental/mcp_server/server.py
index 4a0fb8df65d,a7ca2775fb1..ad8c721db7a
--- a/litellm/proxy/_experimental/mcp_server/server.py
+++ b/litellm/proxy/_experimental/mcp_server/server.py
@@@ -13,7 -14,8 +14,8 @@@ import tim
import traceback
import types
import uuid
+ from collections import Counter
-from collections.abc import AsyncIterator, Callable, Mapping, Sequence
+from collections.abc import AsyncIterator, Callable, Iterable, Mapping, Sequence
from datetime import datetime
from typing import TYPE_CHECKING, Any, Final, NoReturn, Protocol
@@@ -119,40 -131,13 +133,31 @@@ _MCP_ROUTING_PEEK_MAX_BYTES: Final = 40
# ASGI scope keys carrying OTel request state into a stateful MCP message handler.
_MCP_TRANSPORT_SPAN_SCOPE_KEY: Final = "litellm_otel_transport_span"
_MCP_DESTINATIONS_SCOPE_KEY: Final = "litellm_otel_request_destinations"
+_MCP_PROTOCOL_VERSION_HEADER: Final = b"mcp-protocol-version"
+
+
+def unsupported_protocol_version(scope: Scope) -> str | None:
+ """Return the unsupported ``MCP-Protocol-Version`` header value, if any.
+
+ SDK 2's ``StreamableHTTPSessionManager`` routes any version outside
+ ``HANDSHAKE_PROTOCOL_VERSIONS`` to the modern single-exchange path, which
+ bypasses litellm's session/auth model, so the ASGI entry rejects it.
+ """
+ headers: Final[Iterable[tuple[bytes, bytes]]] = scope.get("headers") or ()
+ values: Final = tuple(
+ raw.decode("latin-1").strip() for key, raw in headers if key.lower() == _MCP_PROTOCOL_VERSION_HEADER
+ )
+ for value in values:
+ if value and value not in HANDSHAKE_PROTOCOL_VERSIONS:
+ return value
+ return None
- def _invalidate_byok_cred_cache(user_id: str, server_id: str) -> None:
- """Remove a (user_id, server_id) entry from the BYOK credential cache.
-
- Call this after storing or deleting a credential so subsequent calls
- see the fresh value rather than a stale cached result.
- """
- _byok_cred_cache.pop((user_id, server_id), None)
-
-
- def _write_byok_cred_cache(user_id: str, server_id: str, credential: str | None) -> None:
- """Write a credential value to the cache, evicting all entries if at capacity."""
- if len(_byok_cred_cache) >= _BYOK_CRED_CACHE_MAX_SIZE:
- _byok_cred_cache.clear()
- _byok_cred_cache[(user_id, server_id)] = (credential, time.monotonic())
+ async def _invalidate_byok_cred_cache(user_id: str, server_id: str) -> None:
+ """Drop a stored-or-deleted BYOK credential from this worker's cache and from every peer worker's."""
+ cache_key: Final = byok_credential_cache_key(user_id, server_id)
+ byok_credential_cache.delete_cache(cache_key)
+ await publish_auth_cache_invalidation(cache_key=cache_key)
# Check if MCP is available
@@@ -471,17 -458,11 +476,19 @@@ if MCP_AVAILABLE
except ImportError:
StreamableHTTPSessionManager = None
from mcp.types import (
+ INVALID_REQUEST,
+ CallToolRequestParams,
CallToolResult,
+ GetPromptRequestParams,
+ Implementation,
+ InitializeRequest,
+ ListPromptsResult,
+ ListResourcesResult,
+ ListResourceTemplatesResult,
ListToolsResult,
+ PaginatedRequestParams,
Prompt,
+ ReadResourceRequestParams,
TextContent,
)
from mcp.types import Tool as MCPTool