Skip to content

fix(docs): handle multiple tool calls in process_query#2921

Open
abdouloued wants to merge 1 commit into
modelcontextprotocol:mainfrom
abdouloued:fix/client-quickstart-multiple-tool-calls
Open

fix(docs): handle multiple tool calls in process_query#2921
abdouloued wants to merge 1 commit into
modelcontextprotocol:mainfrom
abdouloued:fix/client-quickstart-multiple-tool-calls

Conversation

@abdouloued

Copy link
Copy Markdown

Problem

The process_query function in the Python client quickstart (docs/docs/develop/build-client.mdx) has two bugs when the model returns multiple tool calls in a single response:

  1. Corrupted conversation history — the for loop appends assistant_message_content to messages and fires a new API call inside the loop, once per tool call. On the second iteration, assistant_message_content has grown and the history is malformed.

  2. Crash on non-text first block — after the loop, the code accesses response.content[0].text unconditionally. If the first content block is a tool use block, this raises AttributeError.

Both bugs are silent when only one tool call is returned per turn but surface as soon as the model decides to batch tool calls.

Fix

Replace the for loop with a while response.stop_reason == "tool_use" loop that:

  • Appends the full assistant turn (response.content) to history once per turn
  • Collects all tool results for that turn before making the next API call
  • Falls through to collect the final text response after all tool rounds complete

This matches the agentic loop pattern described in the Anthropic tool use docs.

The previous implementation fired a new API call inside a for loop for
each tool use block, appending the assistant message to history before
all tool calls in that turn were collected. This corrupted conversation
history when the model returned multiple tool calls in one response, and
crashed on the final content[0].text access if the first block was not
a text block.

Replace with a while loop on stop_reason == "tool_use" that collects
all tool results for a given turn before making the next API call,
matching the agentic loop pattern in the API documentation.
@abdouloued abdouloued requested review from a team as code owners June 15, 2026 19:21
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant