{"record":{"id":"df7d54d762e359db","repo":"siyuan-note/siyuan","slug":"invalid-tool-arguments-w","errorCode":null,"errorMessage":"invalid tool arguments: %w","messagePattern":"invalid tool arguments: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/mcp/server.go","lineNumber":266,"sourceCode":"\t\tTitle:       tool.Title,\n\t\tDescription: tool.Description,\n\t\tInputSchema: tool.InputSchema,\n\t}\n\tif tool.OutputSchema != nil {\n\t\tsdkTool.OutputSchema = tool.OutputSchema\n\t}\n\tif tool.ReadOnlyHint {\n\t\tsdkTool.Annotations = &mcpsdk.ToolAnnotations{ReadOnlyHint: true}\n\t}\n\n\tserver.AddTool(sdkTool, func(ctx context.Context, request *mcpsdk.CallToolRequest) (*mcpsdk.CallToolResult, error) {\n\t\tif allowed != nil && !allowed() {\n\t\t\treturn toolErrorResult(\"MCP capability is disabled or no longer available\"), nil\n\t\t}\n\t\targuments := map[string]any{}\n\t\tif len(request.Params.Arguments) > 0 {\n\t\t\tif err := json.Unmarshal(request.Params.Arguments, &arguments); err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"invalid tool arguments: %w\", err)\n\t\t\t}\n\t\t}\n\t\tif arguments == nil {\n\t\t\targuments = map[string]any{}\n\t\t}\n\t\tif err := validator.ValidateInputContext(ctx, arguments); err != nil {\n\t\t\treturn toolErrorResult(fmt.Sprintf(\"invalid tool arguments: %v\", err)), nil\n\t\t}\n\t\treleaseBoxLeases := func() {}\n\t\tif tool.BoxLeaseResolver != nil {\n\t\t\tleaseContext, contextErr := requestOperationContext(ctx, request)\n\t\t\tif contextErr != nil {\n\t\t\t\tlogging.LogWarnf(\"mcp: acquire request operation scope for tool [%s] failed: %v\", name, contextErr)\n\t\t\t\treturn toolErrorResult(contextErr.Error()), nil\n\t\t\t}\n\t\t\treleaseBoxLeases, err = model.AcquireEncryptedBoxOperations(leaseContext, tool.BoxLeaseResolver(arguments))\n\t\t\tif err != nil {\n\t\t\t\tif errors.Is(err, model.ErrEncryptedBoxNotUnlocked) {","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/mcp/server.go#L248-L284","documentation":"The MCP tools/call handler parses request.Params.Arguments as a JSON object of named arguments. If the raw JSON bytes are malformed (not valid JSON), the handler fails fast with 'invalid tool arguments' wrapping the json.Unmarshal error instead of invoking the tool.","triggerScenarios":"An MCP client sends a tools/call whose params.arguments bytes are invalid JSON — e.g. truncated payload, single quotes instead of JSON quotes, or a JSON array/string instead of an object-shaped raw body (unmarshal into map[string]any fails for non-object types).","commonSituations":"Hand-written MCP clients or curl tests with improperly escaped arguments; a serialization bug in a plugin or SDK producing malformed JSON; proxy/middleware truncating the request body.","solutions":["Validate the arguments payload is well-formed JSON (json.Valid) before sending the tools/call request","Ensure arguments serialize as a JSON object with string keys via a proper JSON marshaller","Check intermediaries for body truncation or re-encoding","Inspect the wrapped %w cause in the error for the exact JSON offset of the syntax problem"],"exampleFix":"// before\nargs := \"{name: 'x'}\"            // invalid JSON\n// after\nargs, _ := json.Marshal(map[string]any{\"name\": \"x\"})","handlingStrategy":"validation","validationCode":"if !json.Valid([]byte(argsJSON)) {\n    return fmt.Errorf(\"arguments are not valid JSON\")\n}\nvar probe map[string]any\nif err := json.Unmarshal([]byte(argsJSON), &probe); err != nil {\n    return fmt.Errorf(\"arguments must be a JSON object: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"result, err := client.CallTool(ctx, req)\nif err != nil && strings.Contains(err.Error(), \"invalid tool arguments\") {\n    // fix and re-serialize arguments before retrying\n}","preventionTips":["Always serialize arguments with a JSON encoder, never string concatenation","Round-trip arguments through json.Marshal before sending in tests","Beware double-encoded JSON strings (\"{\\\"a\\\":1}\" as a string) at proxies"],"tags":["mcp","json","validation"],"backgroundTag":"json-parse-error","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"}