Description
Is your feature request related to a problem? Please describe.
Currently, when using outputSchema
in tool registration, the ToolCallback
doesn't receive type information about the expected output structure. This leads to:
- No type completion for
structuredContent
in the callback - Potential runtime errors due to mismatched structure
- Lower developer experience when building structured outputs
Describe the solution you'd like
Enhance ToolCallback
to be generic over both input and output schema types:
// Current
ToolCallback<Args extends undefined | ZodRawShape>
// Proposed
ToolCallback<InputArgs extends undefined | ZodRawShape, OutputArgs extends undefined | ZodRawShape>
This would enable:
- Type-safe
structuredContent
construction - IDE autocompletion for output fields
- Compile-time validation of output structure
Example:
server.registerTool(
"weather",
{
inputSchema: { city: z.string() },
outputSchema: {
temperature: z.number(),
conditions: z.string()
}
},
async ({ city }) => {
return {
structuredContent: {
// ← Type completion and check would work here
temperature: 25,
conditions: "sunny"
}
};
}
);
Describe alternatives you've considered
Some might argue that the current runtime validation is sufficient.
However, I believe that if we are constraining the data structure for runtime, we should also be able to benefit from that check at compile-time.
Additional context
Considering this issue: #588
It might be more appropriate to use a Type Constraint other than ZodRawShape
.
However, I think it's best to proceed with an implementation based on ZodRawShape
as-is for now.