Skip to content

Validate tool output fails for time.Time and other types that get serialized to strings in JSON #447

@iwyrkore

Description

@iwyrkore

When the output type for a tool is a struct containing a field of type time.Time, the schema properly reports the type as string but the validation fails:

timestamp: type: 2021-01-01 00:00:00 +0000 UTC has type "object", want "string"

This is because the JSON schema validation is being done on the struct and not the marshalled JSON.

To Reproduce
Steps to reproduce the behavior:

  1. Create a tool that returns a struct with a time.Time as structured content:
type MyResult struct {
	Timestamp time.Time `json:"timestamp"`
}

func MyTestToolHandler(
	ctx context.Context,
	req *mcp.CallToolRequest,
	args any) (*mcp.CallToolResult, *MyResult, error) {
	result := MyResult{Timestamp: time.Now()}
	return &mcp.CallToolResult{
		Content: []mcp.Content{
			&mcp.TextContent{Text: "Testing"},
		},
	}, &result, nil
}

var MyTestTool = mcp.Tool{
	Name:        "MyTestTool",
	Description: "Testing tool",
}

mcp.AddTool(mcpServer, &MyTestTool, MyTestToolHandler)
  1. Call the tool MyTestTool and get:
data: {"jsonrpc":"2.0","id":2,"error":{"code":0,"message":"tool output: validating\n\t0x14000106770\nagainst\n\t {\"type\":\"object\",\"required\":[\"timestamp\"],\"properties\":{\"timestamp\":{\"type\":\"string\"}},\"additionalProperties\":false}:\n validating root: validating /properties/timestamp: type: 2025-09-10 16:41:03.191738 -0400 EDT m=+7.869338376 has type \"object\", want \"string\""}}

Expected behavior

Expect to validate against the actual JSON produced and pass and get the following output:

data: {"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"Testing"}],"structuredContent":{"timestamp":"2025-09-10T16:49:16.712372-04:00"}}}

Logs

"error":"tool output: validating\n\t0x14000106770\nagainst\n\t {\"type\":\"object\",\"required\":[\"timestamp\"],\"properties\":{\"timestamp\":{\"type\":\"string\"}},\"additionalProperties\":false}:\n validating root: validating /properties/timestamp: type: 2025-09-10 16:41:03.191738 -0400 EDT m=+7.869338376 has type \"object\", want \"string\""

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions