From 19eb2e8907e97dfa83a951f94e507eed19998c4f Mon Sep 17 00:00:00 2001 From: Dagang Wei Date: Mon, 26 May 2025 22:09:47 -0700 Subject: [PATCH 1/2] 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`. --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index d76d3d267..37871e429 100644 --- a/README.md +++ b/README.md @@ -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__": @@ -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 From e896ce2bc056151c2e03ea3fc60d19c6c9d6688d Mon Sep 17 00:00:00 2001 From: Dagang Wei Date: Mon, 26 May 2025 22:13:10 -0700 Subject: [PATCH 2/2] Docs: add import for mcp.type --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 37871e429..01b1db928 100644 --- a/README.md +++ b/README.md @@ -807,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():