diff --git a/.circleci/scripts/classify_changes.sh b/.circleci/scripts/classify_changes.sh index 21a85c1d914..01bc8290199 100755 --- a/.circleci/scripts/classify_changes.sh +++ b/.circleci/scripts/classify_changes.sh @@ -1,16 +1,22 @@ #!/usr/bin/env bash set -uo pipefail -category="${1:?usage: classify_changes.sh }" +category="${1:?usage: classify_changes.sh }" has_client=false has_backend=false has_ci=false has_provider_harness=false has_cost_map=false +has_mcp_dependencies=false outside_cost_map_set=false while IFS= read -r file || [ -n "$file" ]; do [ -n "$file" ] || continue + case "$file" in + *.md | *.mdx) : ;; + pyproject.toml | */pyproject.toml | uv.lock | uv.toml | .python-version | rust-toolchain.toml | litellm-rust/* | litellm/__init__.py | litellm/proxy/proxy_server.py | litellm/*mcp* | tests/*mcp* | litellm/integrations/arize/* | tests/base_sdk_tests/* | scripts/check_mcp_sdk_install.py | .github/workflows/test-mcp-dependency-resolution.yml | .github/actions/detect-changes/* | .github/actions/setup-uv-with-retries/* | .github/actions/cache-cargo-build/* | .github/scripts/detect_changes.sh | .github/scripts/uv_sync_with_retries.sh | .circleci/scripts/classify_changes.sh | tests/test_litellm/test_circleci_path_filter.py | tests/test_litellm/test_detect_changes.py) + has_mcp_dependencies=true ;; + esac case "$file" in tests/e2e/*/*.py) : ;; tests/e2e/*.py | tests/code_coverage_tests/test_provider_cache.py | tests/code_coverage_tests/test_provider_replay_harness.py | tests/test_litellm/test_circleci_path_filter.py | .circleci/* | pyproject.toml | uv.lock) @@ -31,6 +37,9 @@ while IFS= read -r file || [ -n "$file" ]; do done case "$category" in + mcp-dependencies) + [ "$has_mcp_dependencies" = true ] && echo run || echo skip + ;; cost-map-only) { [ "$has_cost_map" = true ] && [ "$outside_cost_map_set" = false ]; } && echo run || echo skip ;; diff --git a/.github/actions/detect-changes/action.yml b/.github/actions/detect-changes/action.yml index 9b22d2c23a8..b0f9b72f0ee 100644 --- a/.github/actions/detect-changes/action.yml +++ b/.github/actions/detect-changes/action.yml @@ -14,7 +14,7 @@ description: >- inputs: category: - description: "Which classification to apply: backend, client or ui" + description: "Which classification to apply: backend, client, ui, provider-harness, cost-map-only or mcp-dependencies" required: false default: backend github-token: diff --git a/.github/workflows/_test-unit-base.yml b/.github/workflows/_test-unit-base.yml index 617b09a8075..d4e9a65e7c0 100644 --- a/.github/workflows/_test-unit-base.yml +++ b/.github/workflows/_test-unit-base.yml @@ -63,6 +63,11 @@ on: description: "Unique name for the coverage artifact (must be unique per run)" required: true type: string + legacy-mcp-peer: + description: "Install the isolated SDK1 peer for MCP compatibility tests" + required: false + type: boolean + default: false permissions: contents: read @@ -125,10 +130,17 @@ jobs: - name: Install dependencies if: steps.changes.outputs.decision != 'skip' timeout-minutes: 8 + env: + LEGACY_MCP_PEER: ${{ inputs.legacy-mcp-peer }} run: | diff -u model_prices_and_context_window.json litellm/model_prices_and_context_window_backup.json .github/scripts/uv_sync_with_retries.sh --frozen --group ci --group proxy-dev --extra google --extra proxy --extra semantic-router --extra saml uv run --no-sync python -c 'import os, sys; print(sys.version); assert f"{sys.version_info.major}.{sys.version_info.minor}" == os.environ["UV_PYTHON"]' + if [ "$LEGACY_MCP_PEER" = "true" ]; then + uv venv --python "${UV_PYTHON}" .venv-mcp-peer + uv pip install --python .venv-mcp-peer 'mcp==1.28.1' 'langchain-mcp-adapters==0.2.1' + echo "MCP_TEST_PEER_PYTHON=$GITHUB_WORKSPACE/.venv-mcp-peer/bin/python" >> "$GITHUB_ENV" + fi - name: Cache Prisma binaries if: steps.changes.outputs.decision != 'skip' diff --git a/.github/workflows/codspeed.yml b/.github/workflows/codspeed.yml index 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.plugin diff --git a/.github/workflows/test-mcp-dependency-resolution.yml b/.github/workflows/test-mcp-dependency-resolution.yml new file mode 100644 index 00000000000..b5dc573c1c1 --- /dev/null +++ b/.github/workflows/test-mcp-dependency-resolution.yml @@ -0,0 +1,98 @@ +name: LiteLLM MCP Dependency Resolution + +on: + pull_request: + branches: + - main + - litellm_internal_staging + - litellm_oss_staging + - "litellm_**" + +permissions: + contents: read + pull-requests: read + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + resolve: + runs-on: ubuntu-latest + timeout-minutes: 15 + strategy: + fail-fast: false + matrix: + python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"] + + steps: + - uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4.3.0 + with: + persist-credentials: false + + - name: Detect relevant changes + id: changes + uses: ./.github/actions/detect-changes + with: + category: mcp-dependencies + + - name: Set up Python + if: steps.changes.outputs.decision != 'skip' + uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 + with: + python-version: ${{ matrix.python-version }} + + - name: Set up uv + if: steps.changes.outputs.decision != 'skip' + uses: ./.github/actions/setup-uv-with-retries + with: + version: "0.10.9" + + - name: Cache the Rust build + if: steps.changes.outputs.decision != 'skip' + uses: ./.github/actions/cache-cargo-build + + - name: Verify lockfile + if: steps.changes.outputs.decision != 'skip' + run: | + uv lock --check + + - name: Check locked runtime installations + if: steps.changes.outputs.decision != 'skip' + run: | + for extra in core mcp proxy; do + args=() + if [ "$extra" != core ]; then args=(--extra "$extra"); fi + UV_PROJECT_ENVIRONMENT=".venv-$extra" .github/scripts/uv_sync_with_retries.sh --frozen --no-dev --no-editable --python ${{ matrix.python-version }} "${args[@]}" + uv pip check --python ".venv-$extra" + if [ "$extra" = core ]; then + checker=("$GITHUB_WORKSPACE/tests/base_sdk_tests/check_base_sdk_install.py") + else + checker=("$GITHUB_WORKSPACE/scripts/check_mcp_sdk_install.py" --extra "$extra") + fi + (cd "$RUNNER_TEMP" && "$GITHUB_WORKSPACE/.venv-$extra/bin/python" "${checker[@]}") + done + + - name: Build the public wheel + if: steps.changes.outputs.decision != 'skip' + run: uv build --all-packages --wheel --out-dir dist/mcp-check + + - name: Check lowest direct runtime installations + if: steps.changes.outputs.decision != 'skip' + run: | + wheel=$(realpath dist/mcp-check/litellm-[0-9]*.whl) + for extra in core mcp proxy; do + args=() + if [ "$extra" != core ]; then args=(--extra "$extra"); fi + uv pip compile pyproject.toml --no-sources --find-links dist/mcp-check "${args[@]}" --python-version ${{ matrix.python-version }} --resolution lowest-direct -o "lowest-$extra.txt" + uv venv --python ${{ matrix.python-version }} ".venv-lowest-$extra" + uv pip sync --find-links dist/mcp-check --python ".venv-lowest-$extra" "lowest-$extra.txt" + uv pip install --python ".venv-lowest-$extra" --no-deps "$wheel" + uv pip check --python ".venv-lowest-$extra" + if [ "$extra" = core ]; then + checker=("$GITHUB_WORKSPACE/tests/base_sdk_tests/check_base_sdk_install.py") + else + checker=("$GITHUB_WORKSPACE/scripts/check_mcp_sdk_install.py" --extra "$extra") + fi + (cd "$RUNNER_TEMP" && "$GITHUB_WORKSPACE/.venv-lowest-$extra/bin/python" "${checker[@]}") + done diff --git a/.github/workflows/test-mcp.yml b/.github/workflows/test-mcp.yml deleted file mode 100644 index 93ffcbe0586..00000000000 --- a/.github/workflows/test-mcp.yml +++ /dev/null @@ -1,63 +0,0 @@ -name: LiteLLM MCP Tests (folder - tests/mcp_tests) - -on: - pull_request: - branches: - - main - - litellm_internal_staging - - litellm_oss_staging - - "litellm_**" - -permissions: - contents: read - pull-requests: read - -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} - -jobs: - test: - runs-on: ubuntu-latest - timeout-minutes: 25 - - steps: - - uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4.3.0 - with: - persist-credentials: false - - - name: Detect relevant changes - id: changes - uses: ./.github/actions/detect-changes - - - name: Thank You Message - run: | - echo "### 🙏 Thank you for contributing to LiteLLM!" >> $GITHUB_STEP_SUMMARY - echo "Your PR is being tested now. We appreciate your help in making LiteLLM better!" >> $GITHUB_STEP_SUMMARY - - - name: Set up Python - if: steps.changes.outputs.decision != 'skip' - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 - with: - python-version: "3.12" - - - name: Set up uv - if: steps.changes.outputs.decision != 'skip' - uses: ./.github/actions/setup-uv-with-retries - with: - version: "0.10.9" - - - name: Cache the Rust build - if: steps.changes.outputs.decision != 'skip' - uses: ./.github/actions/cache-cargo-build - - - name: Install dependencies - if: steps.changes.outputs.decision != 'skip' - run: | - uv lock --check - .github/scripts/uv_sync_with_retries.sh --frozen --group proxy-dev --extra proxy --extra semantic-router - - - name: Run MCP tests - if: steps.changes.outputs.decision != 'skip' - run: | - uv run --no-sync pytest tests/mcp_tests -x -vv -n 4 --cov=./litellm --cov=./enterprise/litellm_enterprise --cov-report=xml --durations=5 diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index a32b5ebb2a8..aa82a0bf3ee 100644 --- a/.github/workflows/test-unit.yml +++ b/.github/workflows/test-unit.yml @@ -49,6 +49,14 @@ jobs: fail-fast: false matrix: include: + - shard: mcp-integration + artifact-name: mcp-integration + test-path: "tests/mcp_tests" + workers: 2 + reruns: 0 + timeout-minutes: 20 + job-timeout-minutes: 60 + - shard: core-utils artifact-name: core-utils test-path: "tests/test_litellm/litellm_core_utils" @@ -254,3 +262,4 @@ jobs: timeout-minutes: ${{ matrix.timeout-minutes }} job-timeout-minutes: ${{ matrix.job-timeout-minutes }} artifact-name: ${{ matrix.artifact-name }} + legacy-mcp-peer: ${{ matrix.shard == 'mcp-integration' }} diff --git a/litellm/experimental_mcp_client/Readme.md b/litellm/experimental_mcp_client/Readme.md index 4fbd624369c..0c7b0aa76b9 100644 --- a/litellm/experimental_mcp_client/Readme.md +++ b/litellm/experimental_mcp_client/Readme.md @@ -1,6 +1,17 @@ # LiteLLM MCP Client -LiteLLM MCP Client is a client that allows you to use MCP tools with LiteLLM. +LiteLLM MCP Client allows you to use MCP tools with LiteLLM +## MCP Python SDK compatibility +The `mcp` and `proxy` extras require MCP Python SDK 2.2 or newer within the 2.x release line. Installing core LiteLLM without these extras does not require MCP +Existing MCP SDK1 clients can continue connecting to the gateway over the supported legacy MCP protocols. The client and gateway can use different SDK versions in separate Python environments. Modern protocol advertisement remains disabled during the Phase 0 upgrade. An initialize body requesting `2026-07-28` falls back to the supported legacy version `2025-11-25`; an explicit `MCP-Protocol-Version: 2026-07-28` HTTP header is rejected with HTTP 400 + +Code sharing the gateway's Python environment must support SDK2. Its Python API has breaking changes, including renamed imports and snake_case model attributes such as `input_schema`, `is_error`, and `structured_content`. This also applies to callers consuming SDK objects returned by LiteLLM's experimental MCP client. MCP JSON fields retain their protocol spelling, such as `inputSchema` and `isError` + +Upgrade SDK1-dependent libraries before installing them alongside `litellm[mcp]` or `litellm[proxy]`, or keep those clients in a separate environment and connect over the network. For example, `langchain-mcp-adapters==0.2.1` uses SDK1 Python APIs and is tested as a separate legacy client, not as a shared SDK2 dependency + +The shared unit-test workflow runs the MCP integration suite once, with SDK2 in the gateway environment and an isolated SDK1 peer. Keep the SDK1 list/call compatibility test while SDK1 clients are supported; remove it when that support is explicitly retired and the client migration is documented + +See the official [SDK migration guide](https://py.sdk.modelcontextprotocol.io/migration/) for Python API changes diff --git a/litellm/experimental_mcp_client/client.py b/litellm/experimental_mcp_client/client.py index 56ee5f30d02..4b456710057 100644 --- a/litellm/experimental_mcp_client/client.py +++ b/litellm/experimental_mcp_client/client.py @@ -9,57 +9,30 @@ import json import os from collections.abc import Awaitable, Callable, Generator from contextlib import AbstractAsyncContextManager -from datetime import timedelta from functools import partial -from importlib import metadata from types import MappingProxyType -from typing import Any, Final, Protocol, TypeAlias, TypeVar +from typing import Any, Final, TypeAlias, TypeVar -import httpx -from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream -from mcp import ClientSession, McpError, ReadResourceResult, Resource, StdioServerParameters +import httpx2 +from httpx2._client import UseClientDefault +from httpx2._types import AuthTypes +from mcp import ClientSession, MCPError, ReadResourceResult, Resource, StdioServerParameters from mcp.client.sse import sse_client from mcp.client.stdio import stdio_client +from mcp.client.streamable_http import streamable_http_client +from mcp.shared._stream_protocols import ReadStream, WriteStream from mcp.shared.message import SessionMessage -from mcp.shared.session import RequestResponder -from typing_extensions import Unpack _TransportStreams: TypeAlias = tuple[ - MemoryObjectReceiveStream[SessionMessage | Exception], - MemoryObjectSendStream[SessionMessage], - Unpack[tuple[object, ...]], + ReadStream[SessionMessage | Exception], + WriteStream[SessionMessage], ] _TransportContext: TypeAlias = AbstractAsyncContextManager[_TransportStreams] -class _StreamableHttpClientFactory(Protocol): - """The ``streamable_http_client`` entry point this module calls on the installed MCP SDK.""" - - def __call__(self, *, url: str, http_client: httpx.AsyncClient | None) -> _TransportContext: ... - - -streamable_http_client: _StreamableHttpClientFactory | None = None -try: - import mcp.client.streamable_http as streamable_http_module - - streamable_http_client = getattr(streamable_http_module, "streamable_http_client", None) -except ImportError: - pass - -MCP_STREAMABLE_HTTP_REQUIREMENT: Final = "mcp>=1.28.1" - - -def missing_streamable_http_client_error() -> ImportError: - return ImportError( - f"MCP streamable HTTP transport requires {MCP_STREAMABLE_HTTP_REQUIREMENT}, but the installed " - f"mcp {metadata.version('mcp')} does not provide streamable_http_client. " - "Fix with: pip install 'litellm[mcp]' (or upgrade mcp directly: pip install -U mcp)" - ) - - from mcp.types import ( METHOD_NOT_FOUND, - ClientResult, + REQUEST_TIMEOUT, GetPromptRequestParams, GetPromptResult, ListPromptsResult, @@ -68,7 +41,6 @@ from mcp.types import ( Prompt, ResourceTemplate, ServerNotification, - ServerRequest, TextContent, ) from mcp.types import CallToolRequestParams as MCPCallToolRequestParams @@ -153,23 +125,21 @@ def _first_non_cancelled_cause(exc: BaseException) -> BaseException | None: return None -_SDK_READ_TIMEOUT_CODE: Final = int(httpx.codes.REQUEST_TIMEOUT) -"""The code the MCP SDK puts on its own elapsed read timeout, an HTTP status in a field that -otherwise carries JSON-RPC error codes.""" +_SDK_READ_TIMEOUT_CODE: Final = REQUEST_TIMEOUT +"""The code the MCP SDK puts on its own elapsed read timeout.""" def as_mcp_read_timeout(exc: BaseException) -> TimeoutError | None: """Normalize an MCP SDK read timeout for client and gateway diagnostics, or return ``None``. - The SDK reports its own elapsed read timeout as ``McpError`` carrying an HTTP status code in a - field that otherwise holds JSON-RPC error codes, and it relays an upstream's JSON-RPC error - through that same class and field. The numeric code alone therefore cannot separate the two, and - an upstream answering with application code 408 would be reported as a gateway timeout it never - caused. The SDK raises its own from inside an ``except TimeoutError``, so the elapsed timeout is + The SDK reports its own elapsed read timeout as ``MCPError`` carrying ``REQUEST_TIMEOUT`` in a + field that also carries relayed upstream JSON-RPC errors. The numeric code alone therefore + cannot separate the two, and an upstream answering with the same application code would be + reported as a gateway timeout it never caused. The SDK raises its own from inside an ``except TimeoutError``, so the elapsed timeout is on the context chain, while a relayed error is built from a received message and has no such chain; that is the discriminator. """ - if not isinstance(exc, McpError) or exc.error.code != _SDK_READ_TIMEOUT_CODE: + if not isinstance(exc, MCPError) or exc.error.code != _SDK_READ_TIMEOUT_CODE: return None if not isinstance(exc.__context__, TimeoutError): return None @@ -179,9 +149,25 @@ def as_mcp_read_timeout(exc: BaseException) -> TimeoutError | None: TSessionResult = TypeVar("TSessionResult") -class MCPSigV4Auth(httpx.Auth): +class _MCPHTTPClient(httpx2.AsyncClient): + async def send( + self, + request: httpx2.Request, + *, + stream: bool = False, + auth: AuthTypes | UseClientDefault | None = httpx2.USE_CLIENT_DEFAULT, + follow_redirects: bool | UseClientDefault = httpx2.USE_CLIENT_DEFAULT, + ) -> httpx2.Response: + response: Final = await super().send(request, stream=stream, auth=auth, follow_redirects=follow_redirects) + if request.method == "POST" and response.is_error and response.status_code != 404: + await response.aclose() + response.raise_for_status() + return response + + +class MCPSigV4Auth(httpx2.Auth): """ - httpx Auth class that signs each request with AWS SigV4. + httpx2 Auth class that signs each request with AWS SigV4. This is used for MCP servers that require AWS SigV4 authentication, such as AWS Bedrock AgentCore MCP servers. httpx calls auth_flow() for every outgoing request, enabling per-request signature computation. @@ -270,7 +256,7 @@ class MCPSigV4Auth(httpx.Auth): token=sts_creds["SessionToken"], ) - def auth_flow(self, request: httpx.Request) -> Generator[httpx.Request, httpx.Response, None]: + def auth_flow(self, request: httpx2.Request) -> Generator[httpx2.Request, httpx2.Response, None]: from botocore.auth import SigV4Auth from botocore.awsrequest import AWSRequest @@ -314,8 +300,8 @@ class MCPClient: stdio_config: MCPStdioConfig | None = None, extra_headers: dict[str, str] | None = None, ssl_verify: VerifyTypes | None = None, - aws_auth: httpx.Auth | None = None, - resolved_auth: httpx.Auth | None = None, + aws_auth: httpx2.Auth | None = None, + resolved_auth: httpx2.Auth | None = None, sampling_callback: Callable | None = None, elicitation_callback: Callable | None = None, logging_callback: Callable | None = None, @@ -333,10 +319,10 @@ class MCPClient: self.stdio_config: MCPStdioConfig | None = stdio_config self.extra_headers: dict[str, str] | None = extra_headers self.ssl_verify: VerifyTypes | None = ssl_verify - self._aws_auth: httpx.Auth | None = aws_auth - # A pre-resolved httpx.Auth (e.g. from the v2 credential resolver) attached to the + self._aws_auth: httpx2.Auth | None = aws_auth + # A pre-resolved httpx2.Auth (e.g. from the v2 credential resolver) attached to the # upstream client's auth= slot, taking precedence over the SigV4 aws_auth. - self._resolved_auth: httpx.Auth | None = resolved_auth + self._resolved_auth: httpx2.Auth | None = resolved_auth self._last_initialize_instructions: str | None = None self._sampling_callback: Callable | None = sampling_callback self._elicitation_callback: Callable | None = elicitation_callback @@ -348,9 +334,11 @@ class MCPClient: async def discovery_auth_fingerprint(self) -> str: return self._hash_discovery_auth(await self.prepare_request_auth()) - async def prepare_request_auth(self) -> httpx.Request: + async def prepare_request_auth(self) -> httpx2.Request: """Preview the authenticated request without sending it, closing the auth flow afterwards.""" - request: Final = httpx.Request("POST", self.server_url or "http://localhost/", headers=self._get_auth_headers()) + request: Final = httpx2.Request( + "POST", self.server_url or "http://localhost/", headers=self._get_auth_headers() + ) if self._resolved_auth is None: return request flow: Final = self._resolved_auth.async_auth_flow(request) @@ -361,20 +349,20 @@ class MCPClient: await flow.aclose() @staticmethod - def _hash_discovery_auth(request: httpx.Request) -> str: + def _hash_discovery_auth(request: httpx2.Request) -> str: material: Final = json.dumps((str(request.url), tuple(sorted(request.headers.multi_items())))) return hashlib.sha256(material.encode()).hexdigest() def _create_transport_context( self, - ) -> tuple[_TransportContext, httpx.AsyncClient | None]: + ) -> tuple[_TransportContext, httpx2.AsyncClient | None]: """ Create the appropriate transport context based on transport type. Returns: Tuple of (transport_context, http_client). http_client is only set for HTTP transport and needs cleanup. """ - http_client: httpx.AsyncClient | None = None + http_client: httpx2.AsyncClient | None = None if self.transport_type == MCPTransport.stdio: if not self.stdio_config: raise ValueError("stdio_config is required for stdio transport") @@ -397,14 +385,12 @@ class MCPClient: None, ) # HTTP transport (default) - if streamable_http_client is None: - raise missing_streamable_http_client_error() headers = self._get_auth_headers() httpx_client_factory = self._create_httpx_client_factory() verbose_logger.debug("litellm headers for streamable_http_client: %s", headers) http_client = httpx_client_factory( headers=headers, - timeout=httpx.Timeout(self.timeout), + timeout=httpx2.Timeout(self.timeout), ) transport_ctx: Final = streamable_http_client( url=self.server_url, @@ -473,13 +459,14 @@ class MCPClient: transport: Final = await transport_ctx.__aenter__() in_flight_error: BaseException | None = None try: - read_stream, write_stream = transport[0], transport[1] + read_stream: Final = transport[0] + write_stream: Final = transport[1] stream_error: Final[asyncio.Future[Exception]] = asyncio.get_running_loop().create_future() async def receive_message( - message: RequestResponder[ServerRequest, ClientResult] | ServerNotification | Exception, + message: ServerNotification | Exception, ) -> None: - if not isinstance(message, (ValueError, httpx.RequestError, OSError)): + if not isinstance(message, (ValueError, httpx2.HTTPError, OSError)): return if not stream_error.done(): stream_error.set_result(message) @@ -499,7 +486,7 @@ class MCPClient: session_ctx: Final = ClientSession( read_stream, write_stream, - read_timeout_seconds=timedelta(seconds=self.timeout), + read_timeout_seconds=self.timeout, message_handler=receive_message, **session_kwargs, ) @@ -512,7 +499,7 @@ class MCPClient: if isinstance(ins, str) and ins.strip(): self._last_initialize_instructions = ins.strip() return await operation(session) - except McpError: + except MCPError: if stream_error.done(): raise stream_error.result() raise @@ -544,7 +531,7 @@ class MCPClient: quiet_on_error demotes the failure line to debug for callers that own the exception (call_tool / list_tools under raise_on_error), so an expected pass-through re-auth does not emit a warning per call; every other caller keeps the operator-visible warning.""" - http_client: httpx.AsyncClient | None = None + http_client: httpx2.AsyncClient | None = None try: self._last_initialize_instructions = None transport_ctx, http_client = self._create_transport_context() @@ -609,7 +596,7 @@ class MCPClient: elif isinstance(self._mcp_auth_value, dict): headers.update(self._mcp_auth_value) # Note: aws_sigv4 auth is not handled here — SigV4 requires per-request - # signing (including the body hash), so it uses httpx.Auth flow instead + # signing (including the body hash), so it uses httpx2.Auth flow instead # of static headers. See MCPSigV4Auth and _create_httpx_client_factory(). # update the headers with the extra headers if self.extra_headers: @@ -623,9 +610,11 @@ class MCPClient: headers.update(injected or {}) return _strip_header_whitespace(headers) - def _create_httpx_client_factory(self) -> Callable[..., httpx.AsyncClient]: + def _create_httpx_client_factory( + self, *, transport: httpx2.AsyncBaseTransport | None = None + ) -> Callable[..., httpx2.AsyncClient]: """ - Create a custom httpx client factory that uses LiteLLM's SSL configuration. + Create a custom httpx2 client factory that uses LiteLLM's SSL configuration. This factory follows the same CA bundle path logic as http_handler.py: 1. Check ssl_verify parameter (can be SSLContext, bool, or path to CA bundle) 2. Check SSL_VERIFY environment variable @@ -636,10 +625,10 @@ class MCPClient: def factory( *, headers: dict[str, str] | None = None, - timeout: httpx.Timeout | None = None, - auth: httpx.Auth | None = None, - ) -> httpx.AsyncClient: - """Create an httpx.AsyncClient with LiteLLM's SSL configuration.""" + timeout: httpx2.Timeout | None = None, + auth: httpx2.Auth | None = None, + ) -> httpx2.AsyncClient: + """Create an httpx2.AsyncClient with LiteLLM's SSL configuration.""" # Get unified SSL configuration using the same logic as http_handler.py ssl_config: Final = get_ssl_configuration(self.ssl_verify) verbose_logger.debug("MCP client using SSL configuration: %s", type(ssl_config).__name__) @@ -649,7 +638,8 @@ class MCPClient: fallback_auth: Final = self._resolved_auth if self._resolved_auth is not None else self._aws_auth effective_auth: Final = auth if auth is not None else fallback_auth guard: Final = credential_redirect_hook(self.server_url, self._credential_slot) - return httpx.AsyncClient( + return _MCPHTTPClient( + transport=transport, headers=headers, timeout=timeout, auth=effective_auth, @@ -723,7 +713,7 @@ class MCPClient: """The error result ``call_tool`` returns when it swallows a failure (no re-execution).""" return MCPCallToolResult( content=[TextContent(type="text", text=f"{type(exc).__name__}: {exc}")], - isError=True, + is_error=True, ) async def call_tool( @@ -808,12 +798,12 @@ class MCPClient: verbose_logger.debug("MCP client listing tools from %s", self.server_url or "stdio") async def _list_prompts_operation(session: ClientSession) -> ListPromptsResult: - capabilities: Final = session.get_server_capabilities() + capabilities: Final = session.server_capabilities if capabilities is not None and capabilities.prompts is None: return ListPromptsResult(prompts=[]) try: return await session.list_prompts() - except McpError as error: + except MCPError as error: if error.error.code != METHOD_NOT_FOUND: raise verbose_logger.debug( @@ -898,12 +888,12 @@ class MCPClient: verbose_logger.debug("MCP client listing resources from %s", self.server_url or "stdio") async def _list_resources_operation(session: ClientSession) -> ListResourcesResult: - capabilities: Final = session.get_server_capabilities() + capabilities: Final = session.server_capabilities if capabilities is not None and capabilities.resources is None: return ListResourcesResult(resources=[]) try: return await session.list_resources() - except McpError as error: + except MCPError as error: if error.error.code != METHOD_NOT_FOUND: raise verbose_logger.debug( @@ -947,30 +937,30 @@ class MCPClient: verbose_logger.debug("MCP client listing resource templates from %s", self.server_url or "stdio") async def _list_resource_templates_operation(session: ClientSession) -> ListResourceTemplatesResult: - capabilities: Final = session.get_server_capabilities() + capabilities: Final = session.server_capabilities if capabilities is not None and capabilities.resources is None: - return ListResourceTemplatesResult(resourceTemplates=[]) + return ListResourceTemplatesResult(resource_templates=[]) # mutable-ok: MCP result payload try: return await session.list_resource_templates() - except McpError as error: + except MCPError as error: if error.error.code != METHOD_NOT_FOUND: raise verbose_logger.debug( "MCP client list_resource_templates is unsupported by %s: %s", self.server_url or "stdio", error ) - return ListResourceTemplatesResult(resourceTemplates=[]) + return ListResourceTemplatesResult(resource_templates=[]) # mutable-ok: MCP result payload try: result: Final = await self.run_with_session(_list_resource_templates_operation) - resource_template_count: Final = len(result.resourceTemplates) - resource_template_names: Final = [resourceTemplate.name for resourceTemplate in result.resourceTemplates] + resource_template_count: Final = len(result.resource_templates) + resource_template_names: Final = [resource_template.name for resource_template in result.resource_templates] verbose_logger.info( "MCP client listed %s resource templates from %s: %s", resource_template_count, self.server_url or "stdio", resource_template_names, ) - return result.resourceTemplates + return result.resource_templates except asyncio.CancelledError: verbose_logger.warning("MCP client list_resource_templates was cancelled") raise @@ -1000,7 +990,7 @@ class MCPClient: async def _read_resource_operation(session: ClientSession): verbose_logger.debug("MCP client sending read_resource request to session") - return await session.read_resource(url) + return await session.read_resource(str(url)) try: read_resource_result: Final = await self.run_with_session(_read_resource_operation) diff --git a/litellm/experimental_mcp_client/tools.py b/litellm/experimental_mcp_client/tools.py index 51d2139ef3b..a9ee851d529 100644 --- a/litellm/experimental_mcp_client/tools.py +++ b/litellm/experimental_mcp_client/tools.py @@ -26,7 +26,7 @@ from litellm.types.utils import ChatCompletionMessageToolCall ######################################################## def transform_mcp_tool_to_openai_tool(mcp_tool: MCPTool) -> ChatCompletionToolParam: """Convert an MCP tool to an OpenAI tool.""" - normalized_parameters: Final = _normalize_mcp_input_schema(mcp_tool.inputSchema) + normalized_parameters: Final = _normalize_mcp_input_schema(mcp_tool.input_schema) return ChatCompletionToolParam( type="function", @@ -73,7 +73,7 @@ def transform_mcp_tool_to_openai_responses_api_tool( mcp_tool: MCPTool, ) -> FunctionToolParam: """Convert an MCP tool to an OpenAI Responses API tool.""" - normalized_parameters: Final = _normalize_mcp_input_schema(mcp_tool.inputSchema) + normalized_parameters: Final = _normalize_mcp_input_schema(mcp_tool.input_schema) return FunctionToolParam( name=mcp_tool.name, @@ -93,7 +93,7 @@ def transform_mcp_tool_to_anthropic_tool(mcp_tool: MCPTool) -> AnthropicMessages return AnthropicMessagesTool( name=mcp_tool.name, description=mcp_tool.description or "", - input_schema=sanitize_input_schema_for_anthropic(mcp_tool.inputSchema), + input_schema=sanitize_input_schema_for_anthropic(mcp_tool.input_schema), type="custom", ) @@ -129,7 +129,7 @@ async def list_tools_with_pagination( ) tools.extend(result.tools) - next_cursor = getattr(result, "nextCursor", None) + next_cursor = getattr(result, "next_cursor", None) if not isinstance(next_cursor, str) or not next_cursor: return tools if next_cursor in seen_cursors: diff --git a/litellm/integrations/arize/_utils.py b/litellm/integrations/arize/_utils.py index 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 content if payload is None: return diff --git a/litellm/proxy/_experimental/mcp_server/elicitation_handler.py b/litellm/proxy/_experimental/mcp_server/elicitation_handler.py index bbd1c9aaf1e..6155f1f215c 100644 --- a/litellm/proxy/_experimental/mcp_server/elicitation_handler.py +++ b/litellm/proxy/_experimental/mcp_server/elicitation_handler.py @@ -42,9 +42,9 @@ class _DownstreamElicitSession(Protocol): async def elicit_url(self, message: str, url: str, elicitation_id: str) -> "ElicitResult": ... - async def elicit_form(self, message: str, requestedSchema: dict[str, object]) -> "ElicitResult": ... + async def elicit_form(self, message: str, requested_schema: dict[str, object]) -> "ElicitResult": ... - async def elicit(self, message: str, requestedSchema: dict[str, object]) -> "ElicitResult": ... + async def elicit(self, message: str, requested_schema: dict[str, object]) -> "ElicitResult": ... async def handle_elicitation_request( @@ -145,22 +145,22 @@ async def _relay_elicitation_to_downstream( result = await downstream_session.elicit_url( message=params.message, url=params.url, - elicitation_id=params.elicitationId, + elicitation_id=params.elicitation_id, ) elif isinstance(params, ElicitRequestFormParams): # Form mode: relay structured form to client verbose_logger.info("MCP elicitation: relaying form mode to downstream") result = await downstream_session.elicit_form( message=params.message, - requestedSchema=params.requestedSchema, + requested_schema=params.requested_schema, ) else: # Fallback for generic ElicitRequestParams — pass an empty schema - # since elicit() requires requestedSchema as a positional arg. + # since elicit() requires requested_schema as a positional arg. verbose_logger.info("MCP elicitation: relaying generic elicitation to downstream") result = await downstream_session.elicit( message=getattr(params, "message", ""), - requestedSchema=getattr(params, "requestedSchema", {}), + requested_schema=getattr(params, "requested_schema", {}), # mutable-ok: elicitation default schema ) verbose_logger.info( "MCP elicitation: downstream responded with action=%s", diff --git a/litellm/proxy/_experimental/mcp_server/faults/list_outcomes.py b/litellm/proxy/_experimental/mcp_server/faults/list_outcomes.py index 42b2d29cd52..b96a7a74e4a 100644 --- a/litellm/proxy/_experimental/mcp_server/faults/list_outcomes.py +++ b/litellm/proxy/_experimental/mcp_server/faults/list_outcomes.py @@ -14,6 +14,7 @@ from collections.abc import Iterator from typing import Final, Literal, NamedTuple, NoReturn, TypeAlias import httpx +import httpx2 from mcp.types import Tool as MCPTool from pydantic import BaseModel, ConfigDict from typing_extensions import assert_never @@ -63,8 +64,8 @@ class AggregateToolListing(NamedTuple): outcomes: dict[str, ServerOutcome] -def _iter_upstream_responses(exc: BaseException) -> Iterator[httpx.Response]: - """Yield every ``httpx.Response`` in the exception tree, in the shared traversal's deliberate +def _iter_upstream_responses(exc: BaseException) -> Iterator[httpx.Response | httpx2.Response]: + """Yield every upstream ``httpx``/``httpx2`` ``Response`` in the exception tree, in the shared traversal's deliberate order (explicit causes first, ExceptionGroup members in raise order, the incidental ``__context__`` chain last), so a response raised while handling the real failure can never shadow one on the explicit causal chain. Consumers apply their own predicate over the stream: @@ -72,11 +73,11 @@ def _iter_upstream_responses(exc: BaseException) -> Iterator[httpx.Response]: behind an unrelated earlier one.""" for current in iter_exception_tree(exc): response = getattr(current, "response", None) - if isinstance(response, httpx.Response): + if isinstance(response, (httpx.Response, httpx2.Response)): yield response -def _find_upstream_response(exc: BaseException) -> httpx.Response | None: +def _find_upstream_response(exc: BaseException) -> httpx.Response | httpx2.Response | None: return next(_iter_upstream_responses(exc), None) @@ -136,9 +137,9 @@ def classify_list_exception(exc: BaseException) -> ServerListFault: response: Final = _find_upstream_response(exc) if response is not None: return ServerListFault(tag="upstream_error", status_code=response.status_code) - if isinstance(exc, (httpx.TimeoutException,)): + if isinstance(exc, (httpx.TimeoutException, httpx2.TimeoutException)): return ServerListFault(tag="timeout") - if isinstance(exc, httpx.TransportError): + if isinstance(exc, (httpx.TransportError, httpx2.TransportError)): return ServerListFault(tag="unreachable") return ServerListFault(tag="internal") diff --git a/litellm/proxy/_experimental/mcp_server/guardrail_translation/handler.py b/litellm/proxy/_experimental/mcp_server/guardrail_translation/handler.py index c0235077ecd..08a5d2b4135 100644 --- a/litellm/proxy/_experimental/mcp_server/guardrail_translation/handler.py +++ b/litellm/proxy/_experimental/mcp_server/guardrail_translation/handler.py @@ -135,7 +135,7 @@ class MCPGuardrailTranslationHandler(BaseTranslation): mcp_tool: Final = MCPTool( name=mcp_tool_name, description=mcp_tool_description or "", - inputSchema={}, # Call payload has no schema; guardrail gets args from request_data + input_schema={}, # mutable-ok: call payload has no schema; guardrail gets args from request_data ) openai_tool: Final = transform_mcp_tool_to_openai_tool(mcp_tool) fn: Final = openai_tool["function"] diff --git a/litellm/proxy/_experimental/mcp_server/mcp_context.py b/litellm/proxy/_experimental/mcp_server/mcp_context.py index 74cc0c900d9..11325a9f127 100644 --- a/litellm/proxy/_experimental/mcp_server/mcp_context.py +++ b/litellm/proxy/_experimental/mcp_server/mcp_context.py @@ -6,7 +6,23 @@ mcp_server_manager.py and server.py. """ from contextvars import ContextVar -from typing import Final +from typing import TYPE_CHECKING, Final + +if TYPE_CHECKING: + from mcp.server.context import ServerRequestContext + +# The SDK 1.x ``mcp.server.lowlevel.server.request_ctx`` ContextVar was removed in +# SDK 2, which hands each request handler a ``ServerRequestContext`` argument +# instead. The handlers set this var so downstream helpers (session auth caching, +# debug diagnostics, progress forwarding) can reach the same request-scoped state. +active_mcp_request_ctx_var: Final[ContextVar["ServerRequestContext | None"]] = ContextVar( + "active_mcp_request_ctx", default=None +) + + +def get_active_mcp_request_ctx() -> "ServerRequestContext | None": + return active_mcp_request_ctx_var.get() + # Set server-side in proxy_server.py route handlers when a request arrives via # /toolset/{name}/mcp or the toolset fallback in dynamic_mcp_route. diff --git a/litellm/proxy/_experimental/mcp_server/mcp_debug.py b/litellm/proxy/_experimental/mcp_server/mcp_debug.py index 1f157aefdc3..ff482b80b50 100644 --- a/litellm/proxy/_experimental/mcp_server/mcp_debug.py +++ b/litellm/proxy/_experimental/mcp_server/mcp_debug.py @@ -100,6 +100,8 @@ Usage with curl:: http://localhost:4000/mcp/atlassian_mcp """ +from __future__ import annotations + import asyncio import base64 import io @@ -109,17 +111,20 @@ from collections.abc import AsyncIterator, Callable, Mapping from http.cookies import CookieError, SimpleCookie from itertools import islice from types import MappingProxyType -from typing import Final +from typing import TYPE_CHECKING, Final from urllib.parse import parse_qsl, quote, quote_plus, unquote_plus, urlencode import httpx +import httpx2 from pydantic import JsonValue, TypeAdapter from starlette.requests import HTTPConnection from starlette.types import Message, Send from litellm.litellm_core_utils.secret_redaction import REDACTED, redact_string from litellm.litellm_core_utils.sensitive_data_masker import SensitiveDataMasker -from litellm.proxy._experimental.mcp_server.outbound_credentials.types import AuthResolution + +if TYPE_CHECKING: + from litellm.proxy._experimental.mcp_server.outbound_credentials.types import AuthResolution # Header the client sends to opt into debug mode MCP_DEBUG_REQUEST_HEADER: Final = "x-litellm-mcp-debug" @@ -132,9 +137,9 @@ MCP_AUTH_DIAGNOSTICS_SCOPE_KEY: Final = "litellm.mcp.auth_diagnostics" def record_auth_resolution(server_id: str, source: AuthResolution) -> None: - from mcp.server.lowlevel.server import request_ctx + from litellm.proxy._experimental.mcp_server.mcp_context import get_active_mcp_request_ctx - context: Final[object] = request_ctx.get(None) + context: Final[object] = get_active_mcp_request_ctx() request: Final[object] = getattr(context, "request", None) if isinstance(request, HTTPConnection): diagnostics: Final[object] = request.scope.get(MCP_AUTH_DIAGNOSTICS_SCOPE_KEY) @@ -150,6 +155,8 @@ class MCPAuthDiagnostics: self._outcomes = tuple(item for item in self._outcomes if item[0] != server_id) + ((server_id, resolution),) def resolution(self) -> str: + from litellm.proxy._experimental.mcp_server.outbound_credentials.types import AuthResolution + match self._outcomes: case (): return AuthResolution.unresolved.value @@ -159,6 +166,8 @@ class MCPAuthDiagnostics: return AuthResolution.multiple.value def headers(self) -> Mapping[str, str]: + from litellm.proxy._experimental.mcp_server.outbound_credentials.types import AuthResolution + if len(self._outcomes) <= 1: return MappingProxyType({"x-mcp-debug-auth-resolution": self.resolution()}) return MappingProxyType( @@ -372,6 +381,8 @@ class MCPDebug: server_url: str | None = None server_auth_type: str | None = None + from litellm.proxy._experimental.mcp_server.outbound_credentials.types import AuthResolution + auth_resolution: Final = AuthResolution.unresolved.value for server_name in mcp_servers or []: @@ -409,7 +420,7 @@ def _safe_text(value: str, limit: int = _BODY_PREVIEW_CHARS) -> str: return escaped if len(escaped) <= limit else f"{escaped[:limit]}...(truncated)" -def safe_upstream_url(url: httpx.URL) -> str: +def safe_upstream_url(url: httpx.URL | httpx2.URL) -> str: return _safe_text(str(url.copy_with(username="", password="", path="/", query=None, fragment=None))) @@ -449,10 +460,10 @@ def _header_secret_values(name: str, value: str) -> tuple[str, ...]: return (value, credential, decoded, password, unquote_plus(password)) -def _body_secret_values(request: httpx.Request) -> tuple[str, ...] | None: +def _body_secret_values(request: httpx.Request | httpx2.Request) -> tuple[str, ...] | None: try: raw: Final = request.content - except httpx.RequestNotRead: + except (httpx.RequestNotRead, httpx2.RequestNotRead): return None if not raw: return () @@ -478,7 +489,7 @@ def _body_secret_values(request: httpx.Request) -> tuple[str, ...] | None: ) -def _request_secret_values(request: httpx.Request) -> tuple[str, ...] | None: +def _request_secret_values(request: httpx.Request | httpx2.Request) -> tuple[str, ...] | None: body_values: Final = _body_secret_values(request) if body_values is None: return None @@ -537,18 +548,18 @@ def _preview(raw: bytes, content_type: str = "", secrets: tuple[str, ...] = ()) return _safe_text(redact_string(_mask_known_values(json.dumps(parsed, separators=(",", ":")), secrets))) -def _masked_headers(headers: httpx.Headers) -> str: +def _masked_headers(headers: httpx.Headers | httpx2.Headers) -> str: return _safe_text(", ".join(f"{name}={value}" for name, value in headers.items() if name in _SAFE_HEADER_NAMES)) -def _request_body_preview(request: httpx.Request, secrets: tuple[str, ...] | None) -> str: +def _request_body_preview(request: httpx.Request | httpx2.Request, secrets: tuple[str, ...] | None) -> str: try: return _preview(request.content, request.headers.get("content-type", ""), secrets or ()) - except httpx.RequestNotRead: + except (httpx.RequestNotRead, httpx2.RequestNotRead): return "(streamed, not captured)" -def _response_body_preview(response: httpx.Response, secrets: tuple[str, ...] | None) -> str: +def _response_body_preview(response: httpx.Response | httpx2.Response, secrets: tuple[str, ...] | None) -> str: if secrets is None: return "(omitted: request credentials unavailable)" captured: Final = response.extensions.get(_CAPTURE_EXTENSION) @@ -556,7 +567,7 @@ def _response_body_preview(response: httpx.Response, secrets: tuple[str, ...] | return captured try: return _preview(response.content, response.headers.get("content-type", ""), secrets) - except httpx.ResponseNotRead: + except (httpx.ResponseNotRead, httpx2.ResponseNotRead): return "(not read)" @@ -569,7 +580,7 @@ async def _read_error_prefix(chunks: AsyncIterator[bytes], limit: int) -> bytes: return buffer.getvalue() -async def capture_upstream_error_response(response: httpx.Response) -> None: +async def capture_upstream_error_response(response: httpx.Response | httpx2.Response) -> None: if not response.is_error: return try: @@ -584,7 +595,7 @@ async def capture_upstream_error_response(response: httpx.Response) -> None: if secrets is not None else "(omitted: request credentials unavailable)" ) - except (asyncio.TimeoutError, httpx.HTTPError, httpx.StreamError): + except (asyncio.TimeoutError, httpx.HTTPError, httpx.StreamError, httpx2.HTTPError, httpx2.StreamError): response._content = b"" # pyright: ignore[reportPrivateUsage] # rebind-ok: httpx auth retries must survive diagnostic read failures response.extensions[_CAPTURE_EXTENSION] = ( "(unavailable: error body read failed)" # rebind-ok: httpx response hooks communicate through extensions @@ -593,7 +604,7 @@ async def capture_upstream_error_response(response: httpx.Response) -> None: response.extensions[_CAPTURE_EXTENSION] = preview # rebind-ok: httpx response hooks communicate through extensions -def describe_upstream_response(response: httpx.Response) -> str: +def describe_upstream_response(response: httpx.Response | httpx2.Response) -> str: try: request: Final = response.request except RuntimeError: @@ -616,6 +627,6 @@ def describe_upstream_http_failure(exc: BaseException) -> str | None: describe_upstream_response(response) for current in islice(iter_exception_tree(exc), 16) for response in (getattr(current, "response", None),) - if isinstance(response, httpx.Response) + if isinstance(response, (httpx.Response, httpx2.Response)) ) return " | ".join(lines) or None diff --git a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py index 469ea86ad4b..36ecb05208b 100644 --- a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py +++ b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py @@ -34,6 +34,7 @@ from urllib.parse import ParseResult, urlparse import anyio import httpx +import httpx2 from fastapi import HTTPException from httpx import HTTPStatusError from mcp import ReadResourceResult, Resource @@ -194,8 +195,7 @@ from litellm.types.mcp_server.mcp_server_manager import ( from litellm.types.utils import CallTypes if TYPE_CHECKING: - from mcp.client.session import ClientSession - from mcp.shared.context import RequestContext + from mcp.client.session import ClientRequestContext from mcp.types import CreateMessageRequestParams from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj @@ -1297,8 +1297,8 @@ def _passthrough_token_from_mcp_auth_header( return None -async def _materialize_auth_headers(auth: httpx.Auth | None) -> dict[str, str] | None: - """Extract the header a resolved ``httpx.Auth`` would set, as a plain dict, or None. +async def _materialize_auth_headers(auth: httpx2.Auth | None) -> dict[str, str] | None: + """Extract the header a resolved ``httpx2.Auth`` would set, as a plain dict, or None. OpenAPI tool closures egress through ``AsyncHTTPHandler`` methods that accept headers but no ``auth``, so a resolved credential must be materialized into a header value. Driving one step @@ -1313,7 +1313,7 @@ async def _materialize_auth_headers(auth: httpx.Auth | None) -> dict[str, str] | header_name: Final = getattr(auth, "header_name", None) if not isinstance(header_name, str) or not header_name: return None - probe: Final = httpx.Request("GET", "http://localhost/") + probe: Final = httpx2.Request("GET", "http://localhost/") flow: Final = auth.async_auth_flow(probe) try: first_request: Final = await flow.__anext__() @@ -1587,7 +1587,7 @@ def _create_sampling_callback(user_api_key_auth: UserAPIKeyAuth | None = None): return None async def _sampling_callback( - context: "RequestContext[ClientSession, object]", + context: "ClientRequestContext", params: "CreateMessageRequestParams", ): import litellm @@ -4012,7 +4012,7 @@ class MCPServerManager: subject_token: str | None, user_api_key_auth: UserAPIKeyAuth | None, extra_headers: dict[str, str] | None, - ) -> tuple[httpx.Auth | None, dict[str, str] | None]: + ) -> tuple[httpx2.Auth | None, dict[str, str] | None]: """Resolve a v2-owned server's upstream credential into ``(resolved_auth, extra_headers)``. On a missing/rejected per-user credential this raises the mode's discovery challenge @@ -5552,7 +5552,7 @@ class MCPServerManager: verbose_logger.error(error_msg) return CallToolResult( content=[TextContent(type="text", text=error_msg)], - isError=True, + is_error=True, ) try: @@ -5563,7 +5563,7 @@ class MCPServerManager: # Convert the handler result (string response) to CallToolResult format result: Final = CallToolResult( content=[TextContent(type="text", text=str(handler_result))], - isError=False, + is_error=False, ) return result @@ -5579,7 +5579,7 @@ class MCPServerManager: verbose_logger.error(error_msg) return CallToolResult( content=[TextContent(type="text", text=error_msg)], - isError=True, + is_error=True, ) async def pre_call_tool_check( diff --git a/litellm/proxy/_experimental/mcp_server/outbound_credentials/client_credentials.py b/litellm/proxy/_experimental/mcp_server/outbound_credentials/client_credentials.py index 43d97abe4db..3a8e2b3840a 100644 --- a/litellm/proxy/_experimental/mcp_server/outbound_credentials/client_credentials.py +++ b/litellm/proxy/_experimental/mcp_server/outbound_credentials/client_credentials.py @@ -34,6 +34,7 @@ from dataclasses import dataclass from typing import Annotated, Final, Literal import httpx +import httpx2 from pydantic import BaseModel, ConfigDict, Field, SecretStr, TypeAdapter, ValidationError from typing_extensions import assert_never @@ -337,7 +338,7 @@ def _identity_key(config: ClientCredentialsConfig) -> str: return hashlib.sha256(material.encode("utf-8")).hexdigest() -class ClientCredentialsBearerAuth(httpx.Auth): +class ClientCredentialsBearerAuth(httpx2.Auth): """Bearer auth that retries an upstream 401 exactly once with a freshly minted token. The initial token was already resolved (so config/IdP failures surfaced as typed errors @@ -356,7 +357,7 @@ class ClientCredentialsBearerAuth(httpx.Auth): self._access_token = SecretStr(access_token) self._refetch = refetch - async def async_auth_flow(self, request: httpx.Request) -> AsyncGenerator[httpx.Request, httpx.Response]: + async def async_auth_flow(self, request: httpx2.Request) -> AsyncGenerator[httpx2.Request, httpx2.Response]: token: Final = self._access_token.get_secret_value() name, value = self._carrier.header(token) request.headers[name] = value @@ -371,5 +372,5 @@ class ClientCredentialsBearerAuth(httpx.Auth): request.headers[fresh_name] = fresh_value yield request - def sync_auth_flow(self, request: httpx.Request) -> Generator[httpx.Request, httpx.Response, None]: - raise RuntimeError("ClientCredentialsBearerAuth only supports async httpx clients") + def sync_auth_flow(self, request: httpx2.Request) -> Generator[httpx2.Request, httpx2.Response, None]: + raise RuntimeError("ClientCredentialsBearerAuth only supports async httpx2 clients") diff --git a/litellm/proxy/_experimental/mcp_server/outbound_credentials/httpx_auth.py b/litellm/proxy/_experimental/mcp_server/outbound_credentials/httpx_auth.py index e4d8fd25748..aa04469a502 100644 --- a/litellm/proxy/_experimental/mcp_server/outbound_credentials/httpx_auth.py +++ b/litellm/proxy/_experimental/mcp_server/outbound_credentials/httpx_auth.py @@ -1,29 +1,29 @@ -"""Concrete `httpx.Auth` objects the resolver returns for the self-contained modes. +"""Concrete `httpx2.Auth` objects the resolver returns for the self-contained modes. -These are the egress credential as the SDK consumes it: an `httpx.Auth` attached to the +These are the egress credential as the SDK consumes it: an `httpx2.Auth` attached to the upstream `AsyncClient`. The OAuth-flow modes (`authorization_code`, `client_credentials`, `token_exchange`) return SDK-provided auth objects instead and land later. -`auth_flow` mutating the outbound request is the `httpx.Auth` contract, not a house-style -violation: the request is httpx's object, and these carry no state of their own. +`auth_flow` mutating the outbound request is the `httpx2.Auth` contract, not a house-style +violation: the request is httpx2's object, and these carry no state of their own. """ from __future__ import annotations from collections.abc import Generator -import httpx +import httpx2 from pydantic import SecretStr -class NoOpAuth(httpx.Auth): +class NoOpAuth(httpx2.Auth): """Attaches nothing — the `none` mode (and the seam-level default).""" - def auth_flow(self, request: httpx.Request) -> Generator[httpx.Request, httpx.Response, None]: + def auth_flow(self, request: httpx2.Request) -> Generator[httpx2.Request, httpx2.Response, None]: yield request -class StaticHeaderAuth(httpx.Auth): +class StaticHeaderAuth(httpx2.Auth): """Sets one fixed header on every request — the `api_key` family and `passthrough`. The header value is a live credential (a bearer token, an API key, a forwarded user @@ -36,6 +36,6 @@ class StaticHeaderAuth(httpx.Auth): self.header_name = header_name self._header_value = SecretStr(header_value) - def auth_flow(self, request: httpx.Request) -> Generator[httpx.Request, httpx.Response, None]: + def auth_flow(self, request: httpx2.Request) -> Generator[httpx2.Request, httpx2.Response, None]: request.headers[self.header_name] = self._header_value.get_secret_value() yield request diff --git a/litellm/proxy/_experimental/mcp_server/outbound_credentials/resolver.py b/litellm/proxy/_experimental/mcp_server/outbound_credentials/resolver.py index 85c7f68719d..e71353e479c 100644 --- a/litellm/proxy/_experimental/mcp_server/outbound_credentials/resolver.py +++ b/litellm/proxy/_experimental/mcp_server/outbound_credentials/resolver.py @@ -1,7 +1,7 @@ """The one credential resolver: dispatch on the declared mode, fail closed. `resolve_credentials` selects exactly one arm off the server's typed `config` and either -produces an `httpx.Auth` or returns a typed `CredError`. The `match` is over the `AuthConfig` +produces an `httpx2.Auth` or returns a typed `CredError`. The `match` is over the `AuthConfig` variant, so each arm receives its own fully-typed config with no field-presence inference and no precedence cascade. It is wildcard-free with an `assert_never` tail, so adding a mode without an arm fails the type gate (basedpyright `reportMatchNotExhaustive`); a bypassed gate fails loudly @@ -25,6 +25,7 @@ from functools import partial from typing import Final import httpx +import httpx2 from typing_extensions import assert_never from litellm._logging import verbose_proxy_logger @@ -135,7 +136,7 @@ class UpstreamCredentialProvider: self._client_credentials_source = client_credentials_source or ClientCredentialsTokenSource() self._sso_assertion_store: SSOAssertionStore = sso_assertion_store or default_sso_assertion_store() - async def resolve_credentials(self, subject: Subject, server: ServerSpec) -> Result[httpx.Auth, CredError]: + async def resolve_credentials(self, subject: Subject, server: ServerSpec) -> Result[httpx2.Auth, CredError]: match server.config: case NoneConfig(): return self._none(server) @@ -155,7 +156,7 @@ class UpstreamCredentialProvider: return _not_implemented(AuthSpecKind.aws_sigv4) assert_never(server.config) - def _none(self, server: ServerSpec) -> Result[httpx.Auth, CredError]: + def _none(self, server: ServerSpec) -> Result[httpx2.Auth, CredError]: try: resource: Final = httpx.URL(server.resource) except httpx.InvalidURL: @@ -169,12 +170,12 @@ class UpstreamCredentialProvider: Reads from the same per-user store as the ``authorization_code`` arm, so the discovery challenge and the egress agree on whether the user is authorized. Returns a typed ``bool`` - (no ``httpx.Auth``), unlike ``resolve_credentials``. A non-per-user mode has no token in the + (no ``httpx2.Auth``), unlike ``resolve_credentials``. A non-per-user mode has no token in the store, so it reads as False without a per-mode branch here. """ return await self._authz_token(subject, server) is not None - def _passthrough(self, subject: Subject) -> Result[httpx.Auth, CredError]: + def _passthrough(self, subject: Subject) -> Result[httpx2.Auth, CredError]: """Forward the caller's own upstream credential verbatim; the gateway mints nothing. The inbound token is the caller's already-disambiguated ``Authorization`` (never the LiteLLM @@ -186,7 +187,7 @@ class UpstreamCredentialProvider: return Ok(NoOpAuth()) return Ok(StaticHeaderAuth(subject.inbound_token.get_secret_value(), header_name="Authorization")) - def _api_key(self, config: ApiKeyConfig) -> Result[httpx.Auth, CredError]: + def _api_key(self, config: ApiKeyConfig) -> Result[httpx2.Auth, CredError]: match config.key_source: case SharedKey() as source: header_name, header_value = config.header(source.value.get_secret_value()) @@ -196,7 +197,9 @@ class UpstreamCredentialProvider: return Error(CredError.of_not_implemented("api_key BYOK source not implemented yet")) assert_never(config.key_source) - async def _id_jag(self, subject: Subject, server: ServerSpec, config: IdJagConfig) -> Result[httpx.Auth, CredError]: + async def _id_jag( + self, subject: Subject, server: ServerSpec, config: IdJagConfig + ) -> Result[httpx2.Auth, CredError]: match await self._id_jag_subject_token(subject): case Error(err): return Error(err) @@ -261,7 +264,7 @@ class UpstreamCredentialProvider: async def _id_jag_exchange( self, subject: Subject, token: str, server: ServerSpec, config: IdJagConfig - ) -> Result[httpx.Auth, CredError]: + ) -> Result[httpx2.Auth, CredError]: slot: Final = _id_jag_slot_key(subject, server) fingerprint: Final = _id_jag_fingerprint(token, server.server_id, config) @@ -313,7 +316,7 @@ class UpstreamCredentialProvider: async def _client_credentials( self, server_id: str, config: ClientCredentialsConfig - ) -> Result[httpx.Auth, CredError]: + ) -> Result[httpx2.Auth, CredError]: """The M2M arm: resolve a cached (or freshly minted) gateway token; no user context. The token is resolved here, before any upstream request, so a misconfigured grant or an @@ -448,7 +451,7 @@ def _client_auth_fingerprint(client_auth: ClientAuth) -> str: assert_never(client_auth) -def _not_implemented(kind: AuthSpecKind) -> Result[httpx.Auth, CredError]: +def _not_implemented(kind: AuthSpecKind) -> Result[httpx2.Auth, CredError]: return Error(CredError.of_not_implemented(f"{kind.value}: resolver arm not implemented yet")) diff --git a/litellm/proxy/_experimental/mcp_server/outbound_credentials/types.py b/litellm/proxy/_experimental/mcp_server/outbound_credentials/types.py index d186724fd9f..33c3a854058 100644 --- a/litellm/proxy/_experimental/mcp_server/outbound_credentials/types.py +++ b/litellm/proxy/_experimental/mcp_server/outbound_credentials/types.py @@ -30,7 +30,7 @@ from dataclasses import dataclass, field from enum import Enum from typing import Annotated, Final, Literal -import httpx +import httpx2 from expression import case, tag, tagged_union from pydantic import BaseModel, ConfigDict, Field, SecretStr, field_validator from typing_extensions import assert_never @@ -66,7 +66,7 @@ class AuthResolution(str, Enum): @dataclass(frozen=True, slots=True) class ResolvedCredential: - auth: httpx.Auth = field(repr=False) + auth: httpx2.Auth = field(repr=False) source: AuthResolution @@ -110,7 +110,7 @@ class Unauthorized: @tagged_union(frozen=True) class CredError: - """Why a credential could not be produced. Fail-closed: an arm yields this or an `httpx.Auth`. + """Why a credential could not be produced. Fail-closed: an arm yields this or an `httpx2.Auth`. Discriminated on the `Literal` `tag`; consumers `match self.tag` (see `summary`) so the type checker can prove exhaustiveness. Construct via the `of_*` factories. diff --git a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py b/litellm/proxy/_experimental/mcp_server/rest_endpoints.py index 9d895d755dd..15f97a15b73 100644 --- a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py +++ b/litellm/proxy/_experimental/mcp_server/rest_endpoints.py @@ -10,6 +10,7 @@ from uuid import uuid4 import anyio import httpx +import httpx2 from fastapi import APIRouter, Depends, HTTPException, Query, Request, status from pydantic import ValidationError from starlette.datastructures import Headers @@ -120,20 +121,29 @@ def _known_connection_error_message(exc: BaseException, url: str | None, timeout f"within {timeout_seconds:.0f}s. Check that the LiteLLM proxy can reach this URL " "from its network (DNS, egress rules, firewalls) and that the server answers MCP requests." ) - if isinstance(exc, httpx.LocalProtocolError): + if isinstance(exc, (httpx.LocalProtocolError, httpx2.LocalProtocolError)): return ( "Failed to connect to MCP server: a request header is malformed. " "Check static headers for leading/trailing spaces or illegal characters." ) - if isinstance(exc, (httpx.ConnectError, httpx.ConnectTimeout)): + if isinstance(exc, (httpx.ConnectError, httpx.ConnectTimeout, httpx2.ConnectError, httpx2.ConnectTimeout)): return ( "Failed to connect to MCP server: the server is unreachable. Check the URL and that the server is running." ) - if isinstance(exc, httpx.TimeoutException): + if isinstance(exc, (httpx.TimeoutException, httpx2.TimeoutException)): return "Failed to connect to MCP server: the connection timed out." - if isinstance(exc, httpx.HTTPStatusError): + if isinstance(exc, (httpx.HTTPStatusError, httpx2.HTTPStatusError)): return f"Failed to connect to MCP server: it returned HTTP {exc.response.status_code}." - if isinstance(exc, (httpx.NetworkError, httpx.RemoteProtocolError, ConnectionError)): + if isinstance( + exc, + ( + httpx.NetworkError, + httpx.RemoteProtocolError, + httpx2.NetworkError, + httpx2.RemoteProtocolError, + ConnectionError, + ), + ): return ( "Failed to connect to MCP server: the connection was interrupted. " "Check the server and network connection, then retry." @@ -148,7 +158,18 @@ def _known_connection_error_message(exc: BaseException, url: str | None, timeout "Failed to connect to MCP server: the endpoint returned invalid JSON or an invalid MCP response. " "Check the MCP endpoint URL and the server's protocol implementation." ) - if MCP_AVAILABLE and isinstance(exc, McpError): + if MCP_AVAILABLE and isinstance(exc, MCPError): + if exc.error.message.startswith("Unexpected content type:"): + return ( + "Failed to connect to MCP server: the endpoint returned an unsupported content type. " + "Check that the URL is an MCP endpoint, not a web page, and matches the selected transport." + ) + if exc.error.code == -32700 or exc.error.message.startswith("Failed to parse"): + return ( + f"Failed to connect to MCP server: the endpoint returned invalid JSON or an invalid MCP response " + f"(JSON-RPC code {exc.error.code}). " + "Check the MCP endpoint URL and the server's protocol implementation." + ) if exc.error.code == -32000 and exc.error.message == "Connection closed": return ( "Failed to connect to MCP server: the connection was closed before the request completed. " @@ -168,7 +189,7 @@ def _known_connection_error_message(exc: BaseException, url: str | None, timeout if MCP_AVAILABLE: - from mcp.shared.exceptions import McpError + from mcp.shared.exceptions import MCPError from mcp.types import Tool as MCPTool from litellm.experimental_mcp_client.client import MCPClient, as_mcp_read_timeout @@ -518,7 +539,7 @@ if MCP_AVAILABLE: ListMCPToolsRestAPIResponseObject( name=tool.name, description=tool.description, - inputSchema=tool.inputSchema, + inputSchema=tool.input_schema, mcp_info=enriched_mcp_info, ) for tool in tools @@ -1484,7 +1505,7 @@ if MCP_AVAILABLE: effective_timeout: Final = ( min(request.timeout if request.timeout is not None else MCP_CLIENT_TIMEOUT, timeout_seconds) if any( - isinstance(cause, McpError) and as_mcp_read_timeout(cause) is not None + isinstance(cause, MCPError) and as_mcp_read_timeout(cause) is not None for cause in iter_exception_tree(e) ) else timeout_seconds @@ -1635,7 +1656,7 @@ if MCP_AVAILABLE: "message": f"Timed out listing tools after {listing_deadline} seconds. " "The MCP server may be responding slowly or paginating excessively.", } - model_dumped_tools: Final[list[dict]] = [tool.model_dump() for tool in list_tools_result] + model_dumped_tools: Final[list[dict]] = [tool.model_dump(by_alias=True) for tool in list_tools_result] return { "tools": model_dumped_tools, "error": None, diff --git a/litellm/proxy/_experimental/mcp_server/sampling_handler.py b/litellm/proxy/_experimental/mcp_server/sampling_handler.py index fec2a1f9ee6..361d8d5ae31 100644 --- a/litellm/proxy/_experimental/mcp_server/sampling_handler.py +++ b/litellm/proxy/_experimental/mcp_server/sampling_handler.py @@ -18,8 +18,7 @@ if typing.TYPE_CHECKING: from collections.abc import Awaitable, Callable from fastapi import Request - from mcp.client.session import ClientSession - from mcp.shared.context import RequestContext + from mcp.client.session import ClientRequestContext from mcp.types import ( ContentBlock, CreateMessageResult, @@ -333,14 +332,14 @@ def _convert_single_content( return {"type": "text", "text": content.text} elif content_type == "image": image_data: Final[str] = getattr(content, "data", "") - image_mime_type: Final[str] = getattr(content, "mimeType", "image/png") + image_mime_type: Final[str] = getattr(content, "mime_type", "image/png") return { "type": "image_url", "image_url": {"url": f"data:{image_mime_type};base64,{image_data}"}, } elif content_type == "audio": audio_data: Final[str] = getattr(content, "data", "") - audio_mime_type: Final[str] = getattr(content, "mimeType", "audio/wav") + audio_mime_type: Final[str] = getattr(content, "mime_type", "audio/wav") # Map MIME type to OpenAI audio format format_map: Final = { "audio/wav": "wav", @@ -375,7 +374,7 @@ def _convert_single_content( # ToolResultContent → proper OpenAI tool-role message. # Marked so the message-level converter can emit it as a # separate ``{"role": "tool", ...}`` message. - tool_result_use_id: Final = getattr(content, "toolUseId", "") + tool_result_use_id: Final = getattr(content, "tool_use_id", "") nested_content: Final[Sequence[ContentBlock]] = getattr(content, "content", []) if isinstance(nested_content, list): text_parts = [getattr(c, "text", str(c)) for c in nested_content if getattr(c, "type", None) == "text"] @@ -538,7 +537,7 @@ def _extract_tool_results( results: Final = [] for item in items: if getattr(item, "type", None) == "tool_result": - tool_use_id = getattr(item, "toolUseId", "") + tool_use_id = getattr(item, "tool_use_id", "") # Extract text from nested content nested_content: Sequence[ContentBlock] = getattr(item, "content", []) if isinstance(nested_content, list): @@ -573,7 +572,7 @@ def _convert_mcp_tools_to_openai( "function": { "name": tool.name, "description": tool.description or "", - "parameters": tool.inputSchema + "parameters": tool.input_schema or { "type": "object", "properties": {}, @@ -718,7 +717,7 @@ def _convert_openai_response_to_mcp_result( role="assistant", content=content_parts, model=actual_model, - stopReason=stop_reason, + stop_reason=stop_reason, ) # Simple text response text: Final = message.content or "" @@ -726,7 +725,7 @@ def _convert_openai_response_to_mcp_result( role="assistant", content=TextContent(type="text", text=text), model=actual_model, - stopReason=stop_reason, + stop_reason=stop_reason, ) @@ -1066,21 +1065,21 @@ async def _build_completion_kwargs( ) -> dict[str, Any]: openai_messages: Final = _convert_mcp_messages_to_openai( messages=params.messages, - system_prompt=params.systemPrompt, + system_prompt=params.system_prompt, ) completion_kwargs: Final[dict[str, object]] = { "model": model, "messages": openai_messages, - "max_tokens": params.maxTokens, + "max_tokens": params.max_tokens, } if params.temperature is not None: completion_kwargs["temperature"] = params.temperature - if params.stopSequences: - completion_kwargs["stop"] = params.stopSequences + if params.stop_sequences: + completion_kwargs["stop"] = params.stop_sequences openai_tools: Final = _convert_mcp_tools_to_openai(params.tools) if openai_tools: completion_kwargs["tools"] = openai_tools - openai_tool_choice: Final = _convert_mcp_tool_choice_to_openai(params.toolChoice) + openai_tool_choice: Final = _convert_mcp_tool_choice_to_openai(params.tool_choice) if openai_tool_choice is not None: completion_kwargs["tool_choice"] = openai_tool_choice completion_kwargs["metadata"] = {"mcp_metadata": params.metadata} if params.metadata else {} @@ -1137,7 +1136,7 @@ async def _run_guardrails_and_call_llm( async def handle_sampling_create_message( - context: "RequestContext[ClientSession, object]", + context: "ClientRequestContext", params: "CreateMessageRequestParams", default_model: str | None = None, user_api_key_auth: "UserAPIKeyAuth | None" = None, @@ -1180,13 +1179,13 @@ async def handle_sampling_create_message( try: model: Final = _resolve_model_from_preferences( - model_preferences=params.modelPreferences, + model_preferences=params.model_preferences, default_model=default_model, ) verbose_logger.info( "MCP sampling: resolved model=%s from preferences=%s", model, - params.modelPreferences, + params.model_preferences, ) access_denial: Final = await _check_model_access(model, user_api_key_auth) @@ -1228,7 +1227,7 @@ async def handle_sampling_create_message( verbose_logger.info( "MCP sampling: completed successfully, model=%s, stopReason=%s", getattr(result, "model", "unknown"), - getattr(result, "stopReason", "unknown"), + getattr(result, "stop_reason", "unknown"), ) return result except Exception as e: diff --git a/litellm/proxy/_experimental/mcp_server/server.py b/litellm/proxy/_experimental/mcp_server/server.py index 73366b701d2..4ea22ca1f01 100644 --- a/litellm/proxy/_experimental/mcp_server/server.py +++ b/litellm/proxy/_experimental/mcp_server/server.py @@ -15,13 +15,13 @@ 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 import httpx from fastapi import FastAPI, HTTPException -from pydantic import AnyUrl, ConfigDict, TypeAdapter, ValidationError +from pydantic import AnyUrl, ConfigDict, Field, TypeAdapter, ValidationError from starlette.requests import Request as StarletteRequest from starlette.responses import JSONResponse from starlette.types import Message, Receive, Scope, Send @@ -64,6 +64,8 @@ from litellm.proxy._experimental.mcp_server.mcp_context import ( _mcp_gateway_initialize_instructions, _mcp_gateway_server_name, _mcp_proxy_mode, # pyright: ignore[reportPrivateUsage] # server-owned request mode + active_mcp_request_ctx_var, + get_active_mcp_request_ctx, ) from litellm.proxy._experimental.mcp_server.mcp_debug import ( MCP_AUTH_DIAGNOSTICS_SCOPE_KEY, @@ -137,6 +139,24 @@ _MCP_ROUTING_PEEK_MAX_BYTES: Final = 4096 # 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 async def _invalidate_byok_cred_cache(user_id: str, server_id: str) -> None: @@ -156,14 +176,12 @@ try: from mcp import ReadResourceResult, Resource from mcp.server import Server - from mcp.server.lowlevel.helper_types import ReadResourceContents from mcp.server.session import ServerSession as _McpServerSession from mcp.types import ( BlobResourceContents, GetPromptResult, ResourceTemplate, TextResourceContents, - Tool, ) # Robust auth lookup keyed by session_object. @@ -176,7 +194,6 @@ except ImportError as e: # so they will never be accessed at runtime BlobResourceContents = None GetPromptResult = None - ReadResourceContents = None ReadResourceResult = None Resource = None ResourceTemplate = None @@ -277,8 +294,8 @@ def _mcp_meta_trace_carrier(req_ctx: object) -> dict[str, str] | None: span's identity attribution. """ meta: Final = getattr(req_ctx, "meta", None) - extra: Final = getattr(meta, "model_extra", None) - if not isinstance(extra, dict): + extra: Final = meta if isinstance(meta, Mapping) else getattr(meta, "model_extra", None) + if not isinstance(extra, Mapping): return None carrier: Final = {key: extra[key] for key in ("traceparent", "tracestate") if isinstance(extra.get(key), str)} return carrier or None @@ -456,6 +473,7 @@ if MCP_AVAILABLE: AuthContextMiddleware, auth_context_var, ) + from mcp.server.context import ServerRequestContext from mcp.server.lowlevel.server import NotificationOptions from mcp.server.models import InitializationOptions @@ -464,14 +482,23 @@ 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 + from mcp_types.version import HANDSHAKE_PROTOCOL_VERSIONS from litellm.proxy._experimental.mcp_server.auth.litellm_auth_handler import ( MCPAuthenticatedUser, @@ -520,46 +547,20 @@ if MCP_AVAILABLE: Object returned by the /tools/list REST API route. """ - mcp_info: MCPInfo | None = None + mcp_info: MCPInfo | None = Field(default=None, alias="mcp_info") model_config = ConfigDict(arbitrary_types_allowed=True) - def _normalize_resource_contents(contents: list) -> list[ReadResourceContents]: - """Normalize ResourceContents to ReadResourceContents, preserving meta (MCP 1.26.0+).""" - normalized: Final[list[ReadResourceContents]] = [] - for content in contents: - meta = getattr(content, "meta", None) - if meta is None and hasattr(content, "model_dump"): - d = content.model_dump() - meta = d.get("meta") - if meta is None: - meta = d.get("_meta") - if isinstance(content, TextResourceContents): - normalized.append( - ReadResourceContents( - content=content.text, - mime_type=content.mimeType, - meta=meta, - ) - ) - elif isinstance(content, BlobResourceContents): - normalized.append( - ReadResourceContents( - content=content.blob, - mime_type=content.mimeType, - meta=meta, - ) - ) - return normalized - def _gateway_create_initialization_options( self, notification_options: NotificationOptions | None = None, experimental_capabilities: dict[str, dict[str, object]] | None = None, + extensions: dict[str, dict[str, object]] | None = None, ) -> InitializationOptions: base_options: Final = Server.create_initialization_options( self, notification_options=notification_options, experimental_capabilities=experimental_capabilities or {}, + extensions=extensions, ) opts: Final = ( base_options.model_copy( @@ -817,8 +818,7 @@ if MCP_AVAILABLE: ############### MCP Server Routes ####################### ######################################################## - @server.list_tools() - async def handle_list_tools() -> "ListToolsResult | list[Tool]": + async def handle_list_tools(ctx: ServerRequestContext, params: PaginatedRequestParams) -> ListToolsResult: """ List all available tools, with each server's listing outcome attached to the result's ``_meta`` (SERVER_OUTCOMES_META_KEY) so a broken upstream is distinguishable from a healthy @@ -826,12 +826,9 @@ if MCP_AVAILABLE: pass the result through unwrapped, which is what lets the ``_meta`` survive to the client. Also captures the active session for propagation to callbacks. """ - from mcp.server.lowlevel.server import request_ctx - - req_ctx: Final = request_ctx.get(None) - _session_reset_token = None - if req_ctx: - _session_reset_token = active_mcp_session_var.set(req_ctx.session) + req_ctx: Final = ctx + _ctx_reset_token: Final = active_mcp_request_ctx_var.set(ctx) + _session_reset_token: Final = active_mcp_session_var.set(ctx.session) _trace_token = None _transport_token = None _destinations_token = None @@ -864,13 +861,13 @@ if MCP_AVAILABLE: ) if _mcp_proxy_mode.get(): - return [Tool.model_validate(d) for d in get_mcp_proxy_tool_definitions()] # mutable-ok: MCP SDK list + return ListToolsResult(tools=[Tool.model_validate(d) for d in get_mcp_proxy_tool_definitions()]) if getattr( getattr(user_api_key_auth, "object_permission", None), "mcp_tool_search_enabled", False, ): - return [Tool.model_validate(d) for d in get_virtual_tool_definitions()] + return ListToolsResult(tools=[Tool.model_validate(d) for d in get_virtual_tool_definitions()]) # Get mcp_servers from context variable verbose_logger.debug("MCP list_tools - Calling _list_mcp_tools") @@ -886,7 +883,7 @@ if MCP_AVAILABLE: ) verbose_logger.info("MCP list_tools - Successfully returned %s tools", len(listing.tools)) if not listing.outcomes: - return listing.tools + return ListToolsResult(tools=listing.tools) outcome_meta: Final = { SERVER_OUTCOMES_META_KEY: { key: outcome_wire_value(outcome) for key, outcome in listing.outcomes.items() @@ -894,36 +891,32 @@ if MCP_AVAILABLE: } return ListToolsResult.model_validate({"tools": listing.tools, "_meta": outcome_meta}) except HTTPException as e: - from mcp.shared.exceptions import McpError - from mcp.types import INVALID_REQUEST, ErrorData + from mcp.shared.exceptions import MCPError + from mcp.types import INVALID_REQUEST - raise McpError(ErrorData(code=INVALID_REQUEST, message=_http_detail_message(e.detail))) from e + raise MCPError(code=INVALID_REQUEST, message=_http_detail_message(e.detail)) from e except Exception as e: verbose_logger.exception("Error in list_tools endpoint: %s", e) # Return empty list instead of failing completely # This prevents the HTTP stream from failing and allows the client to get a response - return [] + return ListToolsResult(tools=[]) # mutable-ok: MCP result payload finally: _otel_reset_mcp_request_destinations(_destinations_token) _otel_reset_mcp_transport_span(_transport_token) _otel_reset_mcp_trace_carrier(_trace_token) - if _session_reset_token is not None: - active_mcp_session_var.reset(_session_reset_token) + active_mcp_session_var.reset(_session_reset_token) + active_mcp_request_ctx_var.reset(_ctx_reset_token) - def _capture_host_progress_callback(host_server) -> Callable | None: + def _capture_host_progress_callback(ctx: ServerRequestContext) -> Callable | None: """Return a progress-forwarding callback bound to the host MCP session. Returns ``None`` when the host did not supply a progress token. """ - try: - host_ctx: Final = host_server.request_context - except Exception as e: - verbose_logger.warning("Could not capture host progress context: %s", e) - return None + host_ctx: Final = ctx if not (host_ctx and hasattr(host_ctx, "meta") and host_ctx.meta): return None - host_token: Final = getattr(host_ctx.meta, "progressToken", None) + host_token: Final = host_ctx.meta.get("progress_token") if host_token is None or not (hasattr(host_ctx, "session") and host_ctx.session): return None host_session: Final = host_ctx.session @@ -944,10 +937,10 @@ if MCP_AVAILABLE: return forward_progress def _reject_mcp_proxy_operation() -> NoReturn: - from mcp.shared.exceptions import McpError - from mcp.types import METHOD_NOT_FOUND, ErrorData + from mcp.shared.exceptions import MCPError + from mcp.types import METHOD_NOT_FOUND - raise McpError(ErrorData(code=METHOD_NOT_FOUND, message="Operation unavailable on /mcp/proxy")) + raise MCPError(code=METHOD_NOT_FOUND, message="Operation unavailable on /mcp/proxy") async def _build_virtual_call_logging_obj( name: str, @@ -1022,7 +1015,7 @@ if MCP_AVAILABLE: content=[ # mutable-ok: MCP result content TextContent(type="text", text=f"Tool {name} is unavailable on /mcp/proxy") ], - isError=True, + is_error=True, ) if _mcp_proxy_mode.get() and name in MCP_PROXY_TOOL_NAMES: @@ -1104,7 +1097,7 @@ if MCP_AVAILABLE: text=f"Tool {name} requires mcp_tool_search_enabled on the key", ) ], - isError=True, + is_error=True, ) args: Final = arguments or {} @@ -1154,29 +1147,24 @@ if MCP_AVAILABLE: litellm_logging_obj=virtual_logging_obj, ) - @server.call_tool() - async def mcp_server_tool_call(name: str, arguments: dict[str, object] | None) -> CallToolResult: + async def mcp_server_tool_call(ctx: ServerRequestContext, params: CallToolRequestParams) -> CallToolResult: """ Call a specific tool with the provided arguments Args: - name (str): Name of the tool to call - arguments (Dict[str, Any] | None): Arguments to pass to the tool + ctx: SDK request context carrying the client session and HTTP request + params (CallToolRequestParams): Tool name and arguments Returns: - List[Union[MCPTextContent, MCPImageContent, MCPEmbeddedResource]]: Tool execution results - Raises: - HTTPException: If tool not found or arguments missing + CallToolResult: Tool execution results """ - from mcp.server.lowlevel.server import request_ctx from mcp.types import CallToolResult from litellm.exceptions import BlockedPiiEntityError, GuardrailRaisedException from litellm.proxy.litellm_pre_call_utils import add_litellm_data_to_request from litellm.proxy.proxy_server import proxy_config - req_ctx: Final = request_ctx.get(None) - _session_reset_token = None - if req_ctx: - _session_reset_token = active_mcp_session_var.set(req_ctx.session) + req_ctx: Final = ctx + _ctx_reset_token: Final = active_mcp_request_ctx_var.set(ctx) + _session_reset_token: Final = active_mcp_session_var.set(ctx.session) _trace_token = None _transport_token = None _destinations_token = None @@ -1207,8 +1195,8 @@ if MCP_AVAILABLE: # Inside this try so virtual-tool errors convert to isError # CallToolResult instead of raising out of the protocol handler. virtual_tool_result: Final = await _dispatch_virtual_mcp_tool( - name=name, - arguments=arguments, + name=params.name, + arguments=params.arguments, user_api_key_auth=user_api_key_auth, client_ip=_client_ip, mcp_servers=mcp_servers, @@ -1220,9 +1208,9 @@ if MCP_AVAILABLE: if virtual_tool_result is not None: return virtual_tool_result - host_progress_callback: Final = _capture_host_progress_callback(server) + host_progress_callback: Final = _capture_host_progress_callback(ctx) # Create a body date for logging - body_data: Final = {"name": name, "arguments": arguments} + body_data: Final = {"name": params.name, "arguments": params.arguments} # mutable-ok: logging payload # Set trace/session id from raw_headers so spend logs and logging_obj stay consistent (same as A2A) chain_id: Final = get_chain_id_from_headers(raw_headers) if chain_id: @@ -1247,7 +1235,7 @@ if MCP_AVAILABLE: # Authorization is unaffected: it ran before this, and the union is resolved # from the untouched auth object passed to call_mcp_tool below. user_api_key_dict=await MCPRequestHandler.billing_auth_for_tool_call( - user_api_key_auth, tool_name=name + user_api_key_auth, tool_name=params.name ), proxy_config=proxy_config, ) @@ -1273,7 +1261,7 @@ if MCP_AVAILABLE: ) return CallToolResult( content=[TextContent(text=str(e), type="text")], - isError=True, + is_error=True, ) except BlockedPiiEntityError as e: verbose_logger.error("BlockedPiiEntityError in MCP tool call: %s", e) @@ -1284,19 +1272,19 @@ if MCP_AVAILABLE: type="text", ) ], - isError=True, + is_error=True, ) except GuardrailRaisedException as e: verbose_logger.error("GuardrailRaisedException in MCP tool call: %s", e) return CallToolResult( content=[TextContent(text=f"Error: Guardrail violation - {e}", type="text")], - isError=True, + is_error=True, ) except HTTPException as e: verbose_logger.error("HTTPException in MCP tool call: %s", e) return CallToolResult( content=[TextContent(text=f"Error: {_http_detail_message(e.detail)}", type="text")], - isError=True, + is_error=True, ) except MCPUpstreamAuthError as e: # The MCP session manager serializes handler exceptions as JSON-RPC errors, so a @@ -1312,13 +1300,13 @@ if MCP_AVAILABLE: type="text", ) ], - isError=True, + is_error=True, ) except Exception as e: verbose_logger.exception("MCP mcp_server_tool_call - error: %s", e) return CallToolResult( content=[TextContent(text=f"Error: {e}", type="text")], - isError=True, + is_error=True, ) return response @@ -1326,22 +1314,17 @@ if MCP_AVAILABLE: _otel_reset_mcp_request_destinations(_destinations_token) _otel_reset_mcp_transport_span(_transport_token) _otel_reset_mcp_trace_carrier(_trace_token) - if _session_reset_token is not None: - active_mcp_session_var.reset(_session_reset_token) + active_mcp_session_var.reset(_session_reset_token) + active_mcp_request_ctx_var.reset(_ctx_reset_token) - @server.list_prompts() - async def list_prompts() -> list[Prompt]: + async def list_prompts(ctx: ServerRequestContext, params: PaginatedRequestParams) -> ListPromptsResult: """ List all available prompts """ if _mcp_proxy_mode.get(): _reject_mcp_proxy_operation() - from mcp.server.lowlevel.server import request_ctx - - req_ctx: Final = request_ctx.get(None) - _session_reset_token = None - if req_ctx: - _session_reset_token = active_mcp_session_var.set(req_ctx.session) + _ctx_reset_token: Final = active_mcp_request_ctx_var.set(ctx) + _session_reset_token: Final = active_mcp_session_var.set(ctx.session) try: # Get user authentication from context variable @@ -1371,36 +1354,24 @@ if MCP_AVAILABLE: raw_headers=raw_headers, ) verbose_logger.info("MCP list_prompts - Successfully returned %s prompts", len(prompts)) - return prompts + return ListPromptsResult(prompts=prompts) except Exception as e: verbose_logger.exception("Error in list_prompts endpoint: %s", e) # Return empty list instead of failing completely # This prevents the HTTP stream from failing and allows the client to get a response - return [] + return ListPromptsResult(prompts=[]) # mutable-ok: MCP result payload finally: - if _session_reset_token is not None: - active_mcp_session_var.reset(_session_reset_token) + active_mcp_session_var.reset(_session_reset_token) + active_mcp_request_ctx_var.reset(_ctx_reset_token) - @server.get_prompt() - async def get_prompt(name: str, arguments: dict[str, str] | None) -> GetPromptResult: + async def get_prompt(ctx: ServerRequestContext, params: GetPromptRequestParams) -> GetPromptResult: """ Get a specific prompt with the provided arguments - - Args: - name (str): Name of the prompt to get - arguments (Dict[str, Any] | None): Arguments to pass to the prompt - - Returns: - GetPromptResult: Getting prompt execution results """ if _mcp_proxy_mode.get(): _reject_mcp_proxy_operation() - from mcp.server.lowlevel.server import request_ctx - - req_ctx: Final = request_ctx.get(None) - _session_reset_token = None - if req_ctx: - _session_reset_token = active_mcp_session_var.set(req_ctx.session) + _ctx_reset_token: Final = active_mcp_request_ctx_var.set(ctx) + _session_reset_token: Final = active_mcp_session_var.set(ctx.session) try: ( @@ -1415,8 +1386,8 @@ if MCP_AVAILABLE: verbose_logger.debug("MCP mcp_server_tool_call - User API Key Auth from context: %s", user_api_key_auth) return await mcp_get_prompt( - name=name, - arguments=arguments, + name=params.name, + arguments=params.arguments, user_api_key_auth=user_api_key_auth, mcp_auth_header=mcp_auth_header, mcp_servers=mcp_servers, @@ -1425,20 +1396,15 @@ if MCP_AVAILABLE: raw_headers=raw_headers, ) finally: - if _session_reset_token is not None: - active_mcp_session_var.reset(_session_reset_token) + active_mcp_session_var.reset(_session_reset_token) + active_mcp_request_ctx_var.reset(_ctx_reset_token) - @server.list_resources() - async def list_resources() -> list[Resource]: + async def list_resources(ctx: ServerRequestContext, params: PaginatedRequestParams) -> ListResourcesResult: """List all available resources.""" if _mcp_proxy_mode.get(): _reject_mcp_proxy_operation() - from mcp.server.lowlevel.server import request_ctx - - req_ctx: Final = request_ctx.get(None) - _session_reset_token = None - if req_ctx: - _session_reset_token = active_mcp_session_var.set(req_ctx.session) + _ctx_reset_token: Final = active_mcp_request_ctx_var.set(ctx) + _session_reset_token: Final = active_mcp_session_var.set(ctx.session) try: ( @@ -1466,25 +1432,22 @@ if MCP_AVAILABLE: raw_headers=raw_headers, ) verbose_logger.info("MCP list_resources - Successfully returned %s resources", len(resources)) - return resources + return ListResourcesResult(resources=resources) except Exception as e: verbose_logger.exception("Error in list_resources endpoint: %s", e) - return [] + return ListResourcesResult(resources=[]) # mutable-ok: MCP result payload finally: - if _session_reset_token is not None: - active_mcp_session_var.reset(_session_reset_token) + active_mcp_session_var.reset(_session_reset_token) + active_mcp_request_ctx_var.reset(_ctx_reset_token) - @server.list_resource_templates() - async def list_resource_templates() -> list[ResourceTemplate]: + async def list_resource_templates( + ctx: ServerRequestContext, params: PaginatedRequestParams + ) -> ListResourceTemplatesResult: """List all available resource templates.""" if _mcp_proxy_mode.get(): _reject_mcp_proxy_operation() - from mcp.server.lowlevel.server import request_ctx - - req_ctx: Final = request_ctx.get(None) - _session_reset_token = None - if req_ctx: - _session_reset_token = active_mcp_session_var.set(req_ctx.session) + _ctx_reset_token: Final = active_mcp_request_ctx_var.set(ctx) + _session_reset_token: Final = active_mcp_session_var.set(ctx.session) try: ( @@ -1514,24 +1477,19 @@ if MCP_AVAILABLE: verbose_logger.info( "MCP list_resource_templates - Successfully returned %s resource templates", len(resource_templates) ) - return resource_templates + return ListResourceTemplatesResult(resource_templates=resource_templates) except Exception as e: verbose_logger.exception("Error in list_resource_templates endpoint: %s", e) - return [] + return ListResourceTemplatesResult(resource_templates=[]) # mutable-ok: MCP result payload finally: - if _session_reset_token is not None: - active_mcp_session_var.reset(_session_reset_token) + active_mcp_session_var.reset(_session_reset_token) + active_mcp_request_ctx_var.reset(_ctx_reset_token) - @server.read_resource() - async def read_resource(url: AnyUrl) -> list[ReadResourceContents]: + async def read_resource(ctx: ServerRequestContext, params: ReadResourceRequestParams) -> ReadResourceResult: if _mcp_proxy_mode.get(): _reject_mcp_proxy_operation() - from mcp.server.lowlevel.server import request_ctx - - req_ctx: Final = request_ctx.get(None) - _session_reset_token = None - if req_ctx: - _session_reset_token = active_mcp_session_var.set(req_ctx.session) + _ctx_reset_token: Final = active_mcp_request_ctx_var.set(ctx) + _session_reset_token: Final = active_mcp_session_var.set(ctx.session) try: ( @@ -1545,7 +1503,7 @@ if MCP_AVAILABLE: ) = await get_or_extract_auth_context() read_resource_result: Final = await mcp_read_resource( - url=url, + url=params.uri, user_api_key_auth=user_api_key_auth, mcp_auth_header=mcp_auth_header, mcp_servers=mcp_servers, @@ -1554,10 +1512,18 @@ if MCP_AVAILABLE: raw_headers=raw_headers, ) - return _normalize_resource_contents(read_resource_result.contents) + return read_resource_result finally: - if _session_reset_token is not None: - active_mcp_session_var.reset(_session_reset_token) + active_mcp_session_var.reset(_session_reset_token) + active_mcp_request_ctx_var.reset(_ctx_reset_token) + + server.add_request_handler("tools/list", PaginatedRequestParams, handle_list_tools) + server.add_request_handler("tools/call", CallToolRequestParams, mcp_server_tool_call) + server.add_request_handler("prompts/list", PaginatedRequestParams, list_prompts) + server.add_request_handler("prompts/get", GetPromptRequestParams, get_prompt) + server.add_request_handler("resources/list", PaginatedRequestParams, list_resources) + server.add_request_handler("resources/templates/list", PaginatedRequestParams, list_resource_templates) + server.add_request_handler("resources/read", ReadResourceRequestParams, read_resource) ######################################################## ############ End of MCP Server Routes ################## @@ -3296,11 +3262,11 @@ if MCP_AVAILABLE: Guardrails run before the success/failure logging so the masked text, not the raw one, is what gets logged. - A result with ``isError=True`` is logged as a failure (``status="failure"`` + A result with ``is_error=True`` is logged as a failure (``status="failure"`` payload, so OTel marks the span ERROR) while the HTTP wire behavior stays 200 + ``isError: true`` per the MCP spec. The error check runs after ``async_post_mcp_tool_call_hook`` because guardrails may flip the result - to ``isError=True`` in that hook. Raised exceptions never reach here (the + to ``is_error=True`` in that hook. Raised exceptions never reach here (the ``@client`` wrapper and ``call_mcp_tool``'s except path log those), so this cannot double-log a failure. @@ -3635,10 +3601,10 @@ if MCP_AVAILABLE: """Execute a local-registry tool and report whether it succeeded. Returns the result rather than bare content because the verdict is part of it: the content - alone cannot say whether the handler failed, so callers used to stamp isError=False on every + alone cannot say whether the handler failed, so callers used to stamp is_error=False on every outcome and an upstream rejection was served as tool output. - A failure is reported as ``isError=True`` here rather than raised, because the REST surface + A failure is reported as ``is_error=True`` here rather than raised, because the REST surface turns an unrecognized exception into a 500 and an upstream 403 or 429 is not a gateway crash. ``MCPUpstreamAuthError`` is the exception: it propagates so the caller is told to re-authenticate, which both renderers already know how to say. @@ -3660,8 +3626,14 @@ if MCP_AVAILABLE: raise except Exception as e: verbose_logger.exception("Error executing local tool %s: %s", name, e) - return CallToolResult(content=[TextContent(text=f"Error: {e}", type="text")], isError=True) - return CallToolResult(content=[TextContent(text=str(result), type="text")], isError=False) + return CallToolResult( + content=[TextContent(text=f"Error: {e}", type="text")], # mutable-ok: MCP result content + is_error=True, + ) + return CallToolResult( + content=[TextContent(text=str(result), type="text")], # mutable-ok: MCP result content + is_error=False, + ) def _get_mcp_servers_in_path(path: str) -> list[str] | None: """ @@ -3843,7 +3815,7 @@ if MCP_AVAILABLE: def _extract_initialize_client_info(body: bytes) -> Implementation | None: try: - return InitializeRequest.model_validate_json(body).params.clientInfo + return InitializeRequest.model_validate_json(body, by_name=False).params.client_info except ValidationError: return None @@ -4553,6 +4525,21 @@ if MCP_AVAILABLE: async def handle_streamable_http_mcp(scope: Scope, receive: Receive, send: Send) -> None: """Handle MCP requests through StreamableHTTP.""" try: + bad_version: Final = unsupported_protocol_version(scope) + if bad_version is not None: + supported: Final = ", ".join(sorted(HANDSHAKE_PROTOCOL_VERSIONS)) + await JSONResponse( + status_code=400, + content={ # mutable-ok: JSON-RPC error payload + "jsonrpc": "2.0", + "id": None, + "error": { + "code": INVALID_REQUEST, + "message": f"Unsupported MCP-Protocol-Version {bad_version}; supported: {supported}", + }, + }, + )(scope, receive, send) + return path: Final[str] = scope.get("path", "") ( user_api_key_auth, @@ -5179,12 +5166,8 @@ if MCP_AVAILABLE: return None, None, None, None, None, None, None def _get_current_session(): - try: - from mcp.server.lowlevel.server import request_ctx - - return request_ctx.get().session - except (LookupError, ImportError): - return None + ctx: Final = get_active_mcp_request_ctx() + return ctx.session if ctx is not None else None def _cache_auth_context_lazily(): session: Final = _get_current_session() diff --git a/litellm/proxy/_experimental/mcp_server/tool_search.py b/litellm/proxy/_experimental/mcp_server/tool_search.py index e921ab0331e..a482d02c31d 100644 --- a/litellm/proxy/_experimental/mcp_server/tool_search.py +++ b/litellm/proxy/_experimental/mcp_server/tool_search.py @@ -99,11 +99,20 @@ def mcp_tool_search_settings() -> MCPToolSearchSettings | ValidationError: def _tool_result(tool: Tool) -> ToolSearchResult: - return {"name": tool.name, "description": tool.description or "", "inputSchema": tool.inputSchema} + return { + "name": tool.name, + "description": tool.description or "", + "inputSchema": tool.input_schema, + } # mutable-ok: wire schema payload def _scored_result(tool: Tool, score: float) -> ToolSearchResult: - return {"name": tool.name, "description": tool.description or "", "inputSchema": tool.inputSchema, "score": score} + return { + "name": tool.name, + "description": tool.description or "", + "inputSchema": tool.input_schema, + "score": score, + } # mutable-ok: wire schema payload _MCP_PROXY_IDENTITY_META_KEY: Final[str] = "litellm.ai/proxy_tool_identity" @@ -148,11 +157,11 @@ def _proxy_schema_result(tool: Tool) -> MCPProxySchemaResult: "tool_id": mcp_proxy_tool_id(tool), "name": tool.name, "description": tool.description or "", - "inputSchema": tool.inputSchema, + "inputSchema": tool.input_schema, } - if tool.outputSchema is None: + if tool.output_schema is None: return base - return {**base, "outputSchema": tool.outputSchema} # mutable-ok: wire schema payload + return {**base, "outputSchema": tool.output_schema} # mutable-ok: wire schema payload def _tool_text(tool: Tool) -> str: @@ -372,7 +381,7 @@ def _text_tool_result(text: str, is_error: bool) -> CallToolResult: return CallToolResult( content=[TextContent(type="text", text=text)], # mutable-ok: CallToolResult accepts only list content - isError=is_error, + is_error=is_error, ) @@ -565,7 +574,7 @@ async def handle_mcp_proxy_tool( if not isinstance(tool_arguments, dict): return _text_tool_result("arguments must be an object", is_error=True) try: - validate(instance=tool_arguments, schema=tool.inputSchema) + validate(instance=tool_arguments, schema=tool.input_schema) except JsonSchemaValidationError as exc: return _text_tool_result(f"Invalid arguments: {exc.message}", is_error=True) diff --git a/litellm/proxy/_experimental/mcp_server/utils.py b/litellm/proxy/_experimental/mcp_server/utils.py index fb3eb06fd15..6bd080f5216 100644 --- a/litellm/proxy/_experimental/mcp_server/utils.py +++ b/litellm/proxy/_experimental/mcp_server/utils.py @@ -536,7 +536,11 @@ def extract_mcp_tool_result_error_message(result: object) -> str | None: Accepts both ``mcp.types.CallToolResult`` objects and their dict equivalents, duck-typed so the ``mcp`` package is not required. """ - is_error: Final[object] = result.get("isError") if isinstance(result, Mapping) else getattr(result, "isError", None) + is_error: Final[object] = ( + (result.get("isError") if result.get("isError") is not None else result.get("is_error")) + if isinstance(result, Mapping) + else getattr(result, "is_error", None) + ) if is_error is not True: return None content: Final[object] = result.get("content") if isinstance(result, Mapping) else getattr(result, "content", None) @@ -870,8 +874,9 @@ def json_unrewritable_labels(value: object, path_depth: int = 0) -> tuple[str, . def mcp_tool_result_structured_content(result: object) -> object: """The ``structuredContent`` of an MCP tool result, or ``None`` when it has none.""" if isinstance(result, Mapping): - return result.get("structuredContent") - return getattr(result, "structuredContent", None) + structured: Final = result.get("structuredContent") + return structured if structured is not None else result.get("structured_content") + return getattr(result, "structured_content", None) def set_mcp_tool_result_structured_content(result: object, value: object) -> bool: @@ -882,12 +887,12 @@ def set_mcp_tool_result_structured_content(result: object, value: object) -> boo unmasked value in the spend log and the OTel span. """ if isinstance(result, MutableMapping): - result["structuredContent"] = value + result["structured_content" if "structured_content" in result else "structuredContent"] = value return True - if not hasattr(result, "structuredContent"): + if not hasattr(result, "structured_content"): return False try: - setattr(result, "structuredContent", value) # attribute name is fixed by the MCP result shape + setattr(result, "structured_content", value) # attribute name is fixed by the MCP result shape return True except (AttributeError, TypeError, ValueError): return False diff --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.py index 5a6be1089b6..67ef05fc324 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 @@ -34,15 +34,26 @@ def _serialize_mcp_content_item(item: object) -> dict[str, object]: model_dump: Final = getattr(item, "model_dump", None) if callable(model_dump): try: - return dict(model_dump(exclude_none=True)) + dumped: Final[dict[str, object]] = model_dump(exclude_none=True, by_alias=True) + return dict(dumped) except TypeError: - return dict(model_dump()) + dumped_fallback: Final[dict[str, object]] = model_dump() + return dict(dumped_fallback) text: Final = getattr(item, "text", None) if isinstance(text, str): return {"type": getattr(item, "type", "text"), "text": text} return {"type": "text", "text": str(item)} +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``. @@ -219,14 +230,14 @@ class _CiscoAIDefenseMcpMixin: if isinstance(content, list): content[:] = replacement structured_replacement: Final = _CiscoAIDefenseMcpMixin._replacement_structured_content(replacement) - if hasattr(response_obj, "structuredContent"): + if hasattr(response_obj, "structured_content"): try: - setattr(response_obj, "structuredContent", structured_replacement) + setattr(response_obj, "structured_content", structured_replacement) except (AttributeError, TypeError, ValueError): pass - if hasattr(response_obj, "isError"): + if hasattr(response_obj, "is_error"): try: - setattr(response_obj, "isError", True) + setattr(response_obj, "is_error", True) except (AttributeError, TypeError, ValueError): pass return True @@ -487,7 +498,7 @@ class _CiscoAIDefenseMcpMixin: model_dump: Final = getattr(response, "model_dump", None) if callable(model_dump): try: - dumped = model_dump(exclude_none=True) + dumped = model_dump(exclude_none=True, by_alias=True) except TypeError: dumped = model_dump() if isinstance(dumped, dict): @@ -507,8 +518,8 @@ class _CiscoAIDefenseMcpMixin: source: object = None, ) -> dict[str, object]: result: Final[dict[str, object]] = {"content": [_serialize_mcp_content_item(item) for item in content]} - for key in ("structuredContent", "isError"): - value = source.get(key) if isinstance(source, dict) else getattr(source, key, None) + for key, snake_key in (("structuredContent", "structured_content"), ("isError", "is_error")): + value = _source_field(source, key, snake_key) if value is not None and (key != "isError" or isinstance(value, bool)): result[key] = value return result @@ -549,20 +560,21 @@ 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 = True - elif hasattr(response_obj, "structuredContent"): + elif hasattr(response_obj, "structured_content"): try: - setattr(response_obj, "structuredContent", replacement) + setattr(response_obj, "structured_content", replacement) replaced = True except (AttributeError, TypeError, ValueError): pass elif isinstance(response_obj, dict): result: Final = response_obj.get("result") target: Final[dict[object, object]] = result if isinstance(result, dict) else response_obj - if "structuredContent" in target: - target["structuredContent"] = replacement + structured_key: Final = "structured_content" if "structured_content" in target else "structuredContent" + if structured_key in target: + target[structured_key] = replacement replaced = True return replaced diff --git a/litellm/responses/mcp/mcp_streaming_iterator.py b/litellm/responses/mcp/mcp_streaming_iterator.py index 1b19bf77a7d..16e8ac93d59 100644 --- a/litellm/responses/mcp/mcp_streaming_iterator.py +++ b/litellm/responses/mcp/mcp_streaming_iterator.py @@ -105,8 +105,8 @@ async def create_mcp_list_tools_events( "description": getattr(tool, "description", ""), "annotations": {"read_only": False}, **dict.fromkeys( - ("input_schema",) if hasattr(tool, "inputSchema") or hasattr(tool, "input_schema") else (), - getattr(tool, "inputSchema", getattr(tool, "input_schema", None)), + ("input_schema",) if hasattr(tool, "input_schema") else (), + getattr(tool, "input_schema", None), ), } for tool in filtered_mcp_tools diff --git a/litellm/types/mcp.py b/litellm/types/mcp.py index e0fd3e9a69d..83e719810d5 100644 --- a/litellm/types/mcp.py +++ b/litellm/types/mcp.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import enum import re from collections.abc import Awaitable, Callable, Mapping @@ -12,6 +14,7 @@ from typing_extensions import TypedDict from litellm.types.llms.base import HiddenParams if TYPE_CHECKING: + import httpx2 from mcp.types import EmbeddedResource as MCPEmbeddedResource from mcp.types import ImageContent as MCPImageContent from mcp.types import TextContent as MCPTextContent @@ -348,7 +351,7 @@ def custom_credential_slot(headers: Mapping[str, str] | None) -> str | None: def credential_redirect_hook( configured_url: str, slot: str | None -) -> Callable[[httpx.Request], Awaitable[None]] | None: +) -> Callable[[httpx.Request | httpx2.Request], Awaitable[None]] | None: """An httpx request hook dropping ``slot`` once a redirect leaves ``configured_url``'s origin. None when no guard is needed, so callers do not each repeat the exemption: HTTP clients already @@ -358,7 +361,7 @@ def credential_redirect_hook( if not configured_url or not slot or same_header(slot, DEFAULT_CREDENTIAL_HEADER): return None - async def guard(request: httpx.Request) -> None: + async def guard(request: httpx.Request | httpx2.Request) -> None: if slot in request.headers and crosses_origin(configured_url, str(request.url)): del request.headers[slot] diff --git a/pyproject.toml b/pyproject.toml index a017e39e084..f2ee1d92d7f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,13 +18,15 @@ dependencies = [ "httpx[http2]>=0.28.0,<1.0", "openai>=2.20.0,<3.0.0", "python-dotenv>=1.0.0,<2.0", - "tiktoken>=0.8.0,<1.0", + "tiktoken>=0.8.0,<1.0; python_version < '3.14'", + "tiktoken>=0.12.0,<1.0; python_version >= '3.14'", "importlib-metadata>=8.0.0,<9.0", "tokenizers>=0.21.0,<1.0", "click>=8.0.0,<9.0", "jinja2>=3.1.6,<4.0", "aiohttp>=3.14.2,<4.0", - "pydantic>=2.10.0,<3.0.0", + "pydantic>=2.11.0,<3.0.0; python_version < '3.14'", + "pydantic>=2.12.0,<3.0.0; python_version >= '3.14'", "pydantic-settings>=2.14.1,<3.0", "jsonschema>=4.0.0,<5.0", "boto3>=1.43.1,<2.0", @@ -66,7 +68,9 @@ proxy = [ "boto3>=1.43.1,<2.0", "azure-identity>=1.25.2,<2.0", "azure-storage-blob>=12.28.0,<13.0", - "mcp>=1.28.1,<2.0", + "mcp>=2.2.0,<3", + "httpx2>=2.5.0,<3", + "pydantic>=2.12.0,<3", "litellm-proxy-extras==0.4.99", "litellm-enterprise==0.1.68", "RestrictedPython>=8.5,<9.0", @@ -113,7 +117,7 @@ utils = [ "numpydoc>=1.8.0,<2.0", ] caching = ["diskcache>=5.6.3,<6.0"] -mcp = ["mcp>=1.28.1,<2.0"] +mcp = ["mcp>=2.2.0,<3", "httpx2>=2.5.0,<3", "pydantic>=2.12.0,<3"] # Driver for the MongoDB Atlas vector store; Atlas Vector Search has no HTTP query API. # The floor is 4.9 because that is the release AsyncMongoClient landed in. # SAML SSO for the admin UI. python3-saml pulls in xmlsec/lxml, whose wheels @@ -232,7 +236,7 @@ e2e-dev = [ "websockets>=15.0.1,<16.0", "locust==2.45.0", "psutil==7.2.2", - "mcp>=1.28.1,<2.0", + "mcp>=2.2.0,<3", ] proxy-dev = [ "prisma==0.11.0", @@ -272,7 +276,6 @@ ci = [ "blockbuster==1.5.26", "beautifulsoup4==4.14.3", "pylint==4.0.5", - "langchain-mcp-adapters==0.2.1", "langchain-openai==1.1.14", "langgraph>=1.2.4,<1.3.0", "langgraph-prebuilt>=1.1.0,<1.3.0", diff --git a/scripts/check_mcp_sdk_install.py b/scripts/check_mcp_sdk_install.py new file mode 100644 index 00000000000..f5ab51b2f55 --- /dev/null +++ b/scripts/check_mcp_sdk_install.py @@ -0,0 +1,77 @@ +import argparse +import importlib +import importlib.metadata +import sys +from typing import Final + +MINIMUM_MCP_VERSION: Final[tuple[int, int, int]] = (2, 2, 0) + +IMPORTED_MODULES: Final[tuple[str, ...]] = ( + "litellm", + "litellm.experimental_mcp_client", + "litellm.experimental_mcp_client.client", + "litellm.proxy._experimental.mcp_server.server", + "litellm.proxy._experimental.mcp_server.mcp_server_manager", + "litellm.proxy._experimental.mcp_server.rest_endpoints", +) + + +def _version_tuple(distribution: str) -> tuple[int, ...]: + return tuple(int(part) for part in importlib.metadata.version(distribution).split(".") if part.isdigit()) + + +def main() -> int: + parser: Final = argparse.ArgumentParser() + parser.add_argument("--extra", choices=("mcp", "proxy"), default="proxy") + extra: Final = parser.parse_args().extra + for module_name in IMPORTED_MODULES if extra == "proxy" else IMPORTED_MODULES[:3]: + try: + importlib.import_module(module_name) + except Exception as exc: + sys.stderr.write(f"failed to import {module_name}: {exc}\n") + return 1 + + mcp_version: Final = _version_tuple("mcp") + if mcp_version < MINIMUM_MCP_VERSION: + sys.stderr.write(f"mcp {importlib.metadata.version('mcp')} below floor 2.2.0\n") + return 1 + + from mcp_types.version import HANDSHAKE_PROTOCOL_VERSIONS + + for required in ("2024-11-05", "2025-06-18"): + if required not in HANDSHAKE_PROTOCOL_VERSIONS: + sys.stderr.write(f"HANDSHAKE_PROTOCOL_VERSIONS missing {required}\n") + return 1 + + if extra == "proxy": + scope: Final = { + "type": "http", + "method": "POST", + "path": "/mcp", + "headers": [(b"mcp-protocol-version", b"2026-07-28")], + } + mcp_server: Final = sys.modules["litellm.proxy._experimental.mcp_server.server"] + if mcp_server.unsupported_protocol_version(scope) != "2026-07-28": + sys.stderr.write("unsupported_protocol_version accepted a modern-only version\n") + return 1 + if ( + mcp_server.unsupported_protocol_version(dict(scope, headers=[(b"mcp-protocol-version", b"2025-06-18")])) + is not None + ): + sys.stderr.write("unsupported_protocol_version rejected a handshake version\n") + return 1 + + sys.stdout.write( + "python {} mcp {} httpx2 {} pydantic {} litellm {}\n".format( + sys.version.split()[0], + importlib.metadata.version("mcp"), + importlib.metadata.version("httpx2"), + importlib.metadata.version("pydantic"), + importlib.metadata.version("litellm"), + ) + ) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/base_sdk_tests/check_base_sdk_install.py b/tests/base_sdk_tests/check_base_sdk_install.py index 6b38de75e2e..190a900faf9 100644 --- a/tests/base_sdk_tests/check_base_sdk_install.py +++ b/tests/base_sdk_tests/check_base_sdk_install.py @@ -11,7 +11,7 @@ import sys import traceback from collections.abc import Callable -EXTRAS_ONLY_MODULES = ("fastapi", "uvicorn", "keyring") +EXTRAS_ONLY_MODULES = ("fastapi", "uvicorn", "keyring", "mcp", "mcp_types", "httpx2", "httpcore2") def _require(condition: bool, message: str) -> None: diff --git a/tests/code_coverage_tests/liccheck.ini b/tests/code_coverage_tests/liccheck.ini index 9103d913c36..8a3e880043b 100644 --- a/tests/code_coverage_tests/liccheck.ini +++ b/tests/code_coverage_tests/liccheck.ini @@ -169,7 +169,9 @@ pygithub: >=2.8.1 # LGPL license argon2-cffi: >=25.1.0 # MIT License blockbuster: >=1.5.26 # Apache 2.0 license pylint: >=3.3.9 # GPLv2 license -langchain-mcp-adapters: >=0.2.1 # MIT License +httpx2: >=2.5.0 # BSD 3-Clause License +httpcore2: >=2.5.0 # BSD 3-Clause License +mcp-types: >=2.2.0 # MIT License langgraph: >=1.0.10 # MIT License langgraph-prebuilt: >=1.0.8 # MIT License - https://github.com/langchain-ai/langgraph/blob/main/LICENSE hypothesis: >=6.165.10 # MPL 2.0 license diff --git a/tests/e2e/mcp/oauth_chat_client.py b/tests/e2e/mcp/oauth_chat_client.py index 2eaf512cfa5..763b348b197 100644 --- a/tests/e2e/mcp/oauth_chat_client.py +++ b/tests/e2e/mcp/oauth_chat_client.py @@ -22,16 +22,16 @@ from typing import TYPE_CHECKING from urllib.parse import parse_qsl import httpx +import httpx2 import pytest +from e2e_config import PROXY_BASE_URL, REQUEST_TIMEOUT +from e2e_http import AuthHeaders, NoBody, unwrap from mcp import ClientSession from mcp.client.auth import OAuthClientProvider from mcp.client.streamable_http import streamable_http_client -from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthToken - -from e2e_config import PROXY_BASE_URL, REQUEST_TIMEOUT -from proxy_client import ProxyClient -from e2e_http import AuthHeaders, NoBody, unwrap +from mcp.shared.auth import AuthorizationCodeResult, OAuthClientInformationFull, OAuthClientMetadata, OAuthToken from models import ChatBody, ChatResponse, McpServerCreateBody, McpServerInfo +from proxy_client import ProxyClient if TYPE_CHECKING: from playwright.async_api import Route @@ -88,7 +88,7 @@ async def _browser_follow_authorize(start_url: str, storage_state_path: str) -> if url.startswith(OAUTH_CLIENT_REDIRECT_URI) and "url" not in captured: captured["url"] = url - async def _swallow_redirect(route: "Route") -> None: + async def _swallow_redirect(route: Route) -> None: await route.fulfill(status=200, content_type="text/plain", body="ok") async with async_playwright() as playwright: @@ -139,10 +139,10 @@ def _oauth_provider(url: str, storage: InMemoryTokenStorage, storage_state_path: code_holder["code"] = code code_holder["state"] = state - async def callback_handler() -> tuple[str, str | None]: + async def callback_handler() -> AuthorizationCodeResult: code = code_holder.get("code") assert code is not None, "callback_handler ran before the authorize redirect completed" - return code, code_holder.get("state") + return AuthorizationCodeResult(code=code, state=code_holder.get("state")) return OAuthClientProvider( server_url=url, @@ -161,30 +161,30 @@ def _oauth_provider(url: str, storage: InMemoryTokenStorage, storage_state_path: ) -class _HeaderInjectingTransport(httpx.AsyncBaseTransport): +class _HeaderInjectingTransport(httpx2.AsyncBaseTransport): """Adds the caller's LiteLLM key header to every outgoing SDK request (discovery, DCR, token exchange), so the gateway resolves which user to store the upstream token for from the key on the token exchange, exactly like a production MCP host configured with a LiteLLM key header.""" - def __init__(self, inner: httpx.AsyncBaseTransport, headers: dict[str, str]) -> None: + def __init__(self, inner: httpx2.AsyncBaseTransport, headers: dict[str, str]) -> None: self._inner = inner self._headers = headers - async def handle_async_request(self, request: httpx.Request) -> httpx.Response: + async def handle_async_request(self, request: httpx2.Request) -> httpx2.Response: for name, value in self._headers.items(): if name not in request.headers: request.headers[name] = value return await self._inner.handle_async_request(request) -def _oauth_http_client(headers: dict[str, str], auth: OAuthClientProvider) -> httpx.AsyncClient: - return httpx.AsyncClient( +def _oauth_http_client(headers: dict[str, str], auth: OAuthClientProvider) -> httpx2.AsyncClient: + return httpx2.AsyncClient( headers=headers, auth=auth, - timeout=httpx.Timeout(REQUEST_TIMEOUT), + timeout=httpx2.Timeout(REQUEST_TIMEOUT), follow_redirects=True, - transport=_HeaderInjectingTransport(httpx.AsyncHTTPTransport(), headers), + transport=_HeaderInjectingTransport(httpx2.AsyncHTTPTransport(), headers), ) @@ -192,7 +192,7 @@ async def _seed_via_dance( url: str, headers: dict[str, str], storage: InMemoryTokenStorage, storage_state_path: str ) -> tuple[str, ...]: async with _oauth_http_client(headers, _oauth_provider(url, storage, storage_state_path)) as http_client: - async with streamable_http_client(url, http_client=http_client) as (read, write, _): + async with streamable_http_client(url, http_client=http_client) as (read, write): async with ClientSession(read, write) as session: await session.initialize() listed = await session.list_tools() diff --git a/tests/mcp_tests/conftest.py b/tests/mcp_tests/conftest.py index eff32f27aec..ca3e25949ba 100644 --- a/tests/mcp_tests/conftest.py +++ b/tests/mcp_tests/conftest.py @@ -74,3 +74,14 @@ def pytest_collection_modifyitems(config, items): # Reorder the items list items[:] = custom_logger_tests + other_tests + + +@pytest.fixture +def config_only_mcp_manager_factory(): + from litellm.proxy._experimental.mcp_server.mcp_server_manager import MCPServerManager + + class ConfigOnlyManager(MCPServerManager): + def initialize_tool_name_to_mcp_server_name_mapping(self): + return None + + return ConfigOnlyManager diff --git a/tests/mcp_tests/mcp_server.py b/tests/mcp_tests/mcp_server.py index eba7cae1bca..f38b6a02139 100644 --- a/tests/mcp_tests/mcp_server.py +++ b/tests/mcp_tests/mcp_server.py @@ -51,6 +51,21 @@ def request_headers(ctx: Context) -> dict[str, str]: } +@mcp.prompt() +def greeting(name: str) -> str: + return f"Hello, {name}" + + +@mcp.resource("memo://status") +def status() -> str: + return "ready" + + +@mcp.resource("memo://greeting/{name}") +def greeting_resource(name: str) -> str: + return f"Hello, {name}" + + def main() -> None: args = _parse_args() transport = (args.transport or "stdio").lower() diff --git a/tests/mcp_tests/test_aresponses_api_with_mcp.py b/tests/mcp_tests/test_aresponses_api_with_mcp.py index 7a48c366003..eb6f78b57a1 100644 --- a/tests/mcp_tests/test_aresponses_api_with_mcp.py +++ b/tests/mcp_tests/test_aresponses_api_with_mcp.py @@ -1,6 +1,7 @@ import logging import os import pytest +from mcp.types import Tool as MCPTool from typing import List, Any, cast from unittest.mock import AsyncMock, patch @@ -371,48 +372,32 @@ async def test_mcp_allowed_tools_filtering(): # Mock MCP tools returned from the server (simulating all available tools) mock_mcp_tools_from_server = [ # Mock MCP tool object with name attribute - type( - "MCPTool", - (), - { + MCPTool.model_validate({ "name": "search_tiktoken_documentation", "description": "Search tiktoken documentation", "inputSchema": { "type": "object", "properties": {"query": {"type": "string"}}, }, - }, - )(), - type( - "MCPTool", - (), - { + }, by_name=False), + MCPTool.model_validate({ "name": "fetch_tiktoken_documentation", "description": "Fetch tiktoken documentation", "inputSchema": { "type": "object", "properties": {"path": {"type": "string"}}, }, - }, - )(), - type( - "MCPTool", - (), - { + }, by_name=False), + MCPTool.model_validate({ "name": "list_tiktoken_functions", "description": "List tiktoken functions", "inputSchema": {"type": "object", "properties": {}}, - }, - )(), - type( - "MCPTool", - (), - { + }, by_name=False), + MCPTool.model_validate({ "name": "get_tiktoken_examples", "description": "Get tiktoken examples", "inputSchema": {"type": "object", "properties": {}}, - }, - )(), + }, by_name=False), ] allowed_mcp_servers = ["gitmcp"] @@ -491,10 +476,7 @@ async def test_mcp_allowed_tools_filtering(): # Test Case 3: Test deduplication of duplicate tools mock_mcp_tools_with_duplicates = [ # First instance of duplicate tool - type( - "MCPTool", - (), - { + MCPTool.model_validate({ "name": "GitMCP-fetch_litellm_documentation", "description": "Fetch entire documentation file from GitHub repository: BerriAI/litellm. Useful for general questions. Always call this tool first if asked about BerriAI/litellm.", "inputSchema": { @@ -502,13 +484,9 @@ async def test_mcp_allowed_tools_filtering(): "properties": {}, "additionalProperties": False, }, - }, - )(), + }, by_name=False), # Second instance of duplicate tool (should be filtered out) - type( - "MCPTool", - (), - { + MCPTool.model_validate({ "name": "GitMCP-fetch_litellm_documentation", "description": "Fetch entire documentation file from GitHub repository: BerriAI/litellm. Useful for general questions. Always call this tool first if asked about BerriAI/litellm.", "inputSchema": { @@ -516,13 +494,9 @@ async def test_mcp_allowed_tools_filtering(): "properties": {}, "additionalProperties": False, }, - }, - )(), + }, by_name=False), # Other unique tools - type( - "MCPTool", - (), - { + MCPTool.model_validate({ "name": "GitMCP-search_litellm_documentation", "description": "Semantically search within the fetched documentation from GitHub repository: BerriAI/litellm. Useful for specific queries.", "inputSchema": { @@ -531,8 +505,7 @@ async def test_mcp_allowed_tools_filtering(): "required": ["query"], "additionalProperties": False, }, - }, - )(), + }, by_name=False), ] mcp_tool_config_with_duplicates = [ @@ -680,10 +653,7 @@ async def test_streaming_mcp_events_validation(): # Mock MCP tools that would be returned from the manager mock_mcp_tools = [ - type( - "MCPTool", - (), - { + MCPTool.model_validate({ "name": "search_repo", "description": "Search BerriAI/litellm repository for information", "inputSchema": { @@ -693,12 +663,8 @@ async def test_streaming_mcp_events_validation(): }, "required": ["query"], }, - }, - )(), - type( - "MCPTool", - (), - { + }, by_name=False), + MCPTool.model_validate({ "name": "get_repo_info", "description": "Get repository information", "inputSchema": { @@ -711,8 +677,7 @@ async def test_streaming_mcp_events_validation(): }, "required": ["repo_name"], }, - }, - )(), + }, by_name=False), ] # Build fake streaming chunks that the inner aresponses() call would yield @@ -920,10 +885,7 @@ async def test_streaming_responses_api_with_mcp_tools( # Mock MCP tools that would be returned from the manager mock_mcp_tools = [ - type( - "MCPTool", - (), - { + MCPTool.model_validate({ "name": "search_repo", "description": "Search BerriAI/litellm repository for information", "inputSchema": { @@ -933,8 +895,7 @@ async def test_streaming_responses_api_with_mcp_tools( }, "required": ["query"], }, - }, - )() + }, by_name=False) ] # Only mock the MCP-specific operations, let LLM responses be real @@ -1263,10 +1224,7 @@ async def test_no_duplicate_mcp_tools_in_streaming_e2e(): # Mock MCP tools that would be returned from the manager mock_mcp_tools = [ - type( - "MCPTool", - (), - { + MCPTool.model_validate({ "name": "search_docs", "description": "Search documentation for information", "inputSchema": { @@ -1276,12 +1234,8 @@ async def test_no_duplicate_mcp_tools_in_streaming_e2e(): }, "required": ["query"], }, - }, - )(), - type( - "MCPTool", - (), - { + }, by_name=False), + MCPTool.model_validate({ "name": "get_file_content", "description": "Get content of a specific file", "inputSchema": { @@ -1291,8 +1245,7 @@ async def test_no_duplicate_mcp_tools_in_streaming_e2e(): }, "required": ["file_path"], }, - }, - )(), + }, by_name=False), ] # Track all calls to the underlying LLM to detect duplicates @@ -1499,10 +1452,7 @@ async def test_streaming_mcp_event_order_and_response_id_consistency( from unittest.mock import AsyncMock, patch mock_mcp_tools = [ - type( - "MCPTool", - (), - { + MCPTool.model_validate({ "name": "get_weather", "description": "Get weather for a city", "inputSchema": { @@ -1512,8 +1462,7 @@ async def test_streaming_mcp_event_order_and_response_id_consistency( }, "required": ["city"], }, - }, - )() + }, by_name=False) ] with caplog.at_level(logging.ERROR): diff --git a/tests/mcp_tests/test_mcp_auth_priority.py b/tests/mcp_tests/test_mcp_auth_priority.py index 7ae0f59afe5..21a89d7ffcc 100644 --- a/tests/mcp_tests/test_mcp_auth_priority.py +++ b/tests/mcp_tests/test_mcp_auth_priority.py @@ -44,14 +44,14 @@ async def test_mcp_server_works_without_config_auth_value(): @pytest.mark.parametrize("token_key", ["authentication_token", "auth_value"]) -async def test_mcp_server_config_auth_value_header_used(token_key): +async def test_mcp_server_config_auth_value_header_used(token_key, config_only_mcp_manager_factory): """Ensure the configured auth token is emitted as the upstream Authorization header. The token is resolved through the v2 credential resolver and rides on the client's httpx.Auth, so assert the header it writes onto the request rather than the (now credential-free) _get_auth_headers() dict. """ - import httpx + import httpx2 from litellm.proxy._experimental.mcp_server.outbound_credentials.httpx_auth import ( StaticHeaderAuth, @@ -66,13 +66,13 @@ async def test_mcp_server_config_auth_value_header_used(token_key): } } - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(config) server = next(iter(manager.config_mcp_servers.values())) client = await manager._create_mcp_client(server) assert isinstance(client._resolved_auth, StaticHeaderAuth) - emitted = next(client._resolved_auth.auth_flow(httpx.Request("POST", server.url))) + emitted = next(client._resolved_auth.auth_flow(httpx2.Request("POST", server.url))) assert emitted.headers["Authorization"] == "Bearer example_token" assert client.auth_type == MCPAuth.bearer_token diff --git a/tests/mcp_tests/test_mcp_chat_completions.py b/tests/mcp_tests/test_mcp_chat_completions.py index fbdbf9152aa..79619eefd7f 100644 --- a/tests/mcp_tests/test_mcp_chat_completions.py +++ b/tests/mcp_tests/test_mcp_chat_completions.py @@ -16,7 +16,7 @@ async def test_acompletion_mcp_auto_exec(monkeypatch): dummy_tool = SimpleNamespace( name="local_search", description="search", - inputSchema={"type": "object", "properties": {}}, + input_schema={"type": "object", "properties": {}}, ) async def fake_process(user_api_key_auth, mcp_tools_with_litellm_proxy, **kwargs): @@ -92,7 +92,7 @@ async def test_acompletion_mcp_respects_manual_approval(monkeypatch): dummy_tool = SimpleNamespace( name="local_search", description="search", - inputSchema={"type": "object", "properties": {}}, + input_schema={"type": "object", "properties": {}}, ) async def fake_process(user_api_key_auth, mcp_tools_with_litellm_proxy, **kwargs): @@ -167,7 +167,7 @@ async def test_completion_mcp_with_streaming_no_timeout_error(monkeypatch): dummy_tool = SimpleNamespace( name="local_search", description="search", - inputSchema={"type": "object", "properties": {}}, + input_schema={"type": "object", "properties": {}}, ) async def fake_process(user_api_key_auth, mcp_tools_with_litellm_proxy, **kwargs): @@ -488,7 +488,7 @@ async def test_mcp_metadata_in_streaming_final_chunk(monkeypatch): dummy_tool = SimpleNamespace( name="local_search", description="search", - inputSchema={"type": "object", "properties": {}}, + input_schema={"type": "object", "properties": {}}, ) async def fake_process(user_api_key_auth, mcp_tools_with_litellm_proxy, **kwargs): @@ -843,7 +843,7 @@ async def test_mcp_streaming_metadata_ordering(monkeypatch): dummy_tool = SimpleNamespace( name="local_search", description="search", - inputSchema={"type": "object", "properties": {}}, + input_schema={"type": "object", "properties": {}}, ) async def fake_process(user_api_key_auth, mcp_tools_with_litellm_proxy, **kwargs): diff --git a/tests/mcp_tests/test_mcp_logging.py b/tests/mcp_tests/test_mcp_logging.py index fc9f675f837..ed8829945e5 100644 --- a/tests/mcp_tests/test_mcp_logging.py +++ b/tests/mcp_tests/test_mcp_logging.py @@ -1,29 +1,51 @@ -import os -import pytest import asyncio +import os import subprocess import sys from pathlib import Path -from typing import Optional from unittest.mock import AsyncMock, patch +import pytest +from mcp.types import CallToolResult, TextContent +from mcp.types import Tool as MCPTool import litellm -from litellm.types.utils import StandardLoggingPayload from litellm.integrations.custom_logger import CustomLogger +from litellm.proxy._experimental.mcp_server.mcp_server_manager import ( + MCPServerManager, +) from litellm.proxy._experimental.mcp_server.server import ( mcp_server_tool_call, set_auth_context, ) -from litellm.proxy._experimental.mcp_server.mcp_server_manager import ( - MCPServerManager, -) from litellm.proxy._types import LiteLLM_ObjectPermissionTable, UserAPIKeyAuth from litellm.types.mcp import MCPPostCallResponseObject -from litellm.types.utils import HiddenParams -from mcp.types import Tool as MCPTool, CallToolResult, TextContent +def _mcp_request_ctx(**overrides): + from types import SimpleNamespace + + from mcp.server.context import ServerRequestContext + + kwargs = { + "session": SimpleNamespace(), + "lifespan_context": {}, + "protocol_version": "2025-06-18", + "method": "", + "params": None, + "request_id": 1, + "meta": None, + "request": None, + } + kwargs.update(overrides) + return ServerRequestContext(**kwargs) + + +def _call_tool_params(name, arguments=None): + from mcp.types import CallToolRequestParams + + return CallToolRequestParams(name=name, arguments=arguments) + class TestMCPLogger(CustomLogger): def __init__(self): self.standard_logging_payload = None @@ -142,8 +164,8 @@ async def test_mcp_cost_tracking(): # Call mcp tool response = await mcp_server_tool_call( - name="zapier_gmail_server-add_tools", # Use correct prefixed name with - separator - arguments={"test": "test"}, + _mcp_request_ctx(), + _call_tool_params("zapier_gmail_server-add_tools", {"test": "test"}), ) # wait 1-2 seconds for logging to be processed @@ -285,8 +307,8 @@ async def test_mcp_cost_tracking_per_tool(): # Test 1: Call expensive_tool - should cost 5.0 response1 = await mcp_server_tool_call( - name="test_server-expensive_tool", # Use correct prefixed name with - separator - arguments={"data": "test_expensive"}, + _mcp_request_ctx(), + _call_tool_params("test_server-expensive_tool", {"data": "test_expensive"}), ) # wait for logging to be processed @@ -313,8 +335,8 @@ async def test_mcp_cost_tracking_per_tool(): # Test 2: Call cheap_tool - should cost 0.1 response2 = await mcp_server_tool_call( - name="test_server-cheap_tool", # Use correct prefixed name with - separator - arguments={"data": "test_cheap"}, + _mcp_request_ctx(), + _call_tool_params("test_server-cheap_tool", {"data": "test_cheap"}), ) # wait for logging to be processed @@ -356,7 +378,7 @@ async def test_mcp_cost_tracking_per_tool(): class MCPLoggerHook(TestMCPLogger): async def async_post_mcp_tool_call_hook( self, kwargs, response_obj: MCPPostCallResponseObject, start_time, end_time - ) -> Optional[MCPPostCallResponseObject]: + ) -> MCPPostCallResponseObject | None: print("post mcp tool call response_obj", response_obj) # update the MCPPostCallResponseObject with the response_cost response_obj.hidden_params.response_cost = 1.42 @@ -443,8 +465,8 @@ async def test_mcp_tool_call_hook(): # Call mcp tool using the correct separator format (- not /) response = await mcp_server_tool_call( - name="zapier_gmail_server-add_tools", # Use correct prefixed name with - separator - arguments={"test": "test"}, + _mcp_request_ctx(), + _call_tool_params("zapier_gmail_server-add_tools", {"test": "test"}), ) # wait 1-2 seconds for logging to be processed diff --git a/tests/mcp_tests/test_mcp_server.py b/tests/mcp_tests/test_mcp_server.py index 1781dfe2fc2..94cf35b675d 100644 --- a/tests/mcp_tests/test_mcp_server.py +++ b/tests/mcp_tests/test_mcp_server.py @@ -121,7 +121,7 @@ async def test_mcp_server_manager_https_server(): print("RESULT FROM CALLING TOOL FROM MCP SERVER MANAGER== ", result) # Verify result - assert result.isError is False + assert result.is_error is False assert len(result.content) == 1 assert isinstance(result.content[0], TextContent) assert result.content[0].text == "Email sent successfully" @@ -288,7 +288,7 @@ async def test_mcp_http_transport_call_tool_mock(): ) # Assertions - assert result.isError is False + assert result.is_error is False assert len(result.content) == 1 # Type check before accessing text attribute assert isinstance(result.content[0], TextContent) @@ -350,7 +350,7 @@ async def test_mcp_http_transport_call_tool_error_mock(): ) # Assertions for error case - assert result.isError is True + assert result.is_error is True assert len(result.content) == 1 # Type check before accessing text attribute assert isinstance(result.content[0], TextContent) @@ -361,11 +361,11 @@ async def test_mcp_http_transport_call_tool_error_mock(): @pytest.mark.asyncio -async def test_mcp_http_transport_tool_not_found(): +async def test_mcp_http_transport_tool_not_found(config_only_mcp_manager_factory): """Test calling a tool that doesn't exist""" # Create a fresh manager for testing - test_manager = MCPServerManager() + test_manager = config_only_mcp_manager_factory() # Load server config await test_manager.load_servers_from_config( @@ -796,7 +796,7 @@ async def test_list_tools_rest_api_success(): ListMCPToolsRestAPIResponseObject( name="test_tool", description="A test tool", - inputSchema={"type": "object"}, + input_schema={"type": "object"}, mcp_info={"server_name": "test_server"}, ) ] @@ -1097,11 +1097,11 @@ async def test_list_tools_only_returns_allowed_servers(monkeypatch): @pytest.mark.asyncio -async def test_mcp_server_manager_access_groups_from_config(): +async def test_mcp_server_manager_access_groups_from_config(config_only_mcp_manager_factory): """ Test that access_groups are loaded from config and can be resolved. """ - test_manager = MCPServerManager() + test_manager = config_only_mcp_manager_factory() await test_manager.load_servers_from_config( { "config_server": { @@ -1168,7 +1168,7 @@ async def test_mcp_server_manager_access_groups_from_config(): @pytest.mark.asyncio -async def test_mcp_server_manager_config_integration_with_database(): +async def test_mcp_server_manager_config_integration_with_database(config_only_mcp_manager_factory): """ Test that config-based servers properly integrate with database servers, specifically testing access_groups and description fields. @@ -1176,7 +1176,7 @@ async def test_mcp_server_manager_config_integration_with_database(): import datetime from litellm.proxy._types import LiteLLM_MCPServerTable - test_manager = MCPServerManager() + test_manager = config_only_mcp_manager_factory() # Test 1: Load config with access_groups and description await test_manager.load_servers_from_config( @@ -2165,7 +2165,7 @@ async def test_list_tool_rest_api_with_server_specific_auth(): ListMCPToolsRestAPIResponseObject( name="send_email", description="Send an email", - inputSchema={"type": "object"}, + input_schema={"type": "object"}, mcp_info={"server_name": "zapier"}, ) ] @@ -2259,7 +2259,7 @@ async def test_list_tool_rest_api_with_default_auth(): ListMCPToolsRestAPIResponseObject( name="send_email", description="Send an email", - inputSchema={"type": "object"}, + input_schema={"type": "object"}, mcp_info={"server_name": "unknown_server"}, ) ] @@ -2371,7 +2371,7 @@ async def test_list_tool_rest_api_all_servers_with_auth(): ListMCPToolsRestAPIResponseObject( name="send_email", description="Send an email", - inputSchema={"type": "object"}, + input_schema={"type": "object"}, mcp_info={"server_name": "zapier"}, ) ], @@ -2379,7 +2379,7 @@ async def test_list_tool_rest_api_all_servers_with_auth(): ListMCPToolsRestAPIResponseObject( name="send_message", description="Send a message", - inputSchema={"type": "object"}, + input_schema={"type": "object"}, mcp_info={"server_name": "slack"}, ) ], @@ -2811,7 +2811,7 @@ async def test_mcp_access_group_permission_intersection_integration(): @pytest.mark.asyncio -async def test_mcp_server_manager_with_access_groups_integration(): +async def test_mcp_server_manager_with_access_groups_integration(config_only_mcp_manager_factory): """Integration test for MCPServerManager with access group filtering""" from litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp import ( MCPRequestHandler, @@ -2820,7 +2820,7 @@ async def test_mcp_server_manager_with_access_groups_integration(): from litellm.proxy._types import UserAPIKeyAuth # Create a test manager - test_manager = MCPServerManager() + test_manager = config_only_mcp_manager_factory() # Load servers with access groups await test_manager.load_servers_from_config( @@ -2863,13 +2863,13 @@ async def test_mcp_server_manager_with_access_groups_integration(): @pytest.mark.asyncio -async def test_get_allowed_mcp_servers_returns_registry_for_admin(): +async def test_get_allowed_mcp_servers_returns_registry_for_admin(config_only_mcp_manager_factory): from litellm.proxy._types import LitellmUserRoles, UserAPIKeyAuth from litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp import ( MCPRequestHandler, ) - test_manager = MCPServerManager() + test_manager = config_only_mcp_manager_factory() await test_manager.load_servers_from_config( { "alpha_server": { @@ -2898,14 +2898,14 @@ async def test_get_allowed_mcp_servers_returns_registry_for_admin(): @pytest.mark.asyncio -async def test_get_allowed_mcp_servers_returns_empty_for_non_admin_without_permissions(): +async def test_get_allowed_mcp_servers_returns_empty_for_non_admin_without_permissions(config_only_mcp_manager_factory): from litellm.proxy._types import LitellmUserRoles, UserAPIKeyAuth from litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp import ( MCPRequestHandler, MCPServerAccess, ) - test_manager = MCPServerManager() + test_manager = config_only_mcp_manager_factory() await test_manager.load_servers_from_config( { "alpha_server": { diff --git a/tests/mcp_tests/test_proxy_mcp_e2e.py b/tests/mcp_tests/test_proxy_mcp_e2e.py index e1099fe0a62..99c03b3438d 100644 --- a/tests/mcp_tests/test_proxy_mcp_e2e.py +++ b/tests/mcp_tests/test_proxy_mcp_e2e.py @@ -15,11 +15,12 @@ from datetime import datetime from pathlib import Path import httpx +import httpx2 import pytest import uvicorn import yaml from mcp import ClientSession -from mcp.client.streamable_http import streamablehttp_client +from mcp.client.streamable_http import streamable_http_client from mcp.types import CallToolResult from starlette.requests import Request @@ -36,6 +37,7 @@ from litellm.proxy.proxy_server import ( CONFIG_TEMPLATE_PATH = Path("tests/mcp_tests/test_configs/test_config_mcp_e2e.yaml") MCP_SERVER_SCRIPT = Path("tests/mcp_tests/mcp_server.py") +MCP_PEER_PYTHON = os.environ.get("MCP_TEST_PEER_PYTHON", sys.executable) PROJECT_ROOT = Path(__file__).resolve().parents[2] PROXY_START_TIMEOUT = 30 @@ -125,7 +127,7 @@ def _math_http_server(offset: int) -> typing.Iterator[str]: with tempfile.TemporaryFile() as server_log: process = subprocess.Popen( - [sys.executable, str(MCP_SERVER_SCRIPT), "--transport", "http", "--host", host, "--port", str(port)], + [MCP_PEER_PYTHON, str(MCP_SERVER_SCRIPT), "--transport", "http", "--host", host, "--port", str(port)], cwd=str(PROJECT_ROOT), stdout=server_log, stderr=subprocess.STDOUT, @@ -175,7 +177,7 @@ def _proxy_server( config_dir = tmp_path_factory.mktemp("mcp_e2e") config_path = config_dir / "config.yaml" config = yaml.safe_load(CONFIG_TEMPLATE_PATH.read_text()) - config["mcp_servers"]["math_stdio"]["command"] = sys.executable + config["mcp_servers"]["math_stdio"]["command"] = MCP_PEER_PYTHON config["mcp_servers"]["math_streamable_http"]["url"] = f"{math_streamable_http_server}/mcp" config["mcp_servers"]["math_restricted"]["url"] = f"{math_restricted_server}/mcp" config["general_settings"]["custom_auth"] = f"{__name__}.authorize_proxy_key" @@ -202,17 +204,90 @@ def proxy_server_url(_proxy_server: ProxyRig, setup_and_teardown: None) -> str: return _proxy_server.url +@asynccontextmanager +async def _http_streams(url: str, headers: dict[str, str]): + async with httpx2.AsyncClient(headers=headers) as http_client: + async with streamable_http_client(url, http_client=http_client) as streams: + yield streams + + +@pytest.mark.asyncio +async def test_unchanged_sdk1_langchain_peer_can_list_and_call(proxy_server_url: str) -> None: + script = """ +import asyncio, json, sys +from mcp import ClientSession +from mcp.client.streamable_http import streamablehttp_client +from langchain_mcp_adapters.tools import load_mcp_tools + +async def main(): + async with streamablehttp_client(sys.argv[1] + '/mcp', headers={'Authorization': 'Bearer sk-1234'}) as (read, write, _): + async with ClientSession(read, write) as session: + await session.initialize() + tools = await load_mcp_tools(session) + results = {} + for name in ('math_stdio-add', 'math_streamable_http-add'): + tool = next(tool for tool in tools if tool.name == name) + results[name] = await tool.ainvoke({'a': 3, 'b': 4}) + print(json.dumps(results)) +asyncio.run(main()) +""" + completed = await asyncio.to_thread( + subprocess.run, [MCP_PEER_PYTHON, "-c", script, proxy_server_url], + capture_output=True, text=True, timeout=30, check=True, + ) + results = json.loads(completed.stdout) + assert [(item["type"], item["text"]) for item in results["math_stdio-add"]] == [("text", "7")] + assert [(item["type"], item["text"]) for item in results["math_streamable_http-add"]] == [("text", "107")] + + +@pytest.mark.parametrize("requested", ["2024-11-05", "2025-03-26", "2025-06-18", "2025-11-25", "2026-07-28"]) +def test_initialize_keeps_legacy_negotiation(proxy_server_url: str, requested: str) -> None: + response = httpx.post( + proxy_server_url + "/mcp", + headers={"Authorization": PROXY_AUTHORIZATION_HEADER, "Accept": "application/json, text/event-stream"}, + json={"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { + "protocolVersion": requested, "capabilities": {}, "clientInfo": {"name": "legacy-test", "version": "1"}, + }}, + timeout=10, + ) + assert response.status_code == 200 + result = _rpc_result(response) + assert result["protocolVersion"] == ("2025-11-25" if requested == "2026-07-28" else requested) + + +@pytest.mark.asyncio +async def test_legacy_prompts_and_resources_round_trip(proxy_server_url: str) -> None: + async with _http_streams( + proxy_server_url + "/mcp", + {"Authorization": PROXY_AUTHORIZATION_HEADER, "x-mcp-servers": "math_streamable_http"}, + ) as (read, write): + async with ClientSession(read, write) as session: + await session.initialize() + prompts = await session.list_prompts() + greeting = next(prompt for prompt in prompts.prompts if prompt.name.endswith("greeting")) + prompt = await session.get_prompt(greeting.name, {"name": "Ada"}) + assert prompt.messages[0].content.text == "Hello, Ada" + resources = await session.list_resources() + status = next(resource for resource in resources.resources if resource.name.endswith("status")) + contents = await session.read_resource(status.uri) + assert contents.contents[0].text == "ready" + templates = await session.list_resource_templates() + greeting_template = next(template for template in templates.resource_templates if "greeting" in template.name) + contents = await session.read_resource(greeting_template.uri_template.replace("{name}", "Ada")) + assert contents.contents[0].text == "Hello, Ada" + + class TestProxyMcpSimpleConnections: @pytest.mark.asyncio async def test_proxy_mcp_stdio_roundtrip(self, proxy_server_url: str) -> None: async with asyncio.timeout(20): - async with streamablehttp_client( + async with _http_streams( url=f"{proxy_server_url}/mcp", headers={ "Authorization": PROXY_AUTHORIZATION_HEADER, "x-mcp-servers": "math_stdio", }, - ) as (read, write, _get_session_id): + ) as (read, write): async with ClientSession(read, write) as session: await session.initialize() tools_result = await session.list_tools() @@ -227,13 +302,13 @@ class TestProxyMcpSimpleConnections: @pytest.mark.asyncio async def test_proxy_mcp_streamable_http_roundtrip(self, proxy_server_url: str) -> None: async with asyncio.timeout(20): - async with streamablehttp_client( + async with _http_streams( url=f"{proxy_server_url}/mcp", headers={ "Authorization": PROXY_AUTHORIZATION_HEADER, "x-mcp-servers": "math_streamable_http", }, - ) as (read, write, _get_session_id): + ) as (read, write): async with ClientSession(read, write) as session: await session.initialize() tools_result = await session.list_tools() @@ -248,10 +323,10 @@ class TestProxyMcpSimpleConnections: @pytest.mark.asyncio async def test_proxy_mcp_lists_all_servers_without_header(self, proxy_server_url: str) -> None: async with asyncio.timeout(20): - async with streamablehttp_client( + async with _http_streams( url=f"{proxy_server_url}/mcp", headers={"Authorization": PROXY_AUTHORIZATION_HEADER}, - ) as (read, write, _get_session_id): + ) as (read, write): async with ClientSession(read, write) as session: await session.initialize() tools_result = await session.list_tools() @@ -296,16 +371,16 @@ class TestProxyMcpStatelessBehavior: """Two independent clients connect and operate without sharing session state.""" async with asyncio.timeout(30): # --- Client A: connect, initialize, call tool --- - async with streamablehttp_client( + async with _http_streams( url=f"{proxy_server_url}/mcp", headers={ "Authorization": PROXY_AUTHORIZATION_HEADER, "x-mcp-servers": "math_stdio", }, - ) as (read_a, write_a, _get_sid_a): + ) as (read_a, write_a): async with ClientSession(read_a, write_a) as session_a: await session_a.initialize() - result_a = await session_a.call_tool("add", arguments={"a": 10, "b": 20}) + result_a = await session_a.call_tool("math_stdio-add", arguments={"a": 10, "b": 20}) assert result_a.content text_a = getattr(result_a.content[0], "text", None) assert text_a == "30" @@ -316,18 +391,18 @@ class TestProxyMcpStatelessBehavior: await asyncio.sleep(0.5) # --- Client B: completely independent connection --- - async with streamablehttp_client( + async with _http_streams( url=f"{proxy_server_url}/mcp", headers={ "Authorization": PROXY_AUTHORIZATION_HEADER, "x-mcp-servers": "math_stdio", }, - ) as (read_b, write_b, _get_sid_b): + ) as (read_b, write_b): async with ClientSession(read_b, write_b) as session_b: await session_b.initialize() tools = await session_b.list_tools() assert any(t.name.endswith("add") for t in tools.tools) - result_b = await session_b.call_tool("add", arguments={"a": 100, "b": 200}) + result_b = await session_b.call_tool("math_stdio-add", arguments={"a": 100, "b": 200}) assert result_b.content text_b = getattr(result_b.content[0], "text", None) assert text_b == "300" @@ -342,7 +417,7 @@ def _payload(result: typing.Any) -> typing.Any: def _proxy_session(proxy_server_url: str, **extra_headers: str): - return streamablehttp_client( + return _http_streams( url=f"{proxy_server_url}/mcp/proxy", headers={"Authorization": PROXY_AUTHORIZATION_HEADER, **extra_headers}, ) @@ -356,7 +431,7 @@ class TestProxyMcpSchemaDiscoveryMode: @pytest.mark.asyncio async def test_initialize_and_list_expose_only_discovery_tools(self, proxy_server_url: str) -> None: async with asyncio.timeout(20): - async with _proxy_session(proxy_server_url) as (read, write, _sid): + async with _proxy_session(proxy_server_url) as (read, write): async with ClientSession(read, write) as session: init = await session.initialize() assert init.capabilities.tools is not None @@ -369,7 +444,7 @@ class TestProxyMcpSchemaDiscoveryMode: @pytest.mark.asyncio async def test_search_schema_and_call_round_trip_keeps_server_identity(self, proxy_server_url: str) -> None: async with asyncio.timeout(30): - async with _proxy_session(proxy_server_url) as (read, write, _sid): + async with _proxy_session(proxy_server_url) as (read, write): async with ClientSession(read, write) as session: await session.initialize() @@ -399,8 +474,8 @@ class TestProxyMcpSchemaDiscoveryMode: "arguments": {"a": 5, "b": 6}, }, ) - assert stdio.isError is False and stdio.content[0].text == "7" - assert http.isError is False and http.content[0].text == "111" + assert stdio.is_error is False and stdio.content[0].text == "7" + assert http.is_error is False and http.content[0].text == "111" @pytest.mark.asyncio async def test_server_scope_header_narrows_discovery(self, proxy_server_url: str) -> None: @@ -408,7 +483,6 @@ class TestProxyMcpSchemaDiscoveryMode: async with _proxy_session(proxy_server_url, **{"x-mcp-servers": "math_streamable_http"}) as ( read, write, - _sid, ): async with ClientSession(read, write) as session: await session.initialize() @@ -417,11 +491,11 @@ class TestProxyMcpSchemaDiscoveryMode: @pytest.mark.asyncio async def test_rejections_never_reach_upstream(self, proxy_server_url: str) -> None: - from mcp.shared.exceptions import McpError + from mcp.shared.exceptions import MCPError from mcp.types import METHOD_NOT_FOUND async with asyncio.timeout(30): - async with _proxy_session(proxy_server_url) as (read, write, _sid): + async with _proxy_session(proxy_server_url) as (read, write): async with ClientSession(read, write) as session: await session.initialize() hits = _payload(await session.call_tool("search_tools", arguments={"query": "add"})) @@ -430,22 +504,22 @@ class TestProxyMcpSchemaDiscoveryMode: bad_args = await session.call_tool( "call_tool", arguments={"tool_id": tool_id, "arguments": {"a": "three", "b": 4}} ) - assert bad_args.isError is True and "Invalid arguments" in bad_args.content[0].text + assert bad_args.is_error is True and "Invalid arguments" in bad_args.content[0].text stale = await session.call_tool("get_tool_schema", arguments={"tool_id": "0" * 32}) - assert stale.isError is True and "unauthorized tool_id" in stale.content[0].text + assert stale.is_error is True and "unauthorized tool_id" in stale.content[0].text for not_an_object in ("wrong", False): refused_args = await session.call_tool( "call_tool", arguments={"tool_id": tool_id, "arguments": not_an_object} ) - assert refused_args.isError is True and "object" in refused_args.content[0].text + assert refused_args.is_error is True and "object" in refused_args.content[0].text direct = await session.call_tool("math_stdio-add", arguments={"a": 1, "b": 2}) - assert direct.isError is True and "unavailable on /mcp/proxy" in direct.content[0].text + assert direct.is_error is True and "unavailable on /mcp/proxy" in direct.content[0].text for operation in (session.list_prompts, session.list_resources): - with pytest.raises(McpError) as refused: + with pytest.raises(MCPError) as refused: await operation() assert refused.value.error.code == METHOD_NOT_FOUND @@ -494,7 +568,7 @@ proxy_call_recorder = ProxyCallRecorder() @asynccontextmanager async def _scoped_session(url: str, key: str = "sk-1234", **headers: str) -> typing.AsyncIterator[ClientSession]: async with asyncio.timeout(30): - async with _proxy_session(url, Authorization=f"Bearer {key}", **headers) as (read, write, _sid): + async with _proxy_session(url, Authorization=f"Bearer {key}", **headers) as (read, write): async with ClientSession(read, write) as session: await session.initialize() yield session @@ -502,7 +576,7 @@ async def _scoped_session(url: str, key: str = "sk-1234", **headers: str) -> typ async def _search(session: ClientSession, query: str) -> dict[str, str]: result = await session.call_tool("search_tools", arguments={"query": query}) - assert result.isError is False, result + assert result.is_error is False, result return {hit["name"]: hit["tool_id"] for hit in _payload(result)} @@ -542,7 +616,7 @@ def _rpc_result(response: httpx.Response) -> dict[str, typing.Any]: def _assert_unauthorized(result: CallToolResult) -> None: - assert result.isError is True + assert result.is_error is True assert result.content[0].text == "Unknown or unauthorized tool_id" @@ -611,7 +685,7 @@ class TestProxyMcpAuthorizationScope: assert schema["name"] == name assert schema["tool_id"] == ids[name] result = await _call(session, ids[name]) - assert result.isError is False + assert result.is_error is False assert result.content[0].text == expected @pytest.mark.asyncio @@ -652,7 +726,7 @@ class TestProxyMcpAuthorizationScope: result = await session.call_tool( "call_tool", {"tool_id": ids[f"{name}-request_headers"], "arguments": {}} ) - assert result.isError is False + assert result.is_error is False assert _payload(result) == expected @pytest.mark.asyncio @@ -660,7 +734,7 @@ class TestProxyMcpAuthorizationScope: async with _scoped_session(proxy_server_url, "sk-restricted") as session: tool_id = (await _search(session, "add"))["math_restricted-add"] result = await _call(session, tool_id, 123, 456) - assert result.isError is False and result.content[0].text == "779" + assert result.is_error is False and result.content[0].text == "779" async with asyncio.timeout(10): while True: payload = json.loads(await asyncio.to_thread(proxy_call_recorder.events.get, True, 5)) @@ -714,7 +788,7 @@ class TestProxyMcpAuthorizationScope: hits = _payload(await handle_mcp_proxy_tool("search_tools", {"query": "add"}, auth)) tool_id = next(hit["tool_id"] for hit in hits if hit["name"] == "math_stdio-add") result = await handle_mcp_proxy_tool("call_tool", {"tool_id": tool_id, "arguments": arguments}, auth) - assert result.isError is True + assert result.is_error is True assert result.content[0].text == "arguments must be an object" asyncio.run_coroutine_threadsafe(check(), _proxy_server.loop).result(timeout=30) diff --git a/tests/pass_through_tests/test_mcp_routes.py b/tests/pass_through_tests/test_mcp_routes.py index 687efe6195d..e9d18193e7c 100644 --- a/tests/pass_through_tests/test_mcp_routes.py +++ b/tests/pass_through_tests/test_mcp_routes.py @@ -2,14 +2,15 @@ import asyncio import os -from langchain_mcp_adapters.tools import load_mcp_tools -from langchain_openai import ChatOpenAI -from langgraph.prebuilt import create_react_agent from mcp import ClientSession from mcp.client.sse import sse_client async def main(): + from langchain_mcp_adapters.tools import load_mcp_tools + from langchain_openai import ChatOpenAI + from langgraph.prebuilt import create_react_agent + model = ChatOpenAI(model="gpt-4o", api_key="sk-12") async with sse_client(url="http://localhost:4000/mcp/") as (read, write): diff --git a/tests/test_litellm/experimental_mcp_client/test_mcp_client.py b/tests/test_litellm/experimental_mcp_client/test_mcp_client.py index d9ffb0d64fe..b78d61c7bd4 100644 --- a/tests/test_litellm/experimental_mcp_client/test_mcp_client.py +++ b/tests/test_litellm/experimental_mcp_client/test_mcp_client.py @@ -4,22 +4,20 @@ import json import os import sys from collections.abc import AsyncIterator -from importlib import metadata from pathlib import Path from typing import Final from unittest.mock import AsyncMock, MagicMock, patch import anyio -import httpx +import httpx2 import pytest -import respx -from litellm.proxy._experimental.mcp_server.outbound_credentials.httpx_auth import StaticHeaderAuth -from mcp import McpError +from mcp import MCPError from mcp.client.streamable_http import streamable_http_client -from pydantic import ValidationError from mcp.shared.message import SessionMessage from mcp.types import ( - LATEST_PROTOCOL_VERSION, + CONNECTION_CLOSED, + INTERNAL_ERROR, + REQUEST_TIMEOUT, CallToolResult, ErrorData, Implementation, @@ -30,17 +28,16 @@ from mcp.types import ( LoggingMessageNotificationParams, ServerCapabilities, ) +from mcp_types.version import LATEST_HANDSHAKE_VERSION +from pydantic import TypeAdapter, ValidationError # Add the parent directory to the path so we can import litellm - import litellm.experimental_mcp_client.client as mcp_client_module from litellm.experimental_mcp_client.client import ( - MCP_STREAMABLE_HTTP_REQUIREMENT, MCPClient, _first_non_cancelled_cause, _TransportContext, as_mcp_read_timeout, - missing_streamable_http_client_error, strip_auth_scheme, ) from litellm.proxy._experimental.mcp_server.faults.list_outcomes import ( @@ -50,8 +47,23 @@ from litellm.proxy._experimental.mcp_server.faults.list_outcomes import ( from litellm.proxy._experimental.mcp_server.mcp_server_manager import ( _format_byok_openapi_auth_header, ) -from litellm.types.mcp_server.mcp_server_manager import MCPServer +from litellm.proxy._experimental.mcp_server.outbound_credentials.httpx_auth import StaticHeaderAuth from litellm.types.mcp import MCPAuth, MCPStdioConfig, MCPTransport +from litellm.types.mcp_server.mcp_server_manager import MCPServer + +_JSONRPC_MESSAGE_ADAPTER: Final = TypeAdapter(JSONRPCMessage) + + +class _MockTransportClient(MCPClient): + """An MCPClient whose streamable-HTTP transport runs on an httpx2 MockTransport.""" + + def __init__(self, respond, **kwargs): + super().__init__(**kwargs) + self._respond = respond + + def _create_transport_context(self): + http_client = httpx2.AsyncClient(transport=httpx2.MockTransport(self._respond)) + return streamable_http_client(self.server_url, http_client=http_client), http_client class _FakeExceptionGroup(Exception): @@ -171,14 +183,14 @@ class TestMCPClient: call_kwargs = mock_streamable_http_client.call_args[1] assert "http_client" in call_kwargs http_client = call_kwargs["http_client"] - assert isinstance(http_client, httpx.AsyncClient) + assert isinstance(http_client, httpx2.AsyncClient) # Test the factory still creates a client with proper SSL config httpx_factory = client._create_httpx_client_factory() test_client = httpx_factory(headers={"test": "header"}) assert test_client is not None - assert isinstance(test_client, httpx.AsyncClient) + assert isinstance(test_client, httpx2.AsyncClient) assert test_client.headers is not None await test_client.aclose() @@ -228,7 +240,7 @@ class TestMCPClient: # Verify the client was created successfully assert test_client is not None - assert isinstance(test_client, httpx.AsyncClient) + assert isinstance(test_client, httpx2.AsyncClient) # Verify it has the expected properties assert test_client.headers is not None # Clean up @@ -272,13 +284,13 @@ class TestMCPClient: call_kwargs = mock_streamable_http_client.call_args[1] assert "http_client" in call_kwargs http_client = call_kwargs["http_client"] - assert isinstance(http_client, httpx.AsyncClient) + assert isinstance(http_client, httpx2.AsyncClient) httpx_factory = client._create_httpx_client_factory() test_client = httpx_factory(headers={"test": "header"}) assert test_client is not None - assert isinstance(test_client, httpx.AsyncClient) + assert isinstance(test_client, httpx2.AsyncClient) assert test_client.headers is not None await test_client.aclose() @@ -460,12 +472,12 @@ class TestFirstNonCancelledCause: assert _first_non_cancelled_cause(asyncio.CancelledError()) is None def test_unwraps_group_to_non_cancelled_leaf(self): - target = httpx.ConnectError("refused") + target = httpx2.ConnectError("refused") group = _FakeExceptionGroup("g", [asyncio.CancelledError(), target]) assert _first_non_cancelled_cause(group) is target def test_unwraps_nested_group(self): - target = httpx.LocalProtocolError("Illegal header value") + target = httpx2.LocalProtocolError("Illegal header value") inner = _FakeExceptionGroup("inner", [asyncio.CancelledError(), target]) outer = _FakeExceptionGroup("outer", [asyncio.CancelledError(), inner]) assert _first_non_cancelled_cause(outer) is target @@ -476,7 +488,7 @@ class TestFirstNonCancelledCause: @pytest.mark.skipif(sys.version_info < (3, 11), reason="builtin ExceptionGroup requires 3.11+") def test_unwraps_builtin_exception_group(self): - target = httpx.ConnectError("refused") + target = httpx2.ConnectError("refused") group = ExceptionGroup("transport failed", [target]) # noqa: F821 assert _first_non_cancelled_cause(group) is target @@ -512,13 +524,13 @@ class TestExecuteSessionOperationSurfacesTransportError: mock_session_cls, AsyncMock(side_effect=asyncio.CancelledError("cancelled by group")), ) - connect_error = httpx.ConnectError("All connection attempts failed") + connect_error = httpx2.ConnectError("All connection attempts failed") transport_ctx = self._make_transport(_FakeExceptionGroup("transport", [connect_error])) async def _op(session): return "done" - with pytest.raises(httpx.ConnectError): + with pytest.raises(httpx2.ConnectError): await client._execute_session_operation(transport_ctx, _op) @pytest.mark.asyncio @@ -541,7 +553,7 @@ class TestExecuteSessionOperationSurfacesTransportError: init_result = MagicMock() init_result.instructions = None self._make_session(mock_session_cls, AsyncMock(return_value=init_result)) - transport_ctx = self._make_transport(_FakeExceptionGroup("late", [httpx.ConnectError("late cleanup error")])) + transport_ctx = self._make_transport(_FakeExceptionGroup("late", [httpx2.ConnectError("late cleanup error")])) async def _op(session): return "done" @@ -551,11 +563,11 @@ class TestExecuteSessionOperationSurfacesTransportError: class TestMCPClientResolvedAuth: - """A pre-resolved httpx.Auth is attached to the upstream client's auth= slot.""" + """A pre-resolved httpx2.Auth is attached to the upstream client's auth= slot.""" @pytest.mark.asyncio async def test_resolved_auth_feeds_the_auth_slot(self): - resolved = httpx.Auth() + resolved = httpx2.Auth() client = MCPClient(server_url="https://upstream.example.com", resolved_auth=resolved) http_client = client._create_httpx_client_factory()() try: @@ -565,11 +577,11 @@ class TestMCPClientResolvedAuth: @pytest.mark.asyncio async def test_resolved_auth_takes_precedence_over_aws_auth(self): - resolved = httpx.Auth() + resolved = httpx2.Auth() client = MCPClient( server_url="https://upstream.example.com", resolved_auth=resolved, - aws_auth=httpx.Auth(), + aws_auth=httpx2.Auth(), ) http_client = client._create_httpx_client_factory()() try: @@ -579,7 +591,7 @@ class TestMCPClientResolvedAuth: @pytest.mark.asyncio async def test_without_resolved_auth_falls_back_to_aws_auth(self): - aws = httpx.Auth() + aws = httpx2.Auth() client = MCPClient(server_url="https://upstream.example.com", aws_auth=aws) http_client = client._create_httpx_client_factory()() try: @@ -672,7 +684,7 @@ async def test_call_tool_raise_on_error_logs_at_debug_not_error(): with patch.object(client, "run_with_session", side_effect=_raise): with patch.object(mcp_client_module, "verbose_logger") as mock_log: result = await client.call_tool(params, raise_on_error=False) - assert result.isError is True + assert result.is_error is True assert mock_log.error.called, "swallow path must keep error-level visibility" @@ -766,15 +778,15 @@ class _ScriptedUpstream: return await self._task_group.__aexit__(None, None, None) async def _send(self, message): - await self._to_client_tx.send(SessionMessage(JSONRPCMessage(message))) + await self._to_client_tx.send(SessionMessage(message)) async def _serve(self): async for session_message in self._from_client_rx: - request = session_message.message.root + request = session_message.message method = getattr(request, "method", None) if method == "initialize": result = InitializeResult( - protocolVersion=LATEST_PROTOCOL_VERSION, + protocolVersion=LATEST_HANDSHAKE_VERSION, capabilities=ServerCapabilities(), serverInfo=Implementation(name="scripted-upstream", version="1.0.0"), ) @@ -835,36 +847,36 @@ async def test_upstream_json_rpc_error_408_is_not_reported_as_a_client_timeout() """The SDK reports its own elapsed read timeout and relays an upstream JSON-RPC error through the same exception class and the same numeric field, and JSON-RPC error codes are a different namespace from HTTP status codes. An upstream answering with application code 408 must keep - travelling as ``McpError`` so it is never blamed on the gateway as a 504. + travelling as ``MCPError`` so it is never blamed on the gateway as a 504. This is the other half of the pair: the same real transport and the same real session, so one mechanism pins both directions. """ client = _ScriptedClient( timeout=30, - tools_list_error=ErrorData(code=int(httpx.codes.REQUEST_TIMEOUT), message="re-authenticate and retry"), + tools_list_error=ErrorData(code=REQUEST_TIMEOUT, message="re-authenticate and retry"), ) - with pytest.raises(McpError) as exc_info: + with pytest.raises(MCPError) as exc_info: await asyncio.wait_for(client.list_tools(raise_on_error=True), timeout=10) assert not isinstance(exc_info.value, TimeoutError), "an upstream application error is not a gateway timeout" - assert exc_info.value.error.code == int(httpx.codes.REQUEST_TIMEOUT) + assert exc_info.value.error.code == REQUEST_TIMEOUT fault = classify_list_exception(exc_info.value) assert fault.tag != "timeout", "an upstream's own application error must never be reported as a gateway timeout" assert list_fault_http_status(fault) != 504 -def _raise_mcp_error_while_handling_a_timeout(code: int, message: str) -> McpError: - """An ``McpError`` carrying the context chain it would have if it were raised while a +def _raise_mcp_error_while_handling_a_timeout(code: int, message: str) -> MCPError: + """An ``MCPError`` carrying the context chain it would have if it were raised while a ``TimeoutError`` was in flight, which is how the SDK raises its own read timeout.""" try: try: raise TimeoutError() except TimeoutError: - raise McpError(ErrorData(code=code, message=message)) - except McpError as raised: + raise MCPError(code=code, message=message) + except MCPError as raised: return raised @@ -873,20 +885,20 @@ def test_as_mcp_read_timeout_separates_the_sdk_timeout_from_a_relayed_upstream_e upstream JSON-RPC error that happens to use 408, and the context chain alone cannot separate it from any other relayed error that surfaces while a timeout is being handled, so both must hold. """ - timeout_code = int(httpx.codes.REQUEST_TIMEOUT) + timeout_code = REQUEST_TIMEOUT translated = as_mcp_read_timeout(_raise_mcp_error_while_handling_a_timeout(timeout_code, "Timed out while waiting")) assert isinstance(translated, TimeoutError) assert str(translated) == "Timed out while waiting" - relayed_408 = McpError(ErrorData(code=timeout_code, message="upstream said 408")) + relayed_408 = MCPError(code=timeout_code, message="upstream said 408") assert as_mcp_read_timeout(relayed_408) is None, "an upstream 408 with no elapsed timeout is not our timeout" relayed_other = _raise_mcp_error_while_handling_a_timeout(-32603, "upstream internal error") assert as_mcp_read_timeout(relayed_other) is None, "a non-timeout code is not our timeout, whatever the chain" - assert as_mcp_read_timeout(McpError(ErrorData(code=-32603, message="boom"))) is None - assert as_mcp_read_timeout(RuntimeError("not an McpError")) is None + assert as_mcp_read_timeout(MCPError(code=-32603, message="boom")) is None + assert as_mcp_read_timeout(RuntimeError("not an MCPError")) is None @pytest.mark.asyncio @@ -1065,28 +1077,6 @@ def test_openapi_byok_auth_header_emits_exactly_one_scheme(auth_type, auth_value assert _format_byok_openapi_auth_header(server, auth_value) == expected -def test_missing_streamable_http_client_error_names_requirement_and_remedy(): - message = str(missing_streamable_http_client_error()) - - assert MCP_STREAMABLE_HTTP_REQUIREMENT in message - assert "pip install 'litellm[mcp]'" in message - assert metadata.version("mcp") in message - - -@pytest.mark.asyncio -async def test_http_transport_without_streamable_http_client_raises_actionable_import_error(): - client = MCPClient( - server_url="https://mcp-server.example.com", - transport_type=MCPTransport.http, - ) - - with patch.object( # test-quality-ok: simulates mcp<1.24.0 whose module lacks this import-time symbol - mcp_client_module, "streamable_http_client", None - ): - with pytest.raises(ImportError, match=r"pip install 'litellm\[mcp\]'"): - await client.list_tools(raise_on_error=True) - - def test_mcp_extra_matches_proxy_extra_and_supports_streamable_http(): try: import tomllib @@ -1096,17 +1086,50 @@ def test_mcp_extra_matches_proxy_extra_and_supports_streamable_http(): pyproject_path = Path(__file__).parents[3] / "pyproject.toml" with pyproject_path.open("rb") as f: - extras = tomllib.load(f)["project"]["optional-dependencies"] + project = tomllib.load(f) + extras = project["project"]["optional-dependencies"] - mcp_extra = extras["mcp"] - assert len(mcp_extra) == 1 + sdk2_names: Final = frozenset(("mcp", "httpx2", "pydantic")) + mcp_extra: Final = {Requirement(req).name: req for req in extras["mcp"]} + assert mcp_extra == { + name: req + for req in extras["proxy"] + if (name := Requirement(req).name) in sdk2_names + } - proxy_mcp_requirements = [req for req in extras["proxy"] if Requirement(req).name == "mcp"] - assert mcp_extra == proxy_mcp_requirements + specifier: Final = Requirement(mcp_extra["mcp"]).specifier + assert not specifier.contains("1.28.1") + assert specifier.contains("2.2.0") + with (pyproject_path.parent / "uv.lock").open("rb") as f: + locked = tomllib.load(f) + mcp_versions: Final = [package["version"] for package in locked["package"] if package["name"] == "mcp"] + assert len(mcp_versions) == 1 + assert specifier.contains(mcp_versions[0]) - specifier = Requirement(mcp_extra[0]).specifier - assert not specifier.contains("1.23.0") - assert specifier.contains("1.28.1") + +@pytest.mark.parametrize("module", ["mcp", "mcp_types", "httpx2", "httpcore2"]) +def test_base_sdk_guard_rejects_mcp_dependencies(tmp_path: Path, module: str) -> None: + import subprocess + import sys + + (tmp_path / f"{module}.py").write_text("") + checker = Path(__file__).parents[2] / "base_sdk_tests" / "check_base_sdk_install.py" + result = subprocess.run( + [ + sys.executable, + "-S", + "-c", + "import runpy, sys; sys.path.insert(0, sys.argv[2]); " + "runpy.run_path(sys.argv[1])['check_environment_is_base_only']()", + str(checker), + str(tmp_path), + ], + capture_output=True, + text=True, + check=False, + ) + assert result.returncode != 0, f"base-only guard accepted installed {module}" + assert f"{module} installed" in result.stderr @pytest.mark.parametrize( @@ -1161,13 +1184,13 @@ async def test_a_custom_credential_header_is_stripped_when_a_redirect_crosses_or operator moved to its own slot would be replayed to whatever host the upstream redirects to. Verified against real httpx redirect handling, not a hand-built request. """ - seen: "list[tuple[str, str]]" = [] + seen: list[tuple[str, str]] = [] - def handler(request: httpx.Request) -> httpx.Response: + def handler(request: httpx2.Request) -> httpx2.Response: seen.append((request.url.host, request.headers.get("esb-oauth", ""))) if request.url.host == "upstream.example.com": - return httpx.Response(302, headers={"Location": "https://attacker.example.com/collect"}) - return httpx.Response(200) + return httpx2.Response(302, headers={"Location": "https://attacker.example.com/collect"}) + return httpx2.Response(200) client = MCPClient( server_url="https://upstream.example.com/mcp", @@ -1177,7 +1200,7 @@ async def test_a_custom_credential_header_is_stripped_when_a_redirect_crosses_or client.update_auth_value("minted-token") factory = client._create_httpx_client_factory() async with factory(headers=client._get_auth_headers(), timeout=None) as http_client: - http_client._transport = httpx.MockTransport(handler) + http_client._transport = httpx2.MockTransport(handler) await http_client.get("https://upstream.example.com/mcp") assert seen[0] == ("upstream.example.com", "Bearer minted-token") @@ -1253,9 +1276,9 @@ async def test_the_guard_agrees_with_httpx_about_authorization(start: str, targe outcomes. A future httpx that changes its redirect rule reds here instead of silently leaving the custom slot forwarded where Authorization is not (or stripped where it is not needed). """ - seen: "list[tuple[str, str, str]]" = [] + seen: list[tuple[str, str, str]] = [] - def handler(request: httpx.Request) -> httpx.Response: + def handler(request: httpx2.Request) -> httpx2.Response: seen.append( ( str(request.url), @@ -1264,13 +1287,13 @@ async def test_the_guard_agrees_with_httpx_about_authorization(start: str, targe ) ) if str(request.url) == start: - return httpx.Response(302, headers={"Location": target}) - return httpx.Response(200) + return httpx2.Response(302, headers={"Location": target}) + return httpx2.Response(200) client = MCPClient(server_url=start, auth_type=MCPAuth.oauth2, auth_header_name="esb-oauth") factory = client._create_httpx_client_factory() async with factory(headers={"Authorization": "Bearer AUTH", "esb-oauth": "Bearer ESB"}, timeout=None) as http: - http._transport = httpx.MockTransport(handler) + http._transport = httpx2.MockTransport(handler) await http.get(start) _url, authorization, esb = seen[-1] @@ -1298,11 +1321,12 @@ def test_a_differently_cased_injected_header_cannot_shadow_the_slot() -> None: @pytest.mark.parametrize( ("content_type", "body", "expected_type"), [ - ("text/html", b"secret-page", ValueError), - ("application/json", b"secret-invalid-json", ValidationError), - ("application/json", b"", ValidationError), - ("application/json", b'{"secret":"invalid-rpc"}', ValidationError), - ("application/json", b'{"jsonrpc":"2.0","id":0,"result":{"secret":"invalid-schema"}}', ValidationError), + ("text/html", b"secret-page", MCPError), + ("application/json", b"secret-invalid-json", MCPError), + ("application/json", b"", MCPError), + ("application/json", b'{"secret":"invalid-rpc"}', MCPError), + ("application/json", b'{"jsonrpc":"2.0","id":0}', MCPError), + ("application/json", b'{"jsonrpc":"2.0","id":0,"result":{"secret":"bad-schema"}}', ValidationError), ], ) async def test_invalid_http_response_surfaces_without_waiting_for_timeout( @@ -1310,10 +1334,12 @@ async def test_invalid_http_response_surfaces_without_waiting_for_timeout( ) -> None: from litellm.proxy._experimental.mcp_server.rest_endpoints import _connection_error_message - def respond(request: httpx.Request) -> httpx.Response: - return httpx.Response(200, headers={"Content-Type": content_type}, content=body) + def respond(request: httpx2.Request) -> httpx2.Response: + if expected_type is ValidationError: + return httpx2.Response(200, json={**json.loads(body), "id": json.loads(request.content)["id"]}) + return httpx2.Response(200, headers={"Content-Type": content_type}, content=body) - async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as http_client: + async with httpx2.AsyncClient(transport=httpx2.MockTransport(respond)) as http_client: client: Final = MCPClient(server_url="https://example.com/mcp", timeout=30) with pytest.raises(expected_type) as caught: await asyncio.wait_for( @@ -1331,27 +1357,27 @@ async def test_invalid_http_response_surfaces_without_waiting_for_timeout( @pytest.mark.asyncio -@pytest.mark.parametrize("status_code", [200, 401, 503]) +@pytest.mark.parametrize("status_code", [200, 401, 403, 429, 503]) async def test_http_response_handler_preserves_success_and_http_errors(status_code: int) -> None: - def respond(request: httpx.Request) -> httpx.Response: + def respond(request: httpx2.Request) -> httpx2.Response: if request.method == "DELETE": - return httpx.Response(200) + return httpx2.Response(200) payload: Final = json.loads(request.content) if "id" not in payload: - return httpx.Response(202) + return httpx2.Response(202) result: Final = ( { - "protocolVersion": LATEST_PROTOCOL_VERSION, + "protocolVersion": payload["params"]["protocolVersion"], "capabilities": {}, "serverInfo": {"name": "test", "version": "1"}, } if payload["method"] == "initialize" else {"tools": []} ) - return httpx.Response(status_code, json={"jsonrpc": "2.0", "id": payload["id"], "result": result}) + return httpx2.Response(status_code, json={"jsonrpc": "2.0", "id": payload["id"], "result": result}) - async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as http_client: - client: Final = MCPClient(server_url="https://example.com/mcp", timeout=30) + client: Final = MCPClient(server_url="https://example.com/mcp", timeout=30) + async with client._create_httpx_client_factory(transport=httpx2.MockTransport(respond))() as http_client: operation: Final = client._execute_session_operation( streamable_http_client(client.server_url, http_client=http_client), lambda session: session.list_tools() ) @@ -1359,11 +1385,35 @@ async def test_http_response_handler_preserves_success_and_http_errors(status_co result: Final = await asyncio.wait_for(operation, timeout=3) assert result.tools == [] else: - with pytest.raises(httpx.HTTPStatusError) as caught: + with pytest.raises(httpx2.HTTPStatusError) as caught: await asyncio.wait_for(operation, timeout=3) assert caught.value.response.status_code == status_code +@pytest.mark.asyncio +async def test_http_status_check_allows_auth_refresh_before_rejecting() -> None: + from litellm.proxy._experimental.mcp_server.outbound_credentials.client_credentials import ClientCredentialsBearerAuth + + seen = [] + + async def refresh(failed): + assert failed == "stale" + return "fresh" + + def respond(request): + seen.append(request.headers["authorization"]) + return httpx2.Response(401 if len(seen) == 1 else 200, json={"ok": True}) + + from litellm.proxy._experimental.mcp_server.outbound_credentials.types import ClientCredentialsConfig + + auth = ClientCredentialsBearerAuth("stale", refresh, ClientCredentialsConfig()) + client = MCPClient(server_url="https://example.com/mcp", resolved_auth=auth) + async with client._create_httpx_client_factory(transport=httpx2.MockTransport(respond))() as http_client: + response = await http_client.post(client.server_url, json={"method": "tools/list"}) + assert response.status_code == 200 + assert seen == ["Bearer stale", "Bearer fresh"] + + @pytest.mark.asyncio async def test_http_response_handler_preserves_notifications_and_tool_listing() -> None: notification: Final = { @@ -1373,20 +1423,20 @@ async def test_http_response_handler_preserves_notifications_and_tool_listing() } logging_callback: Final = AsyncMock() - def respond(request: httpx.Request) -> httpx.Response: + def respond(request: httpx2.Request) -> httpx2.Response: if request.method == "DELETE": - return httpx.Response(200) + return httpx2.Response(200) payload: Final = json.loads(request.content) if "id" not in payload: - return httpx.Response(202) + return httpx2.Response(202) if payload["method"] == "initialize": - return httpx.Response( + return httpx2.Response( 200, json={ "jsonrpc": "2.0", "id": payload["id"], "result": { - "protocolVersion": LATEST_PROTOCOL_VERSION, + "protocolVersion": payload["params"]["protocolVersion"], "capabilities": {"logging": {}, "tools": {}}, "serverInfo": {"name": "test", "version": "1"}, }, @@ -1397,13 +1447,13 @@ async def test_http_response_handler_preserves_notifications_and_tool_listing() "id": payload["id"], "result": {"tools": [{"name": "search", "inputSchema": {"type": "object"}}]}, } - return httpx.Response( + return httpx2.Response( 200, headers={"Content-Type": "text/event-stream"}, content="".join(f"event: message\ndata: {json.dumps(message)}\n\n" for message in (notification, response)), ) - async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as http_client: + async with httpx2.AsyncClient(transport=httpx2.MockTransport(respond)) as http_client: client: Final = MCPClient(server_url="https://example.com/mcp", timeout=30, logging_callback=logging_callback) result: Final = await asyncio.wait_for( client._execute_session_operation( @@ -1420,24 +1470,24 @@ async def test_http_response_handler_preserves_notifications_and_tool_listing() async def test_invalid_tool_list_schema_is_identified_as_an_upstream_response() -> None: from litellm.proxy._experimental.mcp_server.rest_endpoints import _connection_error_message - def respond(request: httpx.Request) -> httpx.Response: + def respond(request: httpx2.Request) -> httpx2.Response: if request.method == "DELETE": - return httpx.Response(200) + return httpx2.Response(200) payload: Final = json.loads(request.content) if "id" not in payload: - return httpx.Response(202) + return httpx2.Response(202) result: Final = ( { - "protocolVersion": LATEST_PROTOCOL_VERSION, + "protocolVersion": payload["params"]["protocolVersion"], "capabilities": {}, "serverInfo": {"name": "test", "version": "1"}, } if payload["method"] == "initialize" else {"tools": "secret-invalid-tools"} ) - return httpx.Response(200, json={"jsonrpc": "2.0", "id": payload["id"], "result": result}) + return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload["id"], "result": result}) - async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as http_client: + async with httpx2.AsyncClient(transport=httpx2.MockTransport(respond)) as http_client: client: Final = MCPClient(server_url="https://example.com/mcp", timeout=30) with pytest.raises(ValidationError) as caught: await asyncio.wait_for( @@ -1453,7 +1503,7 @@ async def test_invalid_tool_list_schema_is_identified_as_an_upstream_response() assert "secret" not in message -class _DiagnosticSSEStream(httpx.AsyncByteStream): +class _DiagnosticSSEStream(httpx2.AsyncByteStream): def __init__(self, messages: asyncio.Queue[bytes | Exception | None]) -> None: self.messages = messages @@ -1510,26 +1560,26 @@ def _diagnostic_transport(transport: MCPTransport, mode: str, failure_method: st ) messages: Final[asyncio.Queue[bytes | Exception | None]] = asyncio.Queue() - async def respond(request: httpx.Request) -> httpx.Response: + async def respond(request: httpx2.Request) -> httpx2.Response: if request.method == "GET": - return httpx.Response( + return httpx2.Response( 200, headers={"Content-Type": "text/event-stream"}, stream=_DiagnosticSSEStream(messages) ) payload: Final = json.loads(request.content) if "method" not in payload or "id" not in payload: - return httpx.Response(202) + return httpx2.Response(202) if payload["method"] == failure_method and mode != "ok": if mode == "bad-json": await messages.put(b"secret-invalid-json") elif mode == "io-error": - await messages.put(httpx.ReadError("secret-read-error")) + await messages.put(httpx2.ReadError("secret-read-error")) elif mode == "closed": await messages.put(None) elif mode == "silent": await messages.put( b'{"jsonrpc":"2.0","method":"notifications/message","params":{"level":"info","data":"Waiting"}}' ) - return httpx.Response(202) + return httpx2.Response(202) if payload["method"] == "tools/list": for message in ( { @@ -1543,7 +1593,7 @@ def _diagnostic_transport(transport: MCPTransport, mode: str, failure_method: st await messages.put(json.dumps(message).encode()) result: Final = ( { - "protocolVersion": LATEST_PROTOCOL_VERSION, + "protocolVersion": payload["params"]["protocolVersion"], "capabilities": {"tools": {}, "logging": {}}, "serverInfo": {"name": "diagnostic", "version": "1"}, } @@ -1553,14 +1603,14 @@ def _diagnostic_transport(transport: MCPTransport, mode: str, failure_method: st else {"content": [{"type": "text", "text": "pong"}], "isError": False} ) await messages.put(json.dumps({"jsonrpc": "2.0", "id": payload["id"], "result": result}).encode()) - return httpx.Response(202) + return httpx2.Response(202) def factory( headers: dict[str, str] | None = None, - timeout: httpx.Timeout | None = None, - auth: httpx.Auth | None = None, - ) -> httpx.AsyncClient: - return httpx.AsyncClient(transport=httpx.MockTransport(respond), headers=headers, timeout=timeout, auth=auth) + timeout: httpx2.Timeout | None = None, + auth: httpx2.Auth | None = None, + ) -> httpx2.AsyncClient: + return httpx2.AsyncClient(transport=httpx2.MockTransport(respond), headers=headers, timeout=timeout, auth=auth) return sse_client("https://example.com/sse", httpx_client_factory=factory) @@ -1582,7 +1632,7 @@ async def test_transport_parsing_failure_is_preserved(transport: MCPTransport, f @pytest.mark.asyncio async def test_sse_read_failure_is_preserved() -> None: client: Final = MCPClient(server_url="https://example.com/sse", transport_type=MCPTransport.sse, timeout=0.2) - with pytest.raises(httpx.ReadError, match="secret-read-error"): + with pytest.raises(httpx2.ReadError, match="secret-read-error"): await asyncio.wait_for( client._execute_session_operation( _diagnostic_transport(MCPTransport.sse, "io-error", "tools/list"), lambda session: session.list_tools() @@ -1611,11 +1661,11 @@ async def test_transport_completion_and_normal_messages(transport: MCPTransport, pending: Final = client._execute_session_operation(_diagnostic_transport(transport, mode, "tools/list"), operation) if mode == "ok": result: Final = await asyncio.wait_for(pending, timeout=3) - assert result.isError is False + assert result.is_error is False assert result.content[0].text == "pong" logging_callback.assert_awaited_once_with(LoggingMessageNotificationParams(level="info", data="Listing tools")) else: - with pytest.raises(McpError) as caught: + with pytest.raises(MCPError) as caught: await asyncio.wait_for(pending, timeout=3) if mode == "closed": assert "connection was closed" in _connection_error_message(caught.value, client.server_url, 0.2) @@ -1648,20 +1698,20 @@ async def test_transport_cancellation_cleans_up_a_pending_request(transport: MCP await asyncio.wait_for(task, timeout=3) -class _InterruptedHTTPBody(httpx.AsyncByteStream): +class _InterruptedHTTPBody(httpx2.AsyncByteStream): async def __aiter__(self) -> AsyncIterator[bytes]: yield b'{"jsonrpc":' - raise httpx.RemoteProtocolError("secret-incomplete-response") + raise httpx2.RemoteProtocolError("secret-incomplete-response") @pytest.mark.asyncio async def test_interrupted_http_response_preserves_the_transport_failure() -> None: - def respond(request: httpx.Request) -> httpx.Response: - return httpx.Response(200, headers={"Content-Type": "application/json"}, stream=_InterruptedHTTPBody()) + def respond(request: httpx2.Request) -> httpx2.Response: + return httpx2.Response(200, headers={"Content-Type": "application/json"}, stream=_InterruptedHTTPBody()) - async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as http_client: + async with httpx2.AsyncClient(transport=httpx2.MockTransport(respond)) as http_client: client: Final = MCPClient(server_url="https://example.com/mcp", timeout=30) - with pytest.raises(httpx.RemoteProtocolError, match="secret-incomplete-response"): + with pytest.raises(httpx2.RemoteProtocolError, match="secret-incomplete-response"): await asyncio.wait_for( client._execute_session_operation( streamable_http_client(client.server_url, http_client=http_client), @@ -1673,12 +1723,12 @@ async def test_interrupted_http_response_preserves_the_transport_failure() -> No @pytest.mark.asyncio async def test_empty_http_event_stream_uses_the_existing_request_deadline() -> None: - def respond(request: httpx.Request) -> httpx.Response: - return httpx.Response(200, headers={"Content-Type": "text/event-stream"}, content=b"") + def respond(request: httpx2.Request) -> httpx2.Response: + return httpx2.Response(200, headers={"Content-Type": "text/event-stream"}, content=b"") - async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as http_client: + async with httpx2.AsyncClient(transport=httpx2.MockTransport(respond)) as http_client: client: Final = MCPClient(server_url="https://example.com/mcp", timeout=0.2) - with pytest.raises(McpError) as caught: + with pytest.raises(MCPError) as caught: await asyncio.wait_for( client._execute_session_operation( streamable_http_client(client.server_url, http_client=http_client), @@ -1686,7 +1736,8 @@ async def test_empty_http_event_stream_uses_the_existing_request_deadline() -> N ), timeout=3, ) - assert isinstance(as_mcp_read_timeout(caught.value), TimeoutError) + assert caught.value.error.code == CONNECTION_CLOSED + assert "SSE stream ended" in caught.value.error.message @pytest.mark.asyncio @@ -1726,14 +1777,14 @@ async def test_optional_discovery_capabilities_and_errors( "resources/templates/list": {"name": "example", "uriTemplate": "test://{name}"}, }[method] - def respond(request: httpx.Request) -> httpx.Response: + def respond(request: httpx2.Request) -> httpx2.Response: if request.method == "DELETE": - return httpx.Response(200) - payload: Final = JSONRPCMessage.model_validate_json(request.content).root + return httpx2.Response(200) + payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content) if not isinstance(payload, JSONRPCRequest): - return httpx.Response(202) + return httpx2.Response(202) if outcome == "initialize_not_found": - return httpx.Response( + return httpx2.Response( 200, json={ "jsonrpc": "2.0", @@ -1742,13 +1793,13 @@ async def test_optional_discovery_capabilities_and_errors( }, ) if payload.method == "initialize": - return httpx.Response( + return httpx2.Response( 200, json={ "jsonrpc": "2.0", "id": payload.id, "result": { - "protocolVersion": LATEST_PROTOCOL_VERSION, + "protocolVersion": payload.params["protocolVersion"], "capabilities": {} if outcome == "absent" else {advertised if outcome == "other_capability" else capability: {}}, @@ -1757,11 +1808,11 @@ async def test_optional_discovery_capabilities_and_errors( }, ) if outcome == "timeout": - raise httpx.ReadTimeout("Optional list timed out", request=request) + raise httpx2.ReadTimeout("Optional list timed out", request=request) if outcome == "unauthorized": - return httpx.Response(401) + return httpx2.Response(401) if outcome in ("method_not_found", "internal_error", "absent", "other_capability"): - return httpx.Response( + return httpx2.Response( 200, json={ "jsonrpc": "2.0", @@ -1772,26 +1823,24 @@ async def test_optional_discovery_capabilities_and_errors( }, }, ) - return httpx.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": {field: [entry]}}) + return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": {field: [entry]}}) responder: Final = Mock(side_effect=respond) caplog.set_level(logging.DEBUG, logger="LiteLLM") - with respx.mock(base_url="https://example.com") as router: - router.route().mock(side_effect=responder) - client: Final = MCPClient(server_url="https://example.com/mcp") - operation: Final = { - "prompts/list": client.list_prompts, - "resources/list": client.list_resources, - "resources/templates/list": client.list_resource_templates, - }[method] - if raise_on_error and outcome in ("internal_error", "unauthorized", "timeout", "initialize_not_found"): - with pytest.raises((McpError, httpx.HTTPError)): - await operation(raise_on_error=True) - return - result: Final = await operation(raise_on_error=raise_on_error) + client: Final = _MockTransportClient(responder, server_url="https://example.com/mcp") + operation: Final = { + "prompts/list": client.list_prompts, + "resources/list": client.list_resources, + "resources/templates/list": client.list_resource_templates, + }[method] + if raise_on_error and outcome in ("internal_error", "unauthorized", "timeout", "initialize_not_found"): + with pytest.raises((MCPError, httpx2.HTTPError)): + await operation(raise_on_error=True) + return + result: Final = await operation(raise_on_error=raise_on_error) requests: Final = tuple( - JSONRPCMessage.model_validate_json(call.args[0].content).root + _JSONRPC_MESSAGE_ADAPTER.validate_json(call.args[0].content) for call in responder.call_args_list if call.args[0].method == "POST" ) @@ -1816,38 +1865,37 @@ async def test_optional_discovery_capabilities_and_errors( @pytest.mark.parametrize("supports_first", (True, False)) async def test_optional_discovery_uses_each_sessions_capabilities(supports_first: bool) -> None: from unittest.mock import Mock + from mcp.types import JSONRPCRequest capabilities: Final = iter(({"resources": {}}, {}) if supports_first else ({}, {"resources": {}})) - def respond(request: httpx.Request) -> httpx.Response: + def respond(request: httpx2.Request) -> httpx2.Response: if request.method == "DELETE": - return httpx.Response(200) - payload: Final = JSONRPCMessage.model_validate_json(request.content).root + return httpx2.Response(200) + payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content) if not isinstance(payload, JSONRPCRequest): - return httpx.Response(202) + return httpx2.Response(202) result: Final = ( { - "protocolVersion": LATEST_PROTOCOL_VERSION, + "protocolVersion": payload.params["protocolVersion"], "capabilities": next(capabilities), "serverInfo": {"name": "changing", "version": "1"}, } if payload.method == "initialize" else {"resources": [{"name": "example", "uri": "test://example"}]} ) - return httpx.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": result}) + return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": result}) responder: Final = Mock(side_effect=respond) - with respx.mock(base_url="https://example.com") as router: - router.route().mock(side_effect=responder) - client: Final = MCPClient(server_url="https://example.com/mcp") - first: Final = await client.list_resources() - second: Final = await client.list_resources() + client: Final = _MockTransportClient(responder, server_url="https://example.com/mcp") + first: Final = await client.list_resources() + second: Final = await client.list_resources() assert [item.name for item in first] == (["example"] if supports_first else []) assert [item.name for item in second] == ([] if supports_first else ["example"]) requests: Final = tuple( - JSONRPCMessage.model_validate_json(call.args[0].content).root + _JSONRPC_MESSAGE_ADAPTER.validate_json(call.args[0].content) for call in responder.call_args_list if call.args[0].method == "POST" ) @@ -1862,20 +1910,20 @@ async def test_optional_discovery_preserves_cancellation(method: str) -> None: ready: Final = asyncio.Event() pending: Final = asyncio.Event() - async def respond(request: httpx.Request) -> httpx.Response: + async def respond(request: httpx2.Request) -> httpx2.Response: if request.method == "DELETE": - return httpx.Response(200) - payload: Final = JSONRPCMessage.model_validate_json(request.content).root + return httpx2.Response(200) + payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content) if not isinstance(payload, JSONRPCRequest): - return httpx.Response(202) + return httpx2.Response(202) if payload.method == "initialize": - return httpx.Response( + return httpx2.Response( 200, json={ "jsonrpc": "2.0", "id": payload.id, "result": { - "protocolVersion": LATEST_PROTOCOL_VERSION, + "protocolVersion": payload.params["protocolVersion"], "capabilities": {"resources": {}, "prompts": {}}, "serverInfo": {"name": "pending", "version": "1"}, }, @@ -1883,23 +1931,21 @@ async def test_optional_discovery_preserves_cancellation(method: str) -> None: ) ready.set() await pending.wait() - return httpx.Response(202) + return httpx2.Response(202) - with respx.mock(base_url="https://example.com") as router: - router.route().mock(side_effect=respond) - client: Final = MCPClient(server_url="https://example.com/mcp") - operation: Final = { - "prompts/list": client.list_prompts, - "resources/list": client.list_resources, - "resources/templates/list": client.list_resource_templates, - }[method] - task: Final = asyncio.create_task(operation()) - try: - await asyncio.wait_for(ready.wait(), timeout=3) - finally: - task.cancel() - with pytest.raises(asyncio.CancelledError): - await asyncio.wait_for(task, timeout=3) + client: Final = _MockTransportClient(respond, server_url="https://example.com/mcp") + operation: Final = { + "prompts/list": client.list_prompts, + "resources/list": client.list_resources, + "resources/templates/list": client.list_resource_templates, + }[method] + task: Final = asyncio.create_task(operation()) + try: + await asyncio.wait_for(ready.wait(), timeout=3) + finally: + task.cancel() + with pytest.raises(asyncio.CancelledError): + await asyncio.wait_for(task, timeout=3) @@ -1949,3 +1995,63 @@ async def test_request_auth_preview_uses_the_same_effective_headers_as_egress() assert str(request.url) == "https://upstream.example/mcp" assert request.headers["Authorization"] == "Bearer resolved" assert request.headers["X-Trace"] == "trace" + + +@pytest.mark.asyncio +@pytest.mark.parametrize("rpc_error", [False, True]) +async def test_expired_session_preserves_sdk_error_and_next_operation_reinitializes(rpc_error: bool) -> None: + from mcp.types import INVALID_REQUEST, METHOD_NOT_FOUND + + requests = [] + + def respond(request: httpx2.Request) -> httpx2.Response: + if request.method != "POST": + return httpx2.Response(405) + payload = json.loads(request.content) + if "id" not in payload: + return httpx2.Response(202) + requests.append((payload["method"], request.headers.get("mcp-session-id"))) + if payload["method"] == "initialize": + return httpx2.Response(200, headers={"mcp-session-id": f"session-{len(requests)}"}, json={ + "jsonrpc": "2.0", "id": payload["id"], "result": { + "protocolVersion": "2025-06-18", "capabilities": {}, + "serverInfo": {"name": "expiry-test", "version": "1"}, + }, + }) + if len(requests) == 2: + if rpc_error: + return httpx2.Response(404, json={ + "jsonrpc": "2.0", "id": payload["id"], + "error": {"code": METHOD_NOT_FOUND, "message": "Tool catalog unavailable"}, + }) + return httpx2.Response(404) + return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload["id"], "result": {"tools": []}}) + + client = MCPClient(server_url="https://example.com/mcp", timeout=3) + async with client._create_httpx_client_factory(transport=httpx2.MockTransport(respond))() as http_client: + with pytest.raises(MCPError) as caught: + await client._execute_session_operation( + streamable_http_client(client.server_url, http_client=http_client), lambda session: session.list_tools() + ) + assert caught.value.error.code == (METHOD_NOT_FOUND if rpc_error else INVALID_REQUEST) + assert caught.value.error.message == ("Tool catalog unavailable" if rpc_error else "Session terminated") + result = await client._execute_session_operation( + streamable_http_client(client.server_url, http_client=http_client), lambda session: session.list_tools() + ) + assert result.tools == [] + assert requests == [("initialize", None), ("tools/list", "session-1"), ("initialize", None), ("tools/list", "session-3")] + + +@pytest.mark.asyncio +async def test_404_before_session_initialization_preserves_method_not_found() -> None: + from mcp.types import METHOD_NOT_FOUND + + client = MCPClient(server_url="https://example.com/mcp", timeout=3) + transport = httpx2.MockTransport(lambda request: httpx2.Response(404)) + async with client._create_httpx_client_factory(transport=transport)() as http_client: + with pytest.raises(MCPError) as caught: + await client._execute_session_operation( + streamable_http_client(client.server_url, http_client=http_client), lambda session: session.list_tools() + ) + assert caught.value.error.code == METHOD_NOT_FOUND + assert caught.value.error.message == "Not Found" diff --git a/tests/test_litellm/integrations/arize/test_arize_utils.py b/tests/test_litellm/integrations/arize/test_arize_utils.py index 50f2823d632..167b083e147 100644 --- a/tests/test_litellm/integrations/arize/test_arize_utils.py +++ b/tests/test_litellm/integrations/arize/test_arize_utils.py @@ -1235,7 +1235,7 @@ def test_arize_coerce_response_obj_dumps_pydantic_without_get(): coerced = _coerce_response_obj_for_attrs(result) assert isinstance(coerced, dict) - assert coerced["isError"] is False + assert coerced["is_error"] is False assert coerced["content"][0]["text"] == "hi" diff --git a/tests/test_litellm/llms/litellm_proxy/skills/test_skill_search.py b/tests/test_litellm/llms/litellm_proxy/skills/test_skill_search.py index a0f22a59f0c..3f1fe0d5d68 100644 --- a/tests/test_litellm/llms/litellm_proxy/skills/test_skill_search.py +++ b/tests/test_litellm/llms/litellm_proxy/skills/test_skill_search.py @@ -420,7 +420,7 @@ class TestHandleSkillSearchMCP: result = await handle_skill_search( query="language translation", top_k=10_000, user_api_key_dict=UserAPIKeyAuth(user_id="u") ) - assert result.isError is False + assert result.is_error is False assert len(json.loads(result.content[0].text)) == MAX_SKILL_SEARCH_TOP_K @pytest.mark.asyncio @@ -432,5 +432,5 @@ class TestHandleSkillSearchMCP: result = await handle_skill_search( query="language translation", top_k=0, user_api_key_dict=UserAPIKeyAuth(user_id="u") ) - assert result.isError is False + assert result.is_error is False assert len(json.loads(result.content[0].text)) == 1 diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/conftest.py b/tests/test_litellm/proxy/_experimental/mcp_server/conftest.py index 9a66f130d24..76e92efd31a 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/conftest.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/conftest.py @@ -44,3 +44,37 @@ def _hermetic_server_root_path(): finally: if saved is not None: os.environ["SERVER_ROOT_PATH"] = saved + + +@pytest.fixture +def config_only_mcp_manager_factory(): + from litellm.proxy._experimental.mcp_server.mcp_server_manager import MCPServerManager + + class ConfigOnlyManager(MCPServerManager): + def initialize_tool_name_to_mcp_server_name_mapping(self): + return None + + return ConfigOnlyManager + + +@pytest.fixture +def _mcp_request_ctx(): + def _mcp_request_ctx(**overrides): + from types import SimpleNamespace + + from mcp.server.context import ServerRequestContext + + kwargs = { + "session": SimpleNamespace(), + "lifespan_context": {}, + "protocol_version": "2025-06-18", + "method": "", + "params": None, + "request_id": 1, + "meta": None, + "request": None, + } + kwargs.update(overrides) + return ServerRequestContext(**kwargs) + + return _mcp_request_ctx diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/faults/test_list_outcomes.py b/tests/test_litellm/proxy/_experimental/mcp_server/faults/test_list_outcomes.py index 65e2faee1b2..f951499e18f 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/faults/test_list_outcomes.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/faults/test_list_outcomes.py @@ -9,7 +9,7 @@ if sys.version_info < (3, 11): # BaseExceptionGroup is a builtin only from 3.11 import httpx import pytest -from mcp import McpError +from mcp import MCPError from mcp.types import ErrorData from litellm.proxy._experimental.mcp_server.exceptions import ( @@ -45,7 +45,7 @@ def test_upstream_json_rpc_error_code_is_never_read_as_an_http_status(): to answer with application code 408. Classifying that number as a gateway timeout would report a 504 the gateway never caused. A client timeout reaches here already expressed as a ``TimeoutError``, so this taxonomy never has to read the code to tell them apart.""" - upstream_error = McpError(ErrorData(code=int(httpx.codes.REQUEST_TIMEOUT), message="re-authenticate and retry")) + upstream_error = MCPError(code=int(httpx.codes.REQUEST_TIMEOUT), message="re-authenticate and retry") assert classify_list_exception(upstream_error).tag != "timeout" assert list_fault_http_status(classify_list_exception(upstream_error)) != 504 diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/guardrail_translation/test_mcp_guardrail_handler.py b/tests/test_litellm/proxy/_experimental/mcp_server/guardrail_translation/test_mcp_guardrail_handler.py index 28959054195..77e9b987e74 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/guardrail_translation/test_mcp_guardrail_handler.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/guardrail_translation/test_mcp_guardrail_handler.py @@ -652,7 +652,7 @@ async def test_structured_content_is_masked_alongside_content(): returned = await handler.process_output_response(response=response, guardrail_to_apply=guardrail) assert returned.content[0].text == "email " - assert returned.structuredContent == {"contact": {"email": ""}, "balance": 42.0} + assert returned.structured_content == {"contact": {"email": ""}, "balance": 42.0} @pytest.mark.asyncio @@ -673,7 +673,7 @@ async def test_value_present_only_in_structured_content_is_masked(): returned = await handler.process_output_response(response=response, guardrail_to_apply=guardrail) assert "jane@example.com" in guardrail.seen_texts - assert returned.structuredContent == {"records": [{"email": ""}]} + assert returned.structured_content == {"records": [{"email": ""}]} assert returned.content[0].text == "lookup complete" @@ -690,7 +690,7 @@ async def test_structured_content_without_a_match_is_untouched(): returned = await handler.process_output_response(response=response, guardrail_to_apply=guardrail) - assert returned.structuredContent == {"record_id": "C-1001", "balance": 42.0, "active": True, "note": None} + assert returned.structured_content == {"record_id": "C-1001", "balance": 42.0, "active": True, "note": None} @pytest.mark.asyncio @@ -798,4 +798,4 @@ async def test_clean_structured_content_keys_do_not_block(): returned = await handler.process_output_response(response=response, guardrail_to_apply=guardrail) assert returned.content[0].text == "email " - assert returned.structuredContent == {"record_id": "C-1001", "balance": 42.0, "count": 3} + assert returned.structured_content == {"record_id": "C-1001", "balance": 42.0, "count": 3} diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_client_credentials.py b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_client_credentials.py index 774cd022703..1cad9a1fccb 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_client_credentials.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_client_credentials.py @@ -6,6 +6,7 @@ rotation-aware cache keying, expires_in-driven expiry, error classification, and """ import httpx +import httpx2 import pytest from pydantic import SecretStr @@ -322,27 +323,27 @@ async def test_refetch_returns_none_when_the_grant_fails(): assert await source.refetch("s", _config(), failed_access_token="stale") is None -def _upstream(responses: "list[httpx.Response]") -> "tuple[httpx.MockTransport, list[str]]": +def _upstream(responses: "list[httpx2.Response]") -> "tuple[httpx2.MockTransport, list[str]]": # The auth flow re-yields the same Request object on retry, so snapshot the Authorization # value per send; holding the Request would show the post-retry mutation for both entries. seen: "list[str]" = [] - def handler(request: httpx.Request) -> httpx.Response: + def handler(request: httpx2.Request) -> httpx2.Response: seen.append(request.headers.get("Authorization", "")) return responses[min(len(seen) - 1, len(responses) - 1)] - return httpx.MockTransport(handler), seen + return httpx2.MockTransport(handler), seen @pytest.mark.asyncio async def test_bearer_auth_sends_the_token_and_leaves_a_success_alone(): - transport, seen = _upstream([httpx.Response(200)]) + transport, seen = _upstream([httpx2.Response(200)]) async def refetch(failed: str) -> "str | None": raise AssertionError("must not refetch on success") auth = ClientCredentialsBearerAuth("m2m-token", refetch, ClientCredentialsConfig()) - async with httpx.AsyncClient(transport=transport, auth=auth) as client: + async with httpx2.AsyncClient(transport=transport, auth=auth) as client: response = await client.get("https://upstream.example.com/mcp") assert response.status_code == 200 assert seen == ["Bearer m2m-token"] @@ -350,7 +351,7 @@ async def test_bearer_auth_sends_the_token_and_leaves_a_success_alone(): @pytest.mark.asyncio async def test_bearer_auth_retries_a_401_once_with_a_fresh_token(): - transport, seen = _upstream([httpx.Response(401), httpx.Response(200)]) + transport, seen = _upstream([httpx2.Response(401), httpx2.Response(200)]) refetched: "list[str]" = [] async def refetch(failed: str) -> "str | None": @@ -358,7 +359,7 @@ async def test_bearer_auth_retries_a_401_once_with_a_fresh_token(): return "fresh-token" auth = ClientCredentialsBearerAuth("stale-token", refetch, ClientCredentialsConfig()) - async with httpx.AsyncClient(transport=transport, auth=auth) as client: + async with httpx2.AsyncClient(transport=transport, auth=auth) as client: response = await client.get("https://upstream.example.com/mcp") assert response.status_code == 200 assert refetched == ["stale-token"] @@ -370,7 +371,7 @@ async def test_bearer_auth_remembers_the_rotated_token_for_later_requests(): # The auth object lives for the whole MCP session (it is the httpx client's auth), so after a # 401 recovery it must send the fresh token first on subsequent requests; re-sending the # rejected one would burn a 401 round trip and the single retry on every call. - transport, seen = _upstream([httpx.Response(401), httpx.Response(200), httpx.Response(200)]) + transport, seen = _upstream([httpx2.Response(401), httpx2.Response(200), httpx2.Response(200)]) refetched: "list[str]" = [] async def refetch(failed: str) -> "str | None": @@ -378,7 +379,7 @@ async def test_bearer_auth_remembers_the_rotated_token_for_later_requests(): return "fresh-token" auth = ClientCredentialsBearerAuth("stale-token", refetch, ClientCredentialsConfig()) - async with httpx.AsyncClient(transport=transport, auth=auth) as client: + async with httpx2.AsyncClient(transport=transport, auth=auth) as client: first = await client.get("https://upstream.example.com/mcp") second = await client.get("https://upstream.example.com/mcp") assert first.status_code == 200 and second.status_code == 200 @@ -388,13 +389,13 @@ async def test_bearer_auth_remembers_the_rotated_token_for_later_requests(): @pytest.mark.asyncio async def test_bearer_auth_surfaces_the_401_when_the_refetch_fails(): - transport, seen = _upstream([httpx.Response(401)]) + transport, seen = _upstream([httpx2.Response(401)]) async def refetch(failed: str) -> "str | None": return None auth = ClientCredentialsBearerAuth("stale-token", refetch, ClientCredentialsConfig()) - async with httpx.AsyncClient(transport=transport, auth=auth) as client: + async with httpx2.AsyncClient(transport=transport, auth=auth) as client: response = await client.get("https://upstream.example.com/mcp") assert response.status_code == 401 assert len(seen) == 1 @@ -402,7 +403,7 @@ async def test_bearer_auth_surfaces_the_401_when_the_refetch_fails(): @pytest.mark.asyncio async def test_bearer_auth_gives_up_after_a_second_401(): - transport, seen = _upstream([httpx.Response(401), httpx.Response(401)]) + transport, seen = _upstream([httpx2.Response(401), httpx2.Response(401)]) refetched: "list[str]" = [] async def refetch(failed: str) -> "str | None": @@ -410,7 +411,7 @@ async def test_bearer_auth_gives_up_after_a_second_401(): return "fresh-token" auth = ClientCredentialsBearerAuth("stale-token", refetch, ClientCredentialsConfig()) - async with httpx.AsyncClient(transport=transport, auth=auth) as client: + async with httpx2.AsyncClient(transport=transport, auth=auth) as client: response = await client.get("https://upstream.example.com/mcp") assert response.status_code == 401 assert len(seen) == 2 @@ -422,7 +423,7 @@ def test_bearer_auth_rejects_sync_clients(): return None auth = ClientCredentialsBearerAuth("token", refetch, ClientCredentialsConfig()) - with httpx.Client(transport=httpx.MockTransport(lambda request: httpx.Response(200)), auth=auth) as client: + with httpx2.Client(transport=httpx2.MockTransport(lambda request: httpx2.Response(200)), auth=auth) as client: with pytest.raises(RuntimeError): client.get("https://upstream.example.com/mcp") @@ -431,15 +432,15 @@ def test_bearer_auth_rejects_sync_clients(): async def test_bearer_auth_writes_the_minted_token_to_the_configured_header(): seen: "list[dict[str, str]]" = [] - def handler(request: httpx.Request) -> httpx.Response: + def handler(request: httpx2.Request) -> httpx2.Response: seen.append(dict(request.headers)) - return httpx.Response(200) + return httpx2.Response(200) async def refetch(failed: str) -> "str | None": raise AssertionError("must not refetch on success") auth = ClientCredentialsBearerAuth("m2m-token", refetch, ClientCredentialsConfig(header_name="esb-oauth")) - async with httpx.AsyncClient(transport=httpx.MockTransport(handler), auth=auth) as client: + async with httpx2.AsyncClient(transport=httpx2.MockTransport(handler), auth=auth) as client: await client.get("https://upstream.example.com/mcp") assert seen[0]["esb-oauth"] == "Bearer m2m-token" assert "authorization" not in seen[0] @@ -451,9 +452,9 @@ async def test_the_401_refetch_retry_also_targets_the_configured_header(): # would silently send the fresh token to Authorization, so the ESB rejects every recovered # request while the first attempt looked correct. seen: "list[dict[str, str]]" = [] - responses = [httpx.Response(401), httpx.Response(200)] + responses = [httpx2.Response(401), httpx2.Response(200)] - def handler(request: httpx.Request) -> httpx.Response: + def handler(request: httpx2.Request) -> httpx2.Response: seen.append(dict(request.headers)) return responses[min(len(seen) - 1, len(responses) - 1)] @@ -461,7 +462,7 @@ async def test_the_401_refetch_retry_also_targets_the_configured_header(): return "fresh-token" auth = ClientCredentialsBearerAuth("stale-token", refetch, ClientCredentialsConfig(header_name="esb-oauth")) - async with httpx.AsyncClient(transport=httpx.MockTransport(handler), auth=auth) as client: + async with httpx2.AsyncClient(transport=httpx2.MockTransport(handler), auth=auth) as client: response = await client.get("https://upstream.example.com/mcp") assert response.status_code == 200 assert [h["esb-oauth"] for h in seen] == ["Bearer stale-token", "Bearer fresh-token"] diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_httpx_auth.py b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_httpx_auth.py index 9eab089bac6..5a5eea60fce 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_httpx_auth.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_httpx_auth.py @@ -1,10 +1,10 @@ -"""Tests for the concrete httpx.Auth objects the resolver returns. +"""Tests for the concrete httpx2.Auth objects the resolver returns. NoOpAuth must attach nothing; StaticHeaderAuth must set exactly the configured header. These pin the header emission the api_key family and passthrough depend on. """ -import httpx +import httpx2 from litellm.proxy._experimental.mcp_server.outbound_credentials import ( NoOpAuth, @@ -12,7 +12,7 @@ from litellm.proxy._experimental.mcp_server.outbound_credentials import ( ) -def _apply(auth: httpx.Auth, request: httpx.Request) -> httpx.Request: +def _apply(auth: httpx2.Auth, request: httpx2.Request) -> httpx2.Request: flow = auth.auth_flow(request) sent = next(flow) flow.close() @@ -20,19 +20,19 @@ def _apply(auth: httpx.Auth, request: httpx.Request) -> httpx.Request: def test_noop_auth_attaches_no_authorization_header(): - request = httpx.Request("GET", "https://upstream.example.com/mcp") + request = httpx2.Request("GET", "https://upstream.example.com/mcp") _apply(NoOpAuth(), request) assert "authorization" not in request.headers def test_static_header_auth_defaults_to_authorization(): - request = httpx.Request("GET", "https://upstream.example.com/mcp") + request = httpx2.Request("GET", "https://upstream.example.com/mcp") _apply(StaticHeaderAuth("Bearer abc"), request) assert request.headers["Authorization"] == "Bearer abc" def test_static_header_auth_honors_custom_header_name(): - request = httpx.Request("GET", "https://upstream.example.com/mcp") + request = httpx2.Request("GET", "https://upstream.example.com/mcp") _apply(StaticHeaderAuth("raw-key", header_name="X-API-Key"), request) assert request.headers["X-API-Key"] == "raw-key" assert "authorization" not in request.headers diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_resolver.py b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_resolver.py index 5fab4ceec72..0e47bbb9bb1 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_resolver.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_resolver.py @@ -12,7 +12,7 @@ import logging import time from datetime import datetime, timedelta, timezone -import httpx +import httpx2 import jwt as pyjwt import pytest from pydantic import SecretStr @@ -109,8 +109,8 @@ def _spec(config): return ServerSpec(server_id="s", resource="https://upstream.example.com", config=config) -def _emitted(auth: httpx.Auth) -> httpx.Headers: - request = httpx.Request("GET", "https://upstream.example.com/mcp") +def _emitted(auth: httpx2.Auth) -> httpx2.Headers: + request = httpx2.Request("GET", "https://upstream.example.com/mcp") flow = auth.auth_flow(request) next(flow) flow.close() @@ -412,15 +412,15 @@ _M2M = ClientCredentialsConfig( ) -async def _emitted_async(auth: httpx.Auth, respond=None) -> tuple[httpx.Headers, list[httpx.Request]]: +async def _emitted_async(auth: httpx2.Auth, respond=None) -> tuple[httpx2.Headers, list[httpx2.Request]]: """Drive the async auth flow one request at a time, replying via ``respond`` when given.""" - seen: list[httpx.Request] = [] + seen: list[httpx2.Request] = [] - def handler(request: httpx.Request) -> httpx.Response: + def handler(request: httpx2.Request) -> httpx2.Response: seen.append(request) - return respond(request) if respond else httpx.Response(200) + return respond(request) if respond else httpx2.Response(200) - async with httpx.AsyncClient(transport=httpx.MockTransport(handler), auth=auth) as client: + async with httpx2.AsyncClient(transport=httpx2.MockTransport(handler), auth=auth) as client: await client.get("https://upstream.example.com/mcp") return seen[-1].headers, seen @@ -458,9 +458,9 @@ async def test_client_credentials_auth_retries_a_401_through_the_source(): ) assert isinstance(result, Ok) - def respond(request: httpx.Request) -> httpx.Response: + def respond(request: httpx2.Request) -> httpx2.Response: is_stale = request.headers["Authorization"] == "Bearer stale-at" - return httpx.Response(401) if is_stale else httpx.Response(200) + return httpx2.Response(401) if is_stale else httpx2.Response(200) headers, seen = await _emitted_async(result.ok, respond) assert headers["Authorization"] == "Bearer fresh-m2m" diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_custom_fields.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_custom_fields.py index 333d4c98899..e3437bf16f6 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_custom_fields.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_custom_fields.py @@ -18,9 +18,9 @@ from litellm.proxy._types import LiteLLM_MCPServerTable class TestMCPCustomFields: """Test custom fields functionality in MCP server configuration.""" - async def test_custom_fields_preserved_from_config(self): + async def test_custom_fields_preserved_from_config(self, config_only_mcp_manager_factory): """Test that custom fields in mcp_info are preserved when loading from config.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() # Mock config with custom fields mock_config = { @@ -62,9 +62,9 @@ class TestMCPCustomFields: assert mcp_info["priority"] == 10 assert mcp_info["tags"] == ["production", "api"] - async def test_custom_fields_preserved_from_database(self): + async def test_custom_fields_preserved_from_database(self, config_only_mcp_manager_factory): """Test that custom fields in mcp_info are preserved when adding from database.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() # Mock database record with custom fields mock_server = LiteLLM_MCPServerTable( @@ -106,9 +106,9 @@ class TestMCPCustomFields: assert mcp_info["metadata"] == {"source": "database"} assert mcp_info["version"] == "1.0.0" - async def test_empty_mcp_info_handled_gracefully(self): + async def test_empty_mcp_info_handled_gracefully(self, config_only_mcp_manager_factory): """Test that empty or missing mcp_info is handled gracefully.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() # Config with empty mcp_info mock_config = { @@ -130,9 +130,9 @@ class TestMCPCustomFields: # Should have default server_name assert mcp_info["server_name"] == "test_server" - async def test_missing_mcp_info_creates_defaults(self): + async def test_missing_mcp_info_creates_defaults(self, config_only_mcp_manager_factory): """Test that missing mcp_info creates appropriate defaults.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() # Config without mcp_info mock_config = { @@ -155,9 +155,9 @@ class TestMCPCustomFields: assert mcp_info["server_name"] == "test_server" assert mcp_info["description"] == "Server description" - async def test_config_description_fallback(self): + async def test_config_description_fallback(self, config_only_mcp_manager_factory): """Test that description from config level is used as fallback.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() # Config with description at server level but not in mcp_info mock_config = { @@ -179,9 +179,9 @@ class TestMCPCustomFields: assert mcp_info["description"] == "Config level description" assert mcp_info["custom_field"] == "custom_value" - async def test_mcp_info_description_takes_precedence(self): + async def test_mcp_info_description_takes_precedence(self, config_only_mcp_manager_factory): """Test that description in mcp_info takes precedence over config level.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() # Config with description at both levels mock_config = { diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_debug.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_debug.py index b6535e6326a..46ecd4df716 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_debug.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_debug.py @@ -5,20 +5,17 @@ Tests for MCPDebug — MCP OAuth2 debug response headers. import asyncio from typing import Final +import httpx import pytest from starlette.types import Message -from litellm.proxy._experimental.mcp_server.outbound_credentials.types import AuthResolution - -import httpx - from litellm.proxy._experimental.mcp_server.mcp_debug import ( MCP_DEBUG_REQUEST_HEADER, + MCPAuthDiagnostics, MCPDebug, describe_upstream_http_failure, - - MCPAuthDiagnostics, ) +from litellm.proxy._experimental.mcp_server.outbound_credentials.types import AuthResolution class TestIsDebugEnabled: @@ -265,6 +262,7 @@ class TestDescribeUpstreamHttpFailure: assert describe_upstream_http_failure(ConnectionError("refused")) is None + @pytest.mark.parametrize("body", [ b'{"password":"first second","token":"demo-secret"}', b'{"nested":[{"access_token":"first,second"}]}', @@ -464,13 +462,12 @@ def test_diagnostics_keep_requests_separate_and_do_not_collapse_multiple_servers @pytest.mark.asyncio -async def test_concurrent_mcp_messages_record_on_their_own_http_scope() -> None: +async def test_concurrent_mcp_messages_record_on_their_own_http_scope(_mcp_request_ctx) -> None: from unittest.mock import MagicMock - from mcp.server.lowlevel.server import request_ctx - from mcp.shared.context import RequestContext from starlette.requests import Request + from litellm.proxy._experimental.mcp_server.mcp_context import active_mcp_request_ctx_var from litellm.proxy._experimental.mcp_server.mcp_debug import ( MCP_AUTH_DIAGNOSTICS_SCOPE_KEY, record_auth_resolution, @@ -481,16 +478,16 @@ async def test_concurrent_mcp_messages_record_on_their_own_http_scope() -> None: second: Final = MCPAuthDiagnostics() async def record(diagnostics: MCPAuthDiagnostics, source: AuthResolution) -> None: - context: Final = RequestContext( - request_id=1, meta=None, session=session, lifespan_context=None, + context: Final = _mcp_request_ctx( + session=session, request=Request({"type": "http", MCP_AUTH_DIAGNOSTICS_SCOPE_KEY: diagnostics}), ) - token: Final = request_ctx.set(context) + token: Final = active_mcp_request_ctx_var.set(context) try: await asyncio.sleep(0) record_auth_resolution("same-server", source) finally: - request_ctx.reset(token) + active_mcp_request_ctx_var.reset(token) await asyncio.gather(record(first, AuthResolution.stored_user_token), record(second, AuthResolution.per_request_header)) assert first.resolution() == "stored-user-token" diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_elicitation_handler.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_elicitation_handler.py index b93f0d56f8e..a59b02ec01d 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_elicitation_handler.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_elicitation_handler.py @@ -30,7 +30,7 @@ def _form_params(message: str = "fill the form") -> ElicitRequestFormParams: return ElicitRequestFormParams( mode="form", message=message, - requestedSchema={"type": "object", "properties": {}}, + requested_schema={"type": "object", "properties": {}}, ) @@ -39,7 +39,7 @@ def _url_params(message: str = "please authorize") -> ElicitRequestURLParams: mode="url", message=message, url="https://example.com/oauth", - elicitationId="elc-1", + elicitation_id="elc-1", ) @@ -118,7 +118,7 @@ class TestRelayElicitationToDownstream: session.elicit_form.assert_awaited_once() _, kwargs = session.elicit_form.call_args assert kwargs["message"] == "collect name" - assert kwargs["requestedSchema"] == params.requestedSchema + assert kwargs["requested_schema"] == params.requested_schema async def test_should_relay_url_mode(self): accepted = ElicitResult(action="accept") @@ -142,7 +142,7 @@ class TestRelayElicitationToDownstream: # A bare params object that is neither Form nor URL params triggers # the generic fallback path. - params = SimpleNamespace(mode="form", message="hi", requestedSchema={}) + params = SimpleNamespace(mode="form", message="hi", requested_schema={}) result = await _relay_elicitation_to_downstream( params=params, downstream_session=session, diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_env_vars.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_env_vars.py index 36b545ad031..93b894f7645 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_env_vars.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_env_vars.py @@ -1698,7 +1698,7 @@ def test_decrypt_global_env_var_drops_undecryptable_value( @pytest.mark.asyncio async def test_missing_user_env_vars_error_renders_in_mcp_call_tool(): """The MCP ``call_tool`` handler must turn ``MCPMissingUserEnvVarsError`` - into a friendly ``CallToolResult`` with ``isError=True`` so Claude Code + into a friendly ``CallToolResult`` with ``is_error=True`` so Claude Code surfaces the setup URL instead of an opaque internal error.""" from mcp.types import TextContent @@ -1716,7 +1716,7 @@ async def test_missing_user_env_vars_error_renders_in_mcp_call_tool(): content=[TextContent(text=str(err), type="text")], isError=True, ) - assert result.isError is True + assert result.is_error is True text = result.content[0].text # type: ignore[union-attr] assert "CorporateDB" in text assert "CORP_USERNAME" in text diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_metadata_preservation.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_metadata_preservation.py index 5a24ca00c25..86748d99063 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_metadata_preservation.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_metadata_preservation.py @@ -39,15 +39,12 @@ class TestMCPMetadataPreservation: name="hello_widget", description="Display a greeting widget", inputSchema={"type": "object", "properties": {}}, + meta={ + "openai/outputTemplate": "ui://widget/hello.html", + "openai/widgetDescription": "A greeting widget", + "openai/toolInvocation/invoking": "Preparing greeting...", + }, ) - # Add metadata using setattr since MCPTool might not have it in the constructor - tool_with_metadata.metadata = { - "openai/outputTemplate": "ui://widget/hello.html", - "openai/widgetDescription": "A greeting widget", - } - tool_with_metadata._meta = { - "openai/toolInvocation/invoking": "Preparing greeting...", - } # Create prefixed tools prefixed_tools = manager._create_prefixed_tools( @@ -61,22 +58,16 @@ class TestMCPMetadataPreservation: # Check that name is prefixed assert prefixed_tool.name == "test-hello_widget" - # Check that metadata is preserved - assert hasattr(prefixed_tool, "metadata") - assert prefixed_tool.metadata == { + # Check that _meta (the SDK `meta` field) is preserved + assert prefixed_tool.meta == { "openai/outputTemplate": "ui://widget/hello.html", "openai/widgetDescription": "A greeting widget", - } - - # Check that _meta is preserved - assert hasattr(prefixed_tool, "_meta") - assert prefixed_tool._meta == { "openai/toolInvocation/invoking": "Preparing greeting...", } # Check that other fields are preserved assert prefixed_tool.description == "Display a greeting widget" - assert prefixed_tool.inputSchema == {"type": "object", "properties": {}} + assert prefixed_tool.input_schema== {"type": "object", "properties": {}} if __name__ == "__main__": diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_proxy_mode.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_proxy_mode.py index 67b7c5a3414..84d4f1fd083 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_proxy_mode.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_proxy_mode.py @@ -3,7 +3,7 @@ from datetime import datetime import pytest from fastapi import HTTPException -from mcp.shared.exceptions import McpError +from mcp.shared.exceptions import MCPError from pydantic import AnyUrl import litellm @@ -32,7 +32,7 @@ async def test_proxy_call_rejects_non_proxy_tool_names() -> None: ) assert result is not None - assert result.isError is True + assert result.is_error is True assert "unavailable on /mcp/proxy" in result.content[0].text @@ -44,16 +44,28 @@ async def test_proxy_rejects_non_tool_protocol_operations() -> None: assert options.capabilities.resources is None assert options.capabilities.tools is not None - with pytest.raises(McpError): - await server.list_prompts() - with pytest.raises(McpError): - await server.get_prompt("prompt", {}) - with pytest.raises(McpError): - await server.list_resources() - with pytest.raises(McpError): - await server.list_resource_templates() - with pytest.raises(McpError): - await server.read_resource(AnyUrl("https://example.com/resource")) + from types import SimpleNamespace + + from mcp.server.context import ServerRequestContext + from mcp.types import GetPromptRequestParams, PaginatedRequestParams, ReadResourceRequestParams + + ctx = ServerRequestContext( + session=SimpleNamespace(), + lifespan_context={}, + protocol_version="2025-06-18", + method="", + ) + + with pytest.raises(MCPError): + await server.list_prompts(ctx, PaginatedRequestParams()) + with pytest.raises(MCPError): + await server.get_prompt(ctx, GetPromptRequestParams(name="prompt", arguments={})) + with pytest.raises(MCPError): + await server.list_resources(ctx, PaginatedRequestParams()) + with pytest.raises(MCPError): + await server.list_resource_templates(ctx, PaginatedRequestParams()) + with pytest.raises(MCPError): + await server.read_resource(ctx, ReadResourceRequestParams(uri="https://example.com/resource")) class FailureRecorder(CustomLogger): diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_completion_flow.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_completion_flow.py index 78aee7b534f..d17b407a1be 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_completion_flow.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_completion_flow.py @@ -28,14 +28,14 @@ def _params(**overrides): role="user", content=SimpleNamespace(type="text", text="hi") ) ], - systemPrompt="be concise", - maxTokens=128, + system_prompt="be concise", + max_tokens=128, temperature=None, - stopSequences=None, + stop_sequences=None, tools=None, - toolChoice=None, + tool_choice=None, metadata=None, - modelPreferences=None, + model_preferences=None, ) base.update(overrides) return SimpleNamespace(**base) @@ -52,13 +52,13 @@ class TestBuildCompletionKwargs: async def test_should_include_sampling_options_and_tools(self): params = _params( temperature=0.3, - stopSequences=["STOP"], + stop_sequences=["STOP"], tools=[ SimpleNamespace( - name="search", description="d", inputSchema={"type": "object"} + name="search", description="d", input_schema={"type": "object"} ) ], - toolChoice=SimpleNamespace(mode="required"), + tool_choice=SimpleNamespace(mode="required"), metadata={"trace": "abc"}, ) with patch( @@ -179,7 +179,7 @@ class TestHandleSamplingCreateMessagePipeline: assert isinstance(result, CreateMessageResult) assert result.content.text == "the answer is 42" - assert result.stopReason == "endTurn" + assert result.stop_reason== "endTurn" async def test_should_reraise_known_proxy_exceptions(self): from litellm.exceptions import RateLimitError diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_model_access.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_model_access.py index 7c5320ed4f4..8975f42387b 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_model_access.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_model_access.py @@ -212,14 +212,14 @@ class TestSamplingAuthAndBudgetGating: ) params = MagicMock() - params.modelPreferences = None + params.model_preferences = None params.messages = [] params.systemPrompt = None - params.maxTokens = 100 + params.max_tokens = 100 params.temperature = None - params.stopSequences = None + params.stop_sequences = None params.tools = None - params.toolChoice = None + params.tool_choice = None params.metadata = None result = await handle_sampling_create_message( @@ -242,14 +242,14 @@ class TestSamplingAuthAndBudgetGating: auth = _make_user_api_key_auth(models=["gpt-4o"]) params = MagicMock() - params.modelPreferences = None + params.model_preferences = None params.messages = [] params.systemPrompt = None - params.maxTokens = 100 + params.max_tokens = 100 params.temperature = None - params.stopSequences = None + params.stop_sequences = None params.tools = None - params.toolChoice = None + params.tool_choice = None params.metadata = None with ( @@ -304,14 +304,14 @@ class TestSamplingAuthAndBudgetGating: auth = _make_user_api_key_auth(models=["gpt-4o"]) params = MagicMock() - params.modelPreferences = None + params.model_preferences = None params.messages = [] params.systemPrompt = None - params.maxTokens = 100 + params.max_tokens = 100 params.temperature = None - params.stopSequences = None + params.stop_sequences = None params.tools = None - params.toolChoice = None + params.tool_choice = None params.metadata = None budget_error = ErrorData(code=-1, message="ExceededBudget: over limit") diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_response_conversion.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_response_conversion.py index bb17a8f7104..ba130f34964 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_response_conversion.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_response_conversion.py @@ -54,13 +54,13 @@ class TestConvertOpenAIResponseToMcpResult: assert isinstance(result.content, TextContent) assert result.content.text == "hello world" assert result.role == "assistant" - assert result.stopReason == "endTurn" + assert result.stop_reason== "endTurn" def test_should_map_length_finish_reason_to_max_tokens(self): result = _convert_openai_response_to_mcp_result( _response(content="truncated", finish_reason="length"), "gpt-4o" ) - assert result.stopReason == "maxTokens" + assert result.stop_reason== "maxTokens" def test_should_prefer_actual_model_from_response(self): result = _convert_openai_response_to_mcp_result( @@ -79,7 +79,7 @@ class TestConvertOpenAIResponseToMcpResult: "gpt-4o", ) assert isinstance(result, CreateMessageResultWithTools) - assert result.stopReason == "toolUse" + assert result.stop_reason== "toolUse" tool_uses = [c for c in result.content if isinstance(c, ToolUseContent)] assert len(tool_uses) == 1 assert tool_uses[0].name == "get_weather" @@ -113,7 +113,7 @@ class TestConvertMcpToolsToOpenAI: def test_should_convert_tool_with_schema(self): schema = {"type": "object", "properties": {"q": {"type": "string"}}} tool = SimpleNamespace( - name="search", description="search the web", inputSchema=schema + name="search", description="search the web", input_schema=schema ) result = _convert_mcp_tools_to_openai([tool]) assert result == [ @@ -128,7 +128,7 @@ class TestConvertMcpToolsToOpenAI: ] def test_should_default_description_and_parameters(self): - tool = SimpleNamespace(name="noop", description=None, inputSchema=None) + tool = SimpleNamespace(name="noop", description=None, input_schema=None) result = _convert_mcp_tools_to_openai([tool]) fn = result[0]["function"] assert fn["description"] == "" @@ -151,7 +151,7 @@ class TestConvertMcpToolChoiceToOpenAI: class TestConvertImageAndAudioContent: def test_should_convert_image_to_data_uri(self): - content = SimpleNamespace(type="image", data="aGVsbG8=", mimeType="image/jpeg") + content = SimpleNamespace(type="image", data="aGVsbG8=", mime_type="image/jpeg") result = _convert_single_content(content) assert result == { "type": "image_url", @@ -159,20 +159,20 @@ class TestConvertImageAndAudioContent: } def test_should_map_audio_mime_to_format(self): - content = SimpleNamespace(type="audio", data="Zm9v", mimeType="audio/mp3") + content = SimpleNamespace(type="audio", data="Zm9v", mime_type="audio/mp3") result = _convert_single_content(content) assert result["type"] == "input_audio" assert result["input_audio"] == {"data": "Zm9v", "format": "mp3"} def test_should_default_unknown_audio_mime_to_wav(self): - content = SimpleNamespace(type="audio", data="Zm9v", mimeType="audio/weird") + content = SimpleNamespace(type="audio", data="Zm9v", mime_type="audio/weird") result = _convert_single_content(content) assert result["input_audio"]["format"] == "wav" def test_should_flatten_list_content(self): items = [ SimpleNamespace(type="text", text="a"), - SimpleNamespace(type="image", data="x", mimeType="image/png"), + SimpleNamespace(type="image", data="x", mime_type="image/png"), ] result = _convert_mcp_content_to_openai(items) assert isinstance(result, list) diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_tool_conversion.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_tool_conversion.py index b4b219e958c..167847afe1f 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_tool_conversion.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_tool_conversion.py @@ -10,6 +10,8 @@ import json from types import SimpleNamespace from typing import Any, Dict +from mcp.types import TextContent, ToolResultContent + from litellm.proxy._experimental.mcp_server.sampling_handler import ( _convert_mcp_messages_to_openai, _convert_single_content, @@ -21,8 +23,8 @@ from litellm.proxy._experimental.mcp_server.sampling_handler import ( # --------------------------------------------------------------------------- -def _text(text: str) -> SimpleNamespace: - return SimpleNamespace(type="text", text=text) +def _text(text: str) -> TextContent: + return TextContent(type="text", text=text) def _tool_use(*, name: str, tool_id: str, input_data: Dict[str, Any]) -> SimpleNamespace: @@ -31,11 +33,9 @@ def _tool_use(*, name: str, tool_id: str, input_data: Dict[str, Any]) -> SimpleN def _tool_result( *, tool_use_id: str, content: Any = None, is_error: bool = False -) -> SimpleNamespace: - if content is None: - content = [] - return SimpleNamespace( - type="tool_result", toolUseId=tool_use_id, content=content, isError=is_error +) -> ToolResultContent: + return ToolResultContent( + tool_use_id=tool_use_id, content=[] if content is None else content, is_error=is_error ) diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py index 33e14736357..47ec25a90f7 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py @@ -1,6 +1,7 @@ import asyncio import contextlib import contextvars +import json import os from datetime import datetime, timedelta from types import SimpleNamespace @@ -11,6 +12,7 @@ import pytest from fastapi import HTTPException from mcp import ReadResourceResult, Resource from mcp.types import ( + INVALID_REQUEST, BlobResourceContents, CallToolResult, Prompt, @@ -18,9 +20,11 @@ from mcp.types import ( TextContent, TextResourceContents, ) +from mcp_types.version import HANDSHAKE_PROTOCOL_VERSIONS, LATEST_HANDSHAKE_VERSION from pydantic import TypeAdapter -from starlette.types import Receive, Scope, Send +from starlette.types import Message, Receive, Scope, Send +from litellm.proxy._experimental.mcp_server.mcp_context import active_mcp_request_ctx_var from litellm.proxy._types import ( LiteLLM_MCPServerTable, MCPTransport, @@ -30,6 +34,17 @@ from litellm.types.mcp import MCPAuth from litellm.types.mcp_server.mcp_server_manager import MCPOAuthMetadata, MCPServer +def test_mcp_available_on_sdk2(): + from importlib.metadata import version + + from packaging.version import Version + + from litellm.proxy._experimental.mcp_server.server import MCP_AVAILABLE + + assert Version("2.2.0") <= Version(version("mcp")) < Version("3") + assert MCP_AVAILABLE is True + + def _rendered_log_message(call): message = str(call.args[0]) values = call.args[1:] @@ -67,8 +82,22 @@ def cleanup_mcp_global_state(): yield + + + +def _call_tool_params(name, arguments=None): + from mcp.types import CallToolRequestParams + + return CallToolRequestParams(name=name, arguments=arguments) + + +def _paged_params(): + from mcp.types import PaginatedRequestParams + + return PaginatedRequestParams() + @pytest.mark.asyncio -async def test_mcp_server_tool_call_body_contains_request_data(): +async def test_mcp_server_tool_call_body_contains_request_data(_mcp_request_ctx): """Test that proxy_server_request body contains name and arguments""" try: from litellm.proxy._experimental.mcp_server.server import ( @@ -117,7 +146,7 @@ async def test_mcp_server_tool_call_body_contains_request_data(): MagicMock(), ): # Call the function - await mcp_server_tool_call(tool_name, tool_arguments) + await mcp_server_tool_call(_mcp_request_ctx(), _call_tool_params(tool_name, tool_arguments)) # Verify the body contains the expected data assert "proxy_server_request" in captured_data @@ -129,7 +158,7 @@ async def test_mcp_server_tool_call_body_contains_request_data(): @pytest.mark.asyncio -async def test_mcp_server_tool_call_forwards_client_headers_to_logging(): +async def test_mcp_server_tool_call_forwards_client_headers_to_logging(_mcp_request_ctx): """The MCP protocol path must hand the connection's client headers to the pre-call pipeline, so logging callbacks and guardrails see them the way the REST path does.""" try: @@ -169,7 +198,7 @@ async def test_mcp_server_tool_call_forwards_client_headers_to_logging(): mock_call_mcp_tool, ): with patch("litellm.proxy.proxy_server.proxy_config", MagicMock()): - await mcp_server_tool_call("test_tool", {"param": "value"}) + await mcp_server_tool_call(_mcp_request_ctx(), _call_tool_params("test_tool", {"param": "value"})) assert captured_headers.get("x-nuid") == "nuid-1" assert captured_headers.get("x-app-id") == "app-1" @@ -178,7 +207,7 @@ async def test_mcp_server_tool_call_forwards_client_headers_to_logging(): @pytest.mark.asyncio -async def test_mcp_server_tool_call_strips_custom_litellm_key_header(): +async def test_mcp_server_tool_call_strips_custom_litellm_key_header(_mcp_request_ctx): """The deployment can rename the proxy key header via general_settings.litellm_key_header_name. The pre-call pipeline only knows that name if it is passed in, so without it the virtual key reaches metadata.headers and proxy_server_request.headers in plaintext.""" @@ -221,7 +250,7 @@ async def test_mcp_server_tool_call_strips_custom_litellm_key_header(): {"litellm_key_header_name": "x-company-key"}, clear=False, ): - await mcp_server_tool_call("test_tool", {"param": "value"}) + await mcp_server_tool_call(_mcp_request_ctx(), _call_tool_params("test_tool", {"param": "value"})) metadata_headers = captured_data["metadata"]["headers"] assert metadata_headers.get("x-nuid") == "nuid-1" @@ -230,7 +259,7 @@ async def test_mcp_server_tool_call_strips_custom_litellm_key_header(): @pytest.mark.asyncio -async def test_mcp_server_tool_call_relays_upstream_auth_error_as_iserror(): +async def test_mcp_server_tool_call_relays_upstream_auth_error_as_iserror(_mcp_request_ctx): """The MCP session manager serializes handler exceptions as JSON-RPC errors, so a mid-session tool call cannot emit a raw 401 the way the REST path does. mcp_server_tool_call must turn an upstream MCPUpstreamAuthError into an explicit isError result naming the status, not a masked @@ -263,9 +292,9 @@ async def test_mcp_server_tool_call_relays_upstream_auth_error_as_iserror(): ): with patch("litellm.proxy.proxy_server.proxy_config", MagicMock()): with patch("litellm.proxy._experimental.mcp_server.server.verbose_logger", mock_logger): - result = await mcp_server_tool_call("test_tool", {"param": "value"}) + result = await mcp_server_tool_call(_mcp_request_ctx(), _call_tool_params("test_tool", {"param": "value"})) - assert result.isError is True + assert result.is_error is True # The dedicated MCPUpstreamAuthError branch (not the generic Exception fallthrough) produces this # specific message and logs at info, never a traceback via verbose_logger.exception. assert "upstream authentication required" in result.content[0].text @@ -1316,7 +1345,7 @@ async def test_get_tools_from_mcp_servers_continues_when_one_server_fails(): tool1 = MagicMock() tool1.name = "working_tool_1" tool1.description = "Working tool 1" - tool1.inputSchema = {} + tool1.input_schema = {} return [tool1] else: # Failing server raises an exception @@ -1692,15 +1721,15 @@ async def test_scoped_list_agent_veto_attributed_for_differently_cased_server_na @pytest.mark.asyncio -async def test_handle_list_tools_converts_permission_httpexception_to_mcp_error(): +async def test_handle_list_tools_converts_permission_httpexception_to_mcp_error(_mcp_request_ctx): """The MCP protocol handler surfaces a permission HTTPException as a clean JSON-RPC error - (McpError, INVALID_REQUEST) carrying the denial message, instead of a raw 500.""" + (MCPError, INVALID_REQUEST) carrying the denial message, instead of a raw 500.""" try: from litellm.proxy._experimental.mcp_server.server import handle_list_tools except ImportError: pytest.skip("MCP server not available") - from mcp.shared.exceptions import McpError + from mcp.shared.exceptions import MCPError from mcp.types import INVALID_REQUEST denial_message = "MCP server 'github' is not available to this key: the key is bound to agent 'agent-123'" @@ -1716,15 +1745,15 @@ async def test_handle_list_tools_converts_permission_httpexception_to_mcp_error( new=AsyncMock(side_effect=denial), ), ): - with pytest.raises(McpError) as exc_info: - await handle_list_tools() + with pytest.raises(MCPError) as exc_info: + await handle_list_tools(_mcp_request_ctx(), _paged_params()) assert exc_info.value.error.code == INVALID_REQUEST assert exc_info.value.error.message == denial_message @pytest.mark.asyncio -async def test_mcp_server_tool_call_renders_denial_message_not_detail_dict(): +async def test_mcp_server_tool_call_renders_denial_message_not_detail_dict(_mcp_request_ctx): try: from litellm.proxy._experimental.mcp_server.server import mcp_server_tool_call except ImportError: @@ -1743,14 +1772,14 @@ async def test_mcp_server_tool_call_renders_denial_message_not_detail_dict(): new=AsyncMock(side_effect=denial), ), ): - result = await mcp_server_tool_call("github-search_issues", {}) + result = await mcp_server_tool_call(_mcp_request_ctx(), _call_tool_params("github-search_issues", {})) - assert result.isError is True + assert result.is_error is True assert result.content[0].text == f"Error: {denial_message}" @pytest.mark.asyncio -async def test_mcp_server_tool_call_body_with_none_arguments(): +async def test_mcp_server_tool_call_body_with_none_arguments(_mcp_request_ctx): """Test that proxy_server_request body handles None arguments correctly""" try: from litellm.proxy._experimental.mcp_server.server import ( @@ -1798,7 +1827,7 @@ async def test_mcp_server_tool_call_body_with_none_arguments(): MagicMock(), ): # Call the function - await mcp_server_tool_call(tool_name, tool_arguments) + await mcp_server_tool_call(_mcp_request_ctx(), _call_tool_params(tool_name, tool_arguments)) # Verify the body contains the expected data assert "proxy_server_request" in captured_data @@ -1967,11 +1996,9 @@ async def test_streamable_http_session_manager_is_stateless(): ("DELETE", b"", False), ), ) -async def test_mcp_routing_initialize_to_stateful_no_session_to_stateless( +async def test_mcp_routing_initialize_to_stateful_no_session_to_stateless(_mcp_request_ctx, debug: bool, method: str, request_body: bytes, stateful: bool ) -> None: - from mcp.server.lowlevel.server import request_ctx - from mcp.shared.context import RequestContext from starlette.requests import Request from starlette.types import Message, Receive, Scope, Send @@ -1988,14 +2015,12 @@ async def test_mcp_routing_initialize_to_stateful_no_session_to_stateless( async def handle_request(request_scope: Scope, receive: Receive, outgoing: Send) -> None: await outgoing({"type": "http.response.start", "status": 200, "headers": []}) await observe_start(send.await_count) - context: Final = RequestContext( - request_id=1, meta=None, session=MagicMock(), lifespan_context=None, request=Request(request_scope) - ) - token: Final = request_ctx.set(context) + context: Final = _mcp_request_ctx(request=Request(request_scope)) + token: Final = active_mcp_request_ctx_var.set(context) try: record_auth_resolution("s1", AuthResolution.stored_user_token) finally: - request_ctx.reset(token) + active_mcp_request_ctx_var.reset(token) await outgoing(body) stateless_handle: Final = AsyncMock(side_effect=handle_request) @@ -4341,7 +4366,7 @@ async def test_list_tools_single_server_unprefixed_names(): tool = MagicMock() tool.name = f"{server.alias}-toolA" if add_prefix else "toolA" tool.description = "desc" - tool.inputSchema = {} + tool.input_schema = {} return [tool] mock_manager._get_tools_from_server = mock_get_tools_from_server @@ -4420,7 +4445,7 @@ async def test_list_tools_multiple_servers_prefixed_names(): # When multiple servers, add_prefix should be True -> prefixed names tool.name = f"{server.alias}-toolA" if add_prefix else "toolA" tool.description = "desc" - tool.inputSchema = {} + tool.input_schema = {} return [tool] mock_manager._get_tools_from_server = mock_get_tools_from_server @@ -4833,22 +4858,22 @@ async def test_list_tools_filters_by_key_team_permissions(): tool1 = MagicMock() tool1.name = "tool1" tool1.description = "Tool 1" - tool1.inputSchema = {} + tool1.input_schema = {} tool2 = MagicMock() tool2.name = "tool2" tool2.description = "Tool 2" - tool2.inputSchema = {} + tool2.input_schema = {} tool3 = MagicMock() tool3.name = "tool3" tool3.description = "Tool 3 - not allowed" - tool3.inputSchema = {} + tool3.input_schema = {} tool4 = MagicMock() tool4.name = "tool4" tool4.description = "Tool 4 - not allowed" - tool4.inputSchema = {} + tool4.input_schema = {} return [tool1, tool2, tool3, tool4] @@ -4944,22 +4969,22 @@ async def test_list_tools_with_team_tool_permissions_inheritance(): tool1 = MagicMock() tool1.name = "tool1" tool1.description = "Tool 1" - tool1.inputSchema = {} + tool1.input_schema = {} tool2 = MagicMock() tool2.name = "tool2" tool2.description = "Tool 2" - tool2.inputSchema = {} + tool2.input_schema = {} tool3 = MagicMock() tool3.name = "tool3" tool3.description = "Tool 3" - tool3.inputSchema = {} + tool3.input_schema = {} tool4 = MagicMock() tool4.name = "tool4" tool4.description = "Tool 4" - tool4.inputSchema = {} + tool4.input_schema = {} return [tool1, tool2, tool3, tool4] @@ -5041,17 +5066,17 @@ async def test_list_tools_with_no_tool_permissions_shows_all(): tool1 = MagicMock() tool1.name = "tool1" tool1.description = "Tool 1" - tool1.inputSchema = {} + tool1.input_schema = {} tool2 = MagicMock() tool2.name = "tool2" tool2.description = "Tool 2" - tool2.inputSchema = {} + tool2.input_schema = {} tool3 = MagicMock() tool3.name = "tool3" tool3.description = "Tool 3" - tool3.inputSchema = {} + tool3.input_schema = {} return [tool1, tool2, tool3] @@ -5142,22 +5167,22 @@ async def test_list_tools_strips_prefix_when_matching_permissions(): tool1 = MagicMock() tool1.name = "GITMCP-fetch_litellm_documentation" # Prefixed tool1.description = "Fetch docs" - tool1.inputSchema = {} + tool1.input_schema = {} tool2 = MagicMock() tool2.name = "GITMCP-search_litellm_documentation" # Prefixed, not in allowed list tool2.description = "Search docs" - tool2.inputSchema = {} + tool2.input_schema = {} tool3 = MagicMock() tool3.name = "GITMCP-search_litellm_code" # Prefixed tool3.description = "Search code" - tool3.inputSchema = {} + tool3.input_schema = {} tool4 = MagicMock() tool4.name = "GITMCP-fetch_generic_url_content" # Prefixed, not in allowed list tool4.description = "Fetch URL" - tool4.inputSchema = {} + tool4.input_schema = {} return [tool1, tool2, tool3, tool4] @@ -7361,7 +7386,7 @@ async def test_execute_mcp_tool_rest_server_id_authoritative_for_unprefixed_tool captured.update(kwargs) return mcp_module.CallToolResult( content=[TextContent(type="text", text="ok")], - isError=False, + is_error=False, ) with ( @@ -7440,7 +7465,7 @@ async def test_execute_mcp_tool_strips_a_prefix_that_contains_the_separator(): captured.update(kwargs) return mcp_module.CallToolResult( content=[TextContent(type="text", text="ok")], - isError=False, + is_error=False, ) with ( @@ -7507,7 +7532,7 @@ async def test_execute_mcp_tool_rest_server_id_injects_requested_server_credenti fake_client.call_tool = AsyncMock( return_value=mcp_module.CallToolResult( content=[TextContent(type="text", text="ok")], - isError=False, + is_error=False, ) ) @@ -7711,7 +7736,7 @@ async def test_execute_mcp_tool_rest_hyphenated_upstream_tool_name_routes_to_req captured.update(kwargs) return mcp_module.CallToolResult( content=[TextContent(type="text", text="ok")], - isError=False, + is_error=False, ) with ( @@ -7874,7 +7899,7 @@ async def test_execute_mcp_tool_rest_unresolved_prefixed_name_routes_to_requeste captured.update(kwargs) return mcp_module.CallToolResult( content=[TextContent(type="text", text="ok")], - isError=False, + is_error=False, ) with ( @@ -8356,20 +8381,24 @@ class TestMCPMetaTraceCarrier: (e.g. ``litellm.team.id``). Dropping it at the source is the regression guard.""" from types import SimpleNamespace - from mcp.types import RequestParams + from mcp.types import CallToolRequestParams from litellm.proxy._experimental.mcp_server.server import ( _mcp_meta_trace_carrier, ) - meta = RequestParams.Meta.model_validate( + meta = CallToolRequestParams.model_validate( { - "traceparent": "00-11111111111111111111111111111111-2222222222222222-01", - "tracestate": "rojo=1", - "baggage": "litellm.team.id=spoofed-team,litellm.metadata.user_api_key_user_id=attacker", - "progressToken": "p1", - } - ) + "name": "t", + "_meta": { + "traceparent": "00-11111111111111111111111111111111-2222222222222222-01", + "tracestate": "rojo=1", + "baggage": "litellm.team.id=spoofed-team,litellm.metadata.user_api_key_user_id=attacker", + "progressToken": "p1", + }, + }, + by_name=False, + ).meta carrier = _mcp_meta_trace_carrier(SimpleNamespace(meta=meta)) assert carrier == { "traceparent": "00-11111111111111111111111111111111-2222222222222222-01", @@ -8380,7 +8409,7 @@ class TestMCPMetaTraceCarrier: def test_none_when_no_trace_context(self): from types import SimpleNamespace - from mcp.types import RequestParams + from mcp.types import CallToolRequestParams from litellm.proxy._experimental.mcp_server.server import ( _mcp_meta_trace_carrier, @@ -8388,17 +8417,14 @@ class TestMCPMetaTraceCarrier: assert _mcp_meta_trace_carrier(None) is None assert _mcp_meta_trace_carrier(SimpleNamespace(meta=None)) is None - only_progress = RequestParams.Meta.model_validate({"progressToken": "p1"}) + only_progress = CallToolRequestParams.model_validate({"name": "t", "_meta": {"progressToken": "p1"}}, by_name=False).meta assert _mcp_meta_trace_carrier(SimpleNamespace(meta=only_progress)) is None @pytest.mark.asyncio -async def test_stateful_mcp_tool_call_uses_current_requests_otel_destinations() -> None: +async def test_stateful_mcp_tool_call_uses_current_requests_otel_destinations(_mcp_request_ctx) -> None: from types import SimpleNamespace - from mcp.server.lowlevel.server import request_ctx - from mcp.shared.context import RequestContext - from litellm.integrations.otel.model.destination import OtelDestination from litellm.integrations.otel.plumbing.context import ( request_destinations, @@ -8441,20 +8467,14 @@ async def test_stateful_mcp_tool_call_uses_current_requests_otel_destinations() set_auth_context(None, raw_headers={}) destinations_token = set_request_destinations((initialized_destination,)) scope = {_MCP_DESTINATIONS_SCOPE_KEY: (current_destination,)} - current_request_context = RequestContext( - request_id=1, - meta=None, - session=SimpleNamespace(), - lifespan_context=None, - request=SimpleNamespace(scope=scope), - ) - request_token = request_ctx.set(current_request_context) + current_request_context = _mcp_request_ctx(request=SimpleNamespace(scope=scope)) + request_token = active_mcp_request_ctx_var.set(current_request_context) try: - result = await mcp_server_tool_call("otelcontext-observe", {}) - assert result.isError is False + result = await mcp_server_tool_call(current_request_context, _call_tool_params("otelcontext-observe", {})) + assert result.is_error is False assert request_destinations() == (initialized_destination,) finally: - request_ctx.reset(request_token) + active_mcp_request_ctx_var.reset(request_token) reset_request_destinations(destinations_token) global_mcp_tool_registry.tools.pop("otelcontext-observe", None) global_mcp_server_manager.registry.pop(server.server_id, None) @@ -8591,7 +8611,7 @@ def test_extract_mcp_tool_result_error_message(): @pytest.mark.asyncio async def test_fire_mcp_tool_call_logging_iserror_logs_failure(): - """Regression test: a CallToolResult with isError=True must go + """Regression test: a CallToolResult with is_error=True must go down the failure logging path (async_failure_handler + post_call_failure_hook), never async_success_handler.""" from litellm.proxy._experimental.mcp_server.exceptions import MCPToolResultError @@ -8631,7 +8651,7 @@ async def test_fire_mcp_tool_call_logging_iserror_logs_failure(): @pytest.mark.asyncio async def test_fire_mcp_tool_call_logging_success_path_unchanged(): - """isError=False must keep today's behavior: success handler fires, no + """is_error=False must keep today's behavior: success handler fires, no failure logging, no post_call_failure_hook.""" from litellm.proxy._experimental.mcp_server.server import ( _fire_mcp_tool_call_logging, @@ -8750,7 +8770,7 @@ def _real_mcp_logging_obj(call_id: str): @pytest.mark.asyncio async def test_fire_mcp_tool_call_logging_iserror_builds_failure_payload(monkeypatch): - """The standard logging payload for an isError=True result must carry + """The standard logging payload for an is_error=True result must carry status='failure' with the tool's error text, so OTel (whose _parse_error keys off status) marks the MCP span ERROR.""" import litellm @@ -8781,7 +8801,7 @@ async def test_fire_mcp_tool_call_logging_iserror_builds_failure_payload(monkeyp @pytest.mark.asyncio async def test_fire_mcp_tool_call_logging_success_builds_success_payload(monkeypatch): - """isError=False still produces a status='success' payload.""" + """is_error=False still produces a status='success' payload.""" import litellm from litellm.proxy._experimental.mcp_server.server import ( _fire_mcp_tool_call_logging, @@ -8807,9 +8827,9 @@ async def test_fire_mcp_tool_call_logging_success_builds_success_payload(monkeyp @pytest.mark.asyncio async def test_fire_mcp_tool_call_logging_iserror_emits_otel_error_span(monkeypatch): - """End-to-end regression for the OTel symptom: an isError=True tool + """End-to-end regression for the OTel symptom: an is_error=True tool result must reach OTel as an MCP span with StatusCode.ERROR and the tool's - error message, while isError=False stays non-error.""" + error message, while is_error=False stays non-error.""" pytest.importorskip("opentelemetry") from opentelemetry.sdk.trace.export.in_memory_span_exporter import ( InMemorySpanExporter, @@ -9054,7 +9074,7 @@ async def test_aggregate_listing_reports_per_server_outcomes(): tool1 = MagicMock() tool1.name = "working_tool_1" tool1.description = "Working tool 1" - tool1.inputSchema = {} + tool1.input_schema = {} return [tool1] raise MCPServerListError(ServerListFault(tag="upstream_error", status_code=500), server.name) @@ -9103,7 +9123,7 @@ async def test_outcome_keys_use_display_prefix_never_canonical_names(): @pytest.mark.asyncio -async def test_handle_list_tools_attaches_outcome_meta(): +async def test_handle_list_tools_attaches_outcome_meta(_mcp_request_ctx): """The protocol handler returns a ListToolsResult whose _meta carries the per-server outcomes, so MCP clients can tell a degraded listing from a genuinely empty one.""" try: @@ -9139,7 +9159,7 @@ async def test_handle_list_tools_attaches_outcome_meta(): new=AsyncMock(return_value=listing), ), ): - result = await handle_list_tools() + result = await handle_list_tools(_mcp_request_ctx(), _paged_params()) assert isinstance(result, ListToolsResult) wire = result.model_dump(by_alias=True) @@ -9900,7 +9920,7 @@ async def test_list_tools_injects_byok_credential_for_non_oauth2_auth_types(auth tool = MagicMock() tool.name = f"{server.alias}-toolA" if add_prefix else "toolA" tool.description = "desc" - tool.inputSchema = {} + tool.input_schema = {} return [tool] mock_manager = MagicMock() @@ -9928,3 +9948,83 @@ async def test_list_tools_injects_byok_credential_for_non_oauth2_auth_types(auth assert seen_auth_headers == ["personal-api-key"] assert [tool.name for tool in listing.tools] == ["byok-toolA"] + + +@pytest.mark.asyncio +async def test_active_request_ctx_var_feeds_get_current_session(_mcp_request_ctx) -> None: + from litellm.proxy._experimental.mcp_server.server import _get_current_session + + session = SimpleNamespace() + ctx = _mcp_request_ctx(session=session) + token = active_mcp_request_ctx_var.set(ctx) + try: + assert _get_current_session() is session + finally: + active_mcp_request_ctx_var.reset(token) + assert _get_current_session() is None + + +@pytest.mark.asyncio +async def test_active_request_ctx_var_feeds_auth_resolution_recording(_mcp_request_ctx) -> None: + from starlette.requests import Request + + from litellm.proxy._experimental.mcp_server.mcp_debug import ( + MCP_AUTH_DIAGNOSTICS_SCOPE_KEY, + MCPAuthDiagnostics, + record_auth_resolution, + ) + from litellm.proxy._experimental.mcp_server.outbound_credentials.types import AuthResolution + + diagnostics = MCPAuthDiagnostics() + ctx = _mcp_request_ctx(request=Request({"type": "http", MCP_AUTH_DIAGNOSTICS_SCOPE_KEY: diagnostics})) + token = active_mcp_request_ctx_var.set(ctx) + try: + record_auth_resolution("s1", AuthResolution.static_token) + finally: + active_mcp_request_ctx_var.reset(token) + + assert diagnostics.resolution() == "static-token" + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("header_value", "expected_rejected"), + [ + ("2025-06-18", False), + ("2025-11-25", False), + ("2026-07-28", True), + ("1999-01-01", True), + ], +) +async def test_streamable_http_rejects_modern_protocol_version(header_value: str, expected_rejected: bool) -> None: + from litellm.proxy._experimental.mcp_server import server as mcp_module + from litellm.proxy._experimental.mcp_server.server import unsupported_protocol_version + + scope: Scope = { + "type": "http", + "method": "POST", + "path": "/mcp", + "headers": [(b"mcp-protocol-version", header_value.encode("latin-1"))], + } + assert (unsupported_protocol_version(scope) == header_value) is expected_rejected + + if not expected_rejected: + return + + sent: list[Message] = [] + + async def receive() -> Message: + return {"type": "http.request", "body": b"", "more_body": False} + + async def send(message: Message) -> None: + sent.append(message) + + await mcp_module.handle_streamable_http_mcp(scope, receive, send) + + start = next(m for m in sent if m["type"] == "http.response.start") + assert start["status"] == 400 + body = json.loads(b"".join(m.get("body", b"") for m in sent if m["type"] == "http.response.body")) + assert body["error"]["code"] == INVALID_REQUEST + assert header_value in body["error"]["message"] + for version in body["error"]["message"].split("supported: ")[1].split(", "): + assert version in HANDSHAKE_PROTOCOL_VERSIONS diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py index d449ad06642..dc1eed9ed7f 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py @@ -1,5 +1,6 @@ import importlib import asyncio +import functools import json import logging import os @@ -22,7 +23,10 @@ from litellm.proxy._experimental.mcp_server.faults.list_outcomes import ServerLi # Add the parent directory to the path so we can import litellm +import contextlib + import httpx +import httpx2 from mcp import ReadResourceResult, Resource from mcp.types import ( CallToolResult, @@ -81,6 +85,8 @@ def _reload_mcp_manager_module(): return reloaded + + @pytest.fixture(autouse=True) def enable_eager_mcp_oauth_discovery(monkeypatch): monkeypatch.setenv("LITELLM_MCP_OAUTH_DISCOVERY_ON_STARTUP", "1") @@ -416,10 +422,10 @@ class TestMCPServerManager: assert "gateway-client" in dump assert "https://org-idp.example/oauth2/token" in dump - async def test_load_servers_from_config_warns_on_invalid_alias(self, caplog): + async def test_load_servers_from_config_warns_on_invalid_alias(self, config_only_mcp_manager_factory, caplog): """Invalid aliases from config should emit warnings during load.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() config = { "validserver": { "alias": "bad/name", @@ -434,10 +440,10 @@ class TestMCPServerManager: assert any("invalid alias 'bad/name'" in message for message in caplog.messages) @pytest.mark.asyncio - async def test_load_servers_from_config_accepts_valid_alias(self, caplog): + async def test_load_servers_from_config_accepts_valid_alias(self, config_only_mcp_manager_factory, caplog): """Valid aliases should be accepted and populate the registry.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() config = { "validserver": { "alias": "friendly_alias", @@ -1207,8 +1213,8 @@ class TestMCPServerManager: assert server.scopes == ["read"] @pytest.mark.asyncio - async def test_load_servers_from_config_non_oauth2_needs_no_flow(self): - manager = MCPServerManager() + async def test_load_servers_from_config_non_oauth2_needs_no_flow(self, config_only_mcp_manager_factory): + manager = config_only_mcp_manager_factory() config = { "apiserver": { "url": "https://example.com/mcp", @@ -1254,10 +1260,10 @@ class TestMCPServerManager: assert not any("oauth2_id_jag" in message for message in caplog.messages) @pytest.mark.asyncio - async def test_load_servers_from_config_does_not_warn_for_api_key_with_google_sso(self, monkeypatch, caplog): + async def test_load_servers_from_config_does_not_warn_for_api_key_with_google_sso(self, config_only_mcp_manager_factory, monkeypatch, caplog): self._clear_sso_env(monkeypatch) monkeypatch.setenv("GOOGLE_CLIENT_ID", "google-cid") - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() config = { "api_key_server": { "url": "https://example.com/mcp", @@ -1394,9 +1400,9 @@ class TestMCPServerManager: assert server.is_dcr_bridge is False @pytest.mark.asyncio - async def test_load_servers_from_config_coerces_cost_string_to_float(self): + async def test_load_servers_from_config_coerces_cost_string_to_float(self, config_only_mcp_manager_factory): """YAML 1.1 parses `7e-05` as a string; ingest must coerce it to float.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() config = { "google_maps": { "url": "https://example.com/mcp", @@ -1420,9 +1426,9 @@ class TestMCPServerManager: assert isinstance(cost_info["tool_name_to_cost_per_query"]["geocode"], float) @pytest.mark.asyncio - async def test_load_servers_from_config_sets_token_endpoint_auth_method(self): + async def test_load_servers_from_config_sets_token_endpoint_auth_method(self, config_only_mcp_manager_factory): """token_endpoint_auth_method from config is carried onto the MCPServer (LIT-4091).""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() config = { "basic_provider": { "url": "https://example.com/mcp", @@ -1899,7 +1905,7 @@ class TestMCPServerManager: with patch.object(_mgr_mod, "verbose_logger") as mock_log: result = await self._run_call_regular(manager, server) - assert result.isError is True + assert result.is_error is True # A genuine non-auth failure keeps operator visibility at warning level, since call_tool's # raise_on_error demoted the client-layer error log to debug. assert mock_log.warning.called @@ -1933,7 +1939,7 @@ class TestMCPServerManager: proxy_logging_obj=None, ) - assert result.isError is False + assert result.is_error is False assert mock_client.call_tool.call_args.kwargs.get("raise_on_error") is not True def _token_exchange_server(self, server_id: str) -> "MCPServer": @@ -6093,17 +6099,17 @@ class TestMCPServerManager: tool1 = MagicMock() tool1.name = "allowed_tool_1" tool1.description = "This tool is allowed" - tool1.inputSchema = {} + tool1.input_schema = {} tool2 = MagicMock() tool2.name = "blocked_tool" tool2.description = "This tool is not allowed" - tool2.inputSchema = {} + tool2.input_schema = {} tool3 = MagicMock() tool3.name = "allowed_tool_2" tool3.description = "This tool is also allowed" - tool3.inputSchema = {} + tool3.input_schema = {} # Mock the global_mcp_server_manager._get_tools_from_server from litellm.proxy._experimental.mcp_server import rest_endpoints @@ -6143,17 +6149,17 @@ class TestMCPServerManager: tool1 = MagicMock() tool1.name = "tool_1" tool1.description = "Tool 1" - tool1.inputSchema = {} + tool1.input_schema = {} tool2 = MagicMock() tool2.name = "tool_2" tool2.description = "Tool 2" - tool2.inputSchema = {} + tool2.input_schema = {} tool3 = MagicMock() tool3.name = "tool_3" tool3.description = "Tool 3" - tool3.inputSchema = {} + tool3.input_schema = {} # Mock the global_mcp_server_manager._get_tools_from_server from litellm.proxy._experimental.mcp_server import rest_endpoints @@ -6193,12 +6199,12 @@ class TestMCPServerManager: tool1 = MagicMock() tool1.name = "tool_1" tool1.description = "Tool 1" - tool1.inputSchema = {} + tool1.input_schema = {} tool2 = MagicMock() tool2.name = "tool_2" tool2.description = "Tool 2" - tool2.inputSchema = {} + tool2.input_schema = {} # Mock the global_mcp_server_manager._get_tools_from_server from litellm.proxy._experimental.mcp_server import rest_endpoints @@ -6538,7 +6544,7 @@ class TestMCPServerManager: # Return a mock CallToolResult result = MagicMock(spec=CallToolResult) result.content = [{"type": "text", "text": "Tool executed successfully"}] - result.isError = False + result.is_error = False return result mock_client.call_tool.side_effect = mock_call_tool @@ -6569,7 +6575,7 @@ class TestMCPServerManager: # Verify the result assert result is not None - assert result.isError is False + assert result.is_error is False assert len(result.content) > 0 # Verify the MCP client call was awaited exactly once @@ -7887,9 +7893,9 @@ class TestMCPServerTimestamps: assert client.timeout == 0.0 @pytest.mark.asyncio - async def test_load_servers_from_config_preserves_timeout(self): + async def test_load_servers_from_config_preserves_timeout(self, config_only_mcp_manager_factory): """timeout from proxy config is loaded into MCPServer.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() config = { "my_server": { "url": "https://example.com/mcp", @@ -8302,9 +8308,9 @@ class TestMCPServerManagerUpstreamInstructionsCache: assert manager._upstream_initialize_instructions_by_server_id.get("srv") is None @pytest.mark.asyncio - async def test_load_servers_from_config_clears_cache(self): + async def test_load_servers_from_config_clears_cache(self, config_only_mcp_manager_factory): """Reloading config clears any previously cached upstream instructions.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() manager._upstream_initialize_instructions_by_server_id["old"] = "stale" await manager.load_servers_from_config( mcp_servers_config={ @@ -8317,9 +8323,9 @@ class TestMCPServerManagerUpstreamInstructionsCache: assert manager._upstream_initialize_instructions_by_server_id.get("old") is None @pytest.mark.asyncio - async def test_load_servers_reads_instructions_from_config(self): + async def test_load_servers_reads_instructions_from_config(self, config_only_mcp_manager_factory): """instructions field from YAML config is persisted on the MCPServer.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config( mcp_servers_config={ "srv_a": { @@ -9989,7 +9995,7 @@ class TestOBOCallToolRetry: user_api_key_auth=None, ) - assert result.isError is True + assert result.is_error is True manager._cred_provider.invalidate_credentials.assert_not_awaited() manager._create_mcp_client.assert_not_awaited() assert first.attempts == 1 @@ -10014,7 +10020,7 @@ class TestOBOCallToolRetry: user_api_key_auth=None, ) - assert result.isError is True + assert result.is_error is True manager._create_mcp_client.assert_awaited_once() assert first.attempts == 1 and retry.attempts == 1 @@ -10093,7 +10099,7 @@ class TestOBOConcurrencyLimit: assert peak_while_blocked == max_concurrent assert inflight["current"] == 0 - assert all(result.isError is False for result in results) + assert all(result.is_error is False for result in results) class TestOBOEndpointDiscovery: @@ -11219,7 +11225,7 @@ class TestOpenAPIRegistryKeyMatchesRegistration: result = await self._call(server, registered_key, "list_pets") - assert result.isError is False + assert result.is_error is False assert result.content[0].text == "dispatched" @pytest.mark.asyncio @@ -11236,7 +11242,7 @@ class TestOpenAPIRegistryKeyMatchesRegistration: result = await self._call(server, registered_key, "read_wiki_contents") - assert result.isError is False + assert result.is_error is False assert result.content[0].text == "dispatched" @pytest.mark.asyncio @@ -11259,7 +11265,7 @@ class TestOpenAPIRegistryKeyMatchesRegistration: result = await self._call(server, registered_key, "petstore-list_pets") - assert result.isError is False + assert result.is_error is False assert result.content[0].text == "dispatched" @pytest.mark.asyncio @@ -11282,7 +11288,7 @@ class TestOpenAPIRegistryKeyMatchesRegistration: result = await self._call(server, registered_key, "list_pets") - assert result.isError is False + assert result.is_error is False assert result.content[0].text == "dispatched" @pytest.mark.asyncio @@ -11299,7 +11305,7 @@ class TestOpenAPIRegistryKeyMatchesRegistration: result = await self._call(server, "petstore-list_pets", "delete_pet") - assert result.isError is True + assert result.is_error is True assert "not found in registry" in result.content[0].text @@ -11796,7 +11802,7 @@ class TestOpenApiHandlerRelaysUpstreamAuth: with patch.object(global_mcp_tool_registry, "get_tool", return_value=tool): result = await manager._call_openapi_tool_handler(self._server(), "list_reports", {}) - assert result.isError is True + assert result.is_error is True assert "upstream returned HTTP 503" in result.content[0].text @@ -11814,9 +11820,9 @@ class TestConfigServerIdPinning: } @pytest.mark.asyncio - async def test_derived_id_churns_when_connection_fields_change(self): + async def test_derived_id_churns_when_connection_fields_change(self, config_only_mcp_manager_factory): """The behavior the pin exists to escape: editing the url mints a brand-new id.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config()) before = next(iter(manager.config_mcp_servers)) @@ -11828,8 +11834,8 @@ class TestConfigServerIdPinning: assert before != after @pytest.mark.asyncio - async def test_pinned_id_survives_url_transport_auth_and_alias_edits(self): - manager = MCPServerManager() + async def test_pinned_id_survives_url_transport_auth_and_alias_edits(self, config_only_mcp_manager_factory): + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config(server_id="docs-prod-1")) assert list(manager.config_mcp_servers) == ["docs-prod-1"] @@ -11850,8 +11856,8 @@ class TestConfigServerIdPinning: assert manager.config_mcp_servers["docs-prod-1"].url == "https://prod.example.com/mcp" @pytest.mark.asyncio - async def test_absent_server_id_keeps_the_derived_hash(self): - manager = MCPServerManager() + async def test_absent_server_id_keeps_the_derived_hash(self, config_only_mcp_manager_factory): + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config()) @@ -11866,15 +11872,15 @@ class TestConfigServerIdPinning: @pytest.mark.asyncio @pytest.mark.parametrize("bad_value", ["", " ", 123, True, ["docs-prod-1"]]) - async def test_blank_or_non_string_server_id_is_rejected(self, bad_value: Any): - manager = MCPServerManager() + async def test_blank_or_non_string_server_id_is_rejected(self, config_only_mcp_manager_factory, bad_value: Any): + manager = config_only_mcp_manager_factory() with pytest.raises(ValueError, match="server_id must be a non-empty string"): await manager.load_servers_from_config(self._config(server_id=bad_value)) @pytest.mark.asyncio - async def test_two_servers_pinning_the_same_id_are_rejected(self): - manager = MCPServerManager() + async def test_two_servers_pinning_the_same_id_are_rejected(self, config_only_mcp_manager_factory): + manager = config_only_mcp_manager_factory() config: Dict[str, Any] = { "docs_server": {"url": "https://a.example.com/mcp", "server_id": "shared-id"}, "wiki_server": {"url": "https://b.example.com/mcp", "server_id": "shared-id"}, @@ -11884,9 +11890,9 @@ class TestConfigServerIdPinning: await manager.load_servers_from_config(config) @pytest.mark.asyncio - async def test_pinned_id_colliding_with_a_derived_id_is_rejected(self): + async def test_pinned_id_colliding_with_a_derived_id_is_rejected(self, config_only_mcp_manager_factory): """A pin that lands on another entry's derived hash collides just as hard.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() derived = manager._generate_stable_server_id( server_name="docs_server", url="https://a.example.com/mcp", @@ -11903,14 +11909,14 @@ class TestConfigServerIdPinning: await manager.load_servers_from_config(config) @pytest.mark.asyncio - async def test_pinned_id_colliding_with_a_db_backed_server_is_rejected(self): + async def test_pinned_id_colliding_with_a_db_backed_server_is_rejected(self, config_only_mcp_manager_factory): """get_registry() is ``config | registry``, so the db row would hide the config server. The registry is seeded by hand because on a real startup the config loads before the database does, so this check only fires on a later reload. The startup ordering is covered by ``test_db_row_arriving_on_a_pinned_config_id_warns``; the warning there is not redundant. """ - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() manager.registry["db-uuid-1"] = MCPServer( server_id="db-uuid-1", name="db_server", @@ -11922,9 +11928,9 @@ class TestConfigServerIdPinning: await manager.load_servers_from_config(self._config(server_id="db-uuid-1")) @pytest.mark.asyncio - async def test_derived_id_matching_a_db_backed_server_is_not_rejected(self): + async def test_derived_id_matching_a_db_backed_server_is_not_rejected(self, config_only_mcp_manager_factory): """Only a pinned id is an authoring error; a hash collision must not fail startup.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() derived = manager._generate_stable_server_id( server_name="docs_server", url="https://example.com/mcp", @@ -11944,8 +11950,8 @@ class TestConfigServerIdPinning: assert derived in manager.config_mcp_servers @pytest.mark.asyncio - async def test_pinned_id_is_stripped_of_surrounding_whitespace(self): - manager = MCPServerManager() + async def test_pinned_id_is_stripped_of_surrounding_whitespace(self, config_only_mcp_manager_factory): + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config(server_id=" docs-prod-1 ")) @@ -11985,9 +11991,9 @@ class TestConfigServerIdPinning: await manager.reload_servers_from_database() @pytest.mark.asyncio - async def test_db_row_arriving_on_a_pinned_config_id_warns(self, caplog): + async def test_db_row_arriving_on_a_pinned_config_id_warns(self, config_only_mcp_manager_factory, caplog): """The db row loads after config on startup, so the config server is hidden then, not at load.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config(server_id="docs-prod-1")) with caplog.at_level(logging.WARNING, logger="LiteLLM"): @@ -11997,8 +12003,8 @@ class TestConfigServerIdPinning: assert manager.get_registry()["docs-prod-1"].url == "https://db.example.com/mcp" @pytest.mark.asyncio - async def test_db_row_with_a_distinct_id_does_not_warn(self, caplog): - manager = MCPServerManager() + async def test_db_row_with_a_distinct_id_does_not_warn(self, config_only_mcp_manager_factory, caplog): + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config(server_id="docs-prod-1")) with caplog.at_level(logging.WARNING, logger="LiteLLM"): @@ -12008,9 +12014,9 @@ class TestConfigServerIdPinning: assert set(manager.get_registry()) == {"docs-prod-1", "db-uuid-1"} @pytest.mark.asyncio - async def test_pinned_id_matching_another_entrys_server_name_is_rejected(self): + async def test_pinned_id_matching_another_entrys_server_name_is_rejected(self, config_only_mcp_manager_factory): """expand_permission_list resolves against registry keys first, so this steals the grants.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() with pytest.raises(ValueError, match="server_name or alias of MCP server 'wiki_server'"): await manager.load_servers_from_config( @@ -12025,8 +12031,8 @@ class TestConfigServerIdPinning: ) @pytest.mark.asyncio - async def test_pinned_id_matching_another_entrys_alias_is_rejected(self): - manager = MCPServerManager() + async def test_pinned_id_matching_another_entrys_alias_is_rejected(self, config_only_mcp_manager_factory): + manager = config_only_mcp_manager_factory() with pytest.raises(ValueError, match="server_name or alias of MCP server 'wiki_server'"): await manager.load_servers_from_config( @@ -12045,17 +12051,17 @@ class TestConfigServerIdPinning: ) @pytest.mark.asyncio - async def test_pinning_a_servers_own_name_is_allowed(self): + async def test_pinning_a_servers_own_name_is_allowed(self, config_only_mcp_manager_factory): """The most natural pin an operator writes; it resolves to the same server either way.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config(server_id="docs_server")) assert list(manager.config_mcp_servers) == ["docs_server"] @pytest.mark.asyncio - async def test_pinning_a_servers_own_alias_is_allowed(self): - manager = MCPServerManager() + async def test_pinning_a_servers_own_alias_is_allowed(self, config_only_mcp_manager_factory): + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config(alias="docs", server_id="docs")) @@ -12063,9 +12069,9 @@ class TestConfigServerIdPinning: @pytest.mark.asyncio @pytest.mark.parametrize("aliasing_entry_first", [True, False]) - async def test_pinning_own_name_that_is_another_entrys_alias_is_rejected(self, aliasing_entry_first: bool): + async def test_pinning_own_name_that_is_another_entrys_alias_is_rejected(self, config_only_mcp_manager_factory, aliasing_entry_first: bool): """A grant naming 'docs_server' reaches both servers unpinned; the pin would narrow it to one.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() wiki = ( "wiki_server", {"alias": "docs_server", "url": "https://wiki.example.com/mcp", "transport": MCPTransport.http}, @@ -12079,8 +12085,8 @@ class TestConfigServerIdPinning: await manager.load_servers_from_config(dict((wiki, docs) if aliasing_entry_first else (docs, wiki))) @pytest.mark.asyncio - async def test_pinning_own_name_that_is_another_entrys_mapped_alias_is_rejected(self): - manager = MCPServerManager() + async def test_pinning_own_name_that_is_another_entrys_mapped_alias_is_rejected(self, config_only_mcp_manager_factory): + manager = config_only_mcp_manager_factory() with pytest.raises(ValueError, match="server_name or alias of MCP server 'wiki_server'"): await manager.load_servers_from_config( @@ -12096,9 +12102,9 @@ class TestConfigServerIdPinning: ) @pytest.mark.asyncio - async def test_pinning_own_alias_shared_with_a_later_entry_is_rejected(self): + async def test_pinning_own_alias_shared_with_a_later_entry_is_rejected(self, config_only_mcp_manager_factory): """Nothing rejects duplicate aliases, so the first entry's pin would answer the second's grants.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() with pytest.raises(ValueError, match="server_name or alias of MCP server 'docs_server'"): await manager.load_servers_from_config( @@ -12118,9 +12124,9 @@ class TestConfigServerIdPinning: ) @pytest.mark.asyncio - async def test_own_name_pin_resolves_grants_like_the_unpinned_name(self): + async def test_own_name_pin_resolves_grants_like_the_unpinned_name(self, config_only_mcp_manager_factory): """The negative control: a sole-owner self-pin must keep loading and answer the same grants.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config( { @@ -12138,9 +12144,9 @@ class TestConfigServerIdPinning: assert manager.expand_permission_list(["wiki"]) == [wiki_id] @pytest.mark.asyncio - async def test_derived_id_is_not_checked_against_names(self): + async def test_derived_id_is_not_checked_against_names(self, config_only_mcp_manager_factory): """Unpinned configs must keep loading; only a pinned id can be an authoring error.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config( { @@ -12152,9 +12158,9 @@ class TestConfigServerIdPinning: assert len(manager.config_mcp_servers) == 2 @pytest.mark.asyncio - async def test_shadow_warning_is_not_repeated_on_every_reload(self, caplog): + async def test_shadow_warning_is_not_repeated_on_every_reload(self, config_only_mcp_manager_factory, caplog): """reload_servers_from_database runs on the config-reload timer; one warning, not one a tick.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config(server_id="docs-prod-1")) with caplog.at_level(logging.WARNING, logger="LiteLLM"): @@ -12167,8 +12173,8 @@ class TestConfigServerIdPinning: assert second_round == first_round @pytest.mark.asyncio - async def test_shadow_warning_fires_again_when_the_shadowed_set_changes(self, caplog): - manager = MCPServerManager() + async def test_shadow_warning_fires_again_when_the_shadowed_set_changes(self, config_only_mcp_manager_factory, caplog): + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config(server_id="docs-prod-1")) with caplog.at_level(logging.WARNING, logger="LiteLLM"): @@ -12179,9 +12185,9 @@ class TestConfigServerIdPinning: assert len([m for m in caplog.messages if "database entry takes precedence" in m]) == 2 @pytest.mark.asyncio - async def test_pinned_id_matching_a_mapped_alias_is_rejected(self): + async def test_pinned_id_matching_a_mapped_alias_is_rejected(self, config_only_mcp_manager_factory): """An alias can also arrive from litellm_settings.mcp_aliases; it is reserved just the same.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() with pytest.raises(ValueError, match="server_name or alias of MCP server 'wiki_server'"): await manager.load_servers_from_config( @@ -12197,8 +12203,8 @@ class TestConfigServerIdPinning: ) @pytest.mark.asyncio - async def test_pinning_a_servers_own_mapped_alias_is_allowed(self): - manager = MCPServerManager() + async def test_pinning_a_servers_own_mapped_alias_is_allowed(self, config_only_mcp_manager_factory): + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config( self._config(server_id="docs"), @@ -12208,9 +12214,9 @@ class TestConfigServerIdPinning: assert list(manager.config_mcp_servers) == ["docs"] @pytest.mark.asyncio - async def test_mapped_alias_for_an_unknown_server_reserves_nothing(self): + async def test_mapped_alias_for_an_unknown_server_reserves_nothing(self, config_only_mcp_manager_factory): """A dangling mcp_aliases entry is never applied, so it must not fail an unrelated pin.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config( self._config(server_id="wiki"), @@ -12220,9 +12226,9 @@ class TestConfigServerIdPinning: assert list(manager.config_mcp_servers) == ["wiki"] @pytest.mark.asyncio - async def test_config_id_that_is_a_db_server_name_warns(self, caplog): + async def test_config_id_that_is_a_db_server_name_warns(self, config_only_mcp_manager_factory, caplog): """The mirror of the shadow case: here the config entry captures the db server's grants.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config(server_id="db_server")) with caplog.at_level(logging.WARNING, logger="LiteLLM"): @@ -12231,8 +12237,8 @@ class TestConfigServerIdPinning: assert any("db_server" in m and "name or alias of a database-backed" in m for m in caplog.messages) @pytest.mark.asyncio - async def test_capture_warning_is_not_repeated_on_every_reload(self, caplog): - manager = MCPServerManager() + async def test_capture_warning_is_not_repeated_on_every_reload(self, config_only_mcp_manager_factory, caplog): + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config(server_id="db_server")) with caplog.at_level(logging.WARNING, logger="LiteLLM"): @@ -12242,8 +12248,8 @@ class TestConfigServerIdPinning: assert len([m for m in caplog.messages if "name or alias of a database-backed" in m]) == 1 @pytest.mark.asyncio - async def test_config_id_unrelated_to_db_names_does_not_warn(self, caplog): - manager = MCPServerManager() + async def test_config_id_unrelated_to_db_names_does_not_warn(self, config_only_mcp_manager_factory, caplog): + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config(server_id="docs-prod-1")) with caplog.at_level(logging.WARNING, logger="LiteLLM"): @@ -12252,9 +12258,9 @@ class TestConfigServerIdPinning: assert all("name or alias of a database-backed" not in m for m in caplog.messages) @pytest.mark.asyncio - async def test_mapped_alias_for_a_server_with_its_own_alias_reserves_nothing(self): + async def test_mapped_alias_for_a_server_with_its_own_alias_reserves_nothing(self, config_only_mcp_manager_factory): """load_servers_from_config ignores the mapping when the entry sets alias, so it is free.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config( { @@ -12276,9 +12282,9 @@ class TestConfigServerIdPinning: assert len(manager.config_mcp_servers) == 2 @pytest.mark.asyncio - async def test_only_the_first_mapped_alias_for_a_server_is_reserved(self): + async def test_only_the_first_mapped_alias_for_a_server_is_reserved(self, config_only_mcp_manager_factory): """Only the first mapping is applied, so pinning the second one must still load.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config( { @@ -12295,15 +12301,15 @@ class TestConfigServerIdPinning: assert "wiki_two" in manager.config_mcp_servers @pytest.mark.asyncio - async def test_invalid_name_is_reported_before_any_entry_body_is_read(self): + async def test_invalid_name_is_reported_before_any_entry_body_is_read(self, config_only_mcp_manager_factory): """The identifier index walks every entry up front, so a bad name must still fail on the name.""" with pytest.raises(Exception, match="Server name cannot contain"): - await MCPServerManager().load_servers_from_config({"my-server": None}) + await config_only_mcp_manager_factory().load_servers_from_config({"my-server": None}) @pytest.mark.asyncio - async def test_a_shadowing_db_server_reports_only_the_shadow_warning(self, caplog): + async def test_a_shadowing_db_server_reports_only_the_shadow_warning(self, config_only_mcp_manager_factory, caplog): """The db row wins the id outright, so the capture message would contradict the shadow one.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(self._config(server_id="db_server")) with caplog.at_level(logging.WARNING, logger="LiteLLM"): @@ -12314,9 +12320,9 @@ class TestConfigServerIdPinning: assert manager.get_registry()["db_server"].url == "https://db.example.com/mcp" @pytest.mark.asyncio - async def test_an_explicitly_blank_alias_still_blocks_the_mapping(self): + async def test_an_explicitly_blank_alias_still_blocks_the_mapping(self, config_only_mcp_manager_factory): """The loader only consults mcp_aliases when the key is absent, so a blank alias frees it.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config( { @@ -12338,9 +12344,9 @@ class TestConfigServerIdPinning: assert manager.config_mcp_servers["wiki"].url == "https://example.com/mcp" @pytest.mark.asyncio - async def test_a_row_that_shadows_one_id_still_reports_capturing_another(self, caplog): + async def test_a_row_that_shadows_one_id_still_reports_capturing_another(self, config_only_mcp_manager_factory, caplog): """Skipping is per identifier, not per row, so the second collision is not lost.""" - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config( { "docs_server": { @@ -12710,21 +12716,25 @@ async def test_pre_call_tool_check_honors_guardrail_attached_to_key(monkeypatch, ("none", {"Authorization": "Bearer injected"}, "extra-headers", "Bearer injected"), ], ) -async def test_debug_resolution_matches_final_header_conflict_winner( +async def test_debug_resolution_matches_final_header_conflict_winner(_mcp_request_ctx, config: Literal["stored", "static", "none"], extra_headers: dict[str, str] | None, expected_source: str, expected_authorization: str | None, ) -> None: - from mcp.server.lowlevel.server import request_ctx - from mcp.shared.context import RequestContext + from litellm.proxy._experimental.mcp_server.mcp_context import active_mcp_request_ctx_var from starlette.requests import Request from pydantic import SecretStr from litellm.proxy._experimental.mcp_server.auth.litellm_auth_handler import MCPAuthenticatedUser from litellm.proxy._experimental.mcp_server.mcp_debug import MCP_AUTH_DIAGNOSTICS_SCOPE_KEY, MCPAuthDiagnostics from litellm.proxy._experimental.mcp_server.outbound_credentials import ( - ApiKeyConfig, AuthorizationCodeConfig, NoneConfig, ServerSpec, SharedKey, UpstreamCredentialProvider, + ApiKeyConfig, + AuthorizationCodeConfig, + NoneConfig, + ServerSpec, + SharedKey, + UpstreamCredentialProvider, ) from litellm.proxy._experimental.mcp_server.outbound_credentials.oauth_token_store import OAuthToken from litellm.types.mcp_server.mcp_server_manager import MCPServer @@ -12740,10 +12750,11 @@ async def test_debug_resolution_matches_final_header_conflict_winner( store = Store() context = MCPAuthenticatedUser(UserAPIKeyAuth(user_id="alice")) diagnostics = MCPAuthDiagnostics() - token = request_ctx.set(RequestContext( - request_id=1, meta=None, session=MagicMock(), lifespan_context=None, - request=Request({"type": "http", MCP_AUTH_DIAGNOSTICS_SCOPE_KEY: diagnostics}), - )) + token = active_mcp_request_ctx_var.set( + _mcp_request_ctx( + request=Request({"type": "http", MCP_AUTH_DIAGNOSTICS_SCOPE_KEY: diagnostics}), + ) + ) selected = { "stored": AuthorizationCodeConfig(), "static": ApiKeyConfig(key_source=SharedKey(value=SecretStr("static-token"))), @@ -12752,7 +12763,10 @@ async def test_debug_resolution_matches_final_header_conflict_winner( try: auth, remaining = await MCPServerManager()._resolve_v2_auth( server=MCPServer( - server_id="s", name="s", transport="http", url="https://up.example/mcp", + server_id="s", + name="s", + transport="http", + url="https://up.example/mcp", static_headers={"Authorization": "Bearer configured"}, ), spec=ServerSpec(server_id="s", resource="https://up.example/mcp", config=selected), @@ -12768,31 +12782,37 @@ async def test_debug_resolution_matches_final_header_conflict_winner( assert request.headers.get("Authorization") == expected_authorization assert store.calls == (1 if config == "stored" else 0) finally: - request_ctx.reset(token) + active_mcp_request_ctx_var.reset(token) @pytest.mark.asyncio @pytest.mark.parametrize("transport", ["http", "stdio"]) -async def test_debug_reports_legacy_signing_and_non_http_transport(transport: Literal["http", "stdio"]) -> None: - from mcp.server.lowlevel.server import request_ctx - from mcp.shared.context import RequestContext +async def test_debug_reports_legacy_signing_and_non_http_transport(_mcp_request_ctx, transport: Literal["http", "stdio"]) -> None: + from litellm.proxy._experimental.mcp_server.mcp_context import active_mcp_request_ctx_var from starlette.requests import Request from litellm.proxy._experimental.mcp_server.mcp_debug import MCP_AUTH_DIAGNOSTICS_SCOPE_KEY, MCPAuthDiagnostics from litellm.types.mcp_server.mcp_server_manager import MCPServer diagnostics = MCPAuthDiagnostics() - token = request_ctx.set(RequestContext( - request_id=1, meta=None, session=MagicMock(), lifespan_context=None, - request=Request({"type": "http", MCP_AUTH_DIAGNOSTICS_SCOPE_KEY: diagnostics}), - )) + token = active_mcp_request_ctx_var.set( + _mcp_request_ctx( + request=Request({"type": "http", MCP_AUTH_DIAGNOSTICS_SCOPE_KEY: diagnostics}), + ) + ) try: server = MCPServer( - server_id="signed", name="signed", transport=transport, - url="https://up.example/mcp", auth_type="aws_sigv4", - aws_access_key_id="AKIDEXAMPLE", aws_secret_access_key="test-signing-secret", - aws_region_name="us-east-1", aws_service_name="execute-api", - command="python", args=["-c", "pass"], + server_id="signed", + name="signed", + transport=transport, + url="https://up.example/mcp", + auth_type="aws_sigv4", + aws_access_key_id="AKIDEXAMPLE", + aws_secret_access_key="test-signing-secret", + aws_region_name="us-east-1", + aws_service_name="execute-api", + command="python", + args=["-c", "pass"], ) client = await MCPServerManager()._create_mcp_client(server) if transport == "stdio": @@ -12804,7 +12824,7 @@ async def test_debug_reports_legacy_signing_and_non_http_transport(transport: Li assert request.headers["Authorization"].startswith("AWS4-HMAC-SHA256 ") assert "Credential=AKIDEXAMPLE/" in request.headers["Authorization"] finally: - request_ctx.reset(token) + active_mcp_request_ctx_var.reset(token) @pytest.mark.asyncio @@ -13049,6 +13069,32 @@ class _DiscoveryClock: return self.now +from pydantic import TypeAdapter +from mcp.types import JSONRPCMessage + +_JSONRPC_ADAPTER = TypeAdapter(JSONRPCMessage) + + +@contextlib.contextmanager +def _mcp_upstream(respond): + """Drive the SDK's streamable-HTTP transport off an httpx2 MockTransport; respx only sees httpx.""" + from litellm.experimental_mcp_client.client import MCPClient + + def make_client(self, *args, **kwargs): + return httpx2.AsyncClient( + transport=httpx2.MockTransport(respond), + headers=kwargs.get("headers"), + auth=kwargs.get("auth") or self._resolved_auth or self._aws_auth, + ) + + with ( + patch.object( # test-quality-ok: respx cannot intercept httpx2; inject MockTransport through the client factory + MCPClient, "_create_httpx_client_factory", lambda self: functools.partial(make_client, self) + ) + ): + yield + + class _DiscoveryUpstream: def __init__(self) -> None: self.requests: tuple[tuple[str, str], ...] = () @@ -13057,37 +13103,47 @@ class _DiscoveryUpstream: self.release = asyncio.Event() self.release.set() - async def respond(self, request: httpx.Request) -> httpx.Response: - from mcp.types import JSONRPCMessage, JSONRPCRequest + async def respond(self, request: httpx2.Request) -> httpx2.Response: + from mcp.types import JSONRPCRequest if request.method == "DELETE": - return httpx.Response(200) - payload: Final = JSONRPCMessage.model_validate_json(request.content).root + return httpx2.Response(200) + payload: Final = _JSONRPC_ADAPTER.validate_json(request.content) if not isinstance(payload, JSONRPCRequest): - return httpx.Response(202) + return httpx2.Response(202) self.requests = (*self.requests, (payload.method, request.headers.get("authorization", ""))) if payload.method == "initialize": - return httpx.Response(200, json={ - "jsonrpc": "2.0", "id": payload.id, - "result": {"protocolVersion": "2025-03-26", "serverInfo": {"name": "discovery", "version": "1"}, - "capabilities": {} if self.outcome == "unsupported" else {"prompts": {}, "resources": {}}}, - }) + return httpx2.Response( + 200, + json={ + "jsonrpc": "2.0", + "id": payload.id, + "result": { + "protocolVersion": "2025-03-26", + "serverInfo": {"name": "discovery", "version": "1"}, + "capabilities": {} if self.outcome == "unsupported" else {"prompts": {}, "resources": {}}, + }, + }, + ) self.entered.set() await self.release.wait() if self.outcome == "failure": - return httpx.Response(503) + return httpx2.Response(503) if self.outcome == "cancelled": raise asyncio.CancelledError() if self.outcome == "rejected": - return httpx.Response(200, json={"jsonrpc": "2.0", "id": payload.id, - "error": {"code": -32601, "message": "Unsupported"}}) + return httpx2.Response( + 200, json={"jsonrpc": "2.0", "id": payload.id, "error": {"code": -32601, "message": "Unsupported"}} + ) result: Final = { "prompts/list": {"prompts": [{"name": "example", "description": "original"}]}, "resources/list": {"resources": [{"name": "example", "uri": "test://example", "description": "original"}]}, - "resources/templates/list": {"resourceTemplates": [{"name": "example", "uriTemplate": "test://{name}", "description": "original"}]}, + "resources/templates/list": { + "resourceTemplates": [{"name": "example", "uriTemplate": "test://{name}", "description": "original"}] + }, "tools/list": {"tools": []}, }[payload.method] - return httpx.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": result}) + return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": result}) @property def initializes(self) -> int: @@ -13106,11 +13162,13 @@ async def test_discovery_cache_reuses_raw_results_and_expires(kind: str) -> None clock: Final = _DiscoveryClock() manager: Final = MCPServerManager(discovery_clock=clock) upstream: Final = _DiscoveryUpstream() - operation: Final = {"prompts": manager.get_prompts_from_server, "resources": manager.get_resources_from_server, - "templates": manager.get_resource_templates_from_server}[kind] + operation: Final = { + "prompts": manager.get_prompts_from_server, + "resources": manager.get_resources_from_server, + "templates": manager.get_resource_templates_from_server, + }[kind] server: Final = _discovery_server() - with respx.mock(base_url="https://discovery.example") as router: - router.route().mock(side_effect=upstream.respond) + with _mcp_upstream(upstream.respond): first: Final = await operation(server, None) assert len(first) == 1 assert first[0].name == "discovery-example" @@ -13136,10 +13194,12 @@ async def test_discovery_cache_empty_results_and_failures(kind: str, outcome: st manager: Final = MCPServerManager() upstream: Final = _DiscoveryUpstream() upstream.outcome = outcome - operation: Final = {"prompts": manager.get_prompts_from_server, "resources": manager.get_resources_from_server, - "templates": manager.get_resource_templates_from_server}[kind] - with respx.mock(base_url="https://discovery.example") as router: - router.route().mock(side_effect=upstream.respond) + operation: Final = { + "prompts": manager.get_prompts_from_server, + "resources": manager.get_resources_from_server, + "templates": manager.get_resource_templates_from_server, + }[kind] + with _mcp_upstream(upstream.respond): assert await operation(_discovery_server(), None) == [] assert await operation(_discovery_server(), None) == [] assert upstream.initializes == (2 if outcome == "failure" else 1) @@ -13158,15 +13218,25 @@ async def test_discovery_cache_isolates_forwarded_credentials_and_shares_static_ server: Final = _discovery_server() first_user: Final = UserAPIKeyAuth(user_id="first") second_user: Final = UserAPIKeyAuth(user_id="second") - with respx.mock(base_url="https://discovery.example") as router: - router.route().mock(side_effect=upstream.respond) + with _mcp_upstream(upstream.respond): for user in (first_user, second_user): assert len(await manager.get_prompts_from_server(server, user)) == 1 assert upstream.initializes == 1 for credential in ("first-secret", "second-secret", "first-secret"): - assert len(await manager.get_prompts_from_server(server, first_user, extra_headers={"Authorization": credential})) == 1 + assert ( + len( + await manager.get_prompts_from_server( + server, first_user, extra_headers={"Authorization": credential} + ) + ) + == 1 + ) assert upstream.initializes == 3 - assert {auth for method, auth in upstream.requests if method == "prompts/list"} == {"", "first-secret", "second-secret"} + assert {auth for method, auth in upstream.requests if method == "prompts/list"} == { + "", + "first-secret", + "second-secret", + } @pytest.mark.asyncio @@ -13176,9 +13246,10 @@ async def test_discovery_cache_coalesces_and_survives_waiter_cancellation() -> N manager: Final = MCPServerManager() upstream: Final = _DiscoveryUpstream() upstream.release.clear() - with respx.mock(base_url="https://discovery.example") as router: - router.route().mock(side_effect=upstream.respond) - tasks: Final = tuple(asyncio.create_task(manager.get_prompts_from_server(_discovery_server(), None)) for _ in range(10)) + with _mcp_upstream(upstream.respond): + tasks: Final = tuple( + asyncio.create_task(manager.get_prompts_from_server(_discovery_server(), None)) for _ in range(10) + ) await asyncio.wait_for(upstream.entered.wait(), timeout=5) tasks[0].cancel() with pytest.raises(asyncio.CancelledError): @@ -13199,8 +13270,7 @@ async def test_discovery_cache_invalidation_during_fetch_does_not_repopulate_old manager: Final = MCPServerManager() upstream: Final = _DiscoveryUpstream() upstream.release.clear() - with respx.mock(base_url="https://discovery.example") as router: - router.route().mock(side_effect=upstream.respond) + with _mcp_upstream(upstream.respond): task: Final = asyncio.create_task(manager.get_prompts_from_server(_discovery_server(), None)) await asyncio.wait_for(upstream.entered.wait(), timeout=5) manager._invalidate_discovery_lists("discovery") @@ -13220,8 +13290,7 @@ async def test_discovery_cache_can_be_disabled(monkeypatch: pytest.MonkeyPatch) monkeypatch.setenv("LITELLM_MCP_DISCOVERY_CACHE_TTL", "0") manager: Final = MCPServerManager() upstream: Final = _DiscoveryUpstream() - with respx.mock(base_url="https://discovery.example") as router: - router.route().mock(side_effect=upstream.respond) + with _mcp_upstream(upstream.respond): assert len(await manager.get_prompts_from_server(_discovery_server(), None)) == 1 assert len(await manager.get_prompts_from_server(_discovery_server(), None)) == 1 assert upstream.initializes == 2 @@ -13345,32 +13414,41 @@ async def test_discovery_cache_tracks_resolved_credentials_across_workers() -> N source: Final = CredentialSource() managers: Final = (MCPServerManager(cred_provider=source), MCPServerManager(cred_provider=source)) server: Final = MCPServer( - server_id="discovery", name="discovery", url="https://discovery.example/mcp", transport=MCPTransport.http, - auth_type=MCPAuth.oauth2, oauth2_flow="authorization_code", client_id="discovery-client", - authorization_url="https://discovery.example/authorize", token_url="https://discovery.example/token", + server_id="discovery", + name="discovery", + url="https://discovery.example/mcp", + transport=MCPTransport.http, + auth_type=MCPAuth.oauth2, + oauth2_flow="authorization_code", + client_id="discovery-client", + authorization_url="https://discovery.example/authorize", + token_url="https://discovery.example/token", ) user: Final = UserAPIKeyAuth(user_id="same-user", api_key="same-key") upstream: Final = _DiscoveryUpstream() - async def respond(request: httpx.Request) -> httpx.Response: + async def respond(request: httpx2.Request) -> httpx2.Response: response: Final = await upstream.respond(request) if '"prompts/list"' not in request.content.decode(): return response - from mcp.types import JSONRPCMessage, JSONRPCRequest + from mcp.types import JSONRPCRequest - payload: Final = JSONRPCMessage.model_validate_json(request.content).root + payload: Final = _JSONRPC_ADAPTER.validate_json(request.content) assert isinstance(payload, JSONRPCRequest) name: Final = {"Bearer token-a": "account-a", "Bearer token-b": "account-b"}[request.headers["authorization"]] - return httpx.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": {"prompts": [{"name": name}]}}) + return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": {"prompts": [{"name": name}]}}) - with respx.mock(base_url="https://discovery.example") as router: - router.route().mock(side_effect=respond) + with _mcp_upstream(respond): for manager in managers: - assert [item.name for item in await manager.get_prompts_from_server(server, user)] == ["discovery-account-a"] + assert [item.name for item in await manager.get_prompts_from_server(server, user)] == [ + "discovery-account-a" + ] assert upstream.initializes == 2 source.token = "token-b" for manager in managers: - assert [item.name for item in await manager.get_prompts_from_server(server, user)] == ["discovery-account-b"] + assert [item.name for item in await manager.get_prompts_from_server(server, user)] == [ + "discovery-account-b" + ] assert upstream.initializes == 4 source.token = None for manager in managers: @@ -13397,14 +13475,19 @@ async def test_discovery_resolves_stored_oauth_for_the_requesting_user() -> None store: Final = TokenStore() manager: Final = MCPServerManager(per_user_oauth_token_store=store) server: Final = MCPServer( - server_id="discovery", name="discovery", url="https://discovery.example/mcp", transport=MCPTransport.http, - auth_type=MCPAuth.oauth2, oauth2_flow="authorization_code", client_id="discovery-client", - authorization_url="https://discovery.example/authorize", token_url="https://discovery.example/token", + server_id="discovery", + name="discovery", + url="https://discovery.example/mcp", + transport=MCPTransport.http, + auth_type=MCPAuth.oauth2, + oauth2_flow="authorization_code", + client_id="discovery-client", + authorization_url="https://discovery.example/authorize", + token_url="https://discovery.example/token", ) user: Final = UserAPIKeyAuth(user_id="requesting-user") upstream: Final = _DiscoveryUpstream() - with respx.mock(base_url="https://discovery.example") as router: - router.route().mock(side_effect=upstream.respond) + with _mcp_upstream(upstream.respond): assert len(await manager.get_prompts_from_server(server, user)) == 1 assert len(await manager.get_prompts_from_server(server, user)) == 1 assert store.calls == (("requesting-user", "discovery"), ("requesting-user", "discovery")) @@ -13506,7 +13589,7 @@ class TestProtectedCredentialPreparation: if dispatch == "managed" else await _handle_local_mcp_tool(add_server_prefix_to_name("echo", get_server_prefix(server)), {}) ) - assert result.isError is True + assert result.is_error is True assert "requires a usable upstream credential" in result.content[0].text assert destination.call_count == 0 @@ -13937,5 +14020,5 @@ async def test_request_selected_during_guardrail_runs_concurrently_with_tool(mon ), timeout=5) assert tool_started.is_set() assert guardrail_started.is_set() is selected - assert result.isError is False + assert result.is_error is False assert result.content[0].text == "executed" diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sigv4_auth.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sigv4_auth.py index e814425c9a2..8cf3bc6fcc7 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sigv4_auth.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sigv4_auth.py @@ -1,7 +1,7 @@ """ Tests for AWS SigV4 authentication in MCP client. -Tests the MCPSigV4Auth httpx.Auth subclass that enables per-request +Tests the MCPSigV4Auth httpx2.Auth subclass that enables per-request SigV4 signing for Bedrock AgentCore MCP servers, plus DB/UI path tests for credential encryption, merge-on-update, and build_from_table. """ @@ -11,7 +11,7 @@ import json import pytest from unittest.mock import patch, MagicMock, AsyncMock -import httpx +import httpx2 from litellm.experimental_mcp_client.client import MCPSigV4Auth, MCPClient from litellm.types.mcp import MCPAuth, MCPTransport @@ -103,7 +103,7 @@ class TestMCPSigV4Auth: aws_service_name="bedrock-agentcore", ) - request = httpx.Request( + request = httpx2.Request( method="POST", url="https://bedrock-agentcore.us-east-1.amazonaws.com/runtimes/test/invocations", headers={"Content-Type": "application/json"}, @@ -128,13 +128,13 @@ class TestMCPSigV4Auth: aws_region_name="us-east-1", ) - request1 = httpx.Request( + request1 = httpx2.Request( method="POST", url="https://example.com/mcp", headers={"Content-Type": "application/json"}, content=b'{"jsonrpc":"2.0","method":"tools/list","id":1}', ) - request2 = httpx.Request( + request2 = httpx2.Request( method="POST", url="https://example.com/mcp", headers={"Content-Type": "application/json"}, @@ -156,7 +156,7 @@ class TestMCPSigV4Auth: aws_region_name="us-east-1", ) - request = httpx.Request( + request = httpx2.Request( method="POST", url="https://example.com/mcp", headers={"Content-Type": "application/json"}, @@ -265,7 +265,7 @@ class TestMCPSigV4AssumeRole: aws_service_name="bedrock-agentcore", ) - request = httpx.Request( + request = httpx2.Request( method="POST", url="https://bedrock-agentcore.us-east-1.amazonaws.com/runtimes/test/invocations", headers={"Content-Type": "application/json"}, @@ -306,7 +306,7 @@ class TestMCPClientSigV4Integration: def test_mcp_client_stores_aws_auth(self): """MCPClient stores the aws_auth parameter.""" - mock_auth = MagicMock(spec=httpx.Auth) + mock_auth = MagicMock(spec=httpx2.Auth) client = MCPClient( server_url="https://example.com/mcp", transport_type=MCPTransport.http, @@ -330,7 +330,7 @@ class TestMCPClientSigV4Integration: factory = client._create_httpx_client_factory() httpx_client = factory( headers={"Content-Type": "application/json"}, - timeout=httpx.Timeout(30.0), + timeout=httpx2.Timeout(30.0), ) # Verify the auth object was actually wired into the httpx client @@ -342,7 +342,7 @@ class TestMCPClientSigV4Integration: aws_access_key_id="AKIAIOSFODNN7EXAMPLE", aws_secret_access_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", ) - explicit_auth = MagicMock(spec=httpx.Auth) + explicit_auth = MagicMock(spec=httpx2.Auth) client = MCPClient( server_url="https://example.com/mcp", @@ -353,7 +353,7 @@ class TestMCPClientSigV4Integration: factory = client._create_httpx_client_factory() httpx_client = factory( headers={"Content-Type": "application/json"}, - timeout=httpx.Timeout(30.0), + timeout=httpx2.Timeout(30.0), auth=explicit_auth, ) @@ -370,7 +370,7 @@ class TestMCPClientSigV4Integration: factory = client._create_httpx_client_factory() httpx_client = factory( headers={"Content-Type": "application/json"}, - timeout=httpx.Timeout(30.0), + timeout=httpx2.Timeout(30.0), ) # No auth should be set when aws_auth is not configured assert httpx_client._auth is None @@ -380,7 +380,7 @@ class TestMCPServerManagerSigV4: """Tests for MCPServerManager config loading with SigV4.""" @pytest.mark.asyncio - async def test_load_config_with_aws_sigv4(self): + async def test_load_config_with_aws_sigv4(self, config_only_mcp_manager_factory): """Config loading correctly parses aws_sigv4 auth type and AWS fields.""" from litellm.proxy._experimental.mcp_server.mcp_server_manager import ( MCPServerManager, @@ -398,7 +398,7 @@ class TestMCPServerManagerSigV4: } } - manager = MCPServerManager() + manager = config_only_mcp_manager_factory() await manager.load_servers_from_config(config) server = next(iter(manager.config_mcp_servers.values())) diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_tool_search.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_tool_search.py index b8935d07774..cb43d2c2592 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_tool_search.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_tool_search.py @@ -85,6 +85,13 @@ FAKE_VECTORS: dict[str, Vector] = { } + + +def _paged_params(): + from mcp.types import PaginatedRequestParams + + return PaginatedRequestParams() + class RecordingEmbedder: def __init__(self) -> None: self.calls: list[tuple[str, ...]] = [] @@ -113,7 +120,7 @@ class TestSearchMcpTools: assert _names(results) == [FX_TOOL.name, WEATHER_TOOL.name, CALENDAR_TOOL.name] assert not isinstance(results, EmbeddingFailed) assert results[0]["score"] > results[1]["score"] > results[2]["score"] - assert results[0]["inputSchema"] == FX_TOOL.inputSchema + assert results[0]["inputSchema"] == FX_TOOL.input_schema @pytest.mark.asyncio async def test_similarity_threshold_drops_weak_matches(self) -> None: @@ -313,10 +320,10 @@ class TestGetVirtualToolDefinitions: for definition in get_virtual_tool_definitions(): tool = Tool.model_validate(definition) - required_arguments = {name: "x" for name in tool.inputSchema["required"]} - validate(instance=required_arguments, schema=tool.inputSchema) + required_arguments = {name: "x" for name in tool.input_schema["required"]} + validate(instance=required_arguments, schema=tool.input_schema) with pytest.raises(ValidationError): - validate(instance={}, schema=tool.inputSchema) + validate(instance={}, schema=tool.input_schema) def test_all_tools_have_description(self) -> None: for tool in get_virtual_tool_definitions(): @@ -562,7 +569,7 @@ class TestCallToolRestApiVirtualTools: mock_tool = MagicMock() mock_tool.name = "github-create_issue" mock_tool.description = "Create a GitHub issue" - mock_tool.inputSchema = {"type": "object", "properties": {}} + mock_tool.input_schema = {"type": "object", "properties": {}} with patch( "litellm.proxy._experimental.mcp_server.server._list_mcp_tools", @@ -633,7 +640,7 @@ class TestCallToolRestApiVirtualTools: mock_fire_logging.assert_awaited_once() assert mock_execute.await_args.kwargs["name"] == "github-create_issue" - assert result.isError is False + assert result.is_error is False assert result.content[0].text == "Issue created" @pytest.mark.asyncio @@ -730,7 +737,7 @@ class TestCallToolRestApiVirtualTools: ): result = await self._get_call_fn()(request=request, user_api_key_dict=user_api_key_dict) - assert result.isError is False + assert result.is_error is False assert mock_search.await_args.kwargs["user_api_key_dict"] is user_api_key_dict assert json.loads(result.content[0].text) == [ { @@ -766,7 +773,7 @@ class TestCallToolRestApiVirtualTools: ) as mock_search: result = await self._get_call_fn()(request=request, user_api_key_dict=user_api_key_dict) - assert result.isError is False + assert result.is_error is False assert mock_search.await_args.kwargs["top_k"] == DEFAULT_SKILL_SEARCH_TOP_K assert mock_search.await_args.kwargs["query"] == "translate a document" @@ -790,7 +797,7 @@ class TestCallToolRestApiVirtualTools: ): result = await self._get_call_fn()(request=request, user_api_key_dict=user_api_key_dict) - assert result.isError is True + assert result.is_error is True assert result.content[0].text == "set agent_search_embedding_model" def _semantic_request(self, query: str = "FX") -> MagicMock: @@ -835,7 +842,7 @@ class TestCallToolRestApiVirtualTools: assert mock_list.await_args.kwargs["user_api_key_auth"] is user_api_key_dict assert key_limits.pre_call_hook.await_args.kwargs["call_type"] == "aembedding" assert key_limits.pre_call_hook.await_args.kwargs["data"]["model"] == "emb" - assert result.isError is False + assert result.is_error is False assert [t["name"] for t in json.loads(result.content[0].text)] == [FX_TOOL.name] @pytest.mark.asyncio @@ -846,7 +853,7 @@ class TestCallToolRestApiVirtualTools: "litellm.proxy.proxy_server.llm_router", None ): result = await self._get_call_fn()(request=self._semantic_request(), user_api_key_dict=user_api_key_dict) - assert result.isError is True + assert result.is_error is True assert "mcp_tool_search.embedding_model" in result.content[0].text @pytest.mark.asyncio @@ -856,7 +863,7 @@ class TestCallToolRestApiVirtualTools: monkeypatch.setattr(litellm, "mcp_tool_search", {"top_k": 0}) user_api_key_dict = UserAPIKeyAuth(api_key="k", object_permission=_make_perm(mcp_tool_search_enabled=True)) result = await self._get_call_fn()(request=self._semantic_request(), user_api_key_dict=user_api_key_dict) - assert result.isError is True + assert result.is_error is True assert "top_k" in result.content[0].text @pytest.mark.asyncio @@ -920,7 +927,7 @@ class TestDispatchVirtualMcpTool: client_ip=None, ) assert result is not None - assert result.isError is True + assert result.is_error is True @pytest.mark.asyncio async def test_routes_search_with_client_ip(self) -> None: @@ -977,7 +984,7 @@ class TestDispatchVirtualMcpTool: name=AGENT_SEARCH_TOOL_NAME, arguments={"query": "x"}, user_api_key_auth=uak, client_ip=None ) assert result is not None - assert result.isError is True + assert result.is_error is True @pytest.mark.asyncio async def test_routes_call_with_client_ip(self) -> None: @@ -1144,78 +1151,28 @@ class TestDispatchVirtualMcpTool: class TestCaptureHostProgressCallback: - """Covers the host progress-forwarding helper extracted from the tool call path.""" + @pytest.mark.parametrize("meta", [None, {}, {"traceparent": "trace"}]) + def test_returns_none_without_progress(self, _mcp_request_ctx, meta) -> None: + from litellm.proxy._experimental.mcp_server.server import _capture_host_progress_callback - def test_returns_none_when_request_context_unavailable(self) -> None: - from litellm.proxy._experimental.mcp_server.server import ( - _capture_host_progress_callback, - ) - - class _NoCtx: - @property - def request_context(self): # type: ignore[no-untyped-def] - raise RuntimeError("no context") - - assert _capture_host_progress_callback(_NoCtx()) is None - - def test_returns_none_when_no_progress_token(self) -> None: - from litellm.proxy._experimental.mcp_server.server import ( - _capture_host_progress_callback, - ) - - host = MagicMock() - host.request_context.meta.progressToken = None - assert _capture_host_progress_callback(host) is None - - def test_returns_callable_when_token_present(self) -> None: - from litellm.proxy._experimental.mcp_server.server import ( - _capture_host_progress_callback, - ) - - host = MagicMock() - host.request_context.meta.progressToken = "tok12345" - host.request_context.session = MagicMock() - assert callable(_capture_host_progress_callback(host)) - - def test_returns_callable_when_token_is_integer(self) -> None: - from litellm.proxy._experimental.mcp_server.server import ( - _capture_host_progress_callback, - ) - - host = MagicMock() - host.request_context.meta.progressToken = 12345 - host.request_context.session = MagicMock() - assert callable(_capture_host_progress_callback(host)) - - def test_returns_callable_when_token_is_zero(self) -> None: - from litellm.proxy._experimental.mcp_server.server import ( - _capture_host_progress_callback, - ) - - host = MagicMock() - host.request_context.meta.progressToken = 0 - host.request_context.session = MagicMock() - assert callable(_capture_host_progress_callback(host)) + assert _capture_host_progress_callback(_mcp_request_ctx(meta=meta)) is None @pytest.mark.asyncio - async def test_forwarded_progress_token_preserves_integer_value(self) -> None: - from litellm.proxy._experimental.mcp_server.server import ( - _capture_host_progress_callback, + @pytest.mark.parametrize("token", ["tok12345", 12345, 0]) + async def test_forwards_wire_progress_token(self, _mcp_request_ctx, token) -> None: + from mcp.types import CallToolRequestParams + + from litellm.proxy._experimental.mcp_server.server import _capture_host_progress_callback + + params = CallToolRequestParams.model_validate( + {"name": "tool", "_meta": {"progressToken": token}}, by_name=False ) - - host = MagicMock() - host.request_context.meta.progressToken = 12345 session = AsyncMock() - host.request_context.session = session - - callback = _capture_host_progress_callback(host) + callback = _capture_host_progress_callback(_mcp_request_ctx(meta=params.meta, session=session)) assert callback is not None await callback(0.5, 1.0) - session.send_progress_notification.assert_awaited_once_with( - progress_token=12345, - progress=0.5, - total=1.0, + progress_token=token, progress=0.5, total=1.0 ) @@ -1223,7 +1180,7 @@ class TestHandleListToolsVirtual: """Covers the protocol list_tools early-return when the flag is enabled.""" @pytest.mark.asyncio - async def test_returns_virtual_tools_when_flag_enabled(self) -> None: + async def test_returns_virtual_tools_when_flag_enabled(self, _mcp_request_ctx) -> None: from litellm.proxy._experimental.mcp_server import server as srv uak = UserAPIKeyAuth(api_key="k", object_permission=_make_perm(mcp_tool_search_enabled=True)) @@ -1232,9 +1189,9 @@ class TestHandleListToolsVirtual: new_callable=AsyncMock, return_value=(uak, None, None, None, None, None, None), ): - tools = await srv.handle_list_tools() + result = await srv.handle_list_tools(_mcp_request_ctx(), _paged_params()) - assert {t.name for t in tools} == { + assert {t.name for t in result.tools} == { MCP_TOOL_SEARCH_TOOL_NAME, MCP_TOOL_CALL_TOOL_NAME, AGENT_SEARCH_TOOL_NAME, @@ -1247,7 +1204,7 @@ class TestMcpServerToolCallErrorHandling: isError CallToolResult instead of letting them raise out of the handler.""" @pytest.mark.asyncio - async def test_virtual_tool_error_returns_iserror_not_raised(self) -> None: + async def test_virtual_tool_error_returns_iserror_not_raised(self, _mcp_request_ctx) -> None: from fastapi import HTTPException from litellm.proxy._experimental.mcp_server import server as srv @@ -1265,12 +1222,17 @@ class TestMcpServerToolCallErrorHandling: side_effect=HTTPException(status_code=403, detail="User not allowed to call this tool"), ), ): + from mcp.types import CallToolRequestParams + result = await srv.mcp_server_tool_call( - name=MCP_TOOL_CALL_TOOL_NAME, - arguments={"tool_name": "other-server-tool", "arguments": {}}, + _mcp_request_ctx(), + CallToolRequestParams( + name=MCP_TOOL_CALL_TOOL_NAME, + arguments={"tool_name": "other-server-tool", "arguments": {}}, + ), ) - assert result.isError is True + assert result.is_error is True assert "User not allowed to call this tool" in result.content[0].text diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_openapi_tool_auth.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_openapi_tool_auth.py index 334bee9800c..ac716bace3c 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_openapi_tool_auth.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_openapi_tool_auth.py @@ -459,7 +459,7 @@ async def test_legacy_local_tool_fallback_still_dispatches_entitled_caller( user_api_key_auth=user, ) - assert result.isError is False + assert result.is_error is False assert executed == [{}] assert "legacy local tool ran" in result.content[0].text @@ -663,12 +663,12 @@ async def test_local_dispatch_reports_the_outcome_instead_of_success(failure: st failure may propagate. `_handle_local_mcp_tool` used to catch every exception and return it as TextContent, and both of - its callers then stamped `isError=False`, so an upstream rejection was served as tool output and + its callers then stamped `is_error=False`, so an upstream rejection was served as tool output and `extract_mcp_tool_result_error_message` logged the request as a success. The two kinds are split by consequence. `MCPUpstreamAuthError` propagates because both renderers know it: the streamable path names the status and the REST path relays a real 401 with the - upstream's WWW-Authenticate. Anything else is reported as `isError=True` right here, because + upstream's WWW-Authenticate. Anything else is reported as `is_error=True` right here, because `call_tool_rest_api` turns an unrecognized exception into HTTP 500 and an upstream 403 or 429 is not a gateway crash. """ @@ -729,7 +729,7 @@ async def test_local_dispatch_reports_the_outcome_instead_of_success(failure: st result = await call # A non-auth upstream failure stays a 200 with isError, so REST does not report it as a gateway 500 - assert result.isError is True + assert result.is_error is True assert "upstream returned HTTP 429" in result.content[0].text diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_rest_endpoints.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_rest_endpoints.py index ac3ad9ed89e..13af58c15c0 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_rest_endpoints.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_rest_endpoints.py @@ -3177,7 +3177,7 @@ async def test_request_selected_tool_specific_guardrail_applies_to_virtual_execu upstream.assert_not_awaited() else: result: Final = await rest_endpoints.call_tool_rest_api(request, user_api_key_dict=caller) - assert result.isError is False + assert result.is_error is False upstream.assert_awaited_once() assert upstream.await_args.kwargs == {"q": "redacted" if selected else "confidential"} @@ -3198,7 +3198,7 @@ class TestGetToolsForSingleServer: def __init__(self, name, description): self.name = name self.description = description - self.inputSchema = {} + self.input_schema = {} mock_tools = [ MockTool("tool1", "First tool"), @@ -3259,7 +3259,7 @@ class TestGetToolsForSingleServer: def __init__(self, name, description): self.name = name self.description = description - self.inputSchema = {} + self.input_schema = {} mock_tools = [ MockTool("tool1", "First tool"), @@ -3307,7 +3307,7 @@ class TestGetToolsForSingleServer: def __init__(self, name, description): self.name = name self.description = description - self.inputSchema = {} + self.input_schema = {} mock_tools = [ MockTool("tool1", "First tool"), @@ -3360,7 +3360,7 @@ class TestGetToolsForSingleServer: def __init__(self, name, description): self.name = name self.description = description - self.inputSchema = {} + self.input_schema = {} mock_tools = [ MockTool("tool1", "First tool"), @@ -3413,7 +3413,7 @@ class TestGetToolsForSingleServer: def __init__(self, name, description): self.name = name self.description = description - self.inputSchema = {} + self.input_schema = {} mock_tools = [ MockTool("tool1", "First tool"), @@ -3475,7 +3475,7 @@ class TestGetToolsForSingleServer: def __init__(self, name): self.name = name self.description = name - self.inputSchema = {} + self.input_schema = {} mock_tools = [MockTool("tool1"), MockTool("tool2"), MockTool("tool3")] @@ -3903,11 +3903,11 @@ class TestConnectionErrorMessage: assert "secret" not in message def test_closed_connection_explains_incomplete_request(self) -> None: - from mcp import McpError + from mcp import MCPError from mcp.types import ErrorData message: Final = rest_endpoints._connection_error_message( - McpError(ErrorData(code=-32000, message="Connection closed", data="secret-data")), None, 30 + MCPError(code=-32000, message="Connection closed", data="secret-data"), None, 30 ) assert "connection was closed before the request completed" in message assert "secret" not in message @@ -3920,8 +3920,8 @@ class TestConnectionErrorMessage: @pytest.mark.parametrize("sdk_timeout", [True, False]) @pytest.mark.parametrize("read_timeout", [0, 1]) async def test_timeout_message_uses_the_deadline_that_expired(self, sdk_timeout: bool, read_timeout: int) -> None: - from mcp import McpError - from mcp.types import ErrorData + from mcp import MCPError + from mcp.types import REQUEST_TIMEOUT, ErrorData async def operation(client: rest_endpoints.MCPClient) -> dict[str, object]: try: @@ -3930,8 +3930,8 @@ class TestConnectionErrorMessage: if not sdk_timeout: raise try: - raise McpError(ErrorData(code=408, message="secret-sdk-timeout")) from elapsed - except McpError as sdk_error: + raise MCPError(code=REQUEST_TIMEOUT, message="secret-sdk-timeout") from elapsed + except MCPError as sdk_error: raise TimeoutError() from sdk_error payload: Final = NewMCPServerRequest( @@ -3947,11 +3947,11 @@ class TestConnectionErrorMessage: assert "reference" in message.lower() def test_sdk_session_terminated_explains_endpoint_and_retry(self) -> None: - from mcp.shared.exceptions import McpError + from mcp.shared.exceptions import MCPError from mcp.types import ErrorData message: Final = rest_endpoints._connection_error_message( - McpError(ErrorData(code=32600, message="Session terminated")), "https://example.com/mcp", 30.0 + MCPError(code=32600, message="Session terminated"), "https://example.com/mcp", 30.0 ) assert "session was terminated" in message @@ -3962,11 +3962,11 @@ class TestConnectionErrorMessage: @pytest.mark.parametrize("code", [-32700, -32601, -32602, -32603, -32000, 32600, 408]) def test_rpc_errors_include_code_without_echoing_upstream_data(self, code: int) -> None: - from mcp.shared.exceptions import McpError + from mcp.shared.exceptions import MCPError from mcp.types import ErrorData message: Final = rest_endpoints._connection_error_message( - McpError(ErrorData(code=code, message="secret-message", data={"token": "secret-data"})), + MCPError(code=code, message="secret-message", data={"token": "secret-data"}), "https://example.com/secret-path?token=secret-query", 30.0, ) @@ -4150,6 +4150,12 @@ class TestToolResponseMcpInfoEnrichment: "alias": "atlassian", } + from fastapi.encoders import jsonable_encoder + + wire = jsonable_encoder(result[0]) + assert wire["inputSchema"] == {"type": "object"} + assert wire["mcp_info"] == result[0].mcp_info + def test_alias_none_is_explicit_in_mcp_info(self): from mcp.types import Tool as MCPTool diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_utils.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_utils.py index 0252fb9843d..842859e5a1e 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_utils.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_utils.py @@ -269,3 +269,17 @@ class TestBuildSyntheticMcpRequest: ) assert request.headers.get("x-user-email") == "alice@corp.example" + + +@pytest.mark.parametrize("field", ["structuredContent", "structured_content"]) +def test_structured_content_redaction_updates_shared_dictionary(field): + from litellm.proxy._experimental.mcp_server.utils import ( + mcp_tool_result_structured_content, + set_mcp_tool_result_structured_content, + ) + + result = {field: {"secret": "sensitive"}, "content": []} + logging_reference = result + assert set_mcp_tool_result_structured_content(result, {"secret": "[REDACTED]"}) is True + assert mcp_tool_result_structured_content(logging_reference) == {"secret": "[REDACTED]"} + assert set(result) == {field, "content"} diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_cisco_ai_defense_mcp.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_cisco_ai_defense_mcp.py index 137b7d24023..826edab694d 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_cisco_ai_defense_mcp.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_cisco_ai_defense_mcp.py @@ -376,9 +376,10 @@ class TestCiscoAIDefenseMCPMode: assert sent_payload["result"]["content"][0]["text"] == text_content assert result is None + @pytest.mark.parametrize("use_wrapper", [True, False]) @pytest.mark.asyncio - async def test_mcp_response_hook_through_real_logging_wrapper(self): - from mcp.types import CallToolResult, TextContent + async def test_mcp_response_hook_through_real_logging_wrapper(self, use_wrapper): + from mcp.types import AudioContent, CallToolResult, EmbeddedResource, ImageContent, TextContent, TextResourceContents from litellm.types.mcp import MCPPostCallResponseObject @@ -387,7 +388,14 @@ class TestCiscoAIDefenseMCPMode: ) real_result = CallToolResult( - content=[TextContent(type="text", text="leak 9045629876")], + content=[ + TextContent(type="text", text="leak 9045629876"), + ImageContent(type="image", data="aGVsbG8=", mimeType="image/png"), + AudioContent(type="audio", data="aGVsbG8=", mimeType="audio/wav"), + EmbeddedResource(type="resource", resource=TextResourceContents( + uri="memo://status", mimeType="text/plain", text="resource text" + )), + ], structuredContent={"patient": {"ssn": "123-45-6789"}}, isError=False, ) @@ -396,15 +404,6 @@ class TestCiscoAIDefenseMCPMode: hidden_params={}, ) - assert isinstance(wrapped.mcp_tool_call_response, list) - assert all( - isinstance(item, tuple) and len(item) == 2 - for item in wrapped.mcp_tool_call_response - ), ( - "Pydantic coercion shape changed — update the normalizer to " - "match the new wire format." - ) - post_mock = AsyncMock(return_value=_safe_response(url=MCP_URL)) with _patch_inspection_post(g, post_mock): result = await g.async_post_mcp_tool_call_hook( @@ -414,7 +413,7 @@ class TestCiscoAIDefenseMCPMode: "mcp_server_name": "vault", "litellm_call_id": "real-wire-call", }, - response_obj=wrapped, + response_obj=wrapped if use_wrapper else real_result, start_time=datetime.now(), end_time=datetime.now(), ) @@ -428,8 +427,8 @@ class TestCiscoAIDefenseMCPMode: sent_payload = post_mock.call_args.kwargs["json"] content_items = sent_payload["result"]["content"] - assert len(content_items) == 1, ( - f"expected exactly 1 content item from the real " + assert len(content_items) == 4, ( + f"expected exactly 4 content items from the real " f"CallToolResult.content list, got {len(content_items)}: " f"{content_items!r}" ) @@ -441,6 +440,11 @@ class TestCiscoAIDefenseMCPMode: f"``content`` field." ) assert content_items[0].get("type") == "text" + assert content_items[1:] == [ + {"type": "image", "data": "aGVsbG8=", "mimeType": "image/png"}, + {"type": "audio", "data": "aGVsbG8=", "mimeType": "audio/wav"}, + {"type": "resource", "resource": {"uri": "memo://status", "mimeType": "text/plain", "text": "resource text"}}, + ] assert sent_payload["result"]["structuredContent"] == { "patient": {"ssn": "123-45-6789"} } @@ -482,7 +486,6 @@ class TestCiscoAIDefenseMCPMode: class TestCiscoAIDefenseRedactListShape: - @staticmethod def _violation_with_redact_response(text: str = "[REDACTED tool output]"): return _mock_inspect_response( @@ -512,8 +515,8 @@ class TestCiscoAIDefenseRedactListShape: tuples_list = [ ("meta", None), ("content", inner_content), - ("structuredContent", {"patient": {"ssn": "123-45-6789"}}), - ("isError", False), + ("structured_content", {"patient": {"ssn": "123-45-6789"}}), + ("is_error", False), ] return tuples_list, lambda: inner_content[0].text @@ -526,16 +529,12 @@ class TestCiscoAIDefenseRedactListShape: from litellm.types.mcp import MCPPostCallResponseObject - g = _make_guardrail( - inspection_type="mcp", event_hook=["pre_mcp_call", "during_mcp_call"] - ) + g = _make_guardrail(inspection_type="mcp", event_hook=["pre_mcp_call", "during_mcp_call"]) content, get_text = getattr(self, factory_name)() response_obj = _mcp_response(content) - with _patch_inspection_post( - g, AsyncMock(return_value=self._violation_with_redact_response()) - ): + with _patch_inspection_post(g, AsyncMock(return_value=self._violation_with_redact_response())): result = await g.async_post_mcp_tool_call_hook( kwargs={"name": "leak", "arguments": {}}, response_obj=response_obj, @@ -544,15 +543,13 @@ class TestCiscoAIDefenseRedactListShape: ) assert result is None or not isinstance(result, MCPPostCallResponseObject), ( - f"Redact silently fell through to block for {factory_name}. " - f"result={result!r}" + f"Redact silently fell through to block for {factory_name}. result={result!r}" ) assert get_text() == "[REDACTED tool output]", ( - f"Redact silently failed for {factory_name}; original text " - f"not rewritten." + f"Redact silently failed for {factory_name}; original text not rewritten." ) if factory_name == "_pydantic_tuple_list_factory": - structured_content = dict(content)["structuredContent"] + structured_content = dict(content)["structured_content"] assert structured_content == {"result": "[REDACTED tool output]"} assert "123-45-6789" not in json.dumps(structured_content) @@ -591,12 +588,12 @@ class TestCiscoAIDefenseRedactListShape: ) assert original_response.content[0].text == "[REDACTED tool output]" - assert "123-45-6789" not in json.dumps(original_response.structuredContent), ( + assert "123-45-6789" not in json.dumps(original_response.structured_content), ( "Redact verdict left the client-visible MCP tool output unchanged. " "The post-call hook receives a wrapped MCPPostCallResponseObject but " "the endpoint returns kwargs['original_response'], so the redaction " "must rewrite that object too. structuredContent still leaks: " - f"{original_response.structuredContent!r}" + f"{original_response.structured_content!r}" ) @@ -712,11 +709,11 @@ class TestCiscoAIDefenseMCPBlockingContract: "Hook must keep returning a MCPPostCallResponseObject for " "dispatcher paths that do honor returned replacements." ) - assert raw_response.isError is True + assert raw_response.is_error is True assert "Blocked by Cisco AI Defense" in raw_response.content[0].text - assert raw_response.structuredContent is not None - assert "Blocked by Cisco AI Defense" in raw_response.structuredContent["result"] - assert "exfiltrated" not in raw_response.structuredContent["result"] + assert raw_response.structured_content is not None + assert "Blocked by Cisco AI Defense" in raw_response.structured_content["result"] + assert "exfiltrated" not in raw_response.structured_content["result"] logging_stub = Logging.__new__(Logging) logging_stub.model_call_details = {} parsed = logging_stub._parse_post_mcp_call_hook_response(response=result) diff --git a/tests/test_litellm/test_circleci_path_filter.py b/tests/test_litellm/test_circleci_path_filter.py index 07fab42bad6..84e2327057d 100644 --- a/tests/test_litellm/test_circleci_path_filter.py +++ b/tests/test_litellm/test_circleci_path_filter.py @@ -49,6 +49,16 @@ CI = [".github/workflows/test-litellm-ui-unit.yml"] @pytest.mark.parametrize( "category,changed,expected", [ + ("mcp-dependencies", ["pyproject.toml"], "run"), + ("mcp-dependencies", ["uv.lock"], "run"), + ("mcp-dependencies", ["litellm/experimental_mcp_client/client.py"], "run"), + ("mcp-dependencies", ["tests/e2e/mcp/oauth_chat_client.py"], "run"), + ("mcp-dependencies", ["litellm-proxy-extras/pyproject.toml"], "run"), + ("mcp-dependencies", ["scripts/check_mcp_sdk_install.py"], "run"), + ("mcp-dependencies", [".github/workflows/test-mcp-dependency-resolution.yml"], "run"), + ("mcp-dependencies", [".circleci/scripts/classify_changes.sh"], "run"), + ("mcp-dependencies", ["litellm/llms/openai/chat/gpt_transformation.py"], "skip"), + ("mcp-dependencies", DOCS + CLIENT, "skip"), ("provider-harness", ["tests/e2e/provider_cache.py"], "run"), ("provider-harness", ["tests/e2e/conftest.py"], "run"), ("provider-harness", ["tests/e2e/e2e_http.py"], "run"), diff --git a/uv.lock b/uv.lock index ab9582ed134..db2fb11c6e3 100644 --- a/uv.lock +++ b/uv.lock @@ -3290,6 +3290,19 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/7e/f5/f66802a942d491edb555dd61e3a9961140fd64c90bce1eafd741609d334d/httpcore-1.0.9-py3-none-any.whl", hash = "sha256:2d400746a40668fc9dec9810239072b40b4484b640a8c38fd654a024c7a1bf55", size = 78784, upload-time = "2025-04-24T22:06:20.566Z" }, ] +[[package]] +name = "httpcore2" +version = "2.13.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "h11" }, + { name = "truststore" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/15/8c/e925b1c92018abb3a1863ce1549d76d2381e334d21d65d4ac8f65dabd78a/httpcore2-2.13.0.tar.gz", hash = "sha256:2adc8be4fb285fbcd6d894298db3b52c177e74b6674eda3a76bd36be3292a3db", size = 67740, upload-time = "2026-09-14T14:18:04.717Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7e/0d/117a771a2bb91df334b66bf4da14cd02f21aefbcfe53180f336ce55e8f90/httpcore2-2.13.0-py3-none-any.whl", hash = "sha256:35ae5be347aa40467b4a5dc032ac67ebb6d27189fc97e8cebcf99616f6a1bb9e", size = 83162, upload-time = "2026-09-14T14:18:02.529Z" }, +] + [[package]] name = "httplib2" version = "0.32.0" @@ -3331,6 +3344,32 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/d2/fd/6668e5aec43ab844de6fc74927e155a3b37bf40d7c3790e49fc0406b6578/httpx_sse-0.4.3-py3-none-any.whl", hash = "sha256:0ac1c9fe3c0afad2e0ebb25a934a59f4c7823b60792691f779fad2c5568830fc", size = 8960, upload-time = "2025-10-10T21:48:21.158Z" }, ] +[[package]] +name = "httpx2" +version = "2.13.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "anyio", marker = "sys_platform != 'emscripten'" }, + { name = "httpcore2", marker = "sys_platform != 'emscripten'" }, + { name = "httpx2-jsfetch", marker = "python_full_version >= '3.12' and sys_platform == 'emscripten'" }, + { name = "idna" }, + { name = "truststore", marker = "sys_platform != 'emscripten'" }, + { name = "typing-extensions", marker = "python_full_version < '3.13'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/b9/a0/e9deef4654132857b5a5dbe4eddd0ac59c2814500e11f2f5044cd81103ee/httpx2-2.13.0.tar.gz", hash = "sha256:81bd07dc67a3701729ef1f777a3c00c915d4539604fdb5afd327f8682f6b7b44", size = 100290, upload-time = "2026-09-14T14:18:05.486Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/fe/d1/a0c72b0e006df654709fbc366cc5bcb53e5aee13e1e3395152c6dd293376/httpx2-2.13.0-py3-none-any.whl", hash = "sha256:fc12720cedf72faa26cca6b4ca394e05c894e7d7933fc45cafe767960804e49a", size = 95565, upload-time = "2026-09-14T14:18:03.553Z" }, +] + +[[package]] +name = "httpx2-jsfetch" +version = "1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/cd/c4/0e5636363151a2a1795e0a77617168b9ca438e1748ec05fc9b5687f93d64/httpx2_jsfetch-1.0.tar.gz", hash = "sha256:70a0e3eabfef7cce5ad9c629f7d01ca05e418f586646f4ddf14782e4c1454c60", size = 6872, upload-time = "2026-08-07T00:13:07.492Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/9b/43/832f631d32e4f1211caa2ba368317739fe71f0b8530e4c9d15dc454bac2a/httpx2_jsfetch-1.0-py3-none-any.whl", hash = "sha256:cb916b707601e69a07721aabc8f3f6659be3a6893bc1ff5c6f9e02241df2da32", size = 6382, upload-time = "2026-08-07T00:13:06.567Z" }, +] + [[package]] name = "huey" version = "2.6.0" @@ -3494,11 +3533,11 @@ wheels = [ [[package]] name = "idna" -version = "3.15" +version = "3.19" source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/82/77/7b3966d0b9d1d31a36ddf1746926a11dface89a83409bf1483f0237aa758/idna-3.15.tar.gz", hash = "sha256:ca962446ea538f7092a95e057da437618e886f4d349216d2b1e294abfdb65fdc", size = 199245, upload-time = "2026-05-12T22:45:57.011Z" } +sdist = { url = "https://files.pythonhosted.org/packages/5f/f7/abb373e5757eaec4b922b92f97ec8d6d7e057cf06778247604fbc4e7c3f3/idna-3.19.tar.gz", hash = "sha256:5e0811a4383b21dc5838069f801c4fb62113b7447663d2530d2bd6e77b49bf15", size = 215237, upload-time = "2026-08-18T05:14:24.27Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/d2/23/408243171aa9aaba178d3e2559159c24c1171a641aa83b67bdd3394ead8e/idna-3.15-py3-none-any.whl", hash = "sha256:048adeaf8c2d788c40fee287673ccaa74c24ffd8dcf09ffa555a2fbb59f10ac8", size = 72340, upload-time = "2026-05-12T22:45:55.733Z" }, + { url = "https://files.pythonhosted.org/packages/57/b0/0e52c878c53f245edd3a11020f20979b3f490f245af532c7cae3027754b5/idna-3.19-py3-none-any.whl", hash = "sha256:815e7be7a7806d54abb586dc943addc79e8b2ee16915059658cbeff4b1b43bf4", size = 68550, upload-time = "2026-08-18T05:14:22.343Z" }, ] [[package]] @@ -4187,20 +4226,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/13/d6/bdf6f0481cc57ef300d6b1eb48cf1400c0409be715d6eb3cabadd1142a09/langchain_core-1.4.8-py3-none-any.whl", hash = "sha256:d84c28b05e3ba8d4271d0827aad5b592ccdaaf986e76768c23503f0a2045e8aa", size = 557416, upload-time = "2026-06-18T19:39:21.902Z" }, ] -[[package]] -name = "langchain-mcp-adapters" -version = "0.2.1" -source = { registry = "https://pypi.org/simple" } -dependencies = [ - { name = "langchain-core" }, - { name = "mcp" }, - { name = "typing-extensions" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/d9/52/cebf0ef5b1acef6cbc63d671171d43af70f12d19f55577909c7afa79fb6e/langchain_mcp_adapters-0.2.1.tar.gz", hash = "sha256:58e64c44e8df29ca7eb3b656cf8c9931ef64386534d7ca261982e3bdc63f3176", size = 36394, upload-time = "2025-12-09T16:28:38.98Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/03/81/b2479eb26861ab36be851026d004b2d391d789b7856e44c272b12828ece0/langchain_mcp_adapters-0.2.1-py3-none-any.whl", hash = "sha256:9f96ad4c64230f6757297fec06fde19d772c99dbdfbca987f7b7cfd51ff77240", size = 22708, upload-time = "2025-12-09T16:28:37.877Z" }, -] - [[package]] name = "langchain-openai" version = "1.1.14" @@ -4537,7 +4562,9 @@ grpc = [ { name = "grpcio" }, ] mcp = [ + { name = "httpx2" }, { name = "mcp" }, + { name = "pydantic" }, ] mlflow = [ { name = "mlflow" }, @@ -4555,12 +4582,14 @@ proxy = [ { name = "granian" }, { name = "gunicorn" }, { name = "hiredis" }, + { name = "httpx2" }, { name = "inquirerpy" }, { name = "litellm-enterprise" }, { name = "litellm-proxy-extras" }, { name = "mcp" }, { name = "orjson" }, { name = "polars" }, + { name = "pydantic" }, { name = "pyjwt" }, { name = "pynacl" }, { name = "pyroscope-io", marker = "sys_platform != 'win32'" }, @@ -4632,7 +4661,6 @@ ci = [ { name = "google-generativeai" }, { name = "jsonlines" }, { name = "langchain" }, - { name = "langchain-mcp-adapters" }, { name = "langchain-openai" }, { name = "langgraph" }, { name = "langgraph-prebuilt" }, @@ -4752,6 +4780,8 @@ requires-dist = [ { name = "gunicorn", marker = "extra == 'proxy'", specifier = ">=23.0.0,<24.0" }, { name = "hiredis", marker = "extra == 'proxy'", specifier = ">=3.0.0,<4.0" }, { name = "httpx", extras = ["http2"], specifier = ">=0.28.0,<1.0" }, + { name = "httpx2", marker = "extra == 'mcp'", specifier = ">=2.5.0,<3" }, + { name = "httpx2", marker = "extra == 'proxy'", specifier = ">=2.5.0,<3" }, { name = "importlib-metadata", specifier = ">=8.0.0,<9.0" }, { name = "inquirerpy", marker = "extra == 'cli'", specifier = ">=0.3.4,<1.0" }, { name = "inquirerpy", marker = "extra == 'proxy'", specifier = ">=0.3.4,<1.0" }, @@ -4763,8 +4793,8 @@ requires-dist = [ { name = "litellm-proxy-extras", marker = "extra == 'proxy'", editable = "litellm-proxy-extras" }, { name = "llm-sandbox", marker = "extra == 'proxy-runtime'", specifier = ">=0.3.39,<1.0" }, { name = "mangum", marker = "extra == 'proxy-runtime'", specifier = ">=0.17.0,<1.0" }, - { name = "mcp", marker = "extra == 'mcp'", specifier = ">=1.28.1,<2.0" }, - { name = "mcp", marker = "extra == 'proxy'", specifier = ">=1.28.1,<2.0" }, + { name = "mcp", marker = "extra == 'mcp'", specifier = ">=2.2.0,<3" }, + { name = "mcp", marker = "extra == 'proxy'", specifier = ">=2.2.0,<3" }, { name = "mlflow", marker = "extra == 'mlflow'", specifier = ">=3.11.1,<4.0" }, { name = "numpy", marker = "extra == 'stt-nvidia-riva'", specifier = ">=1.26.0" }, { name = "numpydoc", marker = "extra == 'utils'", specifier = ">=1.8.0,<2.0" }, @@ -4780,7 +4810,10 @@ requires-dist = [ { name = "prometheus-client", marker = "extra == 'proxy-runtime'", specifier = ">=0.20.0,<1.0" }, { name = "psycopg", marker = "extra == 'extra-proxy'", specifier = ">=3.2,<4.0" }, { name = "psycopg-binary", marker = "extra == 'extra-proxy'", specifier = ">=3.2,<4.0" }, - { name = "pydantic", specifier = ">=2.10.0,<3.0.0" }, + { name = "pydantic", marker = "python_full_version < '3.14'", specifier = ">=2.11.0,<3.0.0" }, + { name = "pydantic", marker = "python_full_version >= '3.14'", specifier = ">=2.12.0,<3.0.0" }, + { name = "pydantic", marker = "extra == 'mcp'", specifier = ">=2.12.0,<3" }, + { name = "pydantic", marker = "extra == 'proxy'", specifier = ">=2.12.0,<3" }, { name = "pydantic-settings", specifier = ">=2.14.1,<3.0" }, { name = "pyjwt", marker = "extra == 'proxy'", specifier = ">=2.13.0,<3.0" }, { name = "pynacl", marker = "extra == 'proxy'", specifier = ">=1.6.2,<2.0" }, @@ -4803,7 +4836,8 @@ requires-dist = [ { name = "soundfile", marker = "extra == 'proxy'", specifier = ">=0.12.1,<1.0" }, { name = "soundfile", marker = "extra == 'stt-nvidia-riva'", specifier = ">=0.12.1" }, { name = "starlette", marker = "extra == 'proxy'", specifier = ">=1.0.1,<2.0" }, - { name = "tiktoken", specifier = ">=0.8.0,<1.0" }, + { name = "tiktoken", marker = "python_full_version < '3.14'", specifier = ">=0.8.0,<1.0" }, + { name = "tiktoken", marker = "python_full_version >= '3.14'", specifier = ">=0.12.0,<1.0" }, { name = "tokenizers", specifier = ">=0.21.0,<1.0" }, { name = "tomlkit", marker = "extra == 'cli'", specifier = ">=0.13.3,<1.0" }, { name = "tomlkit", marker = "extra == 'proxy'", specifier = ">=0.13.3,<1.0" }, @@ -4827,7 +4861,6 @@ ci = [ { name = "google-generativeai", specifier = "==0.8.6" }, { name = "jsonlines", specifier = "==4.0.0" }, { name = "langchain", specifier = "==1.3.9" }, - { name = "langchain-mcp-adapters", specifier = "==0.2.1" }, { name = "langchain-openai", specifier = "==1.1.14" }, { name = "langgraph", specifier = ">=1.2.4,<1.3.0" }, { name = "langgraph-prebuilt", specifier = ">=1.1.0,<1.3.0" }, @@ -4886,7 +4919,7 @@ dev = [ ] e2e-dev = [ { name = "locust", specifier = "==2.45.0" }, - { name = "mcp", specifier = ">=1.28.1,<2.0" }, + { name = "mcp", specifier = ">=2.2.0,<3" }, { name = "playwright", specifier = "==1.61.0" }, { name = "psutil", specifier = "==7.2.2" }, { name = "websockets", specifier = ">=15.0.1,<16.0" }, @@ -5364,15 +5397,15 @@ wheels = [ [[package]] name = "mcp" -version = "1.28.1" +version = "2.2.0" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "anyio" }, - { name = "httpx" }, - { name = "httpx-sse" }, + { name = "httpx2" }, { name = "jsonschema" }, + { name = "mcp-types" }, + { name = "opentelemetry-api" }, { name = "pydantic" }, - { name = "pydantic-settings" }, { name = "pyjwt", extra = ["crypto"] }, { name = "python-multipart" }, { name = "pywin32", marker = "sys_platform == 'win32'" }, @@ -5382,9 +5415,22 @@ dependencies = [ { name = "typing-inspection" }, { name = "uvicorn", marker = "sys_platform != 'emscripten'" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/6e/77/9450b8f251a13affb6281997d0523c4615f8a8b35d0b21ff30db3a5aac9d/mcp-1.28.1.tar.gz", hash = "sha256:d51e36a5f5644faea4f85ea649bfffa6bc6c26770d42798ad6a3de3d2ba69683", size = 638501, upload-time = "2026-06-26T12:57:29.093Z" } +sdist = { url = "https://files.pythonhosted.org/packages/76/31/ac54fb0fdd5b37de704486e288bba4fbbb463f24cfcfedbede407b854513/mcp-2.2.0.tar.gz", hash = "sha256:2dc37ecb1974becdcebdbf7561e7c15a07dbbf20ba21ba16c3593b3038b3afbd", size = 4084129, upload-time = "2026-09-07T16:06:23.439Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/e2/5e/d118fce19f87a2e7d8101c35c8ae0ec289098a4df0ff244cec23e415aca0/mcp-1.28.1-py3-none-any.whl", hash = "sha256:2726bca5e7193f61c5dde8b12500a6de2d9acf6d1a1c0be9e8c2e706437991df", size = 222620, upload-time = "2026-06-26T12:57:27.218Z" }, + { url = "https://files.pythonhosted.org/packages/1b/ff/8e7eade68b8a28f7da0ed1085544341b51f9c935dbf6b95c76b7edfea6a0/mcp-2.2.0-py3-none-any.whl", hash = "sha256:bde982589473a060ae145e3406e9a5333fe538c97229ba841f5a7f92be004f81", size = 365656, upload-time = "2026-09-07T16:06:19.711Z" }, +] + +[[package]] +name = "mcp-types" +version = "2.2.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "pydantic" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/ae/91/762d7755d971aff8a28d75f7961656148edf27875c8026e6385aaab08ae7/mcp_types-2.2.0.tar.gz", hash = "sha256:d3ed53703ddd10d9c6399f29d322bb66f3f67ab41348ac8556ba23e07fedefad", size = 65892, upload-time = "2026-09-07T16:06:25.187Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/8f/d7/6ffba5d8cd5dd9b8a19478875c50e04945314ba5074e84d749283f27f62d/mcp_types-2.2.0-py3-none-any.whl", hash = "sha256:ea476b73ee86709ab5abc9452385ed36cc05907e582355622e294595c9a04f13", size = 69106, upload-time = "2026-09-07T16:06:21.461Z" }, ] [[package]] @@ -9845,6 +9891,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/ce/13/53c2ab6ac27804769314554a062e0651a44db2360be47e21cf0a29d202ee/traceloop_sdk-0.33.12-py3-none-any.whl", hash = "sha256:d47a474afbf4a68ff38a702dbaca7b17d2d4f0b0e14dc2f1560b6bdd3859ac75", size = 25932, upload-time = "2024-11-13T20:29:25.174Z" }, ] +[[package]] +name = "truststore" +version = "0.10.4" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/53/a3/1585216310e344e8102c22482f6060c7a6ea0322b63e026372e6dcefcfd6/truststore-0.10.4.tar.gz", hash = "sha256:9d91bd436463ad5e4ee4aba766628dd6cd7010cf3e2461756b3303710eebc301", size = 26169, upload-time = "2025-08-12T18:49:02.73Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/19/97/56608b2249fe206a67cd573bc93cd9896e1efb9e98bce9c163bcdc704b88/truststore-0.10.4-py3-none-any.whl", hash = "sha256:adaeaecf1cbb5f4de3b1959b42d41f6fab57b2b1666adb59e89cb0b53361d981", size = 18660, upload-time = "2025-08-12T18:49:01.46Z" }, +] + [[package]] name = "typer" version = "0.25.1"