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
10 changes: 6 additions & 4 deletions v2/pkg/engine/jsonschema/variables_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,12 @@ func (v *VariablesSchemaBuilder) EnterVariableDefinition(ref int) {

// GetSchema returns the built schema
func (v *VariablesSchemaBuilder) GetSchema() *JsonSchema {
// If we have required fields, the root schema cannot be nullable
if len(v.schema.Required) > 0 {
v.schema.Nullable = false
}
// The root variables object is always a concrete object and must never be
// nullable: the variables container is either present or omitted, never the
// JSON literal null. Emitting a nullable root (type ["object","null"] under
// JSON Schema 2020-12) breaks strict consumers such as the MCP SDK, which
// require the input schema's type to be exactly "object".
v.schema.Nullable = false
// Attach definitions for any recursive input types referenced via "$ref"
if len(v.defs) > 0 {
v.schema.Defs = v.defs
Expand Down
32 changes: 10 additions & 22 deletions v2/pkg/engine/jsonschema/variables_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ func TestBuildJsonSchema(t *testing.T) {
"type": "object"
}
},
"type": [
"object",
"null"
]
"type": "object"
}`

// Compare actual JSON with expected JSON
Expand Down Expand Up @@ -1362,17 +1359,14 @@ func TestBuildJsonSchema(t *testing.T) {
"type": "object"
}
},
"type": [
"object",
"null"
]
"type": "object"
}`

// Compare actual JSON with expected JSON
assert.JSONEq(t, expectedJSON, string(data), "JSON schema does not match expected structure")
})

t.Run("root schema nullable based on required arguments", func(t *testing.T) {
t.Run("root schema is always a non-nullable object", func(t *testing.T) {
// Define schema with required and optional arguments
schemaSDL := scalarDefinitions + `
schema {
Expand Down Expand Up @@ -1452,7 +1446,10 @@ func TestBuildJsonSchema(t *testing.T) {
data2, err := json.MarshalIndent(schema2, "", " ")
require.NoError(t, err)

// Define expected JSON schema for optional argument case
// Define expected JSON schema for optional argument case.
// Even when every variable is optional, the root variables object stays a
// non-nullable "object": the container is omitted or present, never the
// JSON literal null. Only the individual optional fields are nullable.
expectedJSON2 := `{
"additionalProperties": false,
"properties": {
Expand All @@ -1463,10 +1460,7 @@ func TestBuildJsonSchema(t *testing.T) {
]
}
},
"type": [
"object",
"null"
]
"type": "object"
}`

// Compare actual JSON with expected JSON
Expand Down Expand Up @@ -1553,10 +1547,7 @@ func TestBuildJsonSchema(t *testing.T) {
"type": "object"
}
},
"type": [
"object",
"null"
]
"type": "object"
}`

// Compare actual JSON with expected JSON
Expand Down Expand Up @@ -1624,10 +1615,7 @@ func TestBuildJsonSchema(t *testing.T) {
"description": "ISO-8601 date time format"
}
},
"type": [
"object",
"null"
]
"type": "object"
}`

// Compare actual JSON with expected JSON
Expand Down
Loading