|
44 | 44 | - [Advanced Usage](#advanced-usage)
|
45 | 45 | - [Low-Level Server](#low-level-server)
|
46 | 46 | - [Writing MCP Clients](#writing-mcp-clients)
|
| 47 | + - [Parsing Tool Results](#parsing-tool-results) |
47 | 48 | - [MCP Primitives](#mcp-primitives)
|
48 | 49 | - [Server Capabilities](#server-capabilities)
|
49 | 50 | - [Documentation](#documentation)
|
@@ -1605,6 +1606,75 @@ async def main():
|
1605 | 1606 |
|
1606 | 1607 | For a complete working example, see [`examples/clients/simple-auth-client/`](examples/clients/simple-auth-client/).
|
1607 | 1608 |
|
| 1609 | +### Parsing Tool Results |
| 1610 | + |
| 1611 | +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. |
| 1612 | + |
| 1613 | +```python |
| 1614 | +"""examples/snippets/clients/parsing_tool_results.py""" |
| 1615 | + |
| 1616 | +import asyncio |
| 1617 | + |
| 1618 | +from mcp import ClientSession, StdioServerParameters, types |
| 1619 | +from mcp.client.stdio import stdio_client |
| 1620 | + |
| 1621 | + |
| 1622 | +async def parse_tool_results(): |
| 1623 | + """Demonstrates how to parse different types of content in CallToolResult.""" |
| 1624 | + server_params = StdioServerParameters( |
| 1625 | + command="python", args=["path/to/mcp_server.py"] |
| 1626 | + ) |
| 1627 | + |
| 1628 | + async with stdio_client(server_params) as (read, write): |
| 1629 | + async with ClientSession(read, write) as session: |
| 1630 | + await session.initialize() |
| 1631 | + |
| 1632 | + # Example 1: Parsing text content |
| 1633 | + result = await session.call_tool("get_data", {"format": "text"}) |
| 1634 | + for content in result.content: |
| 1635 | + if isinstance(content, types.TextContent): |
| 1636 | + print(f"Text: {content.text}") |
| 1637 | + |
| 1638 | + # Example 2: Parsing structured content from JSON tools |
| 1639 | + result = await session.call_tool("get_user", {"id": "123"}) |
| 1640 | + if hasattr(result, "structuredContent") and result.structuredContent: |
| 1641 | + # Access structured data directly |
| 1642 | + user_data = result.structuredContent |
| 1643 | + print(f"User: {user_data.get('name')}, Age: {user_data.get('age')}") |
| 1644 | + |
| 1645 | + # Example 3: Parsing embedded resources |
| 1646 | + result = await session.call_tool("read_config", {}) |
| 1647 | + for content in result.content: |
| 1648 | + if isinstance(content, types.EmbeddedResource): |
| 1649 | + resource = content.resource |
| 1650 | + if isinstance(resource, types.TextResourceContents): |
| 1651 | + print(f"Config from {resource.uri}: {resource.text}") |
| 1652 | + elif isinstance(resource, types.BlobResourceContents): |
| 1653 | + print(f"Binary data from {resource.uri}") |
| 1654 | + |
| 1655 | + # Example 4: Parsing image content |
| 1656 | + result = await session.call_tool("generate_chart", {"data": [1, 2, 3]}) |
| 1657 | + for content in result.content: |
| 1658 | + if isinstance(content, types.ImageContent): |
| 1659 | + print(f"Image ({content.mimeType}): {len(content.data)} bytes") |
| 1660 | + |
| 1661 | + # Example 5: Handling errors |
| 1662 | + result = await session.call_tool("failing_tool", {}) |
| 1663 | + if result.isError: |
| 1664 | + print("Tool execution failed!") |
| 1665 | + for content in result.content: |
| 1666 | + if isinstance(content, types.TextContent): |
| 1667 | + print(f"Error: {content.text}") |
| 1668 | + |
| 1669 | + |
| 1670 | +async def main(): |
| 1671 | + await parse_tool_results() |
| 1672 | + |
| 1673 | + |
| 1674 | +if __name__ == "__main__": |
| 1675 | + asyncio.run(main()) |
| 1676 | +``` |
| 1677 | + |
1608 | 1678 | ### MCP Primitives
|
1609 | 1679 |
|
1610 | 1680 | The MCP protocol defines three core primitives that servers can implement:
|
|
0 commit comments