{"record":{"id":"c1e523b49120b4ee","repo":"googleapis/mcp-toolbox","slug":"error-unmarshalling-projection-s","errorCode":null,"errorMessage":"error unmarshalling projection: %s","messagePattern":"error unmarshalling projection: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/tools/mongodb/mongodbfind/mongodbfind.go","lineNumber":162,"sourceCode":"\n\tsort := bson.M{}\n\tfor _, p := range sortParameters {\n\t\tsort[p.GetName()] = paramsMap[p.GetName()]\n\t}\n\topts = opts.SetSort(sort)\n\n\tif len(projectPayload) > 0 {\n\n\t\tresult, err := parameters.PopulateTemplateWithJSON(\"MongoDBFindProjectString\", projectPayload, paramsMap)\n\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error populating project payload: %s\", err)\n\t\t}\n\n\t\tvar projection any\n\t\terr = bson.UnmarshalExtJSON([]byte(result), false, &projection)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error unmarshalling projection: %s\", err)\n\t\t}\n\n\t\topts = opts.SetProjection(projection)\n\t\tlogger.DebugContext(ctx, fmt.Sprintf(\"Projection is set to %v\", projection))\n\t}\n\n\tif limit > 0 {\n\t\topts = opts.SetLimit(limit)\n\t\tlogger.DebugContext(ctx, fmt.Sprintf(\"Limit is being set to %d\", limit))\n\t}\n\treturn opts, nil\n}\n\nfunc (t Tool) Invoke(ctx context.Context, s sources.Source, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {\n\tsource, ok := s.(compatibleSource)\n\tif !ok {\n\t\treturn nil, util.NewClientServerError(\"source used is not compatible with the tool\", http.StatusInternalServerError, nil)\n\t}","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/tools/mongodb/mongodbfind/mongodbfind.go#L144-L180","documentation":"After the project payload template renders, the result is parsed as MongoDB Extended JSON via bson.UnmarshalExtJSON. If the rendered string is not valid Extended JSON, the tool wraps the failure as 'error unmarshalling projection'. The template rendered fine, but its output is not a valid projection document.","triggerScenarios":"Invoke() with a project payload whose rendered output is invalid Extended JSON — e.g. unquoted keys produced by the template, wrong value types, or stray text after rendering.","commonSituations":"Using standard JSON with unquoted keys expecting Extended JSON acceptance is fine, but using shell-style Mongo syntax like {name: 1} with invalid characters; template injecting non-JSON values (dates, ObjectIds as strings) incorrectly; missing $-operator prefix typos.","solutions":["Correct the projection JSON in the tool config to valid Extended JSON (e.g. {\"name\": 1}).","If injecting values via template, ensure they serialize as JSON primitives.","Test the rendered output of the template for validity before use."],"exampleFix":"# before\nproject: '{ name: 1, created_at: $date }'\n# after\nproject: '{ \"name\": 1, \"created_at\": { \"$date\": \"{{.params.start}}\" } }'","handlingStrategy":"validation","validationCode":"// Go: pre-validate rendered projection parses\nvar probe any\nif err := bson.UnmarshalExtJSON([]byte(rendered), false, &probe); err != nil {\n    return fmt.Errorf(\"invalid projection JSON: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"opts, err := getOptions(ctx, sortParams, projectPayload, limit, paramsMap)\nif err != nil && strings.Contains(err.Error(), \"error unmarshalling projection\") {\n    // fall back to no projection\n    opts, err = getOptions(ctx, sortParams, \"\", limit, paramsMap)\n}","preventionTips":["Write projections as strict Extended JSON (quote all keys, use {\"$date\": ...} style).","Avoid injecting raw non-JSON values through templates.","Unit-test the rendered projection string with bson.UnmarshalExtJSON."],"tags":["go","mongodb","json-parsing","projection"],"backgroundTag":"json-parse-error","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"}