Skip to content

Commit 3458f0b

Browse files
authored
Preserve Tool _meta when marshaling to JSON (#609)
* [tool-meta] Add _meta to tool response output Needed for openai ui integration Issue: none Test plan: 🤞 * [tool-meta] add a ton of tests I guess * [tool-meta] simpler test * [tool-meta] undo * [tool-meta] feedback
1 parent 8891432 commit 3458f0b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

mcp/tools.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,11 @@ func (t Tool) MarshalJSON() ([]byte, error) {
613613

614614
m["annotations"] = t.Annotations
615615

616+
// Marshal Meta if present
617+
if t.Meta != nil {
618+
m["_meta"] = t.Meta
619+
}
620+
616621
return json.Marshal(m)
617622
}
618623

mcp/tools_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,3 +1396,54 @@ func TestNewItemsAPICompatibility(t *testing.T) {
13961396
})
13971397
}
13981398
}
1399+
1400+
// TestToolMetaMarshaling tests that the Meta field is properly marshaled as _meta in JSON output
1401+
func TestToolMetaMarshaling(t *testing.T) {
1402+
meta := map[string]any{"version": "1.0.0", "author": "test"}
1403+
// Marshal the tool to JSON
1404+
data, err := json.Marshal(Tool{
1405+
Name: "test-tool",
1406+
Description: "A test tool with meta data",
1407+
Meta: NewMetaFromMap(meta),
1408+
InputSchema: ToolInputSchema{
1409+
Type: "object",
1410+
Properties: map[string]any{
1411+
"input": map[string]any{
1412+
"type": "string",
1413+
"description": "Test input",
1414+
},
1415+
},
1416+
},
1417+
})
1418+
assert.NoError(t, err)
1419+
1420+
// Unmarshal to map for comparison
1421+
var result map[string]any
1422+
err = json.Unmarshal(data, &result)
1423+
assert.NoError(t, err)
1424+
1425+
// Check if _meta field is present and correct
1426+
assert.Contains(t, result, "_meta", "Tool with Meta should include _meta field")
1427+
assert.Equal(t, meta, result["_meta"], "_meta field should match expected value")
1428+
}
1429+
1430+
func TestToolMetaMarshalingOmitsWhenNil(t *testing.T) {
1431+
// Marshal a tool without Meta
1432+
data, err := json.Marshal(Tool{
1433+
Name: "test-tool-no-meta",
1434+
Description: "A test tool without meta data",
1435+
InputSchema: ToolInputSchema{
1436+
Type: "object",
1437+
Properties: map[string]any{},
1438+
},
1439+
})
1440+
assert.NoError(t, err)
1441+
1442+
// Unmarshal to map
1443+
var result map[string]any
1444+
err = json.Unmarshal(data, &result)
1445+
assert.NoError(t, err)
1446+
1447+
// Check that _meta field is not present
1448+
assert.NotContains(t, result, "_meta", "Tool without Meta should not include _meta field")
1449+
}

0 commit comments

Comments
 (0)