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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(mcp): resolve SDK2 wire-shape regressions in guardrail, arize, and benchmark paths
joshua4a7d8bbffa59
diff --git a/.github/workflows/codspeed.yml b/.github/workflows/codspeed.ymlindex 7e013b7bb0b..fd7513a3937 100644--- a/.github/workflows/codspeed.yml+++ b/.github/workflows/codspeed.yml@@ -69,7 +69,7 @@ jobs:uv run --frozen --no-default-groups--with pytest==8.3.5--with pytest-codspeed==4.3.0- --with "mcp>=1.26.0,<2.0"+ --with "mcp>=2.2.0,<3.0"--with "a2a-sdk>=1.1.0,<2.0"pytest-p pytest_codspeed.plugin@@ -86,7 +86,7 @@ jobs:uv run --frozen --no-default-groups--with pytest==8.3.5--with pytest-codspeed==4.3.0- --with "mcp>=1.26.0,<2.0"+ --with "mcp>=2.2.0,<3.0"--with "a2a-sdk>=1.1.0,<2.0"pytest-p pytest_codspeed.plugindiff --git a/litellm/integrations/arize/_utils.py b/litellm/integrations/arize/_utils.pyindex 5a5324eae5e..0271cf1e03c 100644--- a/litellm/integrations/arize/_utils.py+++ b/litellm/integrations/arize/_utils.py@@ -1139,7 +1139,10 @@ def _set_mcp_tool_output(span: "Span", coerced_response_obj: object) -> None:safe_set_attribute(span, SpanAttributes.OUTPUT_MIME_TYPE, OpenInferenceMimeTypeValues.TEXT.value)return- structured: Final[object] = coerced_response_obj.get("structuredContent")+ structured: Final[object] = coerced_response_obj.get(+ "structured_content",+ coerced_response_obj.get("structuredContent"), # pyright: ignore[reportUnknownMemberType] # tolerant dual-spelling lookup on untyped payloads+ )payload: Final[object] = content if content else structured if structured is not None else contentif payload is None:returndiff --git a/litellm/proxy/guardrails/guardrail_hooks/cisco_ai_defense/cisco_ai_defense_mcp.py b/litellm/proxy/guardrails/guardrail_hooks/cisco_ai_defense/cisco_ai_defense_mcp.pyindex 8d5a7c7fecb..7bbe785b4fa 100644--- a/litellm/proxy/guardrails/guardrail_hooks/cisco_ai_defense/cisco_ai_defense_mcp.py+++ b/litellm/proxy/guardrails/guardrail_hooks/cisco_ai_defense/cisco_ai_defense_mcp.py@@ -7,7 +7,7 @@ while preserving the existing public import path.from collections.abc import Sequencefrom datetime import datetime-from typing import TYPE_CHECKING, Final, Optional+from typing import TYPE_CHECKING, Final, Optional, castfrom fastapi import HTTPException@@ -45,6 +45,24 @@ def _serialize_mcp_content_item(item: object) -> dict[str, object]:return {"type": "text", "text": str(item)}+def _coerce_pair_list_source(source: object) -> object:+ if not isinstance(source, list):+ return source+ try:+ return dict(cast("Sequence[tuple[str, object]]", source)) # pyright: ignore[reportUnknownArgumentType] # response_obj arrives untyped; dict() rejects non-pair shapes+ except (TypeError, ValueError):+ return source+++def _source_field(source: object, key: str, snake_key: str) -> object:+ if isinstance(source, dict):+ for candidate in (key, snake_key):+ if candidate in source:+ return source[candidate] # pyright: ignore[reportUnknownVariableType] # dict-shaped sources arrive untyped+ return None+ return getattr(source, snake_key, None)++class _CiscoAIDefenseMcpMixin:"""MCP-specific instance methods for ``CiscoAIDefenseGuardrail``.@@ -508,9 +526,10 @@ class _CiscoAIDefenseMcpMixin:content: Sequence[object],source: object = None,) -> dict[str, object]:+ source_map: Final[object] = _coerce_pair_list_source(source)result: Final[dict[str, object]] = {"content": [_serialize_mcp_content_item(item) for item in content]}for key, snake_key in (("structuredContent", "structured_content"), ("isError", "is_error")):- value = source.get(key) if isinstance(source, dict) else getattr(source, snake_key, None)+ value = _source_field(source_map, key, snake_key)if value is not None and (key != "isError" or isinstance(value, bool)):result[key] = valuereturn result@@ -551,7 +570,7 @@ class _CiscoAIDefenseMcpMixin:and all(isinstance(item, tuple) and len(item) == 2 and isinstance(item[0], str) for item in response_obj)):for index, item in enumerate(response_obj):- if item[0] == "structuredContent":+ if item[0] in ("structuredContent", "structured_content"):response_obj[index] = (item[0], replacement)replaced = Trueelif hasattr(response_obj, "structured_content"):diff --git a/litellm/types/mcp.py b/litellm/types/mcp.pyindex 240ff68aacc..2f2c2e6cd1f 100644--- a/litellm/types/mcp.py+++ b/litellm/types/mcp.py@@ -1,3 +1,5 @@+from __future__ import annotations+import enumimport refrom collections.abc import Awaitable, Callable, Mapping@@ -6,13 +8,13 @@ from typing import TYPE_CHECKING, Any, Final, Literalfrom urllib.parse import urlsplitimport httpx-import httpx2from pydantic import BaseModel, ConfigDict, Fieldfrom typing_extensions import TypedDictfrom litellm.types.llms.base import HiddenParamsif TYPE_CHECKING:+ import httpx2from mcp.types import EmbeddedResource as MCPEmbeddedResourcefrom mcp.types import ImageContent as MCPImageContentfrom mcp.types import TextContent as MCPTextContent