litellm/litellm/mcp_client
jesus-berri 3e0bcc508e
fix(mcp): return an upstream -32042 URL elicitation error from /mcp as a JSON-RPC error (#52384)
* test(mcp): add red /mcp contract for an upstream -32042 URL elicitation error

A tools/call that the upstream answers with -32042 must reach the /mcp
client as a JSON-RPC error with code and data.elicitations intact. The
stub provider moves to a shared factory so the existing REST case and
the new /mcp case script the same peer.

* fix(mcp): return an upstream -32042 URL elicitation error from /mcp as a JSON-RPC error

An upstream tools/call answered with JSON-RPC error -32042 was flattened into an isError text result, so the client never saw data.elicitations[].url. The four catches that built that result now re-raise it and the MCP SDK serializes it with code, message and data intact. /mcp-rest/tools/call keeps its HTTP 200 isError text result, and every other error keeps its current path.

Co-Authored-By: bot_apk <apk@cognition.ai>

* fix(mcp): keep the REST virtual mcp_tool_call result for an upstream -32042

The virtual mcp_tool_call branch of /mcp-rest/tools/call had no guard, so the re-raised -32042 became an HTTP 500. Both REST branches now share one guard that renders it as the HTTP 200 isError text result, while /mcp still relays it as a JSON-RPC error on its virtual path. The MCP client also re-raises -32042 before its error-level log, since it is an expected consent signal.

Co-Authored-By: bot_apk <apk@cognition.ai>

* test(mcp): add integration cells for upstream -32042 URL elicitation across entry points, auth paths and chaos

Covers every /mcp entry point, the official SDK client, SDK-built upstreams over http, sse and stdio,
token exchange, delegated and per-user OAuth servers, the REST error result, unusual error payloads,
spend log rows, a two-proxy burst, a killed worker, an upstream outage and a held reply.

The LLM auto-execute cell pins the tool result text and spend status that the merge base produces.

* test(mcp): assert the auto-executed -32042 tool call hands the model the error and logs a failure

Co-Authored-By: bot_apk <apk@cognition.ai>

* fix(mcp): run post_mcp_call guardrails over a relayed -32042 URL elicitation's message text

Co-Authored-By: bot_apk <apk@cognition.ai>

* fix(mcp): keep the LLM auto-execute path unchanged for -32042 and drop the unmasked cause from the relayed error

Co-Authored-By: bot_apk <apk@cognition.ai>

* fix(mcp): let a during-call guardrail block outrank a relayed -32042 and run the post tool call hook on its messages

Co-Authored-By: bot_apk <apk@cognition.ai>

* fix(mcp): flatten a -32042 for REST before the shared post-call path so guardrails and the tool call hook run once

Co-Authored-By: bot_apk <apk@cognition.ai>

* fix(mcp): run the -32042 guardrails and tool call hook outside the except block so a block cannot chain the upstream error

Co-Authored-By: bot_apk <apk@cognition.ai>

* test(mcp): drive the -32042 relay unit test through a real post_mcp_call guardrail

Co-Authored-By: bot_apk <apk@cognition.ai>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: bot_apk <apk@cognition.ai>
2026-11-19 08:56:01 -08:00
..
__init__.py refactor(mcp): drop the experimental label from the MCP server and client paths (#48513) 2026-10-27 17:47:50 +00:00
client.py fix(mcp): return an upstream -32042 URL elicitation error from /mcp as a JSON-RPC error (#52384) 2026-11-19 08:56:01 -08:00
Readme.md refactor(mcp): drop the experimental label from the MCP server and client paths (#48513) 2026-10-27 17:47:50 +00:00
tools.py feat(mcp): cache upstream tool lists with an admin-set lifetime (#51160) 2026-11-12 08:48:04 -08:00

LiteLLM MCP Client

LiteLLM MCP Client allows you to use MCP tools with LiteLLM

Install the optional dependencies with pip install 'litellm[mcp]', then use the existing public imports:

from litellm.mcp_client import call_openai_tool, load_mcp_tools
from litellm.mcp_client.client import MCPClient

client = MCPClient(server_url="https://mcp.example.com/mcp")

litellm.experimental_mcp_client is a deprecated alias of litellm.mcp_client that warns on import. Core import litellm works without the MCP extra. Importing the MCP client without its MCP or HTTPX2 dependency raises an error with this installation command

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 for Python API changes

Custom HTTP clients and authentication

MCP HTTP and SSE transports now use httpx2. Custom authentication passed through aws_auth or resolved_auth must implement httpx2.Auth. Integrations that override the client's HTTP client factory or customize its event hooks must use httpx2.AsyncClient, request, response, timeout and transport types

HTTPX1 clients, auth objects and hooks are not adapted by a compatibility shim. Migrate those integrations to HTTPX2 before upgrading. Ordinary MCPClient construction and LiteLLM's existing helper imports remain supported; this does not restore SDK1 Python imports or camelCase SDK model attributes in the shared Python environment

HTTP redirects

For streamable HTTP POST requests, the MCP SDK follows method-preserving redirects such as HTTP 307/308 within the configured endpoint's origin. Redirects to another path on the same scheme, host and port work. The SDK also permits an HTTP-to-HTTPS upgrade on the same host using the default ports

Redirects to a different origin are rejected before the destination receives a request or credentials. Configure the final MCP endpoint URL directly if the server redirects to a different host or port. Setting the HTTP client's follow_redirects option does not override the SDK's policy