Bug in the official tutorial docs: Tool use example code lacks agentic loop, causing multi-turn calls to fail
Maintainer thường phản hồi trong vòng 5 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 76/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- python
- Lĩnh vực
- documentation
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.)
- Ngôn ngữ chính
- TypeScript
- Star
- 1.2k
- Fork
- 688
- Merge trung bình
- 2 ngày 18 giờ
- Pull request đã merge (30 ngày)
- 21
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của modelcontextprotocol/quickstart-resources
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
modelcontextprotocol/quickstart-resources#199 ·
Maintainer thường phản hồi trong vòng 5 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
modelcontextprotocol/quickstart-resources#191 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 5 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
modelcontextprotocol/quickstart-resources#190 ·
Maintainer thường phản hồi trong vòng 5 ngày
-
enhancement
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
modelcontextprotocol/quickstart-resources#46 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 5 ngày
-
Support for recurring tool callsĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
modelcontextprotocol/quickstart-resources#29 ·
Maintainer thường phản hồi trong vòng 5 ngày
Tất cả issue của modelcontextprotocol/quickstart-resources
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
supadata-ai/mcp#27 ·
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
capricorn86/happy-dom#2485 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
优化导入 OCR 模型选择文件的按钮样式Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
siyuan-note/siyuan#20430 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Albert-Weasker/niubigeo#194 ·
Maintainer thường phản hồi trong vòng 1 ngày