How to beginner · 3 min read

How to use langchain-mcp-adapters package

Quick answer
The langchain-mcp-adapters package enables integration of AI agents with external tools and resources via the Model Context Protocol (MCP). Use it by installing the package, importing adapters like MCPStdIOAdapter, and connecting them to your AI agent to enable tool usage and context sharing.

PREREQUISITES

  • Python 3.8+
  • pip install langchain-mcp-adapters
  • Basic knowledge of LangChain and AI agents

Setup

Install the langchain-mcp-adapters package via pip and ensure you have Python 3.8 or higher. No API keys are required specifically for this package, but your AI agent may require keys depending on the model used.

bash
pip install langchain-mcp-adapters

Step by step

This example shows how to create an MCP server using the MCPStdIOAdapter to connect an AI agent to external tools via standard input/output streams.

python
from mcp.server.stdio import stdio_server

# Start the MCP server that listens on stdio
if __name__ == '__main__':
    stdio_server()

# Run this script in a terminal or integrate with your AI agent to enable MCP communication.
output
Server started, listening on stdio for MCP messages...

Common variations

You can use different adapters like MCPWebSocketAdapter for WebSocket communication or integrate MCP with LangChain agents by wrapping the MCP server as a tool adapter. Async usage is supported by running the server in an async event loop.

python
import asyncio
from mcp.server.stdio import stdio_server

async def main():
    await stdio_server()

if __name__ == '__main__':
    asyncio.run(main())
output
Async MCP server running on stdio...

Troubleshooting

  • If the MCP server does not respond, ensure your AI agent sends properly formatted MCP JSON messages.
  • Check that no other process is blocking stdio or WebSocket ports used by the adapter.
  • Use logging to debug message flow and errors in MCP communication.

Key Takeaways

  • Install langchain-mcp-adapters to enable MCP protocol integration for AI agents.
  • Use MCPStdIOAdapter for simple stdio-based communication with external tools.
  • Async and WebSocket adapters provide flexible integration options for different environments.
Verified 2026-04
Verify ↗