Bug in the official tutorial docs: Tool use example code lacks agentic loop, causing multi-turn calls to fail
维护者通常 5 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 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
- 派生
- 692
- 平均合并
- 2 天 18 小时
- 30 天内合并 PR
- 21
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
modelcontextprotocol/quickstart-resources 的其他 Issue
-
难度 5/5 一周以上 新手友好度 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 小时 新手友好度 78/100
db-ux-design-system/core-web#8461 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
Cannot run an integration on windows when power shell is selected as the default profile terminal未关闭Priority/Blocker Type/Bug
难度 2/5 1-3 小时 新手友好度 76/100
wso2/product-integrator#2767 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 85/100
-
severity:low
难度 1/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复