[GH-ISSUE #4223] [BUG]: Calling MCP servers multiple times (infinite loop) #2683

Closed
opened 2026-02-22 18:30:46 -05:00 by yindo · 6 comments
Owner

Originally created by @bechir2000 on GitHub (Jul 31, 2025).
Original GitHub issue: https://github.com/Mintplex-Labs/anything-llm/issues/4223

How are you running AnythingLLM?

All versions

What happened?

Hello, I am using Anthropic Claude 3.5 Sonnet with AWS Bedrock. I have a problem when calling my MCP server. Sometimes, it will work normally in one loop. Sometimes, it takes 7 or 8 loops (maybe infinite), despite MCP server answering with correct result.
Even with the basic MCP weather, it could get the answer in one loop, or sometimes more.

Image Image

Are there known steps to reproduce?

No response

Originally created by @bechir2000 on GitHub (Jul 31, 2025). Original GitHub issue: https://github.com/Mintplex-Labs/anything-llm/issues/4223 ### How are you running AnythingLLM? All versions ### What happened? Hello, I am using Anthropic Claude 3.5 Sonnet with AWS Bedrock. I have a problem when calling my MCP server. Sometimes, it will work normally in one loop. Sometimes, it takes 7 or 8 loops (maybe infinite), despite MCP server answering with correct result. Even with the basic MCP weather, it could get the answer in one loop, or sometimes more. <img width="801" height="691" alt="Image" src="https://github.com/user-attachments/assets/226e41b0-925b-418d-9876-212ddb66ed37" /> <img width="866" height="691" alt="Image" src="https://github.com/user-attachments/assets/b1645dd2-b971-4d9a-8fcd-aec02b1b7703" /> ### Are there known steps to reproduce? _No response_
yindo added the possible bug label 2026-02-22 18:30:46 -05:00
yindo closed this issue 2026-02-22 18:30:46 -05:00
Author
Owner

@timothycarambat commented on GitHub (Jul 31, 2025):

Which model? Some models just a not great at exiting tool calls and will keep calling for no reason. It is like a worse version of a hallucination.

@timothycarambat commented on GitHub (Jul 31, 2025): Which model? Some models just a not great at exiting tool calls and will _keep_ calling for no reason. It is like a worse version of a hallucination.
Author
Owner

@bechir2000 commented on GitHub (Jul 31, 2025):

I am using Anthropic Claude 4 Sonnet with aws Bedrock. The idea of the mcp server is that it returns code snippets of an internal React library.
Could it that be the problem? The format of the response?

@bechir2000 commented on GitHub (Jul 31, 2025): I am using Anthropic Claude 4 Sonnet with aws Bedrock. The idea of the mcp server is that it returns code snippets of an internal React library. Could it that be the problem? The format of the response?
Author
Owner

@timothycarambat commented on GitHub (Jul 31, 2025):

That should be returning just raw text, which I assume is the intended output of the MCP in general. Obviously Sonnet 4 is a huge powerful model. That being said, I will say we have seem suprisingly bad tool calls (MCP or otherwise) with Sonnet 4.

Is the looping behavior consistent? I know it sounds weird but im willing to bet this does not happen with 3.5...

@timothycarambat commented on GitHub (Jul 31, 2025): That should be returning just raw text, which I assume is the intended output of the MCP in general. Obviously Sonnet 4 is a huge powerful model. That being said, I will say we have seem _suprisingly bad_ tool calls (MCP or otherwise) with Sonnet 4. Is the looping behavior consistent? I know it sounds weird but im willing to bet this does not happen with 3.5...
Author
Owner

@bechir2000 commented on GitHub (Jul 31, 2025):

Well, in fact it happens with 3.5 and 4. It is very strange. And it is always happening. Sometimes, 2 loops, sometimes 5.
I even looked at the logs, everything seems normal. But what I didn't understand, why anythingllm keeps calling the tool multiple times, even though it receives answers from the first loop?

@bechir2000 commented on GitHub (Jul 31, 2025): Well, in fact it happens with 3.5 and 4. It is very strange. And it is always happening. Sometimes, 2 loops, sometimes 5. I even looked at the logs, everything seems normal. But what I didn't understand, why anythingllm keeps calling the tool multiple times, even though it receives answers from the first loop?
Author
Owner

@bechir2000 commented on GitHub (Aug 7, 2025):

If anyone is wondering, I fixed the problem. The agent handler of Anythingllm will stop calling the mcp server if it receives similar call to the function with same parameters. So, I followed the code, and the idea is to go to server/utils/agents/aibitat/providers/helpers/untooled.js and add a counter there.
If the counter exceeds 2 (for example), we will return return { toolCall: null, text: null };
Full code now :
`const call = safeJsonParse(response, null);
if (call === null) return { toolCall: null, text: response }; // failed to parse, so must be text.

const { valid, reason } = this.validFuncCall(call, functions);
if (!valid) {
  this.providerLog(`Invalid function tool call: ${reason}.`);
  return { toolCall: null, text: null };
}
//new code
if (!this.callCounts[call.name]) {
  this.callCounts[call.name] = 0;
}
this.callCounts[call.name]++;

// NEW: Stop after 2 calls to any function
if (this.callCounts[call.name] > 2) {
  this.providerLog(
    `Function ${call.name} has been called ${this.callCounts[call.name]} times. Stopping to prevent excessive usage.`
  );
  return { toolCall: null, text: null };
}
// end of new code
if (this.deduplicator.isDuplicate(call.name, call.arguments)) {
  this.providerLog(
    `Function tool with exact arguments has already been called this stack.`
  );
  return { toolCall: null, text: null };
}

return { toolCall: call, text: null };`
@bechir2000 commented on GitHub (Aug 7, 2025): If anyone is wondering, I fixed the problem. The agent handler of Anythingllm will stop calling the mcp server if it receives similar call to the function with same parameters. So, I followed the code, and the idea is to go to server/utils/agents/aibitat/providers/helpers/untooled.js and add a counter there. If the counter exceeds 2 (for example), we will return `return { toolCall: null, text: null };` Full code now : `const call = safeJsonParse(response, null); if (call === null) return { toolCall: null, text: response }; // failed to parse, so must be text. const { valid, reason } = this.validFuncCall(call, functions); if (!valid) { this.providerLog(`Invalid function tool call: ${reason}.`); return { toolCall: null, text: null }; } //new code if (!this.callCounts[call.name]) { this.callCounts[call.name] = 0; } this.callCounts[call.name]++; // NEW: Stop after 2 calls to any function if (this.callCounts[call.name] > 2) { this.providerLog( `Function ${call.name} has been called ${this.callCounts[call.name]} times. Stopping to prevent excessive usage.` ); return { toolCall: null, text: null }; } // end of new code if (this.deduplicator.isDuplicate(call.name, call.arguments)) { this.providerLog( `Function tool with exact arguments has already been called this stack.` ); return { toolCall: null, text: null }; } return { toolCall: call, text: null };`
Author
Owner

@fungiboletus commented on GitHub (Oct 12, 2025):

I think the bug is still there. I experienced it on version 1.9.0 with various models, because my MCP tool takes free text as input. It just loops forever.

Could it be possible to re-open this issue @timothycarambat ? Thanks.

@fungiboletus commented on GitHub (Oct 12, 2025): I think the bug is still there. I experienced it on version 1.9.0 with various models, because my MCP tool takes free text as input. It just loops forever. Could it be possible to re-open this issue @timothycarambat ? Thanks.
yindo changed title from [BUG]: Calling MCP servers multiple times (infinite loop) to [GH-ISSUE #4223] [BUG]: Calling MCP servers multiple times (infinite loop) 2026-06-05 14:47:57 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mintplex-Labs/anything-llm#2683