LangGraph Server 中的 MCP 端点¶
模型上下文协议(MCP)是一个开放协议,用于以模型无关的格式描述工具和数据源,使 LLM 能够通过结构化 API 发现和使用它们。
LangGraph Server 使用可流式 HTTP 传输实现 MCP。这允许将 LangGraph 智能代理**暴露为 **MCP 工具,使其可以与任何支持可流式 HTTP 的 MCP 兼容客户端一起使用。
MCP 端点位于 LangGraph Server 的 /mcp 路径。
要求¶
要使用 MCP,请确保已安装以下依赖项:
langgraph-api >= 0.2.3langgraph-sdk >= 0.1.61
使用以下命令安装:
要使用 MCP,请确保已安装 api 和 sdk 包。
将智能代理暴露为 MCP 工具¶
部署后,你的智能代理将以此配置出现在 MCP 端点中作为工具:
- 工具名称:智能代理的名称。
- 工具描述:智能代理的描述。
- 工具输入模式:智能代理的输入模式。
设置名称和描述¶
你可以在 langgraph.json 中设置智能代理的名称和描述:
{
"graphs": {
"my_agent": {
"path": "./my_agent/agent.py:graph",
"description": "A description of what the agent does"
}
},
"env": ".env"
}
{
"graphs": {
"my_agent": {
"path": "./my_agent/agent.ts:graph",
"description": "A description of what the agent does"
}
},
"env": ".env"
}
部署后,你可以使用 LangGraph SDK 更新名称和描述。
模式¶
定义清晰、最小的输入和输出模式,以避免向 LLM 暴露不必要的内部复杂性。
默认的 MessagesState 使用 AnyMessage,它支持多种消息类型,但对于直接 LLM 暴露来说过于通用。
相反,定义使用显式类型输入和输出结构的**自定义智能代理或工作流**。
例如,一个回答文档问题的工作流可能如下所示:
from langgraph.graph import StateGraph, START, END
from typing_extensions import TypedDict
# Define input schema
class InputState(TypedDict):
question: str
# Define output schema
class OutputState(TypedDict):
answer: str
# Combine input and output
class OverallState(InputState, OutputState):
pass
# Define the processing node
def answer_node(state: InputState):
# Replace with actual logic and do something useful
return {"answer": "bye", "question": state["question"]}
# Build the graph with explicit schemas
builder = StateGraph(OverallState, input_schema=InputState, output_schema=OutputState)
builder.add_node(answer_node)
builder.add_edge(START, "answer_node")
builder.add_edge("answer_node", END)
graph = builder.compile()
# Run the graph
print(graph.invoke({"question": "hi"}))
有关更多详细信息,请参阅底层概念指南。
使用概述¶
要启用 MCP:
- 升级到使用 langgraph-api>=0.2.3。如果你正在部署 LangGraph Platform,如果你创建新修订版,这将自动为你完成。
- MCP 工具(智能代理)将自动暴露。
- 使用任何支持可流式 HTTP 的 MCP 兼容客户端连接。
客户端¶
使用 MCP 兼容客户端连接到 LangGraph 服务器。以下示例展示如何使用 langchain-mcp-adapters 进行连接。
使用以下命令安装适配器:
以下是如何连接到远程 MCP 端点并将智能代理用作工具的示例:
# Create server parameters for stdio connection
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
import asyncio
from langchain_mcp_adapters.tools import load_mcp_tools
from langgraph.prebuilt import create_react_agent
server_params = {
"url": "https://mcp-finance-agent.xxx.us.langgraph.app/mcp",
"headers": {
"X-Api-Key":"lsv2_pt_your_api_key"
}
}
async def main():
async with streamablehttp_client(**server_params) as (read, write, _):
async with ClientSession(read, write) as session:
# Initialize the connection
await session.initialize()
# Load the remote graph as if it was a tool
tools = await load_mcp_tools(session)
# Create and run a react agent with the tools
agent = create_react_agent("openai:gpt-4.1", tools)
# Invoke the agent with a message
agent_response = await agent.ainvoke({"messages": "What can the finance agent do for me?"})
print(agent_response)
if __name__ == "__main__":
asyncio.run(main())
使用 MCP 兼容客户端连接到 LangGraph 服务器。以下示例展示如何使用 @langchain/mcp-adapters 进行连接。
以下是如何连接到远程 MCP 端点并将智能代理用作工具的示例:
import { MultiServerMCPClient } from "@langchain/mcp-adapters";
import { createReactAgent } from "@langchain/langgraph";
import { ChatOpenAI } from "@langchain/openai";
async function main() {
const client = new MultiServerMCPClient({
mcpServers: {
"finance-agent": {
url: "https://mcp-finance-agent.xxx.us.langgraph.app/mcp",
headers: {
"X-Api-Key": "lsv2_pt_your_api_key",
},
},
},
});
const tools = await client.getTools();
const model = new ChatOpenAI({
model: "gpt-4o-mini",
temperature: 0,
});
const agent = createReactAgent({
model,
tools,
});
const response = await agent.invoke({
input: "What can the finance agent do for me?",
});
console.log(response);
}
main();
会话行为¶
当前的 LangGraph MCP 实现不支持会话。每个 /mcp 请求都是无状态且独立的。
认证¶
/mcp 端点使用与 LangGraph API 其余部分相同的认证。有关设置详细信息,请参阅认证指南。
禁用 MCP¶
要禁用 MCP 端点,请在 langgraph.json 配置文件中将 disable_mcp 设置为 true:
这将阻止服务器暴露 /mcp 端点。