Skip to content

模型上下文协议 (Model Context Protocol, MCP) ​

通过开放的 MCP 协议标准连接外部数据库、API 与文件系统

模型上下文协议 (Model Context Protocol, MCP) 是一套由 Anthropic 发起、现已成为业界标准的开放协议,旨在以安全、统一的格式向 AI 智能体暴露外部工具、数据源与业务能力。

在 LangChain 和 Deep Agents 中,你可以通过官方适配器库 @langchain/mcp-adapters 将任何现成的 MCP 服务器一键转换为智能体可调用的工具。

安装依赖包 (Install) ​

安装官方 MCP 适配器:

bash
npm install @langchain/mcp-adapters@^2.0.0 @langchain/core @langchain/langgraph

快速起步 (Quickstart) ​

使用 MultiServerMCPClient 连接到一个或多个 MCP 服务器,拉取工具列表并传递给智能体:

bash
pnpm add @langchain/mcp-adapters@^2.0.0 @langchain/core @langchain/langgraph

传输协议支持 (Transports) ​

MCP 规范定义了两种核心传输机制,LangChain 适配器均予以原生支持:

  1. STDIO 传输 (标准输入输出):通过子进程管道(Standard I/O)在本地启动并与 MCP 服务器通信。非常适合调用本地 CLI、SQLite、本地文件系统等工具。
  2. HTTP / SSE 传输 (Server-Sent Events):通过网络调用远程独立部署的 MCP 服务端,适合连接云数据库、SaaS API 与企业微服务。
bash
yarn add @langchain/mcp-adapters@^2.0.0 @langchain/core @langchain/langgraph

下一步与进阶配置 ​

  • 身份认证与安全鉴权:配置带 Bearer Token 或 OAuth 的远程 MCP 连接。
  • 工具白名单过滤:当 MCP 服务器暴露了几十个工具时,仅按需筛选部分工具给智能体。
  • 在生产环境中部署:通过 LangSmith 托管的 MCP 连接器管理最终用户的鉴权凭据。
bash
bun add @langchain/mcp-adapters@^2.0.0 @langchain/core @langchain/langgraph

Add @modelcontextprotocol/client too if you import SDK types or transports.

Quickstart ​

Connect an agent to the LangChain docs MCP server to search the documentation. This example requires langchain, @langchain/anthropic, and an Anthropic API key. See the LangChain quickstart for setup.

Keep the adapter open while the agent runs, then close it in finally:

ts
import { MCPAdapter } from "@langchain/mcp-adapters";
import { createAgent } from "langchain";

async function main() {
  const adapter = new MCPAdapter({
    servers: { docs: { url: "https://docs.langchain.com/mcp" } },
  });

  try {
    const tools = await adapter.listTools();
    const agent = createAgent({ model: "claude-sonnet-5", tools });
    const result = await agent.invoke({
      messages: [
        {
          role: "user",
          content: "How do I add short-term memory to a LangChain agent?",
        },
      ],
    });
    console.log(result.messages.at(-1)?.text);
  } finally {
    await adapter.close();
  }
}

await main();

View example trace

Open a public LangSmith run for this example.

LangChain docs MCP server

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:

ts
> import { MCPAdapter } from "@langchain/mcp-adapters";
> import { createAgent } from "langchain";
>
> async function main() {
>   const adapter = new MCPAdapter({
>     servers: { docs: { url: "https://docs.langchain.com/mcp" } }, 
>   });
>
>   try {
>     const tools = await adapter.listTools();
>     const agent = createAgent({ model: "claude-sonnet-5", tools });
>     return await agent.invoke({
>       messages: [
>         {
>           role: "user",
>           content: "How do I add short-term memory to a LangChain agent?",
>         },
>       ],
>     });
>   } finally {
>     await adapter.close();
>   }
> }
> ```

>
> > **[View example trace](https://smith.langchain.com/public/e5a365b0-d712-46ca-ab53-61cb0e4e1acd/r)**
> >
> > Open a public LangSmith run for this example.
>
> > [!NOTE]
> >
> > 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](/use-these-docs.md).
>
> The server exposes these tools:
>
> | Tool | Description |
> | - | - |
> | `search_docs_by_lang_chain` | Search docs for relevant guides, how-tos, and examples. |
> | `query_docs_filesystem_docs_by_lang_chain` | Read or search docs through a virtual filesystem (`rg`, `head`, `cat`, and related commands). |
> | `submit_feedback` | Report a problem with a documentation page. |
>
> `MCPAdapter` exposes these tools with the server-name prefix, for example `docs_search_docs_by_lang_chain`.

## Transports

[`MCPAdapter`](https://reference.langchain.com/javascript/langchain-mcp-adapters/MCPAdapter) accepts a map of named server definitions. It selects the transport from each definition, so one adapter can connect to local stdio servers and remote HTTP servers at the same time:

```ts theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { MCPAdapter } from "@langchain/mcp-adapters";

const adapter = new MCPAdapter({
  servers: {
    // A local server launched as a subprocess over stdio.
    local: {
      command: "node",
      args: ["./weather-server.js"],
    },

    // A remote server reached over Streamable HTTP.
    remote: {
      url: "https://example.com/mcp",
    },

    // A legacy server with an explicit SSE endpoint.
    legacy: {
      url: "https://legacy.example.com/sse",
      transport: "sse",
      mode: "legacy",
    },
  },
});

try {
  const tools = await adapter.listTools();
  // Pass tools to createAgent({ tools, ... }).
} finally {
  await adapter.close();
}

View example trace

Open a public LangSmith run for this example.

Each server definition can use one of these transports:

  • stdio: Provide command and args. The adapter starts the command as a child process and communicates over standard input and output.
  • Streamable HTTP: Provide url. This is the default transport for URL-based servers, so transport: "http" is optional.
  • SSE: Provide url and set transport: "sse". Use this transport only for legacy servers that expose an SSE endpoint.

Transport selection and protocol negotiation are separate. The optional mode setting controls which MCP protocol era the client accepts. See Protocol eras.

NOTE

For new remote servers, omit both transport and mode. The adapter uses Streamable HTTP with automatic protocol negotiation. Specify these settings only when connecting to a server with known legacy requirements.

Next steps ​

Tools

Load MCP tools into agents, control their execution, and handle their outputs.

Connections

Connection lifecycle, multiple servers, protocol eras, and caching.

Authentication

Bearer tokens, OAuth 2.1, and per-user server auth.


Connect these docs to your agent of choice via MCP for real-time answers.

Edit this page on GitHub or file an issue.