Skip to content

Docs: Update CallToolResult parsing in README #812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,22 @@ async def run():

# Call a tool
result = await session.call_tool("tool-name", arguments={"arg1": "value"})
# Parse the result (type: CallToolResult)
for item in result.content:
if isinstance(item, types.TextContent):
# Extract text directly from TextContent
print(f"Tool output (TextContent): {item.text}")
elif isinstance(item, types.EmbeddedResource):
# Check if the embedded resource contains text
if isinstance(item.resource, types.TextResourceContents):
print(f"Tool output (EmbeddedResource - Text): {item.resource.text}")
elif isinstance(item.resource, types.BlobResourceContents):
print(f"Tool output (EmbeddedResource - Blob): URI {item.resource.uri}, MIME Type {item.resource.mimeType}")
elif isinstance(item, types.ImageContent):
# Showing only a snippet of image data
print(f"Tool output (ImageContent): MIME Type {item.mimeType}, Data (base64): {item.data[:30]}...")
else:
print(f"Tool output (Unknown Content Type): {type(item)}")


if __name__ == "__main__":
Expand All @@ -791,7 +807,7 @@ Clients can also connect using [Streamable HTTP transport](https://modelcontextp

```python
from mcp.client.streamable_http import streamablehttp_client
from mcp import ClientSession
from mcp import ClientSession, types


async def main():
Expand All @@ -807,6 +823,22 @@ async def main():
await session.initialize()
# Call a tool
tool_result = await session.call_tool("echo", {"message": "hello"})
# Parse the result (type: CallToolResult)
for item in tool_result.content:
if isinstance(item, types.TextContent):
# Extract text directly from TextContent
print(f"Tool output (TextContent): {item.text}")
elif isinstance(item, types.EmbeddedResource):
# Check if the embedded resource contains text
if isinstance(item.resource, types.TextResourceContents):
print(f"Tool output (EmbeddedResource - Text): {item.resource.text}")
elif isinstance(item.resource, types.BlobResourceContents):
print(f"Tool output (EmbeddedResource - Blob): URI {item.resource.uri}, MIME Type {item.resource.mimeType}")
elif isinstance(item, types.ImageContent):
# Showing only a snippet of image data
print(f"Tool output (ImageContent): MIME Type {item.mimeType}, Data (base64): {item.data[:30]}...")
else:
print(f"Tool output (Unknown Content Type): {type(item)}")
```

### OAuth Authentication for Clients
Expand Down
Loading