Skip to content

Implement consistent structuredContent handling per specification clarification #911

@dsp-ant

Description

@dsp-ant

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:

// 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:

  1. Implement the specified client behavior for structuredContent
  2. Ensure proper fallback to content when needed
  3. Validate that servers populate both fields consistently
  4. Add tests to verify the correct behavior

References

cc: @dsp-ant

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions