Skip to content

Commit 19eb2e8

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 5441767 commit 19eb2e8

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
@@ -779,6 +779,22 @@ async def run():
779779

780780
# Call a tool
781781
result = await session.call_tool("tool-name", arguments={"arg1": "value"})
782+
# Parse the result (type: CallToolResult)
783+
for item in result.content:
784+
if isinstance(item, types.TextContent):
785+
# Extract text directly from TextContent
786+
print(f"Tool output (TextContent): {item.text}")
787+
elif isinstance(item, types.EmbeddedResource):
788+
# Check if the embedded resource contains text
789+
if isinstance(item.resource, types.TextResourceContents):
790+
print(f"Tool output (EmbeddedResource - Text): {item.resource.text}")
791+
elif isinstance(item.resource, types.BlobResourceContents):
792+
print(f"Tool output (EmbeddedResource - Blob): URI {item.resource.uri}, MIME Type {item.resource.mimeType}")
793+
elif isinstance(item, types.ImageContent):
794+
# Showing only a snippet of image data
795+
print(f"Tool output (ImageContent): MIME Type {item.mimeType}, Data (base64): {item.data[:30]}...")
796+
else:
797+
print(f"Tool output (Unknown Content Type): {type(item)}")
782798

783799

784800
if __name__ == "__main__":
@@ -807,6 +823,22 @@ async def main():
807823
await session.initialize()
808824
# Call a tool
809825
tool_result = await session.call_tool("echo", {"message": "hello"})
826+
# Parse the result (type: CallToolResult)
827+
for item in tool_result.content:
828+
if isinstance(item, types.TextContent):
829+
# Extract text directly from TextContent
830+
print(f"Tool output (TextContent): {item.text}")
831+
elif isinstance(item, types.EmbeddedResource):
832+
# Check if the embedded resource contains text
833+
if isinstance(item.resource, types.TextResourceContents):
834+
print(f"Tool output (EmbeddedResource - Text): {item.resource.text}")
835+
elif isinstance(item.resource, types.BlobResourceContents):
836+
print(f"Tool output (EmbeddedResource - Blob): URI {item.resource.uri}, MIME Type {item.resource.mimeType}")
837+
elif isinstance(item, types.ImageContent):
838+
# Showing only a snippet of image data
839+
print(f"Tool output (ImageContent): MIME Type {item.mimeType}, Data (base64): {item.data[:30]}...")
840+
else:
841+
print(f"Tool output (Unknown Content Type): {type(item)}")
810842
```
811843

812844
### OAuth Authentication for Clients

0 commit comments

Comments
 (0)