Bug in the official tutorial docs: Tool use example code lacks agentic loop, causing multi-turn calls to fail
メンテナーはふだん 5 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 76/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- python
調査の方向性
Start with “The Query Processing Logic” in the MCP building tutorial at the linked build-client page, focusing on the async process_query function and its message handling. Confirm that repeated tool_use responses are processed with their tool results until a plain-text response is returned, including tool execution errors; the issue’s corrected snippet defines the expected behavior.
索引モデルが issue の本文から書いたものです。
説明
The original example code (The Query Processing Logic in the MCP building tutorial site https://modelcontextprotocol.io/docs/2026-07-28/develop/build-client) has the following core issues:
- Single-turn Limitation: After the initial API call, the code only checks and processes tool_use blocks once. If Claude requests additional tool calls in its second response (e.g., needing to chain a second tool based on the output of the first), the original code fails to handle them—it simply ignores subsequent tool calls, causing complex multi-step tasks to fail.
- Lack of Agentic Loop Structure: It does not use a loop to continuously maintain conversation state. A correct implementation should, after each model response, check whether tool_use blocks are present; if so, execute the tools, append the results to the message history, and continue with the next API call—repeating this cycle until the model outputs a plain-text final answer.
The following corrected snippet resolves these issues by introducing a while True loop, correctly implementing a multi-turn tool-calling agent loop. Below is the corrected code version:
async def process_query(client, query: str) -> str:
messages = [
{"role": "user",
"content": query}
]
tool_list = await client.list_tools()
available_tools = [{
"name": tool.name,
"description": tool.description,
"input_schema": tool.input_schema,
} for tool in tool_list.tools]
final_texts = []
while True:
response = await anthropic.messages.create(
model=MODEL,
max_tokens=1000,
messages=messages,
tools=available_tools,
)
# Append the full assistant response back to the message history for context
messages.append({
"role": "assistant",
"content": response.content,
})
tool_uses = []
for block in response.content:
if block.type == "text":
final_texts.append(block.text)
elif block.type == "tool_use":
tool_uses.append(block)
# If no tool_use is requested, this is the final answer; exit the loop
if not tool_uses:
break
tool_results = []
for block in tool_uses:
try:
result = await client.call_tool(block.name, block.input)
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": format_tool_result(result), # Assumes format_tool_result is defined elsewhere
"is_error": bool(result.is_error),
})
except Exception as e:
# Tool execution errors must also be returned as tool_result to the model
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": f"Tool execution error: {e}",
"is_error": True,
})
# Feed the tool results back as a user message to continue the next turn
messages.append({
"role": "user",
"content": tool_results,
})
return "\n".join(final_texts)
(Note: format_tool_result is assumed to be a helper function that extracts text from the tool response, similar to "\n".join(block.text for block in result.content if isinstance(block, TextContent)) in the original code.)
- 主要言語
- TypeScript
- スター
- 1.2k
- フォーク
- 688
- 平均マージ
- 2日 18時間
- マージ済み PR(30日)
- 21
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
modelcontextprotocol/quickstart-resources のほかの issue
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
modelcontextprotocol/quickstart-resources#199 ·
メンテナーはふだん 5 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
modelcontextprotocol/quickstart-resources#191 · コメント 1 件 ·
メンテナーはふだん 5 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
modelcontextprotocol/quickstart-resources#190 ·
メンテナーはふだん 5 日以内に返信
-
enhancement
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
modelcontextprotocol/quickstart-resources#46 · コメント 1 件 ·
メンテナーはふだん 5 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
modelcontextprotocol/quickstart-resources#29 ·
メンテナーはふだん 5 日以内に返信
modelcontextprotocol/quickstart-resources の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
cockpit-project/cockpit-machines#2835 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
cloudflare/kumo#866 ·
メンテナーはふだん 1 日以内に返信
-
area:connector bug
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
environment: OPENCODE_API_KEY is described as a Console service account key, but a Go subscription key works対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
tester-army/e2e#1015 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信