Skip to content

fix(mcp-chat): loop tool calls until a final answer#9472

Open
Ernstsen wants to merge 1 commit into
backstage:mainfrom
Ernstsen:fix-mcp-chat-multi-round-tool-loop
Open

fix(mcp-chat): loop tool calls until a final answer#9472
Ernstsen wants to merge 1 commit into
backstage:mainfrom
Ernstsen:fix-mcp-chat-multi-round-tool-loop

Conversation

@Ernstsen

@Ernstsen Ernstsen commented Jun 15, 2026

Copy link
Copy Markdown

Enable multi-round tool calls for MCP-Chat plugin

Issue:

When an llm-request in the MCP-Chat required trailing tool-calls the response would look like this:

<|tool_call>call:catalog.query-catalog-entities{query:<|"|>Platform Engineering<|"|>}<tool_call|>

Cause:

processQuery ran a single round of tool calls, then made a follow-up LLM request WITHOUT tools. When a question needed more than one tool call, the model attempted another call in that tool-less follow-up; OpenAI-compatible servers don't parse tool calls when tools is absent, so the model's raw tool-call tokens leaked into the response content (and the follow-up content discarded the real answer).

Replace the single round with a capped agentic loop (max 8 iterations) that always passes the tools on every sendMessage call and executes returned tool calls until the model responds with a final text answer, with a graceful fallback if the cap is reached.

The per-call execute/ record logic is extracted into a private helper, for readability.

Adds tests covering the multi-round loop and the iteration cap.

✔️ Checklist

  • A changeset describing the change and affected packages. (more info)
  • Added or updated documentation
  • Tests for new functionality and regression tests for bug fixes
  • Screenshots attached (for UI changes)
  • All your commits have a Signed-off-by line in the message. (more info)

@Ernstsen Ernstsen requested a review from Lucifergene as a code owner June 15, 2026 05:56
Copilot AI review requested due to automatic review settings June 15, 2026 05:56
@Ernstsen Ernstsen requested a review from awanlin June 15, 2026 05:56
@backstage-goalie

Copy link
Copy Markdown
Contributor

Changed Packages

Package Name Package Path Changeset Bump Current Version
@backstage-community/plugin-mcp-chat-backend workspaces/mcp-chat/plugins/mcp-chat-backend patch v0.11.0

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR fixes multi-round tool-calling behavior in processQuery by iterating through tool calls until the model returns a final answer (or a max-iteration cap is reached), preventing later rounds from losing access to tool definitions.

Changes:

  • Reworked processQuery to loop across multiple LLM/tool-execution rounds with a max-iteration safeguard and a fallback reply.
  • Extracted tool execution + conversation-history updates into a helper (executeAndRecordToolCall).
  • Added tests validating tools are passed on every round and that max-iteration fallback triggers.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
workspaces/mcp-chat/plugins/mcp-chat-backend/src/services/MCPClientServiceImpl.ts Implements multi-round tool loop, iteration cap, and refactors tool execution/message recording.
workspaces/mcp-chat/plugins/mcp-chat-backend/src/services/MCPClientServiceImpl.test.ts Adds regression tests for multi-round tool passing and max-iteration fallback behavior.
workspaces/mcp-chat/.changeset/fix-multi-round-tool-loop.md Adds a patch changeset describing the multi-step tool-call fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread workspaces/mcp-chat/plugins/mcp-chat-backend/src/services/MCPClientServiceImpl.ts Outdated
processQuery ran a single round of tool calls, then made a follow-up
LLM request WITHOUT tools. When a question needed more than one tool
call, the model attempted another call in that tool-less follow-up;
OpenAI-compatible servers don't parse tool calls when `tools` is
absent, so the model's raw tool-call tokens leaked into the response
content (and the follow-up content discarded the real answer).

Replace the single round with a capped agentic loop (max 8 iterations)
that always passes the tools on every sendMessage call and executes
returned tool calls until the model responds with a final text answer,
with a graceful fallback if the cap is reached. The per-call execute/
record logic is extracted into a private helper.

Adds tests covering the multi-round loop and the iteration cap.

Signed-off-by: Johannes Sjølander Ernstsen <jsje@ramboll.com>
@Ernstsen Ernstsen force-pushed the fix-mcp-chat-multi-round-tool-loop branch from e035364 to 208ef52 Compare June 15, 2026 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants