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
5 changes: 5 additions & 0 deletions mcp/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,11 @@ func (t Tool) MarshalJSON() ([]byte, error) {

m["annotations"] = t.Annotations

// Marshal Meta if present
if t.Meta != nil {
m["_meta"] = t.Meta
}

return json.Marshal(m)
}

Expand Down
51 changes: 51 additions & 0 deletions mcp/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1396,3 +1396,54 @@ func TestNewItemsAPICompatibility(t *testing.T) {
})
}
}

// TestToolMetaMarshaling tests that the Meta field is properly marshaled as _meta in JSON output
func TestToolMetaMarshaling(t *testing.T) {
meta := map[string]any{"version": "1.0.0", "author": "test"}
// Marshal the tool to JSON
data, err := json.Marshal(Tool{
Name: "test-tool",
Description: "A test tool with meta data",
Meta: NewMetaFromMap(meta),
InputSchema: ToolInputSchema{
Type: "object",
Properties: map[string]any{
"input": map[string]any{
"type": "string",
"description": "Test input",
},
},
},
})
assert.NoError(t, err)

// Unmarshal to map for comparison
var result map[string]any
err = json.Unmarshal(data, &result)
assert.NoError(t, err)

// Check if _meta field is present and correct
assert.Contains(t, result, "_meta", "Tool with Meta should include _meta field")
assert.Equal(t, meta, result["_meta"], "_meta field should match expected value")
}

func TestToolMetaMarshalingOmitsWhenNil(t *testing.T) {
// Marshal a tool without Meta
data, err := json.Marshal(Tool{
Name: "test-tool-no-meta",
Description: "A test tool without meta data",
InputSchema: ToolInputSchema{
Type: "object",
Properties: map[string]any{},
},
})
assert.NoError(t, err)

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

// Check that _meta field is not present
assert.NotContains(t, result, "_meta", "Tool without Meta should not include _meta field")
}
Loading