{"record":{"id":"26592f87ad99c510","repo":"googleapis/mcp-toolbox","slug":"only-one-of-collection-or-collectionallowedvalu","errorCode":null,"errorMessage":"only one of 'collection' or 'collectionAllowedValues' can be set, not both","messagePattern":"only one of 'collection' or 'collectionAllowedValues' can be set, not both","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/tools/mongodb/mongodbcommon/util.go","lineNumber":31,"sourceCode":"// limitations under the License.\n\n// Package mongodbcommon holds helpers shared across the MongoDB tools.\npackage mongodbcommon\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/googleapis/mcp-toolbox/internal/util\"\n\t\"github.com/googleapis/mcp-toolbox/internal/util/parameters\"\n)\n\n// CollectionKey is the runtime parameter name used to pick a collection.\nconst CollectionKey string = \"collection\"\n\n// ValidateCollectionConfig rejects setting collection and collectionAllowedValues together.\nfunc ValidateCollectionConfig(collection string, allowedValues []string) error {\n\tif collection != \"\" && len(allowedValues) > 0 {\n\t\treturn fmt.Errorf(\"only one of 'collection' or 'collectionAllowedValues' can be set, not both\")\n\t}\n\treturn nil\n}\n\n// WithRuntimeCollectionParam adds a required collection parameter when none is set in config.\nfunc WithRuntimeCollectionParam(collection string, allowedValues []string, params parameters.Parameters) parameters.Parameters {\n\tif collection != \"\" {\n\t\treturn params\n\t}\n\topts := []parameters.StringParameterOption{parameters.WithStringRequired(true)}\n\tif len(allowedValues) > 0 {\n\t\tallowed := make([]any, len(allowedValues))\n\t\tfor i, v := range allowedValues {\n\t\t\tallowed[i] = v\n\t\t}\n\t\topts = append(opts, parameters.WithStringAllowedValues(allowed))\n\t}\n\tcollectionParam := parameters.NewStringParameter(CollectionKey, \"The name of the collection to operate on.\", opts...)","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/tools/mongodb/mongodbcommon/util.go#L13-L49","documentation":"mongodbcommon.ValidateCollectionConfig enforces that a MongoDB tool picks its target collection in exactly one way: either a fixed `collection` in the tool config, or a runtime `collection` parameter constrained by `collectionAllowedValues` — never both. Setting both is ambiguous, so initialization fails with this error.","triggerScenarios":"Declaring a MongoDB tool config (e.g. mongodb-aggregate, mongodb-delete-many) where `collection` is non-empty AND `collectionAllowedValues` has at least one entry; ValidateCollectionConfig is called from Initialize and from parameter-processing anonymous functions.","commonSituations":"Migrating from a fixed-collection tool to a runtime-collection tool but forgetting to remove the old `collection` field; copy-pasting a template that includes both keys; merging two tool definitions.","solutions":["Remove `collectionAllowedValues` if you want a fixed `collection` in config","Remove the `collection` field and keep `collectionAllowedValues` if the collection should be chosen at runtime","Keep only one of the two keys in the tool's YAML block"],"exampleFix":"// before\nmy-tool:\n  kind: mongodb-aggregate\n  source: my-mongo\n  collection: orders\n  collectionAllowedValues: [orders, customers]\n// after\nmy-tool:\n  kind: mongodb-aggregate\n  source: my-mongo\n  collection: orders","handlingStrategy":"validation","validationCode":"cfg, _ := yaml.Marshal(toolCfg)\nvar t struct {\n    Collection              string   `yaml:\"collection\"`\n    CollectionAllowedValues []string `yaml:\"collectionAllowedValues\"`\n}\nyaml.Unmarshal(cfg, &t)\nif t.Collection != \"\" && len(t.CollectionAllowedValues) > 0 {\n    return errors.New(\"set only one of collection or collectionAllowedValues\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide up front whether the collection is static or runtime-selected","Never copy both keys from templates; prune unused fields after edits","Add a CI lint that rejects MongoDB tool configs with both fields set"],"tags":["go","mongodb","config-validation","mutually-exclusive-fields"],"backgroundTag":"mutually-exclusive-config-fields","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"}