From 23327f56476b237a31c0f9c4a74fdb18fff22b52 Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Wed, 25 Jun 2025 15:06:35 -0400 Subject: [PATCH] docs: update mcp docs (#5191) Update MCP docs --- docs/docs/agents/mcp.md | 121 ++++++++++++++++++++++--------- docs/docs/concepts/server-mcp.md | 10 +-- docs/mkdocs.yml | 4 +- 3 files changed, 90 insertions(+), 45 deletions(-) diff --git a/docs/docs/agents/mcp.md b/docs/docs/agents/mcp.md index 919f5a967..9b654ffa5 100644 --- a/docs/docs/agents/mcp.md +++ b/docs/docs/agents/mcp.md @@ -7,7 +7,7 @@ hide: - tags --- -# MCP Integration +# Use MCP [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) is an open protocol that standardizes how applications provide tools and context to language models. LangGraph agents can use tools defined on MCP servers through the `langchain-mcp-adapters` library. @@ -23,41 +23,91 @@ pip install langchain-mcp-adapters The `langchain-mcp-adapters` package enables agents to use tools defined across one or more MCP servers. -```python title="Agent using tools defined on MCP servers" -# highlight-next-line -from langchain_mcp_adapters.client import MultiServerMCPClient -from langgraph.prebuilt import create_react_agent -# highlight-next-line -client = MultiServerMCPClient( - { - "math": { - "command": "python", - # Replace with absolute path to your math_server.py file - "args": ["/path/to/math_server.py"], - "transport": "stdio", - }, - "weather": { - # Ensure you start your weather server on port 8000 - "url": "http://localhost:8000/mcp", - "transport": "streamable_http", - } - } -) -# highlight-next-line -tools = await client.get_tools() -agent = create_react_agent( - "anthropic:claude-3-7-sonnet-latest", +=== "In an agent" + + ```python title="Agent using tools defined on MCP servers" # highlight-next-line - tools -) -math_response = await agent.ainvoke( - {"messages": [{"role": "user", "content": "what's (3 + 5) x 12?"}]} -) -weather_response = await agent.ainvoke( - {"messages": [{"role": "user", "content": "what is the weather in nyc?"}]} -) -``` + from langchain_mcp_adapters.client import MultiServerMCPClient + from langgraph.prebuilt import create_react_agent + + # highlight-next-line + client = MultiServerMCPClient( + { + "math": { + "command": "python", + # Replace with absolute path to your math_server.py file + "args": ["/path/to/math_server.py"], + "transport": "stdio", + }, + "weather": { + # Ensure you start your weather server on port 8000 + "url": "http://localhost:8000/mcp", + "transport": "streamable_http", + } + } + ) + # highlight-next-line + tools = await client.get_tools() + agent = create_react_agent( + "anthropic:claude-3-7-sonnet-latest", + # highlight-next-line + tools + ) + math_response = await agent.ainvoke( + {"messages": [{"role": "user", "content": "what's (3 + 5) x 12?"}]} + ) + weather_response = await agent.ainvoke( + {"messages": [{"role": "user", "content": "what is the weather in nyc?"}]} + ) + ``` + +=== "In a workflow" + + ```python + from langchain_mcp_adapters.client import MultiServerMCPClient + from langgraph.graph import StateGraph, MessagesState, START + from langgraph.prebuilt import ToolNode, tools_condition + + from langchain.chat_models import init_chat_model + model = init_chat_model("openai:gpt-4.1") + + client = MultiServerMCPClient( + { + "math": { + "command": "python", + # Make sure to update to the full absolute path to your math_server.py file + "args": ["./examples/math_server.py"], + "transport": "stdio", + }, + "weather": { + # make sure you start your weather server on port 8000 + "url": "http://localhost:8000/mcp/", + "transport": "streamable_http", + } + } + ) + tools = await client.get_tools() + + def call_model(state: MessagesState): + response = model.bind_tools(tools).invoke(state["messages"]) + return {"messages": response} + + builder = StateGraph(MessagesState) + builder.add_node(call_model) + builder.add_node(ToolNode(tools)) + builder.add_edge(START, "call_model") + builder.add_conditional_edges( + "call_model", + tools_condition, + ) + builder.add_edge("tools", "call_model") + graph = builder.compile() + math_response = await graph.ainvoke({"messages": "what's (3 + 5) x 12?"}) + weather_response = await graph.ainvoke({"messages": "what is the weather in nyc?"}) + ``` + + ## Custom MCP servers @@ -106,4 +156,5 @@ if __name__ == "__main__": ## Additional resources - [MCP documentation](https://modelcontextprotocol.io/introduction) -- [MCP Transport documentation](https://modelcontextprotocol.io/docs/concepts/transports) \ No newline at end of file +- [MCP Transport documentation](https://modelcontextprotocol.io/docs/concepts/transports) +- [langchain_mcp_adapters](https://github.com/langchain-ai/langchain-mcp-adapters) \ No newline at end of file diff --git a/docs/docs/concepts/server-mcp.md b/docs/docs/concepts/server-mcp.md index 658e69399..7f144e87f 100644 --- a/docs/docs/concepts/server-mcp.md +++ b/docs/docs/concepts/server-mcp.md @@ -6,20 +6,14 @@ hide: - tags --- -# MCP Endpoint +# MCP endpoint in LangGraph Server The **Model Context Protocol (MCP)** is an open protocol for describing tools and data sources in a model-agnostic format, enabling LLMs to discover and use them via a structured API. [LangGraph Server](./langgraph_server.md) implements MCP using the [Streamable HTTP transport](https://spec.modelcontextprotocol.io/specification/2025-03-26/basic/transports/#streamable-http). This allows LangGraph **agents** to be exposed as **MCP tools**, making them usable with any MCP-compliant client supporting Streamable HTTP. -The MCP endpoint is available at: - -``` -/mcp -``` - -on [LangGraph Server](./langgraph_server.md). +The MCP endpoint is available at `/mcp` on [LangGraph Server](./langgraph_server.md). ## Requirements diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index a10204556..f5a928f01 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -172,8 +172,8 @@ nav: - Prebuilt implementation: agents/multi-agent.md - Custom implementation: how-tos/multi_agent.ipynb - MCP: - - Use MCP tools: agents/mcp.md - - Server deployment via MCP: concepts/server-mcp.md + - Use MCP: agents/mcp.md + - Server API: concepts/server-mcp.md - Deployment: - Basic deployment: agents/deployment.md - Set up your application: