{"id":1530,"library":"langchain-mcp-adapters","title":"LangChain MCP Adapters","description":"This library provides a lightweight wrapper that makes Anthropic Model Context Protocol (MCP) tools compatible with LangChain and LangGraph agents. It automatically converts MCP tools, manages connections to multiple MCP servers, and seamlessly integrates them into LangChain workflows. The current version is 0.2.2 and it appears to have an active development and release cadence, with version 0.2.0 released in December 2025 and ongoing updates.","status":"active","version":"0.2.2","language":"en","source_language":"en","source_url":"https://github.com/langchain-ai/langchain-mcp-adapters","tags":["langchain","anthropic","mcp","tools","agents","llm","protocol"],"install":[{"cmd":"pip install langchain-mcp-adapters langchain-core","lang":"bash","label":"Install core library and LangChain dependencies"}],"dependencies":[{"reason":"Primary framework for integration.","package":"langchain"},{"reason":"Core components for LangChain integration, often required alongside `langchain`.","package":"langchain-core"},{"reason":"The underlying Model Context Protocol (MCP) SDK, used for client sessions and server parameters.","package":"mcp"},{"reason":"Optional: For creating custom MCP servers, if you are building your own tools.","package":"fastmcp","optional":true},{"reason":"Optional: For using Anthropic models with LangChain agents.","package":"anthropic","optional":true},{"reason":"Optional: For using OpenAI models with LangChain agents.","package":"openai","optional":true}],"imports":[{"symbol":"MultiServerMCPClient","correct":"from langchain_mcp_adapters.client import MultiServerMCPClient"},{"symbol":"load_mcp_tools","correct":"from langchain_mcp_adapters.tools import load_mcp_tools"},{"note":"MCP's ClientSession is directly from the 'mcp' library, not the adapter.","wrong":"from langchain_mcp_adapters.client import ClientSession","symbol":"ClientSession","correct":"from mcp import ClientSession"},{"note":"MCP's StdioServerParameters is directly from the 'mcp' library, not the adapter.","wrong":"from langchain_mcp_adapters.client import StdioServerParameters","symbol":"StdioServerParameters","correct":"from mcp import StdioServerParameters"}],"quickstart":{"code":"import os\nimport asyncio\nfrom langchain_mcp_adapters.client import MultiServerMCPClient\nfrom langchain.agents import create_agent\nfrom langchain_core.messages import HumanMessage\n\n# NOTE: For this example to be runnable, you need a running MCP server.\n# For a 'math' server, you could use fastmcp:\n# # math_server.py\n# from fastmcp import FastMCP\n# mcp = FastMCP(\"Math\")\n# @mcp.tool()\n# def add(a: int, b: int) -> int:\n#     \"\"\"Add two numbers\"\"\"\n#     return a + b\n# if __name__ == \"__main__\":\n#     mcp.run(transport=\"stdio\")\n\n# And start it from your terminal: python /path/to/math_server.py\n\nasync def main():\n    # Set your LLM API key as an environment variable\n    # e.g., export OPENAI_API_KEY=\"your_key_here\"\n    # Or, for Anthropic: export ANTHROPIC_API_KEY=\"your_key_here\"\n    if not os.environ.get('OPENAI_API_KEY') and not os.environ.get('ANTHROPIC_API_KEY'):\n        print(\"Please set OPENAI_API_KEY or ANTHROPIC_API_KEY environment variable.\")\n        return\n\n    client = MultiServerMCPClient(\n        {\n            \"math\": {\n                \"transport\": \"stdio\", # Local subprocess communication\n                \"command\": \"python\", # Path to python interpreter\n                \"args\": [\"/path/to/your/math_server.py\"], # ABSOLUTE path to your math_server.py\n            },\n            \"weather\": {\n                \"transport\": \"http\", # HTTP-based remote server\n                \"url\": \"http://localhost:8000/mcp\", # Ensure your weather server is running on port 8000\n                \"onConnectionError\": \"ignore\" # Ignore if this server is not running for demo\n            }\n        }\n    )\n\n    # Retrieve tools from the connected MCP servers\n    tools = await client.get_tools()\n    print(f\"Loaded {len(tools)} tools.\")\n\n    # Example: Create an agent using LangChain's create_agent\n    # Choose your LLM. For example, \"openai:gpt-4o\" or \"anthropic:claude-3-opus-20240229\"\n    agent = create_agent(\"openai:gpt-4o\", tools)\n\n    # Invoke the agent with a message that uses a tool\n    print(\"\\nInvoking agent for math query...\")\n    math_response = await agent.ainvoke(\n        {\"messages\": [HumanMessage(content=\"what's (3 + 5) x 12?\")]}\n    )\n    print(\"Math Agent Response:\", math_response)\n\n    print(\"\\nInvoking agent for weather query (may fail if server not running)...\")\n    weather_response = await agent.ainvoke(\n        {\"messages\": [HumanMessage(content=\"what is the weather in nyc?\")]}\n    )\n    print(\"Weather Agent Response:\", weather_response)\n\n    await client.close() # Important to close client to terminate subprocesses\n\nif __name__ == \"__main__\":\n    asyncio.run(main())","lang":"python","description":"This quickstart demonstrates how to set up `MultiServerMCPClient` to connect to multiple (mock) MCP servers and use their tools with a LangChain agent. It shows configuration for both `stdio` (local subprocess) and `http` transports. Remember to replace `/path/to/your/math_server.py` with an actual path to a running MCP math server and ensure your API keys for the chosen LLM are set as environment variables."},"warnings":[{"fix":"Consider strategies like lazy-loading schemas (if supported by future versions or custom logic), using a gateway for selective loading, or carefully managing the number and verbosity of exposed tools.","message":"Connecting to multiple MCP servers can lead to high token consumption due to all tool schemas being preloaded into the LLM's system prompt. This 'token overhead' can be significant, especially with many verbose tool definitions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Implement a centralized authentication mechanism, potentially via a gateway that handles credentials for upstream servers, or leverage `authProvider` options in `MultiServerMCPClient` for OAuth 2.0 (new in v0.4.6 of JS version, check Python equivalent).","message":"Each MCP server can have distinct authentication requirements (API keys, OAuth, etc.), leading to 'Auth Fragmentation' and complex credential management across multiple development and production environments.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Carefully validate MCP tool schemas and `MultiServerMCPClient` configurations, paying close attention to required parameters and expected data types for each transport (e.g., `command` for `stdio`, `url` for `http`/`sse`). Enable verbose logging for debugging.","message":"Schema misalignment between MCP tool input/output JSON schemas and LangChain's expectations, or invalid connection configurations for `MultiServerMCPClient`, can lead to `ZodError` or silent failures.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Utilize `onConnectionError: 'ignore'` for non-critical servers during development. Implement robust error handling and monitoring for production environments. Ensure proper server startup and network connectivity, especially for HTTP/SSE transports.","message":"Managing different transport protocols (stdio, HTTP, SSE) and handling connection issues (e.g., server startup delays, unreachable servers) can add complexity to setup and debugging.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use the `prefixToolNameWithServerName` option in `MultiServerMCPClient` to automatically add a server-specific prefix to tool names, preventing collisions.","message":"When connecting to multiple MCP servers, tools from different servers might have conflicting names, leading to ambiguity or unexpected behavior if not properly handled.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}