Skip to content

Add type safety for structuredContent in ToolCallback with outputSchema #669

Open
@sushichan044

Description

@sushichan044

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:

  1. No type completion for structuredContent in the callback
  2. Potential runtime errors due to mismatched structure
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions