fix(mcp): add empty properties to object schemas for OpenAI compatibility (#321)

MCP tools that accept no parameters return {type: "object"} without a
properties field, which is valid per JSON Schema spec. However, OpenAI
API strictly requires "properties" on object schemas and rejects
requests with HTTP 400: "object schema missing properties".

Add default empty properties map when type is "object" and the MCP
server provides none.

Co-authored-by: Luvu182 <208665161+Luvu182@users.noreply.github.com>
This commit is contained in:
Luan VuandLuvu182 authored and GitHub committed 2026-03-21 07:35:24 +07:00
1 parent 7200ecb7fe
commit 19d34aacec
2 files changed
+16

No files matched your search

+3
View File
@@ -125,6 +125,9 @@ func inputSchemaToMap(schema mcpgo.ToolInputSchema) map[string]any {
}
if len(schema.Properties) > 0 {
m["properties"] = schema.Properties
} else if m["type"] == "object" {
// OpenAI requires "properties" even when empty for object schemas.
m["properties"] = map[string]any{}
}
if len(schema.Required) > 0 {
m["required"] = schema.Required
+13
View File
@@ -47,6 +47,19 @@ func TestInputSchemaToMap_EmptyType(t *testing.T) {
}
}
func TestInputSchemaToMap_ObjectNoProperties(t *testing.T) {
schema := mcpgo.ToolInputSchema{Type: "object"}
m := inputSchemaToMap(schema)
props, ok := m["properties"].(map[string]any)
if !ok || props == nil {
t.Fatal("expected empty properties map for object schema, got nil — OpenAI rejects object schemas without properties")
}
if len(props) != 0 {
t.Errorf("expected empty properties, got %v", props)
}
}
func TestExtractTextContent(t *testing.T) {
result := &mcpgo.CallToolResult{
Content: []mcpgo.Content{