Skip to content

Commit 1ee3f06

Browse files
committed
fix: Properly parse tool call results from latest schema for MCP Python SDK
1 parent a27489c commit 1ee3f06

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

e2e_tests/python/server_clients/server.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,31 @@ async def execute_tool(
9595
result = await self.session.call_tool(tool_name, arguments)
9696
logging.info(f"Finished executing {tool_name}")
9797

98+
if hasattr(result, "isError") and result.isError:
99+
raise Exception(f"Error executing tool: {result}")
100+
101+
if hasattr(result, "structuredContent") and result.structuredContent:
102+
return {
103+
"toolUseId": tool_use_id,
104+
"content": [{"text": str(result.structuredContent)}],
105+
"status": "success",
106+
}
107+
108+
content = []
109+
if hasattr(result, "content"):
110+
for block in result.content:
111+
if hasattr(block, "type") and block.type == "text":
112+
content.append({"text": block.text})
113+
else:
114+
raise Exception(
115+
f"Unexpected content type: {getattr(block, 'type', 'unknown')}"
116+
)
117+
else:
118+
raise Exception(f"No content found in result: {str(result)}")
119+
98120
return {
99121
"toolUseId": tool_use_id,
100-
"content": [{"text": str(result)}],
122+
"content": content,
101123
"status": "success",
102124
}
103125

e2e_tests/python/server_clients/servers.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ async def execute_tool(
5656
arguments=arguments,
5757
)
5858

59-
if isinstance(result, dict) and "progress" in result:
60-
progress = result["progress"]
61-
total = result["total"]
62-
percentage = (progress / total) * 100
63-
logging.info(f"Progress: {progress}/{total} " f"({percentage:.1f}%)")
64-
raise "Does not support progress notifications from tools yet"
65-
6659
return {"toolResult": result}
6760
except ValueError:
6861
return {

examples/chatbots/python/server_clients/server.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,31 @@ async def execute_tool(
9595
result = await self.session.call_tool(tool_name, arguments)
9696
logging.info(f"Finished executing {tool_name}")
9797

98-
if isinstance(result, dict) and "progress" in result:
99-
progress = result["progress"]
100-
total = result["total"]
101-
percentage = (progress / total) * 100
102-
logging.info(
103-
f"Progress: {progress}/{total} " f"({percentage:.1f}%)"
104-
)
105-
raise "Does not support progress notifications from tools yet"
98+
if hasattr(result, "isError") and result.isError:
99+
raise Exception(f"Error executing tool: {result}")
100+
101+
if hasattr(result, "structuredContent") and result.structuredContent:
102+
return {
103+
"toolUseId": tool_use_id,
104+
"content": [{"text": str(result.structuredContent)}],
105+
"status": "success",
106+
}
107+
108+
content = []
109+
if hasattr(result, "content"):
110+
for block in result.content:
111+
if hasattr(block, "type") and block.type == "text":
112+
content.append({"text": block.text})
113+
else:
114+
raise Exception(
115+
f"Unexpected content type: {getattr(block, 'type', 'unknown')}"
116+
)
117+
else:
118+
raise Exception(f"No content found in result: {str(result)}")
106119

107120
return {
108121
"toolUseId": tool_use_id,
109-
"content": [{"text": str(result)}],
122+
"content": content,
110123
"status": "success",
111124
}
112125

0 commit comments

Comments
 (0)