-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Open
Labels
Description
Summary
Following the specification issue modelcontextprotocol/modelcontextprotocol#1411, the TypeScript SDK needs to ensure consistent handling of structuredContent
vs content
fields in tool responses.
Problem
Currently there's inconsistency across MCP implementations:
- Some clients use
structuredContent
when available (e.g., Cursor) - Others ignore it entirely
- This leads to different behavior with the same servers
Current SDK Behavior
The TypeScript SDK has validation implemented:
typescript-sdk/src/client/index.ts
Lines 442 to 475 in 3dd074f
// Check if the tool has an outputSchema | |
const validator = this.getToolOutputValidator(params.name); | |
if (validator) { | |
// If tool has outputSchema, it MUST return structuredContent (unless it's an error) | |
if (!result.structuredContent && !result.isError) { | |
throw new McpError( | |
ErrorCode.InvalidRequest, | |
`Tool ${params.name} has an output schema but did not return structured content` | |
); | |
} | |
// Only validate structured content if present (not when there's an error) | |
if (result.structuredContent) { | |
try { | |
// Validate the structured content (which is already an object) against the schema | |
const isValid = validator(result.structuredContent); | |
if (!isValid) { | |
throw new McpError( | |
ErrorCode.InvalidParams, | |
`Structured content does not match the tool's output schema: ${this._ajv.errorsText(validator.errors)}` | |
); | |
} | |
} catch (error) { | |
if (error instanceof McpError) { | |
throw error; | |
} | |
throw new McpError( | |
ErrorCode.InvalidParams, | |
`Failed to validate structured content: ${error instanceof Error ? error.message : String(error)}` | |
); | |
} | |
} | |
} |
However, the handling needs to align with the specification clarification.
Required Changes
Once modelcontextprotocol/modelcontextprotocol#1411 is resolved, update the SDK to:
- Implement the specified client behavior for
structuredContent
- Ensure proper fallback to
content
when needed - Validate that servers populate both fields consistently
- Add tests to verify the correct behavior
References
- Specification issue: Clarify client handling of structuredContent vs content fields modelcontextprotocol#1411
- Discord discussion: https://discord.com/channels/1358869848138059966/1411057080902746293
cc: @dsp-ant
tuomastik-artific