How do I let an agent choose the MongoDB collection at runtime in mcp-toolbox?
In googleapis/mcp-toolbox, the MongoDB MCP tools (find, aggregate, insert, update, delete, etc.) require a
collectionvalue in the tool config, so each tool is pinned to one collection and an agent cannot pick a collection based on the user's query. Is there a way to let the agent supply the collection at execution time? Issue #1679 was raised for this and is now closed as completed — what does the fix look like?Originally asked on GitHub: https://github.com/googleapis/mcp-toolbox/issues/1679
4 answers
Per the issue tracker, this issue is closed as completed. In the thread itself, the fix was implemented in a PR that was still awaiting review, so no released version is confirmed here.
The root cause, found by a contributor, is that
collectionis avalidate:"required"field on each MongoDB tool's config, andInvokepassest.Cfg.Collectiondirectly into the driver call (e.g.source.Find(...)). The agreed approach: drop the required flag and, when config has no collection, expose it as a plain runtime string parameter using the same patternclickhouse-list-tablesalready uses for its runtime database (build withNewStringParameter, read back inInvokeviaparams.AsMap()["collection"].(string)). The fallback logic looks like:gocollection := t.Cfg.Collection if collection == "" { collection, _ = params.AsMap()["collection"].(string) if collection == "" { return nil, util.NewAgentError("collection must be set in config or provided as a parameter", nil) } }Precedence is config-wins: a contributor proposed it and another contributor endorsed it, so existing configs stay backward-compatible and agents can't reach collections the developer didn't explicitly expose. The plan covers all 9 MongoDB tools (find, find-one, aggregate, insert-one/many, update-one/many, delete-one/many) plus unit/integration tests and docs. To keep a runtime choice scoped, restrict the parameter with the same
WithStringAllowedValuesoption other tools use.Security note: when you omit
collectionfrom config, the agent can pass any string, so constrain the runtime parameter to known collections via allowedValues if your database contains collections you don't want exposed.Did this work for you?What @SWITCHin2 tried on GitHub (Outcome unclear)
A contributor implemented the change following the proposed plan (drop required on collection, expose it as a runtime parameter with config-wins fallback) and submitted a PR; at the time of writing the PR was assigned for review and still had review comments.
Source: https://github.com/googleapis/mcp-toolbox/issues/1679#issuecomment-5088208577 (opens external site)
Did this work for you?What @SWITCHin2 tried on GitHub (Outcome unclear)
A contributor traced the root cause (validate:"required" field; Invoke passes t.Cfg.Collection straight into the driver call) and proposed the runtime-parameter approach modeled on clickhouse-list-tables, with config-wins precedence and an error when collection is neither configured nor supplied.
Source: https://github.com/googleapis/mcp-toolbox/issues/1679#issuecomment-5061016264 (opens external site)
Did this work for you?What a GitHub user tried on GitHub (Outcome unclear)
The issue author proposed making
collectionoptional in tool config and accepting it as a runtime tool argument, with the runtime value taking precedence over config.Source: https://github.com/googleapis/mcp-toolbox/issues/1679 (opens external site)
Did this work for you?
Related questions
- GenAI Toolbox: "oidc" is not a valid kind of auth source — does it support Keycloak?
- How to set a default value for env vars in GenAI Toolbox/mcp-toolbox configs?
- Does googleapis/mcp-toolbox support a per-tool read_only flag?
- Does mcp-toolbox support Snowflake as a database source?
- BigQuery MCP list_table_ids only returns 50 tables – was this fixed?