|
1 | 1 | # Tool Results |
| 2 | + |
| 3 | +👨💼 When users interact with our journal entries, they expect immediate feedback and reliable actions. If a user deletes an entry, they need to know it actually worked - not just hope for the best. The problem is: how do we get structured, validated data back from our MCP tools so we can build intelligent workflows that respond appropriately to different outcomes? |
| 4 | + |
| 5 | +```ts |
| 6 | +// Tool returns both human-readable and structured data |
| 7 | +const result = await sendMcpMessage( |
| 8 | + 'tool', |
| 9 | + { |
| 10 | + toolName: 'analyze_code', |
| 11 | + params: { filePath: 'src/utils/validation.ts' }, |
| 12 | + }, |
| 13 | + { schema: codeAnalysisSchema, signal: unmountSignal }, |
| 14 | +) |
| 15 | + |
| 16 | +// Client can access structured data and respond intelligently |
| 17 | +const { metrics, suggestions, complexity } = result.structuredContent |
| 18 | +if (complexity > 10) { |
| 19 | + showRefactorWarning(suggestions) |
| 20 | +} |
| 21 | +updateCodeMetrics(metrics) |
| 22 | +``` |
| 23 | + |
| 24 | +To make this happen, we use Zod schemas to validate and type the structured content returned by MCP tools. This enables rich integrations where the client can understand and act on tool results programmatically, rather than requiring manual user intervention for every step. |
| 25 | + |
| 26 | +The structured content is defined by the tool's `outputSchema` and validated using Zod schemas on the client side, ensuring type safety and reliable data flow between the MCP server and client applications. |
| 27 | + |
| 28 | +<callout-info> |
| 29 | + With structured content, MCP clients can build sophisticated workflows where |
| 30 | + tools can trigger automatic actions based on their results, creating a more |
| 31 | + intelligent and responsive user experience. |
| 32 | +</callout-info> |
| 33 | + |
| 34 | +```mermaid |
| 35 | +sequenceDiagram |
| 36 | + User->>Iframe: Click "Delete Entry" |
| 37 | + Iframe->>App: Send tool message via postMessage |
| 38 | + App->>MCP: Call delete_entry tool |
| 39 | + MCP->>MCP: Process deletion |
| 40 | + MCP->>App: Return structured result |
| 41 | + App->>Iframe: Send result via postMessage |
| 42 | + Iframe->>Iframe: Validate with Zod schema |
| 43 | + Iframe->>Iframe: Check success status |
| 44 | + Iframe->>User: Update UI based on result |
| 45 | +``` |
| 46 | + |
| 47 | +The goal is to make tool interactions feel intelligent and responsive, so users can trust that their actions have real consequences and get immediate, accurate feedback about what happened. |
| 48 | + |
| 49 | +Remember, our `delete_entry` tool returns structured content with a `success` boolean. You'll need to create a Zod schema to validate this data and use it in your `sendMcpMessage` call to get type-safe access to the result! |
| 50 | + |
| 51 | +👨💼 Thanks Kellie! |
| 52 | + |
| 53 | +Now, start by creating the schema, implement the tool call, and handle both success and failure cases to make your journal deletion feel reliable and responsive. |
0 commit comments