litellm/litellm/proxy/mcp_server/tool_outcome.py
Mateo Wang 0a1dbc0fb4
refactor(mcp): drop the experimental label from the MCP server and client paths (#48513)
* refactor(mcp): drop the experimental label from the MCP server and client paths

Move litellm/proxy/_experimental/mcp_server to litellm/proxy/mcp_server and litellm/experimental_mcp_client to litellm/mcp_client, with the mirrored tests moved under tests/unit/proxy/mcp_server and tests/unit/mcp_client. Both old packages stay as shims that warn with DeprecationWarning, re-export the new package's public names and alias every submodule in sys.modules so deep imports keep resolving to the same objects. The OpenAPI docs gate now puts the merge base's enterprise package on PYTHONPATH so its base counts do not mix the checked-out enterprise code with the base litellm tree

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci(mcp): keep tests/unit/proxy/_experimental as a sharded root for the shim tests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(mcp): alias namespace subpackages and bind attributes in the deprecated MCP shims

pkgutil.walk_packages skipped mcp_server/auth because it has no __init__.py, so old imports of litellm.proxy._experimental.mcp_server.auth.* failed, and sys.modules entries alone left litellm.experimental_mcp_client.tools unreachable as an attribute. Walk the package on disk and setattr each alias on its old parent.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-27 17:47:50 +00:00

56 lines
1.6 KiB
Python

"""SDK-free half of the result conversion boundary.
``openapi_to_mcp_generator`` and ``contracts`` must import without the ``mcp``
package installed, so the compatibility enum and the tagged handler outcomes
live here; ``result_conversion`` turns them into SDK results.
"""
from __future__ import annotations
from dataclasses import dataclass
from enum import Enum
from typing import Final
from mcp_types.version import MODERN_PROTOCOL_VERSIONS
from pydantic import JsonValue, TypeAdapter, ValidationError
_JSON_VALUE: Final = TypeAdapter(JsonValue)
class WireCompat(str, Enum):
LEGACY = "legacy"
MODERN = "modern"
def wire_compat_for(protocol_version: str) -> WireCompat:
return WireCompat.MODERN if protocol_version in MODERN_PROTOCOL_VERSIONS else WireCompat.LEGACY
@dataclass(frozen=True, slots=True)
class TextResult:
text: str
@dataclass(frozen=True, slots=True)
class JsonResult:
value: JsonValue
original_text: str
def parse_http_body(body: str) -> TextResult | JsonResult:
if not body.strip():
return TextResult(body)
try:
value: Final = _JSON_VALUE.validate_json(body)
except ValidationError:
return TextResult(body)
if value is None:
return TextResult(body)
return JsonResult(value=value, original_text=body)
def handler_outcome(value: object) -> TextResult | JsonResult:
"""Normalize what a registered tool handler returned; OpenAPI handlers already return a tagged outcome."""
if isinstance(value, (TextResult, JsonResult)):
return value
return TextResult(str(value))