跳到内容

MCP

Model Context Protocol (MCP) 是一种开放协议,它规范了应用程序如何向语言模型提供工具和上下文。LangGraph 代理可以通过 langchain-mcp-adapters 库使用在 MCP 服务器上定义的工具。

MCP

安装 langchain-mcp-adapters 库以在 LangGraph 中使用 MCP 工具

pip install langchain-mcp-adapters

向 MCP 服务器进行身份验证

您可以设置自定义身份验证中间件,以便在您的 LangGraph Platform 部署中对用户进行 MCP 服务器身份验证,从而获取用户范围工具的访问权限。

注意

自定义身份验证是 LangGraph Platform 的一项功能。

此流程的架构示例

sequenceDiagram
  %% Actors
  participant ClientApp as Client
  participant AuthProv  as Auth Provider
  participant LangGraph as LangGraph Backend
  participant SecretStore as Secret Store
  participant MCPServer as MCP Server

  %% Platform login / AuthN
  ClientApp  ->> AuthProv: 1. Login (username / password)
  AuthProv   -->> ClientApp: 2. Return token
  ClientApp  ->> LangGraph: 3. Request with token

  Note over LangGraph: 4. Validate token (@auth.authenticate)
  LangGraph  -->> AuthProv: 5. Fetch user info
  AuthProv   -->> LangGraph: 6. Confirm validity

  %% Fetch user tokens from secret store
  LangGraph  ->> SecretStore: 6a. Fetch user tokens
  SecretStore -->> LangGraph: 6b. Return tokens

  Note over LangGraph: 7. Apply access control (@auth.on.*)

  %% MCP round-trip
  Note over LangGraph: 8. Build MCP client with user token
  LangGraph  ->> MCPServer: 9. Call MCP tool (with header)
  Note over MCPServer: 10. MCP validates header and runs tool
  MCPServer  -->> LangGraph: 11. Tool response

  %% Return to caller
  LangGraph  -->> ClientApp: 12. Return resources / tool output

更多信息,请参阅LangGraph 服务器中的 MCP 端点