Skip to content

Commit d6b8732

Browse files
committed
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 70014a2 commit d6b8732

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,22 @@ async def run():
767767

768768
# Call a tool
769769
result = await session.call_tool("tool-name", arguments={"arg1": "value"})
770+
# Parse the result (type: CallToolResult)
771+
for item in result.content:
772+
if isinstance(item, types.TextContent):
773+
# Extract text directly from TextContent
774+
print(f"Tool output (TextContent): {item.text}")
775+
elif isinstance(item, types.EmbeddedResource):
776+
# Check if the embedded resource contains text
777+
if isinstance(item.resource, types.TextResourceContents):
778+
print(f"Tool output (EmbeddedResource - Text): {item.resource.text}")
779+
elif isinstance(item.resource, types.BlobResourceContents):
780+
print(f"Tool output (EmbeddedResource - Blob): URI {item.resource.uri}, MIME Type {item.resource.mimeType}")
781+
elif isinstance(item, types.ImageContent):
782+
# Showing only a snippet of image data
783+
print(f"Tool output (ImageContent): MIME Type {item.mimeType}, Data (base64): {item.data[:30]}...")
784+
else:
785+
print(f"Tool output (Unknown Content Type): {type(item)}")
770786

771787

772788
if __name__ == "__main__":
@@ -795,6 +811,22 @@ async def main():
795811
await session.initialize()
796812
# Call a tool
797813
tool_result = await session.call_tool("echo", {"message": "hello"})
814+
# Parse the result (type: CallToolResult)
815+
for item in tool_result.content:
816+
if isinstance(item, types.TextContent):
817+
# Extract text directly from TextContent
818+
print(f"Tool output (TextContent): {item.text}")
819+
elif isinstance(item, types.EmbeddedResource):
820+
# Check if the embedded resource contains text
821+
if isinstance(item.resource, types.TextResourceContents):
822+
print(f"Tool output (EmbeddedResource - Text): {item.resource.text}")
823+
elif isinstance(item.resource, types.BlobResourceContents):
824+
print(f"Tool output (EmbeddedResource - Blob): URI {item.resource.uri}, MIME Type {item.resource.mimeType}")
825+
elif isinstance(item, types.ImageContent):
826+
# Showing only a snippet of image data
827+
print(f"Tool output (ImageContent): MIME Type {item.mimeType}, Data (base64): {item.data[:30]}...")
828+
else:
829+
print(f"Tool output (Unknown Content Type): {type(item)}")
798830
```
799831

800832
### OAuth Authentication for Clients

0 commit comments

Comments
 (0)