{"record":{"id":"9cb98b9e0bba64ec","repo":"siyuan-note/siyuan","slug":"invalid-input-schema-w","errorCode":null,"errorMessage":"invalid input schema: %w","messagePattern":"invalid input schema: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/mcp/tools/validation.go","lineNumber":53,"sourceCode":"\tmaxToolValueNodes         = 256 << 10\n\ttoolValidationTime        = 2 * time.Second\n\ttoolValidationConcurrency = 4\n)\n\ntype ToolValidator struct {\n\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 {","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/mcp/tools/validation.go#L35-L71","documentation":"CompileToolValidator compiles a tool's InputSchema (and optional OutputSchema) into a resolved jsonschema validator. If the input schema cannot be resolved — malformed JSON structure, invalid JSON Schema keywords, unsupported constructs, or failing the internal complexity/annotation checks — the compile fails wrapped as 'invalid input schema'. The tool cannot be registered until its schema is fixed.","triggerScenarios":"Registering an MCP tool whose ToolSchema.InputSchema fails resolveToolSchema: JSON marshal/unmarshal failure, depth beyond 64, more than 16384 nodes, exceeding 1 MiB serialized size, invalid param header annotations (input schemas only), root type not \"object\", or jsonschema.Resolve failing (e.g. broken $ref, invalid type keyword values).","commonSituations":"Hand-written tool schemas with typos in keywords (e.g. \"type\": \"objet\"), $ref pointers to non-existent definitions, deeply generated schemas exceeding node/depth limits, or schemas built programmatically that accidentally emit a non-object root.","solutions":["Read the wrapped inner error (%w) to see the exact schema failure (resolve error, complexity limit, or annotation check).","Ensure InputSchema.Type is exactly \"object\" and the schema is valid JSON Schema per google/jsonschema-go.","Reduce schema size/complexity if the inner error mentions depth/node/byte limits (depth 64, nodes 16384, 1 MiB).","Fix or remove invalid $ref/$defs references and validate header annotations on properties.","Test the schema by calling CompileToolValidator directly in a unit test before registration."],"exampleFix":"// before\nInputSchema: ToolSchema{Type: \"string\", Properties: props} // root type must be \"object\"\n// after\nInputSchema: ToolSchema{Type: \"object\", Properties: props, Required: []string{\"zipPath\"}}","handlingStrategy":"validation","validationCode":"function assertValidInputSchema(schema) {\n  if (!schema || schema.type !== \"object\") throw new Error('input schema root type must be \"object\"');\n  if (JSON.stringify(schema).length > 1048576) throw new Error(\"schema exceeds 1 MiB\");\n}","typeGuard":"function hasObjectInputSchema(t) {\n  return t != null && t.InputSchema != null && t.InputSchema.Type === \"object\";\n}","tryCatchPattern":"validator, err := CompileToolValidator(tool)\nif err != nil {\n  if strings.HasPrefix(err.Error(), \"invalid input schema:\") {\n    return fmt.Errorf(\"tool %s registration failed: %w\", tool.Name, err)\n  }\n  return err\n}","preventionTips":["Always set InputSchema.Type to \"object\" for MCP tools.","Validate schemas with a JSON Schema linter before registration.","Keep $ref pointers within the same document ($defs).","Add a registration-time test compiling every tool's validator."],"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"}