Skip to content

Commit df35989

Browse files
functiconsfelixweinberger
authored andcommitted
Docs: Update CallToolResult parsing in README
The example in README.md for parsing the result of `session.call_tool` has been updated to reflect the structure of `CallToolResult`. This change aligns the README example with the type definitions in `mcp/types.py`.
1 parent c0bc666 commit df35989

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

README.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,11 +1410,22 @@ async def run():
14101410

14111411
# Call a tool (add tool from fastmcp_quickstart)
14121412
result = await session.call_tool("add", arguments={"a": 5, "b": 3})
1413-
result_unstructured = result.content[0]
1414-
if isinstance(result_unstructured, types.TextContent):
1415-
print(f"Tool result: {result_unstructured.text}")
1416-
result_structured = result.structuredContent
1417-
print(f"Structured tool result: {result_structured}")
1413+
# Parse the result (type: CallToolResult)
1414+
for item in result.content:
1415+
if isinstance(item, types.TextContent):
1416+
# Extract text directly from TextContent
1417+
print(f"Tool output (TextContent): {item.text}")
1418+
elif isinstance(item, types.EmbeddedResource):
1419+
# Check if the embedded resource contains text
1420+
if isinstance(item.resource, types.TextResourceContents):
1421+
print(f"Tool output (EmbeddedResource - Text): {item.resource.text}")
1422+
elif isinstance(item.resource, types.BlobResourceContents):
1423+
print(f"Tool output (EmbeddedResource - Blob): URI {item.resource.uri}, MIME Type {item.resource.mimeType}")
1424+
elif isinstance(item, types.ImageContent):
1425+
# Showing only a snippet of image data
1426+
print(f"Tool output (ImageContent): MIME Type {item.mimeType}, Data (base64): {item.data[:30]}...")
1427+
else:
1428+
print(f"Tool output (Unknown Content Type): {type(item)}")
14181429

14191430

14201431
def main():
@@ -1459,6 +1470,25 @@ async def main():
14591470
tools = await session.list_tools()
14601471
print(f"Available tools: {[tool.name for tool in tools.tools]}")
14611472

1473+
# Call a tool
1474+
tool_result = await session.call_tool("echo", {"message": "hello"})
1475+
# Parse the result (type: CallToolResult)
1476+
for item in tool_result.content:
1477+
if isinstance(item, types.TextContent):
1478+
# Extract text directly from TextContent
1479+
print(f"Tool output (TextContent): {item.text}")
1480+
elif isinstance(item, types.EmbeddedResource):
1481+
# Check if the embedded resource contains text
1482+
if isinstance(item.resource, types.TextResourceContents):
1483+
print(f"Tool output (EmbeddedResource - Text): {item.resource.text}")
1484+
elif isinstance(item.resource, types.BlobResourceContents):
1485+
print(f"Tool output (EmbeddedResource - Blob): URI {item.resource.uri}, MIME Type {item.resource.mimeType}")
1486+
elif isinstance(item, types.ImageContent):
1487+
# Showing only a snippet of image data
1488+
print(f"Tool output (ImageContent): MIME Type {item.mimeType}, Data (base64): {item.data[:30]}...")
1489+
else:
1490+
print(f"Tool output (Unknown Content Type): {type(item)}")
1491+
14621492

14631493
if __name__ == "__main__":
14641494
asyncio.run(main())

0 commit comments

Comments
 (0)