From ce4caa3ac8a28f523590f10dfd102f7a11f31bfd Mon Sep 17 00:00:00 2001 From: Marco Perini Date: Mon, 12 May 2025 17:55:38 -0700 Subject: [PATCH] docs: mcp adapters example for deployed graph (#4666) Example to connect to the MCP server of a deployed graph in langgraph platform (langchain-mcp-adapters). The graph can be used as a MCP tool. ![image](https://github.com/user-attachments/assets/fb5c36d4-0f04-4814-b0b7-bf7fcf6f8381) --------- Co-authored-by: Vadym Barda --- docs/docs/concepts/server-mcp.md | 45 +++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/docs/docs/concepts/server-mcp.md b/docs/docs/concepts/server-mcp.md index 54f542e83..7d4280b59 100644 --- a/docs/docs/concepts/server-mcp.md +++ b/docs/docs/concepts/server-mcp.md @@ -162,7 +162,50 @@ Use an MCP-compliant client to connect to the LangGraph server. The following ex === "Python" - No official MCP client is available for Python yet. + + Install the adapter with: + + ```bash + pip install langchain-mcp-adapters + ``` + + Here is an example of how to connect to a remote MCP endpoint and use an agent as a tool: + + ```python + # 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()) + ``` ## Session behavior