Tool refresh reports tools_updated but loses the in-place updates when the same refresh removes stale items
🐞 Bug Summary
POST /gateways/{id}/tools/refresh returns, and logs, a non-zero tools_updated count. Yet when the same refresh also removes a stale item, the existing tools it reports as updated keep their old input_schema, description and updated_at in the database. Added and removed tools are applied correctly.
So clients keep receiving the old schema and description for a changed tool. The refresh result says it was updated, and nothing on the gateway indicates a problem.
🧩 Affected Component
- [x]
mcpgateway- API (POST /gateways/{id}/tools/refresh) - [x] Likely every caller of
_sync_gateway_catalog+_reconcile_gateway_catalogthat can update and remove in the same pass (auto-refresh, gateway update, reactivation). Only the manual refresh was tested. - [ ]
mcpgateway.wrapper- stdio wrapper - [ ] Federation or Transports
- [ ] CLI, Makefiles, or shell scripts
- [ ] Container setup (Docker/Podman/Compose)
- [ ] Other
🔁 Steps to Reproduce
-
Register an MCP gateway whose upstream server exposes tools
A,BandC. -
Change the upstream server so that:
Agets a differentinputSchema(for example a new required property);Bgets a different description;Cis removed;- a new tool
Dis added.
-
Call
POST /gateways/{id}/tools/refresh(no delete first). -
The response reports
tools_added = 1,tools_updated = 2,tools_removed = 1. -
Read
AandBback (GET /tools/{id}, or thetoolstable):- expected: new schema / description, a new
updated_at; - actual: the old schema and description, the old
updated_at, same tool IDs.
Cis gone andDexists, as expected. - expected: new schema / description, a new
Control: the same change without removing C persists the updates of A and B correctly.
Workaround: delete the gateway's tools and refresh again. The tools are recreated with the current definitions, but with new IDs, so virtual-server associations must be re-created.
🔬 Controlled reproduction
A minimal MCP server (Python stdlib, streamable HTTP) whose tools/list returns one of three tool sets, switched between steps without a restart. Registered as a new gateway with no auth, not attached to any virtual server. Every refresh is POST /gateways/{id}/tools/refresh with an admin token. The values come from the tools table: SHA-256 prefixes of input_schema and description, and updated_at.
| Step | Refresh response | probe_a (schema) |
probe_b (description) |
Other |
|---|---|---|---|---|
| 1. Register, V1 | n/a | c8e566b8c267, t1 |
b71746fc7bd9, t1 |
probe_c present |
2. V2: probe_a gets a new required property, probe_b a new description; nothing removed |
added 0 / updated 2 / removed 0 | f57a366fa77b, t2 (persisted) |
4458b4b3e1a0, t2 (persisted) |
probe_c unchanged |
3. V3: probe_a and probe_b change again, probe_c removed, probe_d added |
added 1 / updated 2 / removed 1 | f57a366fa77b, t2: still the V2 value |
4458b4b3e1a0, t2: still the V2 value |
probe_c gone, probe_d added |
| 4. V3: delete the tools, then refresh | added 3 / updated 0 / removed 0 | 43bbbc6dff3d (V3), new ID |
dcddea77bc9b (V3), new ID |
probe_d new ID |
In-place updates are persisted when nothing is removed (step 2), and silently lost when the same refresh also removes an item (step 3), although tools_updated reports them.
Two earlier field observations on the same version, both with OAuth token-exchange gateways, match this pattern; both refreshes included removals (+10/~23/-5 and +1/~2/-1).
🤔 Expected Behavior
Tools reported in tools_updated are persisted with the new upstream definitions in the same commit as the adds and removes. The intent is already in the code: _sync_gateway_catalog compares the schema and description fields and overwrites them on the existing DbTool.
📓 Logs / Error Output
No error is logged. The refresh logs success, for example:
Refreshed gateway <name>: tools(+1/-1/~2), resources(+0/-0/~0), prompts(+0/-0/~0)
🔍 Analysis (code reading; matches the controlled reproduction above)
References: v1.0.11 (077071bb) and current main (a211ab7d). The relevant code is the same in both.
_refresh_gateway_tools_resources_promptscalls_sync_gateway_catalog, which modifies existingDbToolobjects in place (existing_tool.input_schema = tool.input_schema, …; v1.0.11gateway_service.py:6188, main:6554). The count comes fromdb.dirty(v1.0.11:6890, main:7371), so the objects are dirty but not flushed.- The session is created with
autoflush=False(db.py: v1.0.11:582, main:585). _reconcile_gateway_catalogthen deletes stale items with bulkdb.execute(delete(DbTool)…)and, if anything was removed, callsdb.expire(gateway)(v1.0.11:6493, main:6971).Gateway.toolsisrelationship(..., cascade="all, delete-orphan", passive_deletes=True)(db.py: v1.0.11:4764, main:4769).allincludesrefresh-expire, sodb.expire(gateway)cascades to everyToolingateway.toolsand discards their pending, unflushed attribute changes.gateway.resourcesandgateway.promptsbehave the same way.- The later
db.commit()writes the deletes and the flushed adds, but the in-place updates are already gone. The count reported in step 1 is stale.
This is why a refresh that changes tools but removes nothing persists the updates: no expire happens (step 2 of the reproduction), while step 3 loses them.
💡 Possible fix (suggestion, untested)
Flush pending in-place changes before the stale-item deletion / expire in _reconcile_gateway_catalog, for example db.flush() before the bulk DELETEs. Or expire only the relationship collections instead of cascading into the children.
A regression test can assert that, after a refresh with at least one removal and one schema change, the changed tool has the new input_schema and description in the database, not only in the counter.
🧠 Environment Info
| Key | Value |
|---|---|
| Version or commit | v1.0.11 (077071bb); code path unchanged on main a211ab7d |
| Runtime | Python as shipped in the v1.0.11 image, Gunicorn/Uvicorn, PostgreSQL 18 |
| Platform / OS | Linux container, amd64 |
| Container | Our own build of v1.0.11 with downstream patches. None of them changes _sync_gateway_catalog or _reconcile_gateway_catalog. One adds a row lock (with_for_update) in the refresh path, which does not affect the expire behaviour. |
| Gateway auth | Reproduction: no auth. Field observations: OAuth token-exchange (RFC 8693), refreshed with an authenticated user token. Not specific to either. |
🧩 Additional Context
- Related code: #6046 / PR #6049 touched
_reconcile_gateway_catalogfor a different symptom (catalog deletion on empty fetch). This report is about updates being lost when the catalog is not empty. - The difference between counter and database is silent. In our case it led an operator to use "refresh plus re-link" instead of delete and rescan, which left changed tools on stale schemas in one environment.
Same root cause as #7152 (the update_gateway path): db.expire(gateway) in _reconcile_gateway_catalog, with autoflush=False, discards pending changes. A fix in _reconcile_gateway_catalog should cover both.