|
| 1 | +# OutputSchema Support Implementation |
| 2 | + |
| 3 | +This document summarizes the changes made to support tools with `outputSchema` in the MCP TypeScript SDK. |
| 4 | + |
| 5 | +## Changes Made |
| 6 | + |
| 7 | +### Server-Side Changes |
| 8 | + |
| 9 | +#### 1. Tool Registration (mcp.ts) |
| 10 | + |
| 11 | +- Added support for parsing and storing `outputSchema` when registering tools |
| 12 | +- Updated the `tool()` method to handle outputSchema parameter in various overload combinations |
| 13 | +- Added new overloads to support tools with outputSchema: |
| 14 | + ```typescript |
| 15 | + tool<Args extends ZodRawShape>( |
| 16 | + name: string, |
| 17 | + paramsSchema: Args, |
| 18 | + outputSchema: Tool["outputSchema"], |
| 19 | + cb: ToolCallback<Args>, |
| 20 | + ): RegisteredTool; |
| 21 | + ``` |
| 22 | + |
| 23 | +#### 2. Tool Listing |
| 24 | + |
| 25 | +- Modified `ListToolsResult` handler to include outputSchema in tool definitions |
| 26 | +- Only includes outputSchema in the response if it's defined for the tool |
| 27 | + |
| 28 | +#### 3. Tool Execution |
| 29 | + |
| 30 | +- Updated `CallToolRequest` handler to validate structured content based on outputSchema |
| 31 | +- Added automatic backward compatibility: |
| 32 | + - If a tool has outputSchema and returns `structuredContent` but no `content`, the server automatically generates a text representation |
| 33 | + - This ensures compatibility with clients that don't support structured content |
| 34 | +- Added validation to ensure: |
| 35 | + - Tools with outputSchema must return structuredContent (unless error) |
| 36 | + - Tools without outputSchema must not return structuredContent |
| 37 | + - Tools without outputSchema must return content |
| 38 | + |
| 39 | +#### 4. Backward Compatibility |
| 40 | + |
| 41 | +The implementation maintains full backward compatibility: |
| 42 | +- Tools without outputSchema continue to work as before |
| 43 | +- Tools with outputSchema can optionally provide both `structuredContent` and `content` |
| 44 | +- If only `structuredContent` is provided, `content` is auto-generated as JSON |
| 45 | + |
| 46 | +### Client-Side Changes |
| 47 | + |
| 48 | +#### 1. Schema Caching and Validation (index.ts) |
| 49 | + |
| 50 | +- Added `_cachedTools` and `_cachedToolOutputSchemas` maps to cache tool definitions and their parsed Zod schemas |
| 51 | +- The client converts JSON Schema to Zod schema using the `json-schema-to-zod` library for runtime validation |
| 52 | +- Added dependency: `json-schema-to-zod` for converting JSON schemas to Zod schemas |
| 53 | + |
| 54 | +#### 2. Tool Listing |
| 55 | + |
| 56 | +- Modified `listTools` to parse and cache output schemas: |
| 57 | + - When a tool has an outputSchema, the client converts it to a Zod schema |
| 58 | + - Schemas are cached for validation during tool calls |
| 59 | + - Handles errors gracefully with warning logs if schema parsing fails |
| 60 | + |
| 61 | +#### 3. Tool Execution |
| 62 | + |
| 63 | +- Enhanced `callTool` method with comprehensive validation: |
| 64 | + - Tools with outputSchema must return `structuredContent` (validates this requirement) |
| 65 | + - Tools without outputSchema must not return `structuredContent` |
| 66 | + - Validates structured content against the cached Zod schema |
| 67 | + - Provides detailed error messages when validation fails |
| 68 | + |
| 69 | +#### 4. Error Handling |
| 70 | + |
| 71 | +The client throws `McpError` with appropriate error codes: |
| 72 | +- `ErrorCode.InvalidRequest` when required structured content is missing or unexpected |
| 73 | +- `ErrorCode.InvalidParams` when structured content doesn't match the schema |
| 74 | + |
| 75 | +### Testing |
| 76 | + |
| 77 | +#### Server Tests |
| 78 | + |
| 79 | +Added comprehensive test suite (`mcp-outputschema.test.ts`) covering: |
| 80 | +- Tool registration with outputSchema |
| 81 | +- ListToolsResult including outputSchema |
| 82 | +- Tool execution with structured content |
| 83 | +- Automatic backward compatibility behavior |
| 84 | +- Error cases and validation |
| 85 | + |
| 86 | +#### Client Tests |
| 87 | + |
| 88 | +Added tests in `index.test.ts` covering: |
| 89 | +- Validation of structured content against output schemas |
| 90 | +- Error handling when structured content doesn't match schema |
| 91 | +- Error handling when tools with outputSchema don't return structured content |
| 92 | +- Error handling when tools without outputSchema return structured content |
| 93 | +- Complex JSON schema validation including nested objects, arrays, and strict mode |
| 94 | +- Validation of additional properties when `additionalProperties: false` |
| 95 | + |
| 96 | +### Examples |
| 97 | + |
| 98 | +Created two example servers: |
| 99 | +1. `outputSchema.ts` - Using the low-level Server API |
| 100 | +2. `mcpServerOutputSchema.ts` - Using the high-level McpServer API |
| 101 | + |
| 102 | +These examples demonstrate: |
| 103 | +- Tools with structured output (weather data, CSV processing, BMI calculation) |
| 104 | +- Tools that return both structured and readable content |
| 105 | +- Traditional tools without outputSchema for comparison |
| 106 | + |
| 107 | +## API Usage |
| 108 | + |
| 109 | +### Registering a tool with outputSchema: |
| 110 | + |
| 111 | +```typescript |
| 112 | +server.tool( |
| 113 | + "calculate_bmi", |
| 114 | + "Calculate BMI given height and weight", |
| 115 | + { |
| 116 | + height_cm: z.number(), |
| 117 | + weight_kg: z.number() |
| 118 | + }, |
| 119 | + { |
| 120 | + type: "object", |
| 121 | + properties: { |
| 122 | + bmi: { type: "number" }, |
| 123 | + category: { type: "string" } |
| 124 | + }, |
| 125 | + required: ["bmi", "category"] |
| 126 | + }, |
| 127 | + async ({ height_cm, weight_kg }) => { |
| 128 | + // Calculate BMI... |
| 129 | + return { |
| 130 | + structuredContent: { |
| 131 | + bmi: calculatedBmi, |
| 132 | + category: bmiCategory |
| 133 | + } |
| 134 | + }; |
| 135 | + } |
| 136 | +); |
| 137 | +``` |
| 138 | + |
| 139 | +### Tool callback return values: |
| 140 | + |
| 141 | +- For tools with outputSchema: Return `{ structuredContent: {...} }` |
| 142 | +- For backward compatibility: Optionally include `{ structuredContent: {...}, content: [...] }` |
| 143 | +- For tools without outputSchema: Return `{ content: [...] }` as before |
| 144 | + |
| 145 | +## Implementation Summary |
| 146 | + |
| 147 | +### Key Design Decisions |
| 148 | + |
| 149 | +1. **Backward Compatibility**: The server automatically generates `content` from `structuredContent` for clients that don't support structured output |
| 150 | +2. **Schema Validation**: The client validates all structured content against the tool's output schema using Zod |
| 151 | +3. **Caching**: The client caches parsed schemas to avoid re-parsing on every tool call |
| 152 | +4. **Error Handling**: Both client and server validate the correct usage of `structuredContent` vs `content` based on whether a tool has an outputSchema |
| 153 | + |
| 154 | +### Implementation Notes |
| 155 | + |
| 156 | +1. **Server Side**: |
| 157 | + - Automatically handles backward compatibility by serializing structuredContent to JSON |
| 158 | + - Validates that tools properly use structuredContent vs content based on their outputSchema |
| 159 | + - All existing tools continue to work without changes |
| 160 | + |
| 161 | +2. **Client Side**: |
| 162 | + - Converts JSON Schema to Zod schemas for runtime validation |
| 163 | + - Caches schemas for performance |
| 164 | + - Provides detailed validation errors when structured content doesn't match schemas |
| 165 | + - Enforces proper usage of structuredContent based on outputSchema presence |
| 166 | + |
| 167 | +3. **Compatibility**: |
| 168 | + - The implementation follows the spec requirements |
| 169 | + - Maintains full backward compatibility |
| 170 | + - Provides a good developer experience with clear error messages |
| 171 | + - Ensures both old and new clients can work with servers that support outputSchema |
0 commit comments