feat(langchain): `langchain.mcp` namespace, `MCPAdapter` #39939
All Vouch review confidence checks passed
Residual risk 0.23 = Change Risk (0.57) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.57 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #39939"
general
weight: 0.35
eyurtsev
Adds
langchain.mcp: adapt an MCP server into LangChain tools ready forcreate_agent.langchain-mcp-adaptersstays the place for the full surface (interceptors, callbacks, prompts, resources). This is the short path for the common case.Public API
MCPAdapter(target, , elicitation=None), with aclientproperty,list_tools(, cache_mode="use"), and async context management.as_langchain_tool(tool, client, , elicitation=None)converts a single tool for callers managing their own client.MCPAdapteraccepts anythingfastmcp.Clientaccepts — a URL,Path, in-process server,ClientTransport,MCPConfig(or its dict form), or a pre-builtClient— plus a FastMCPClientGroupfor a fleet of servers behind one client. Inference is FastMCP's, so new target types work without changes here. The target union isMCPAdapterTarget; it is not part of the package's public surface (it is only useful for annotating atarget), but it stays importable fromlangchain.mcp.adapterfor that purpose. AnMCPConfignaming several servers yields one prefixed toolset from one adapter:Tool conversion
Tool results are ported from
langchain-mcp-adapters, so a call reaches a model in the same shape either way. Results become LangChain content blocks (audio raisesNotImplementedError); structured content becomes anMCPToolArtifact;args_schemais the tool'sinput_schema.Tool metadata is grouped under a single
mcpnamespace so a consumer can tell an MCP tool's provenance apart and keep tool-level fields distinct from the serving server's identity:Server identity comes off the connection (a
Toolcarries no server field) and is read at conversion time while the client is connected. This metadata rides onto the LangChain tool'smetadata, so it also lands on the tool's traced run —mcp.server.namebecomes filterable in traces. The examples PR uses the destructive hint to gate a tool behind human approval.An
isError=Trueresult becomes aToolMessagewithstatus="error"carrying the server's own error content, so the agent can correct itself instead of the run ending. Transport failures and unconvertible content still raise.FastMCP clients are reentrant and reference-counted, so the adapter adds no second layer — tools hold the client and stay callable after the adapter's context exits.
Discovery caching
list_tools(cache_mode=...)selects how discovery interacts with the client-side response cache (SEP-2549):use(default) serves a cached tool list within the server's TTL hint,refreshcalls the server and repopulates it,bypassskips the cache. The cache and its per-principal isolation are configured on the client itself (Client(cache=...)); this only selects how discovery reads it. The default isuseso a configured cache is honored — note this differs from a bareClientGroup.list_tools(), whose own default isrefresh.Elicitation
Some MCP tools need input before they can finish.
elicitation="interrupt"surfaces the question as a LangGraph interrupt:Answers correlate by the server's own request keys, so nothing extra is needed on resume. Several requests in one round share one interrupt; successive rounds each get their own. The payload types live in
langchain.mcp.elicitation, split so the type system carries the protocol's rules:modediscriminates form from URL requests, and only an accept can carry content, narrowed to the scalar shapes the wire accepts.It is opt-in because a declared capability is a promise on the wire — servers only build flows that depend on it once a client says yes — so left unset, a server whose tool requires an answer refuses the call rather than running without one. The loop is driven through
session.call_tool(..., allow_input_required=True)rather than a FastMCP handler, which would convert theGraphInterruptinto an MCP error. Resuming re-issues the call from its first round, since the interrupt unwound it; a server that asks before doing work repeats nothing, which the tests assert by counting tool-body executions.Scope is elicitation only. Embedded sampling and roots requests raise, since driving the loop by hand bypasses the FastMCP callbacks that answer them, as does a continuation round carrying only
request_state— that is the protocol's long-running-work channel, and serving it would mean polling a remote server from inside a tool call.Building on FastMCP
Handing the client to FastMCP is what keeps this small. Most of the protocol surface is inherited rather than written:
initializehandshake and the 2026-07-28server/discoverrevision, and picks per connection — so one agent can hold tools from servers on different revisions at once, with no protocol mode to choose. There is a test that builds exactly that agent.auth="oauth", bearer tokens, customhttpxauth, and TLS viaverifyall arrive without code here.ClientGrouptarget lets one adapter serve a fleet of servers, prefixing each server's tools and routing every call back to the client that serves it.This requires
fastmcp>=4.0.0— the GA line whose multi-server config routes to modern-protocol servers, which theMCPConfigandClientGroupsupport rely on.Release note
New
langchain.mcpnamespace.MCPAdapteradapts any targetfastmcp.Clientaccepts — a URL, a local script, an in-process server, anMCPConfignaming several servers, or a pre-built client — as well as a FastMCPClientGroup, into LangChain tools ready forcreate_agent.MCPAdapter.list_tools(cache_mode=...)discovers tools with optional client-side response caching.as_langchain_toolconverts a single MCP tool for callers managing their own client. Tool metadata (annotations,_meta, and the serving server's identity) is grouped under anmcpnamespace on each tool.elicitation="interrupt"surfaces a server's mid-call questions as LangGraph interrupts. Requires themcpextra:pip install "langchain[mcp]".Notes for review
Pathor script-path target launches a local subprocess with no opt-in keyword. Intended, and the target comes from application code rather than a model, but it loosens the previous default and is awkward to walk back once released._metareaches the tool's LangSmith run verbatim; keeping it nested undermcp.tool._meta(rather than flattened) keeps that clearly demarcated.elicitation="interrupt"installs a sentinel handler purely to trip the SDK's identity comparison against its default callback, which is the only way the SDK lets a client declare the capability; a test asserts the negotiated capability so a change upstream cannot quietly stop servers asking._await_monitoredreaches for FastMCP's private_await_with_session_monitoring, without which a session dying mid-elicitation hangs the tool call; the fallback now warns.tests/unit_tests/mcp/conftest.pygrantsblockbustertwo narrow allowances, both caused upstream in FastMCP: entering a serverContextresolves an optional dependency throughimportlib.metadataon every in-process request, andmcp.client.sessionimportsjsonschemalazily on first tool call.---
Prepared with the assistance of an AI agent.*