Skip to content

Using field types with custom JSON format breaks schema inference #153

@johanbrandhorst

Description

@johanbrandhorst

Describe the bug
When using mcp.ToolHandlerFor and a field with a custom JSON format (implementing json.Marshaler, for example), the inferred schema will not match the resulting JSON output.

To Reproduce
Steps to reproduce the behavior:

  1. Define a struct with a custom JSON format, such as time.Time:
    type MyToolParams struct {
    	MyTimestamp time.Time `json:"my_timestamp" jsonschema:"the timestamp for the tool call"`
    }
  2. Use the struct in a tool call:
    func GetTime(ctx context.Context, cc *mcp.ServerSession, params *mcp.CallToolParamsFor[MyToolParams]) (*mcp.CallToolResultFor[MyToolParams], error) {
    	return &mcp.CallToolResultFor[MyToolParams]{
    		StructuredContent: params,
    	}
    }

When using the created endpoint, the schema will claim that my_timestamp is an object, but of course the response will have my_timestamp as a string.

Expected behavior
I'm not entirely sure what the solution is here, but this behavior was unexpected to me (since inference is mostly "just working").

Additional context
I think it's possible to work around this by specifying a custom InputSchema and OutputSchema when registering the tool. I'm mostly creating this so that we have a place to discuss this somewhat pointy edge that will likely hit many new users.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingneeds investigationstatus unclear, requires more work and discussion

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions