{"record":{"id":"a6406e3797e95c6a","repo":"siyuan-note/siyuan","slug":"invalid-output-schema-w","errorCode":null,"errorMessage":"invalid output schema: %w","messagePattern":"invalid output schema: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/mcp/tools/validation.go","lineNumber":59,"sourceCode":"\tinput           *jsonschema.Resolved\n\toutput          *jsonschema.Resolved\n\tvalidationSlots chan struct{}\n}\n\nfunc CompileToolValidator(tool *Tool) (*ToolValidator, error) {\n\tif tool == nil {\n\t\treturn nil, fmt.Errorf(\"tool is nil\")\n\t}\n\n\tinput, err := resolveToolSchema(tool.InputSchema, true)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"invalid input schema: %w\", err)\n\t}\n\n\tvar output *jsonschema.Resolved\n\tif tool.OutputSchema != nil {\n\t\tif output, err = resolveToolSchema(*tool.OutputSchema, false); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"invalid output schema: %w\", err)\n\t\t}\n\t}\n\treturn &ToolValidator{\n\t\tinput:           input,\n\t\toutput:          output,\n\t\tvalidationSlots: make(chan struct{}, toolValidationConcurrency),\n\t}, nil\n}\n\nfunc resolveToolSchema(schema ToolSchema, requireObject bool) (*jsonschema.Resolved, error) {\n\tif schema.Raw != nil {\n\t\tif err := validateJSONComplexity(schema.Raw, maxToolSchemaDepth, maxToolSchemaNodes); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\tdata, err := json.Marshal(schema)\n\tif err != nil {\n\t\treturn nil, err","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/mcp/tools/validation.go#L41-L77","documentation":"CompileToolValidator optionally compiles a tool's OutputSchema into a resolved validator used by ValidateOutputContext. If the output schema fails resolveToolSchema — invalid JSON Schema, unresolvable references, or complexity limits — registration fails wrapped as 'invalid output schema'. Unlike input schemas, the root type is not required to be \"object\" (requireObject=false).","triggerScenarios":"Registering a tool with tool.OutputSchema != nil whose schema fails to resolve: broken $ref, invalid JSON Schema keywords, JSON marshal/unmarshal error, serialized size over 1 MiB, depth over 64, or more than 16384 nodes.","commonSituations":"Tools declaring structured output with a mistyped or recursive schema without proper $defs, copy-pasted schemas from another validator dialect with unsupported keywords, or auto-generated output schemas that exceed the size/complexity budget.","solutions":["Inspect the wrapped inner error for the precise failure (resolve error vs complexity limit).","Validate the OutputSchema as standalone JSON Schema (e.g. with a linter) before registering.","Fix broken $ref/$defs references; jsonschema.Resolve(nil) is called with no base URI, so relative refs must resolve within the document.","Shrink or flatten the schema if it exceeds 1 MiB / 64 depth / 16384 nodes.","Drop OutputSchema (return nil) if the tool does not actually guarantee structured content, since defining it makes structured content mandatory at validation time."],"exampleFix":"// before\nOutputSchema: &ToolSchema{Type: \"object\", Properties: map[string]Property{\"items\": {Type: \"array\", Ref: \"#/definitions/Item\"}}} // missing definitions\n// after\nOutputSchema: &ToolSchema{Type: \"object\", Definitions: map[string]any{\"Item\": itemSchema}, Properties: map[string]Property{\"items\": {Type: \"array\", Ref: \"#/definitions/Item\"}}}","handlingStrategy":"validation","validationCode":"function assertValidOutputSchema(schema) {\n  const s = JSON.stringify(schema);\n  if (s.length > 1048576) throw new Error(\"output schema exceeds 1 MiB\");\n  for (const ref of s.match(/\"\\$ref\":\"([^\"]+)\"/g) ?? []) {\n    if (!schema[\"$defs\"] && !schema.definitions) throw new Error(\"dangling $ref: \" + ref);\n  }\n}","typeGuard":"function declaresResolvableOutputSchema(t) {\n  return t?.OutputSchema == null || typeof t.OutputSchema === \"object\";\n}","tryCatchPattern":"validator, err := CompileToolValidator(tool)\nif err != nil {\n  if strings.HasPrefix(err.Error(), \"invalid output schema:\") {\n    return fmt.Errorf(\"tool %s output schema rejected: %w\", tool.Name, err)\n  }\n  return err\n}","preventionTips":["Validate OutputSchema standalone as JSON Schema before attaching it to a tool.","Define all $defs in the same schema document; Resolve is called with a nil base URI.","Keep output schemas small and flat — 1 MiB / 64 depth / 16384 node budgets apply.","Only declare OutputSchema if the handler always returns structured content."],"tags":["mcp","json-schema","validation","tools"],"backgroundTag":"schema-validation-failed","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}