You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[v2] Should raise error at build time if zod outputSchema is not consistent with the structuredContent type #2754
I created a tool that outputs a structured content like:
server.registerTool(
'ping',
{
description: 'Returns a string pong with a timestamp',
outputSchema: z.object({data: z.string()}) // <---- Wrong return type, should be simply z.string()!
},
async () => {
const str = `Pong at ${new Date()}`
return {
content: [{ type: 'text', text: str }], // It seems to be added automatically by the library, but typescript complains if only structuredContent is present
structuredContent: str
};
}
);
Using only static analysis, we can directly see that this won't work, since outputSchema expects an object but structuredContent outputs a string… and indeed, at runtime this produces an error. Yet, I'd love this kind of error to be catched at build time, with a proper Typescript error.
What did you expect?
An error.
Code to reproduce
server.registerTool('ping',{description: 'Returns a string pong with a timestamp',outputSchema: z.object({data: z.string()})// <---- Wrong return type, should be simply z.string()!},async()=>{conststr=`Pong at ${newDate()}`return{content: [{type: 'text',text: str}],// It seems to be added automatically by the library, but typescript complains if only structuredContent is presentstructuredContent: str};});
I have a fix prepared for this: thread the output schema through registerTool callback typing so successful structuredContent must match its inferred output type. Error and input-required responses keep their current behavior, and runtime validation stays in place.
The change covers Standard Schema providers and the deprecated raw Zod shape overload, with compiler regressions and checks against the built ESM/CJS declarations. I will open a PR with the implementation and validation details.
I type-checked the snippet above against each PR's preview build. With @modelcontextprotocol/server@2.0.0 it compiles cleanly. With each of the three PRs it fails with Type 'string' is not assignable to type '{ data: string; }', and a tool that returns structuredContent: { data: 'ok' } still compiles.
AI disclosure: this comment was written with the help of Claude Code.
Activity
hngpt52 commentedon Sep 12, 2026
I have a fix prepared for this: thread the output schema through
registerToolcallback typing so successfulstructuredContentmust match its inferred output type. Error and input-required responses keep their current behavior, and runtime validation stays in place.The change covers Standard Schema providers and the deprecated raw Zod shape overload, with compiler regressions and checks against the built ESM/CJS declarations. I will open a PR with the implementation and validation details.
errmakov commentedon Sep 14, 2026
There are three open PRs for this, two of them from before this issue was filed:
registerToolcallback'sstructuredContentfromoutputSchema, types only.main.I type-checked the snippet above against each PR's preview build. With
@modelcontextprotocol/server@2.0.0it compiles cleanly. With each of the three PRs it fails withType 'string' is not assignable to type '{ data: string; }', and a tool that returnsstructuredContent: { data: 'ok' }still compiles.AI disclosure: this comment was written with the help of Claude Code.