{"record":{"id":"e281faa69b962977","repo":"github/github-mcp-server","slug":"failed-to-unmarshal-json-text-w","errorCode":null,"errorMessage":"failed to unmarshal JSON text: %w","messagePattern":"failed to unmarshal JSON text: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/github/csv_output.go","lineNumber":114,"sourceCode":"\t}\n\n\tcsvText, err := jsonTextToCSV(text.Text)\n\tif err != nil {\n\t\treturn utils.NewToolResultErrorFromErr(\"failed to convert response to CSV\", err)\n\t}\n\n\tresult.Content = []mcp.Content{&mcp.TextContent{Text: csvText}}\n\tresult.StructuredContent = nil\n\treturn result\n}\n\nfunc jsonTextToCSV(text string) (string, error) {\n\tdecoder := json.NewDecoder(strings.NewReader(text))\n\tdecoder.UseNumber()\n\n\tvar value any\n\tif err := decoder.Decode(&value); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to unmarshal JSON text: %w\", err)\n\t}\n\n\tdoc := csvDocument(value)\n\tif len(doc.metadata) == 0 && len(doc.rows) == 0 {\n\t\treturn \"\", nil\n\t}\n\n\tvar buf bytes.Buffer\n\twriteCSVMetadata(&buf, doc.metadata)\n\tif len(doc.rows) == 0 {\n\t\treturn buf.String(), nil\n\t}\n\n\theaders := csvHeaders(doc.rows)\n\tif len(headers) == 0 {\n\t\treturn buf.String(), nil\n\t}\n","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/github/github-mcp-server/blob/0ea1f775a7c73eff1bd2e25904d01136756bbfe2/pkg/github/csv_output.go#L96-L132","documentation":"jsonTextToCSV failed to decode the tool's JSON output text before converting it to CSV. json.Decoder.Decode returns errors like SyntaxError (malformed JSON), UnexpectedEOF (truncated text), or invalid UTF-8 errors when the input is not a complete, single JSON document. This means the upstream tool produced non-JSON text — typically an error message, plain-text output, or a truncated payload — while the server was configured with CSV output format.","triggerScenarios":"Running any tool with output format csv where the tool's textual result is not valid JSON: tools that return plain strings (URLs, plain text), error strings placed in TextContent, or responses truncated by size limits.","commonSituations":"MCP client requests output format csv for a tool whose content is inherently non-JSON; version changes that made a tool emit human-readable text; empty TextContent from a failed upstream call.","solutions":["Re-run the tool without the CSV output format to see the raw payload and confirm it is JSON","Choose CSV output only for tabular tools (list_* tools returning arrays of objects)","If the payload looks like JSON but still fails, check for truncation (content window limits) or BOM/whitespace prefix","Report tools that emit non-JSON text so they can be made CSV-safe upstream"],"exampleFix":"// before\nvar value any\nif err := decoder.Decode(&value); err != nil {\n\treturn \"\", fmt.Errorf(\"failed to unmarshal JSON text: %w\", err)\n}\n\n// after — reject non-JSON early with a payload preview\nif !json.Valid([]byte(text)) {\n\treturn \"\", fmt.Errorf(\"tool output is not valid JSON (csv conversion aborted): %.120s\", text)\n}","handlingStrategy":"validation","validationCode":"// before requesting CSV conversion, verify the text is a JSON document\nfunc isConvertibleJSON(text string) bool {\n\ttrimmed := strings.TrimSpace(text)\n\treturn strings.HasPrefix(trimmed, \"{\") || strings.HasPrefix(trimmed, \"[\")\n}","typeGuard":"func isJSONText(s string) bool { return json.Valid([]byte(s)) }","tryCatchPattern":"if err := decoder.Decode(&value); err != nil {\n\tvar syntaxErr *json.SyntaxError\n\tif errors.As(err, &syntaxErr) {\n\t\t// not JSON at all: return the raw text instead of failing the whole tool call\n\t\treturn text, nil\n\t}\n\treturn \"\", fmt.Errorf(\"failed to unmarshal JSON text: %w\", err)\n}","preventionTips":["Only request CSV output for tools that emit JSON arrays/objects (list_* tools)","Keep tool outputs valid JSON even on partial failures so format conversion stays possible","Watch for truncated payloads when content-window limits are in play — re-fetch with pagination instead of converting"],"tags":["go","json","csv","output-format","encoding"],"backgroundTag":null,"analyzedSha":"0ea1f775a7c73eff1bd2e25904d01136756bbfe2","analyzedAt":"2026-08-15T18:10:19.804Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}