Skip to main content
Model Context Protocol (MCP) is an open protocol that standardizes how applications provide tools and context to LLMs. LangChain agents can use tools defined on MCP servers using the @langchain/mcp-adapters library.

Quickstart

Install the @langchain/mcp-adapters library:
@langchain/mcp-adapters enables agents to use tools defined across one or more MCP servers.
MultiServerMCPClient is stateless by default. Each tool invocation creates a fresh MCP ClientSession, executes the tool, and then cleans up.
Accessing multiple MCP servers
Trace MCP tool calls alongside your agent’s reasoning steps with LangSmith. Follow the tracing quickstart to get set up.
The LangChain docs MCP server is a public HTTP endpoint at https://docs.langchain.com/mcp. Connect an agent to it to search and read documentation without writing custom tools.
The docs MCP server is public and does not require an API key. For IDE and coding-agent setup (Claude Code, Cursor, and others), see Use docs programmatically.
The server exposes these tools:

Custom servers

To create your own MCP servers, you can use the @modelcontextprotocol/sdk library. This library provides a simple way to define tools and run them as servers.
To test your agent with MCP tool servers, use the following examples:
Math server (stdio transport)
Weather server (SSE transport)

Transports

MCP supports different transport mechanisms for client-server communication.

HTTP

The http transport (also referred to as streamable-http) uses HTTP requests for client-server communication. See the MCP HTTP transport specification for more details. Use a local URL for servers you run yourself, or a hosted URL such as the LangChain docs MCP server (https://docs.langchain.com/mcp), which is public and does not require an API key.

Passing headers

When connecting to MCP servers over HTTP, you can include custom headers (for example, for authentication or tracing) using the headers field in the connection configuration. This example uses the LangChain docs MCP server; replace the header values for a server that requires authentication:
Passing headers with MultiServerMCPClient

Authentication

The @langchain/mcp-adapters library uses the official MCP TypeScript SDK under the hood, which allows you to provide a custom authentication mechanism by implementing the OAuthClientProvider interface.

stdio

Client launches server as a subprocess and communicates via standard input/output. Best for local tools and simple setups.

Core features

Tools

Tools allow MCP servers to expose executable functions that LLMs can invoke to perform actions—such as querying databases, calling APIs, or interacting with external systems. LangChain converts MCP tools into LangChain tools, making them directly usable in any LangChain agent or workflow.

Loading tools

Use client.getTools() to retrieve tools from MCP servers and pass them to your agent:
When an MCP tool execution fails (CallToolResult with isError: true), @langchain/mcp-adapters raises a ToolException. Wrap tool calls in a try/catch to handle these errors. Unlike the Python adapter, the TypeScript adapter does not return the error to the model as a failed tool message.

Structured content

MCP tools can return structured content alongside the human-readable text response. This is useful when a tool needs to return machine-parseable data (like JSON) in addition to text that gets shown to the model. When an MCP tool returns structuredContent, the adapter adds an mcp_structured_content entry to the tool message artifact array. You can also use tool interceptors to process or transform structured content automatically. Extracting structured content from artifact After invoking your agent, you can access the structured content from tool messages in the response:

Multimodal tool content

MCP tools can return multimodal content (images, text, etc.) in their responses. When an MCP server returns content with multiple parts (e.g., text and images), the adapter converts them to LangChain’s standard content blocks. You can access the standardized representation via the contentBlocks property on the ToolMessage:
This allows you to handle multimodal tool responses in a provider-agnostic way, regardless of how the underlying MCP server formats its content.

Resources

Resources allow MCP servers to expose data—such as files, database records, or API responses—that can be read by clients.

Loading resources

Use client.listResources() to discover resources and client.readResource() to read their contents:

Advanced features

Tool interceptors

MCP servers run as separate processes—they cannot access LangGraph runtime information like the store, context, or agent state. Use beforeToolCall and afterToolCall hooks on MultiServerMCPClient to modify tool arguments, headers, or results:
  • beforeToolCall: Can return { args?, headers? }. Headers are supported for HTTP and SSE transports. Stdio connections do not support custom headers.
  • afterToolCall: Can return { result } where result is a [content, artifact] tuple, a ToolMessage, a LangGraph Command, or the original result.

Progress notifications

Subscribe to progress updates for long-running tool executions with onProgress:

Logging

The MCP protocol supports logging notifications from servers. Subscribe with onMessage:
You can also set the server logging level with client.setLoggingLevel("debug") or client.setLoggingLevel("server_name", "debug").

Additional resources