r/mcp • u/Longjumping_Bad_879 • 23m ago
question Does MCP code mode introduce a trade-off between token efficiency, context retention, and error recovery?
One of the main arguments for code mode seems to be that intermediate tool calls happen within the execution environment rather than being individually exposed to the llm. This reduces the number of tool call interactions and avoids injecting potentially large intermediate responses into the model's context.
While this makes sense, I see a few potential problems, particularly in multi turn conversational workflows.
1. Loss of intermediate context across user queries
Consider an e-commerce order management system exposed through mcp tools.
A user asks:
Query 1: "Find all orders above $500 placed in the last 30 days."
To accomplish this, the LLM might need to orchestrate several MCP tools:
- Get available stores/accounts.
- Get available order resources.
- Get order fields and their metadata.
- Fetch and filter the relevant orders.
With traditional mcp tool calling, the intermediate responses from these calls are available in the llm's conversation context. Also, for the scope of this problem, please assume that the tools are paginated, allows searches and the results are pretty scoped and limited.
Now suppose the user follows up with:
Query 2: "Which customer has placed the most orders?"
The llms might already have the relevant account, order resource, and field metadata from the previous interaction. It could potentially reuse that information without repeating the discovery process.
However, with code mode, if only the final result from query 1 is returned to the llm, the intermediate metadata might no longer be available in its context. Consequently, the LLM might have to execute the initial discovery sequence all over again. Am I right in thinking that this can lead to redundant tool executions in multi-turn workflows?
2. What about retaining intermediate information?
One possible solution would be to guide the llm (through a skill that ships with the mcp server) to generate code such that it accumulates all intermediate tool responses in a variable and return them alongside the final result. However, this seems to defeat the original purpose of code mode.
If we return all intermediate information to the llm, aren't we effectively bringing back the same context/token consumption that code mode was supposed to eliminate?
Another possibility is to selectively return only important metadata. But here's the problem: the next user query is unpredictable**.** The llm cannot necessarily know which pieces of intermediate information will become relevant in the next turn.
For example, the next question could concern customer information, payment fields, shipping details, product information, or some other metadata retrieved during the initial orchestration. How do you handle this without either losing potentially useful context or returning large amounts of intermediate data?
3. Error handling and recovery
Imagine a sequence of four tool calls where the fourth call fails.
- How does the LLM get sufficiently detailed error information to identify and correct the problem?
- If the LLM modifies the code and retries, the first three successful tool calls have to execute again.
- The LLM has to always generate code such that any intermediate tool call errors are properly fallbacked with a proper error message with a possible response that helps the LLM to not execute the initial 3 tools again. All the error cases and null and other cases needs to be handled properly by the LLM for this.
Without proper checkpointing and error propagation, it seems that recovering from failures could introduce additional tool executions and latency.
Is this fundamentally a trade-off?
My current understanding is that Code Mode optimizes token consumption and tool orchestration, but potentially introduces trade-offs around retaining intermediate information across multiple user turns, avoiding redundant tool executions, debugging (error visibility), recovering from partial execution failures.
perhaps persistent execution state with on-demand retrieval could address some of these concerns, but that introduces additional architectural complexity.
I'd particularly like to hear from people who have implemented or used MCP Code Mode in real-world, multi-turn workflows.
Note: My examples might not be perfect but please consider examples of tasks where the intermediate tool call responses would help with certain follow up questions.
1
u/Okoear 14m ago
I don't see why the code in 2 couldn't log the results somewhere(at this point everything my agents do is logged and cached for later analysis) so you can reach in the results of needed without having an agent in mid turn.
I fail to see the issue, it's a tool in the box.