[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-72272":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":10,"language":11,"languages":10,"totalLinesOfCode":10,"stars":12,"forks":13,"watchers":14,"openIssues":15,"contributorsCount":16,"subscribersCount":16,"size":16,"stars1d":17,"stars7d":18,"stars30d":19,"stars90d":16,"forks30d":16,"starsTrendScore":20,"compositeScore":21,"rankGlobal":10,"rankLanguage":10,"license":22,"archived":23,"fork":23,"defaultBranch":24,"hasWiki":25,"hasPages":23,"topics":26,"createdAt":10,"pushedAt":10,"updatedAt":32,"readmeContent":33,"aiSummary":34,"trendingCount":16,"starSnapshotCount":16,"syncStatus":35,"lastSyncTime":36,"discoverSource":37},72272,"langchain-mcp-adapters","langchain-ai\u002Flangchain-mcp-adapters","langchain-ai","LangChain 🔌 MCP","https:\u002F\u002Fdocs.langchain.com\u002Foss\u002Fpython\u002Flangchain\u002Fmcp",null,"Python",3558,445,19,44,0,4,8,32,12,29.95,"MIT License",false,"main",true,[27,28,29,30,31],"langchain","langgraph","mcp","python","tools","2026-06-12 02:03:01","# LangChain MCP Adapters\n\nThis library provides a lightweight wrapper that makes [Anthropic Model Context Protocol (MCP)](https:\u002F\u002Fmodelcontextprotocol.io\u002Fintroduction) tools compatible with [LangChain](https:\u002F\u002Fgithub.com\u002Flangchain-ai\u002Flangchain) and [LangGraph](https:\u002F\u002Fgithub.com\u002Flangchain-ai\u002Flanggraph).\n\n![MCP](static\u002Fimg\u002Fmcp.png)\n\n> [!note]\n> A JavaScript\u002FTypeScript version of this library is also available at [langchainjs](https:\u002F\u002Fgithub.com\u002Flangchain-ai\u002Flangchainjs\u002Ftree\u002Fmain\u002Flibs\u002Flangchain-mcp-adapters\u002F).\n\n## Features\n\n- 🛠️ Convert MCP tools into [LangChain tools](https:\u002F\u002Fpython.langchain.com\u002Fdocs\u002Fconcepts\u002Ftools\u002F) that can be used with [LangGraph](https:\u002F\u002Fgithub.com\u002Flangchain-ai\u002Flanggraph) agents\n- 📦 A client implementation that allows you to connect to multiple MCP servers and load tools from them\n\n## Installation\n\n```bash\npip install langchain-mcp-adapters\n```\n\n## Quickstart\n\nHere is a simple example of using the MCP tools with a LangGraph agent.\n\n```bash\npip install langchain-mcp-adapters langgraph \"langchain[openai]\"\n\nexport OPENAI_API_KEY=\u003Cyour_api_key>\n```\n\n### Server\n\nFirst, let's create an MCP server that can add and multiply numbers.\n\n```python\n# math_server.py\nfrom mcp.server.fastmcp import FastMCP\n\nmcp = FastMCP(\"Math\")\n\n@mcp.tool()\ndef add(a: int, b: int) -> int:\n    \"\"\"Add two numbers\"\"\"\n    return a + b\n\n@mcp.tool()\ndef multiply(a: int, b: int) -> int:\n    \"\"\"Multiply two numbers\"\"\"\n    return a * b\n\nif __name__ == \"__main__\":\n    mcp.run(transport=\"stdio\")\n```\n\n### Client\n\n```python\n# Create server parameters for stdio connection\nfrom mcp import ClientSession, StdioServerParameters\nfrom mcp.client.stdio import stdio_client\n\nfrom langchain_mcp_adapters.tools import load_mcp_tools\nfrom langchain.agents import create_agent\n\nserver_params = StdioServerParameters(\n    command=\"python\",\n    # Make sure to update to the full absolute path to your math_server.py file\n    args=[\"\u002Fpath\u002Fto\u002Fmath_server.py\"],\n)\n\nasync with stdio_client(server_params) as (read, write):\n    async with ClientSession(read, write) as session:\n        # Initialize the connection\n        await session.initialize()\n\n        # Get tools\n        tools = await load_mcp_tools(session)\n\n        # Create and run the agent\n        agent = create_agent(\"openai:gpt-4.1\", tools)\n        agent_response = await agent.ainvoke({\"messages\": \"what's (3 + 5) x 12?\"})\n```\n\n## Multiple MCP Servers\n\nThe library also allows you to connect to multiple MCP servers and load tools from them:\n\n### Server\n\n```python\n# math_server.py\n...\n\n# weather_server.py\nfrom typing import List\nfrom mcp.server.fastmcp import FastMCP\n\nmcp = FastMCP(\"Weather\")\n\n@mcp.tool()\nasync def get_weather(location: str) -> str:\n    \"\"\"Get weather for location.\"\"\"\n    return \"It's always sunny in New York\"\n\nif __name__ == \"__main__\":\n    mcp.run(transport=\"http\")\n```\n\n```bash\npython weather_server.py\n```\n\n### Client\n\n```python\nfrom langchain_mcp_adapters.client import MultiServerMCPClient\nfrom langchain.agents import create_agent\n\nclient = MultiServerMCPClient(\n    {\n        \"math\": {\n            \"command\": \"python\",\n            # Make sure to update to the full absolute path to your math_server.py file\n            \"args\": [\"\u002Fpath\u002Fto\u002Fmath_server.py\"],\n            \"transport\": \"stdio\",\n        },\n        \"weather\": {\n            # Make sure you start your weather server on port 8000\n            \"url\": \"http:\u002F\u002Flocalhost:8000\u002Fmcp\",\n            \"transport\": \"http\",\n        }\n    }\n)\ntools = await client.get_tools()\nagent = create_agent(\"openai:gpt-4.1\", tools)\nmath_response = await agent.ainvoke({\"messages\": \"what's (3 + 5) x 12?\"})\nweather_response = await agent.ainvoke({\"messages\": \"what is the weather in nyc?\"})\n```\n\n> [!note]\n> Example above will start a new MCP `ClientSession` for each tool invocation. If you would like to explicitly start a session for a given server, you can do:\n>\n> ```python\n> from langchain_mcp_adapters.tools import load_mcp_tools\n>\n> client = MultiServerMCPClient({...})\n> async with client.session(\"math\") as session:\n>     tools = await load_mcp_tools(session)\n> ```\n\n## Streamable HTTP\n\nMCP now supports [streamable HTTP](https:\u002F\u002Fmodelcontextprotocol.io\u002Fspecification\u002F2025-03-26\u002Fbasic\u002Ftransports#streamable-http) transport.\n\nTo start an [example](examples\u002Fservers\u002Fstreamable-http-stateless\u002F) streamable HTTP server, run the following:\n\n```bash\ncd examples\u002Fservers\u002Fstreamable-http-stateless\u002F\nuv run mcp-simple-streamablehttp-stateless --port 3000\n```\n\nAlternatively, you can use FastMCP directly (as in the examples above).\n\nTo use it with Python MCP SDK `streamablehttp_client`:\n\n```python\n# Use server from examples\u002Fservers\u002Fstreamable-http-stateless\u002F\n\nfrom mcp import ClientSession\nfrom mcp.client.streamable_http import streamablehttp_client\n\nfrom langchain.agents import create_agent\nfrom langchain_mcp_adapters.tools import load_mcp_tools\n\nasync with streamablehttp_client(\"http:\u002F\u002Flocalhost:3000\u002Fmcp\") as (read, write, _):\n    async with ClientSession(read, write) as session:\n        # Initialize the connection\n        await session.initialize()\n\n        # Get tools\n        tools = await load_mcp_tools(session)\n        agent = create_agent(\"openai:gpt-4.1\", tools)\n        math_response = await agent.ainvoke({\"messages\": \"what's (3 + 5) x 12?\"})\n```\n\nUse it with `MultiServerMCPClient`:\n\n```python\n# Use server from examples\u002Fservers\u002Fstreamable-http-stateless\u002F\nfrom langchain_mcp_adapters.client import MultiServerMCPClient\nfrom langchain.agents import create_agent\n\nclient = MultiServerMCPClient(\n    {\n        \"math\": {\n            \"transport\": \"http\",\n            \"url\": \"http:\u002F\u002Flocalhost:3000\u002Fmcp\"\n        },\n    }\n)\ntools = await client.get_tools()\nagent = create_agent(\"openai:gpt-4.1\", tools)\nmath_response = await agent.ainvoke({\"messages\": \"what's (3 + 5) x 12?\"})\n```\n\n## Passing runtime headers\n\nWhen connecting to MCP servers, you can include custom headers (e.g., for authentication or tracing) using the `headers` field in the connection configuration. This is supported for the following transports:\n\n- `sse`\n- `http` (or `streamable_http`)\n\n### Example: passing headers with `MultiServerMCPClient`\n\n```python\nfrom langchain_mcp_adapters.client import MultiServerMCPClient\nfrom langchain.agents import create_agent\n\nclient = MultiServerMCPClient(\n    {\n        \"weather\": {\n            \"transport\": \"http\",\n            \"url\": \"http:\u002F\u002Flocalhost:8000\u002Fmcp\",\n            \"headers\": {\n                \"Authorization\": \"Bearer YOUR_TOKEN\",\n                \"X-Custom-Header\": \"custom-value\"\n            },\n        }\n    }\n)\ntools = await client.get_tools()\nagent = create_agent(\"openai:gpt-4.1\", tools)\nresponse = await agent.ainvoke({\"messages\": \"what is the weather in nyc?\"})\n```\n\n> Only `sse` and `http` transports support runtime headers. These headers are passed with every HTTP request to the MCP server.\n\n## Using with LangGraph StateGraph\n\n```python\nfrom langchain_mcp_adapters.client import MultiServerMCPClient\nfrom langgraph.graph import StateGraph, MessagesState, START\nfrom langgraph.prebuilt import ToolNode, tools_condition\n\nfrom langchain.chat_models import init_chat_model\nmodel = init_chat_model(\"openai:gpt-4.1\")\n\nclient = MultiServerMCPClient(\n    {\n        \"math\": {\n            \"command\": \"python\",\n            # Make sure to update to the full absolute path to your math_server.py file\n            \"args\": [\".\u002Fexamples\u002Fmath_server.py\"],\n            \"transport\": \"stdio\",\n        },\n        \"weather\": {\n            # make sure you start your weather server on port 8000\n            \"url\": \"http:\u002F\u002Flocalhost:8000\u002Fmcp\",\n            \"transport\": \"http\",\n        }\n    }\n)\ntools = await client.get_tools()\n\ndef call_model(state: MessagesState):\n    response = model.bind_tools(tools).invoke(state[\"messages\"])\n    return {\"messages\": response}\n\nbuilder = StateGraph(MessagesState)\nbuilder.add_node(call_model)\nbuilder.add_node(ToolNode(tools))\nbuilder.add_edge(START, \"call_model\")\nbuilder.add_conditional_edges(\n    \"call_model\",\n    tools_condition,\n)\nbuilder.add_edge(\"tools\", \"call_model\")\ngraph = builder.compile()\nmath_response = await graph.ainvoke({\"messages\": \"what's (3 + 5) x 12?\"})\nweather_response = await graph.ainvoke({\"messages\": \"what is the weather in nyc?\"})\n```\n\n## Using with LangGraph API Server\n\n> [!TIP]\n> Check out [this guide](https:\u002F\u002Flangchain-ai.github.io\u002Flanggraph\u002Ftutorials\u002Flanggraph-platform\u002Flocal-server\u002F) on getting started with LangGraph API server.\n\nIf you want to run a LangGraph agent that uses MCP tools in a LangGraph API server, you can use the following setup:\n\n```python\n# graph.py\nfrom contextlib import asynccontextmanager\nfrom langchain_mcp_adapters.client import MultiServerMCPClient\nfrom langchain.agents import create_agent\n\nasync def make_graph():\n    client = MultiServerMCPClient(\n        {\n            \"weather\": {\n                # make sure you start your weather server on port 8000\n                \"url\": \"http:\u002F\u002Flocalhost:8000\u002Fmcp\",\n                \"transport\": \"http\",\n            },\n            # ATTENTION: MCP's stdio transport was designed primarily to support applications running on a user's machine.\n            # Before using stdio in a web server context, evaluate whether there's a more appropriate solution.\n            # For example, do you actually need MCP? or can you get away with a simple `@tool`?\n            \"math\": {\n                \"command\": \"python\",\n                # Make sure to update to the full absolute path to your math_server.py file\n                \"args\": [\"\u002Fpath\u002Fto\u002Fmath_server.py\"],\n                \"transport\": \"stdio\",\n            },\n        }\n    )\n    tools = await client.get_tools()\n    agent = create_agent(\"openai:gpt-4.1\", tools)\n    return agent\n```\n\nIn your [`langgraph.json`](https:\u002F\u002Flangchain-ai.github.io\u002Flanggraph\u002Fcloud\u002Freference\u002Fcli\u002F#configuration-file) make sure to specify `make_graph` as your graph entrypoint:\n\n```json\n{\n  \"dependencies\": [\".\"],\n  \"graphs\": {\n    \"agent\": \".\u002Fgraph.py:make_graph\"\n  }\n}\n```\n","该项目提供了一个轻量级的包装器，使得Anthropic Model Context Protocol (MCP)工具能够与LangChain和LangGraph兼容。核心功能包括将MCP工具转换为可被LangGraph代理使用的LangChain工具，并支持连接多个MCP服务器以加载工具。采用Python开发，适用于需要整合不同来源AI工具的应用场景中，特别是在构建复杂的自然语言处理或对话系统时，能够简化多模型间的协作流程。通过pip安装后，用户可以快速搭建起基于MCP协议的服务端与客户端，实现如数学计算、天气查询等功能的无缝接入。",2,"2026-06-11 03:41:07","high_star"]