Skip to main content

LangChain/LangGraph Integration

LangGraph is a library for building stateful, multi-actor applications with Large Language Models (LLMs). This guide shows how to integrate LangGraph with Cequence AI Gateway using the LangChain MCP Adapters.

Requirements

  • Python 3.8 or higher
  • LangGraph
  • langchain-mcp-adapters

Install the required dependencies:

pip install langgraph langchain-mcp-adapters

Configuration

To integrate LangGraph with Cequence AI Gateway, you'll use the MultiServerMCPClient from the langchain-mcp-adapters library:

from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent

# Configure the MCP client to connect to AI Gateway
client = MultiServerMCPClient(
{
"weather": {
"transport": "streamable_http",
"url": "<CEQUENCE_AI_GATEWAY_MCP_URL>",
"headers": {
"Authorization": "Bearer YOUR_TOKEN",
"X-Custom-Header": "custom-value"
},
}
}
)

# Get tools from the MCP server
tools = await client.get_tools()

# Create a LangGraph agent with the tools
agent = create_react_agent("openai:gpt-4.1", tools)

# Use the agent
response = await agent.ainvoke({"messages": "<YOUR_PROMPT>"})

Key Components

MultiServerMCPClient

The MultiServerMCPClient allows you to connect to multiple MCP (Model Context Protocol) servers, including Cequence AI Gateway endpoints.

Configuration Options

  • transport: Use "streamable_http" for HTTP-based communication
  • url: Your AI Gateway MCP endpoint URL
  • headers: Authentication and custom headers for your requests

Agent Creation

Use LangGraph's create_react_agent to create an agent that can utilize the tools provided by AI Gateway.

Best Practices

  1. Authentication: Always use proper authentication tokens when connecting to AI Gateway
  2. Error Handling: Implement proper error handling for network requests
  3. Resource Management: Close connections properly when done
  4. Configuration Management: Store sensitive configuration like tokens in environment variables

Repository Reference

For more information and advanced usage examples, see the langchain-mcp-adapters repository.

Next Steps

  • Explore additional MCP servers you can connect to
  • Learn about LangGraph's advanced features for building complex AI workflows
  • Configure monitoring and logging for your AI Gateway integration