{"record":{"id":"cdbf7e3e47dd5540","repo":"googleapis/mcp-toolbox","slug":"failed-to-marshal-response-to-json-w","errorCode":null,"errorMessage":"failed to marshal response to JSON: %w","messagePattern":"failed to marshal response to JSON: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/server/mcp.go","lineNumber":349,"sourceCode":"\n\tselect {\n\t// if context is cancelled, return an empty string\n\tcase <-ctx.Done():\n\t\treturn \"\", ctx.Err()\n\t// return error if error is found\n\tcase err := <-errChan:\n\t\treturn \"\", err\n\t// return line if successful\n\tcase line := <-readChan:\n\t\treturn line, nil\n\t}\n}\n\n// write writes to stdout with response to client\nfunc (s *stdioSession) write(_ context.Context, response any) error {\n\tres, err := json.Marshal(response)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to marshal response to JSON: %w\", err)\n\t}\n\n\t_, err = fmt.Fprintf(s.writer, \"%s\\n\", res)\n\treturn err\n}\n\n// mcpRouter creates a router that represents the routes under /mcp\nfunc mcpRouter(s *Server) (chi.Router, error) {\n\tr := chi.NewRouter()\n\n\tr.Use(middleware.AllowContentType(\"application/json\", \"application/json-rpc\", \"application/jsonrequest\"))\n\tr.Use(middleware.StripSlashes)\n\tr.Use(render.SetContentType(render.ContentTypeJSON))\n\t// Inject logger into ctx\n\tr.Use(func(next http.Handler) http.Handler {\n\t\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\t\tctx := util.WithLogger(r.Context(), s.logger)\n\t\t\tnext.ServeHTTP(w, r.WithContext(ctx))","sourceCodeStart":331,"sourceCodeEnd":367,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/server/mcp.go#L331-L367","documentation":"Error \"failed to marshal response to JSON: %w\" thrown in googleapis/mcp-toolbox.","triggerScenarios":"Thrown at internal/server/mcp.go:349 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}