|
41 | 41 | - [Advanced Usage](#advanced-usage) |
42 | 42 | - [Low-Level Server](#low-level-server) |
43 | 43 | - [Writing MCP Clients](#writing-mcp-clients) |
| 44 | + - [Parsing Tool Results](#parsing-tool-results) |
44 | 45 | - [MCP Primitives](#mcp-primitives) |
45 | 46 | - [Server Capabilities](#server-capabilities) |
46 | 47 | - [Documentation](#documentation) |
@@ -787,12 +788,18 @@ async def run(): |
787 | 788 | elif isinstance(item, types.EmbeddedResource): |
788 | 789 | # Check if the embedded resource contains text |
789 | 790 | if isinstance(item.resource, types.TextResourceContents): |
790 | | - print(f"Tool output (EmbeddedResource - Text): {item.resource.text}") |
| 791 | + print( |
| 792 | + f"Tool output (EmbeddedResource - Text): {item.resource.text}" |
| 793 | + ) |
791 | 794 | elif isinstance(item.resource, types.BlobResourceContents): |
792 | | - print(f"Tool output (EmbeddedResource - Blob): URI {item.resource.uri}, MIME Type {item.resource.mimeType}") |
| 795 | + print( |
| 796 | + f"Tool output (EmbeddedResource - Blob): URI {item.resource.uri}, MIME Type {item.resource.mimeType}" |
| 797 | + ) |
793 | 798 | elif isinstance(item, types.ImageContent): |
794 | 799 | # Showing only a snippet of image data |
795 | | - print(f"Tool output (ImageContent): MIME Type {item.mimeType}, Data (base64): {item.data[:30]}...") |
| 800 | + print( |
| 801 | + f"Tool output (ImageContent): MIME Type {item.mimeType}, Data (base64): {item.data[:30]}..." |
| 802 | + ) |
796 | 803 | else: |
797 | 804 | print(f"Tool output (Unknown Content Type): {type(item)}") |
798 | 805 |
|
@@ -831,12 +838,18 @@ async def main(): |
831 | 838 | elif isinstance(item, types.EmbeddedResource): |
832 | 839 | # Check if the embedded resource contains text |
833 | 840 | if isinstance(item.resource, types.TextResourceContents): |
834 | | - print(f"Tool output (EmbeddedResource - Text): {item.resource.text}") |
| 841 | + print( |
| 842 | + f"Tool output (EmbeddedResource - Text): {item.resource.text}" |
| 843 | + ) |
835 | 844 | elif isinstance(item.resource, types.BlobResourceContents): |
836 | | - print(f"Tool output (EmbeddedResource - Blob): URI {item.resource.uri}, MIME Type {item.resource.mimeType}") |
| 845 | + print( |
| 846 | + f"Tool output (EmbeddedResource - Blob): URI {item.resource.uri}, MIME Type {item.resource.mimeType}" |
| 847 | + ) |
837 | 848 | elif isinstance(item, types.ImageContent): |
838 | 849 | # Showing only a snippet of image data |
839 | | - print(f"Tool output (ImageContent): MIME Type {item.mimeType}, Data (base64): {item.data[:30]}...") |
| 850 | + print( |
| 851 | + f"Tool output (ImageContent): MIME Type {item.mimeType}, Data (base64): {item.data[:30]}..." |
| 852 | + ) |
840 | 853 | else: |
841 | 854 | print(f"Tool output (Unknown Content Type): {type(item)}") |
842 | 855 | ``` |
@@ -894,6 +907,74 @@ async def main(): |
894 | 907 |
|
895 | 908 | For a complete working example, see [`examples/clients/simple-auth-client/`](examples/clients/simple-auth-client/). |
896 | 909 |
|
| 910 | +### Parsing Tool Results |
| 911 | + |
| 912 | +When calling tools through MCP, the `CallToolResult` object contains the tool's response in a structured format. Understanding how to parse this result is essential for properly handling tool outputs. |
| 913 | + |
| 914 | +```python |
| 915 | +"""examples/snippets/clients/parsing_tool_results.py""" |
| 916 | + |
| 917 | +import asyncio |
| 918 | + |
| 919 | +from mcp import ClientSession, StdioServerParameters, types |
| 920 | +from mcp.client.stdio import stdio_client |
| 921 | + |
| 922 | + |
| 923 | +async def parse_tool_results(): |
| 924 | + """Demonstrates how to parse different types of content in CallToolResult.""" |
| 925 | + server_params = StdioServerParameters( |
| 926 | + command="python", args=["path/to/mcp_server.py"] |
| 927 | + ) |
| 928 | + |
| 929 | + async with stdio_client(server_params) as (read, write): |
| 930 | + async with ClientSession(read, write) as session: |
| 931 | + await session.initialize() |
| 932 | + |
| 933 | + # Example 1: Parsing text content |
| 934 | + result = await session.call_tool("get_data", {"format": "text"}) |
| 935 | + for content in result.content: |
| 936 | + if isinstance(content, types.TextContent): |
| 937 | + print(f"Text: {content.text}") |
| 938 | + |
| 939 | + # Example 2: Parsing structured content from JSON tools |
| 940 | + result = await session.call_tool("get_user", {"id": "123"}) |
| 941 | + if hasattr(result, "structuredContent") and result.structuredContent: |
| 942 | + # Access structured data directly |
| 943 | + user_data = result.structuredContent |
| 944 | + print(f"User: {user_data.get('name')}, Age: {user_data.get('age')}") |
| 945 | + |
| 946 | + # Example 3: Parsing embedded resources |
| 947 | + result = await session.call_tool("read_config", {}) |
| 948 | + for content in result.content: |
| 949 | + if isinstance(content, types.EmbeddedResource): |
| 950 | + resource = content.resource |
| 951 | + if isinstance(resource, types.TextResourceContents): |
| 952 | + print(f"Config from {resource.uri}: {resource.text}") |
| 953 | + elif isinstance(resource, types.BlobResourceContents): |
| 954 | + print(f"Binary data from {resource.uri}") |
| 955 | + |
| 956 | + # Example 4: Parsing image content |
| 957 | + result = await session.call_tool("generate_chart", {"data": [1, 2, 3]}) |
| 958 | + for content in result.content: |
| 959 | + if isinstance(content, types.ImageContent): |
| 960 | + print(f"Image ({content.mimeType}): {len(content.data)} bytes") |
| 961 | + |
| 962 | + # Example 5: Handling errors |
| 963 | + result = await session.call_tool("failing_tool", {}) |
| 964 | + if result.isError: |
| 965 | + print("Tool execution failed!") |
| 966 | + for content in result.content: |
| 967 | + if isinstance(content, types.TextContent): |
| 968 | + print(f"Error: {content.text}") |
| 969 | + |
| 970 | + |
| 971 | +async def main(): |
| 972 | + await parse_tool_results() |
| 973 | + |
| 974 | + |
| 975 | +if __name__ == "__main__": |
| 976 | + asyncio.run(main()) |
| 977 | +``` |
897 | 978 |
|
898 | 979 | ### MCP Primitives |
899 | 980 |
|
|
0 commit comments