Using langchain_mcp_adapters
to connect my LangGraph agent with a MCP server, I had an error:
Using the following code:
import os
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain.agents import create_agent
from agents.prompt import template
dot_api_key = os.getenv("DOT_API_KEY")
client = MultiServerMCPClient(
{
"dotcms": {
"command": "node",
"args": ["...."],
"transport": "stdio",
"env": {
"DOTCMS_URL": "http://localhost:8080",
"AUTH_TOKEN": dot_api_key
}
},
}
)
async def make_graph():
tools = await client.get_tools()
prompt = template.render()
agent = create_agent("openai:gpt-4.1", tools, prompt=prompt)
return agent
Apparently, for each request using LangGraph Studio, a new session is created; therefore, with an MCP server that needs to maintain a session, it fails. Here is the evidence of the error.
The server has a tool named context_initialization
that must be called before any other tool to initialize the context. My LangGraph agent correctly identifies and calls context_initialization
first, and the logs show that the call is successful.
However, when the agent attempts to call a second tool (e.g., content_search
), the execution fails with the error ToolException: Cannot execute tool “content_search” because context initialization is required first. This indicates that the state from the initialization was lost between the two tool calls.
As seen in this screenshot from the logs, the context_initialization
tool runs first, but the subsequent call to content_search
fails.
I reported this issue as a bug here: You must call the "context_initialization" tool before using any other tools. · Issue #337 · langchain-ai/langchain-mcp-adapters · GitHub but I’m not sure if it’s a real bug or a misconfiguration.