Skip to content

Type generics for CallToolResult - Require 'structuredContent' if 'outputSchema' defined. - #859

Closed
KKonstantinov wants to merge 12 commits into
modelcontextprotocol:mainfrom
KKonstantinov:feature/tool-output-type-generics
Closed

KKonstantinov wants to merge 12 commits into
modelcontextprotocol:mainfrom
KKonstantinov:feature/tool-output-type-generics

Conversation

@KKonstantinov

Copy link
Copy Markdown
Contributor

The structuredContent tool output is currently not utilizing Typescript's capabilities and the check is performed only runtime.

If outputSchema is defined, as per the implementation, structuredContent is required in the response.

Motivation and Context

Mitigating runtime-only handling and proper type checking when outputSchema is defined.

How Has This Been Tested?

The changes are in types only - the results of the PR can be observed in tests, //@ts-expect-error has to be added now when testing for tools defined with outputSchema and not returning structuredContent.

Breaking Changes

No need, only if they had a runtime error anyways will they get a type error.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

@KKonstantinov
KKonstantinov requested review from a team and ihrpr August 10, 2025 07:10
@KKonstantinov

Copy link
Copy Markdown
Contributor Author

Please review, but this could be further improved by not allowing "structuredContent" at all when "outputSchema" is defined.

Currently, it'll only generate a typecheck error if "outputSchema" is defined but "structuredContent" is missing, but it will not error when "structuredContent" is returned and "outputSchema" wasn't defined in the first place. (e.g. if the user forgot by accident).

cc @ihrpr

@KKonstantinov KKonstantinov mentioned this pull request Aug 19, 2025
10 of 23 tasks

@cliffhall cliffhall left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion for changes to mcpServerOutputSchema.ts example.

Would be good to see explicit tests for CallToolResultUnstructuredSchema and CallToolResultStructuredSchema.

Comment thread src/examples/server/mcpServerOutputSchema.ts

@ochafik ochafik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @KKonstantinov !

Comment thread src/types.ts Outdated
export type CallToolResult = Infer<typeof CallToolResultSchema>;
export type CallToolResultUnstructured = Infer<typeof CallToolResultUnstructuredSchema>;
export type CallToolResultStructured<OArgs extends ZodRawShape> = Infer<typeof CallToolResultStructuredSchema> & {
structuredContent: z.infer<z.ZodObject<OArgs, 'strip'>>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what 'strip' adds here? (the actual runtime zod schema won't strip extra fields)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have removed it, frankly I don't recall why I had added it at the time of this PR, might have been related to the removal of passthrough from runtime but yes, just doing it at compile time doesn't help, removed.

Comment thread src/server/mcp.ts Outdated
extra: RequestHandlerExtra<ServerRequest, ServerNotification>,
) => CallToolResult | Promise<CallToolResult>
: (extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => CallToolResult | Promise<CallToolResult>;
) => OutputArgs extends ZodRawShape

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you extract the return type to avoid this repetition?

export type ToolCallback<InputArgs extends undefined | ZodRawShape = undefined, OutputArgs extends undefined | ZodRawShape = undefined> =
  InputArgs extends ZodRawShape
  ? (args: z.objectOutputType<InputArgs, ZodTypeAny>, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => CallToolResult<OutputArgs>
  : (extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => CallToolResult<OutputArgs>;

type CallToolResult<OutputArgs extends undefined | ZodRawShape = undefined> =
  OutputArgs extends ZodRawShape
    ? CallToolResultStructured<OutputArgs> | Promise<CallToolResultStructured<OutputArgs>>
    : OutputArgs extends undefined
    ? CallToolResultUnstructured | Promise<CallToolResultUnstructured>
    : never;

@KKonstantinov KKonstantinov Sep 29, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Have called it "CallToolResultByOutputArgsType" for a lack of a better name (open to suggestions..).

CallToolResult is already used and imported - that is the union type between structured and unstructured.

Unfortunately I don't think we can make CallToolResult generic in the core types.ts (although I very much wish we could) because it then won't match the non-generic type defined in https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-06-18/schema.ts

The reality is the CallToolResult in https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-06-18/schema.ts NOT being generic is a bit misleading, because it implies returning structuredContent even when there is no output schema defined.

Comment thread src/server/mcp.ts Outdated
@@ -1165,13 +1167,21 @@ export class ResourceTemplate {
* - `content` if the tool does not have an outputSchema

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could you update this comment? (cf. https://modelcontextprotocol.io/specification/2025-06-18/server/tools#structured-content)

- `content`: if an outputSchema is defined, content *SHOULD* have the serialized JSON structuredContent in a text content for backwards compatibility

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@KKonstantinov

Copy link
Copy Markdown
Contributor Author

Tks for the comments @ochafik , had missed these last week, now addressed.

@ochafik ochafik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @KKonstantinov ! Two last nits, looks great otherwise!

Comment thread src/server/mcp.ts Outdated
Comment thread src/types.ts Outdated
@KKonstantinov
KKonstantinov requested a review from a team as a code owner October 31, 2025 06:36
@KKonstantinov

Copy link
Copy Markdown
Contributor Author

Thanks @KKonstantinov ! Two last nits, looks great otherwise!

@ochafik sorry just coming back to this, comments are done!

@mattzcarey

Copy link
Copy Markdown
Member

Hey @KKonstantinov wanna get this merged this week?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants