Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mcp/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ func (t Tool) MarshalJSON() ([]byte, error) {
return nil, fmt.Errorf("tool %s has both OutputSchema and RawOutputSchema set: %w", t.Name, errToolSchemaConflict)
}
m["outputSchema"] = t.RawOutputSchema
} else {
} else if t.OutputSchema.Type != "" { // If no output schema is specified, do not return anything
m["outputSchema"] = t.OutputSchema
}

Expand Down
60 changes: 42 additions & 18 deletions mcp/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,27 +586,51 @@ func TestToolWithOutputSchema(t *testing.T) {
Email string `json:"email,omitempty" jsonschema_description:"Email address"`
}

tool := NewTool("test_tool",
WithDescription("Test tool with output schema"),
WithOutputSchema[TestOutput](),
WithString("input", Required()),
)

// Check that RawOutputSchema was set
assert.NotNil(t, tool.OutputSchema)
tests := []struct {
name string
tool Tool
expectedOutputSchema bool
}{
{
name: "default behavior",
tool: NewTool("test_tool",
WithDescription("Test tool with output schema"),
WithOutputSchema[TestOutput](),
WithString("input", Required()),
),
expectedOutputSchema: true,
},
{
name: "no output schema is set",
tool: NewTool("test_tool",
WithDescription("Test tool with no output schema"),
WithString("input", Required()),
),
expectedOutputSchema: false,
},
}

// Marshal and verify structure
data, err := json.Marshal(tool)
assert.NoError(t, err)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Marshal and verify structure
data, err := json.Marshal(tt.tool)
assert.NoError(t, err)

var toolData map[string]any
err = json.Unmarshal(data, &toolData)
assert.NoError(t, err)
var toolData map[string]any
err = json.Unmarshal(data, &toolData)
assert.NoError(t, err)

// Verify outputSchema exists
outputSchema, exists := toolData["outputSchema"]
assert.True(t, exists)
assert.NotNil(t, outputSchema)
// Verify outputSchema exists
outputSchema, exists := toolData["outputSchema"]
if tt.expectedOutputSchema {
assert.True(t, exists)
assert.NotNil(t, outputSchema)
} else {
assert.False(t, exists)
assert.Nil(t, outputSchema)
}
})
}
}

// TestNewToolResultStructured tests that the NewToolResultStructured function
Expand Down