Quick question, I have an agent (typical ReAct agent, system prompt + some tools to get the job done) that gets a list of entities, user select one entity by typing eg. “the 3rd one”, then agent needs to call another tool with some id for that entity. The problem is agent mess up the id, either picks different id or different entity from the list so I get unreilable results.
Do you know any patterns to help agent pick the right data for the tool in general (there are a lot of cases like, call 2 tools combine results and call 3rd tool with some data coming from those)
It’s difficult to help without more context. If you could share a trace of your agent’s execution, that would be really helpful.
A few questions that might help us narrow down the issue:
Tool descriptions: Have you clearly described the entities and their IDs in the tool descriptions and arguments to avoid ambiguity?
System prompt: How strong are your guardrails for this behavior in the system prompt?
Model: Which model are you using? Have you tried a stronger model or increased the reasoning effort of your current model? If so, did you see better results?
Number of tools: How many tools is your agent using? In my experience, keeping the number of tools around 5–6 can be a good starting point.
Human-in-the-loop: I’d also suggest considering a human-in-the-loop approach, provided your users have enough domain knowledge to handle these cases. You can learn more here: Human-in-the-loop - Docs by LangChain
My strongest recommendation would be to start by trying a stronger model or increasing the reasoning effort of your current model before jumping on other things.
For the future reference, we solved our problem by adding small tool to the agent to extract the selected item for a list deterministically in python code.
The current flow looks like this
user → “show me list of entities”
agent → shows the list → calling tool from external MCP server
Hello @piotrmorek ,
I do not understand the need of second tool call, why not just one tool call which can save on token:
user → “show me list of entities”
agent → shows the list → calling tool from external MCP server
user → “select 2nd option”
agent calls select_final_id:
- You pass selected entity ID as an InjectedState. When user select 2nd Option you can update the graph state key entity_id to user selected ID and then pass in select_final_id
- In this same tool you get the JSON of the entity, and then call the MCP server tool directly through invoke after loading it through Langchain MCPAdapater.
This way you only have single tool call, determenstic entity matching and agent never have to guess the Entity or ID.
@piotrmorek, select_final_id was just a placeholder name for a local@tool. Same idea as your select_entity_from_list`, except it also calls the external MCP tool in Python so the model never has to copy the entity ID into a second tool call.
You don’t need to reimplement your MCP tools. Keep loading them with MCPAdapter and create_agent, add one wrapper, and don’t expose the downstream MCP tool to the agent if the model keeps passing the wrong ID, only expose “list” + “act on selection”.
Just sample code to make clear how flow is in my head:
import json
from typing import Annotated, Any
from langchain.agents import create_agent
from langchain.mcp import MCPAdapter
from langchain.tools import InjectedState, tool
from langchain_core.messages import ToolMessage
MCP_URL = "https://your-mcp-server/mcp"
LIST_ENTITIES = "list_entities" # external MCP tool name
ACT_ON_ENTITY = "act_on_entity" # external MCP tool name
def _last_list_payload(messages: list) -> list[dict]:
for msg in reversed(messages):
if isinstance(msg, ToolMessage) and msg.name == LIST_ENTITIES:
return json.loads(msg.content)
raise ValueError("No entity list found in tool history")
async def make_agent():
async with MCPAdapter(MCP_URL) as adapter:
by_name = {t.name: t for t in await adapter.list_tools()}
@tool
async def act_on_selected_entity(
index: int,
state: Annotated[dict[str, Any], InjectedState],
) -> str:
"""Run the action for the entity the user picked (1-based index, e.g. 2 = second option)."""
entity = _last_list_payload(state["messages"])[index - 1]
result = await by_name[ACT_ON_ENTITY].ainvoke({"entity_id": entity["id"]})
return str(result)
return create_agent(
"anthropic:claude-sonnet-5",
[by_name[LIST_ENTITIES], act_on_selected_entity], # omit ACT_ON_ENTITY
system_prompt=(
"List entities with the list tool. When the user picks an option, "
"call act_on_selected_entity with the 1-based index only."
),
)
Flow: user asks for the list → agent calls MCP list_entities → user says “2nd option” → agent calls act_on_selected_entity(index=2) → Python resolves the ID and invokes the MCP tool. Deterministic matching, one model tool call for the risky step.
InjectedState is optional; you can also pass index only and scan state["messages"] inside the tool, which is what the snippet does.
I’d avoid having the agent reproduce the ID from conversation context. Instead, keep the original entity list in application state and resolve “the 3rd one” to its exact entity and ID in your code.
For multi-step tool calls, pass structured outputs (with a schema) between steps and validate IDs before calling the next tool. This makes the workflow much more reliable than relying on the LLM to remember and copy identifiers correctly.
@Steve180 could you draft some example? “keep the original entity list in application state” I understand that right after using the tool to get list of entites - we save them in state.
Then user is saying “3rd one”, what should happen now? Agent should call another tool that takes an index (LLM would produce the index, it would be “3”) and takes the data from state? Why state and not the history messages?