`PUT /gateways/{id}` on an `authorization_code` gateway with discovered tools returns 200 but drops all changes and deletes the tools
Summary
For a gateway with auth_type: oauth and grant_type: authorization_code that already has discovered tools, PUT /gateways/{id} answers 200 but stores none of the changed fields (e.g. oauth_config.scopes, description), and all tools of the gateway are deleted. A second identical PUT then succeeds, because there are no tools left. Without discovered tools the first PUT works.
Steps to reproduce (v1.0.11 Docker image, PostgreSQL, CACHE_TYPE=database)
- Create a gateway with
auth_type: oauth,grant_type: authorization_code,oauth_config.scopes = ["a", "offline_access"], pointing to a reachable MCP server with one tool. - Discover its tools:
POST /gateways/{id}/tools/refresh(withX-Upstream-Authorizationfor the upstream server). The gateway now has one tool. PUT /gateways/{id}withoauth_config.scopes = ["a", "b", "offline_access"]and a newdescription→ 200, but the response already shows the old scopes and description.GET /gateways/{id},GET /gatewaysand the authorize URL (/oauth/authorize/{id}→scope=a offline_access) still show the old values;GET /toolsshows no tool of the gateway any more.- The same PUT again → 200, now the new scopes and description are stored.
Cause (same code in v1.0.11 and on main as of commit 9ea4dd73, 2026-10-05):
update_gatewayre-initialises the gateway via_initialize_gateway, which returns without connecting forauthorization_code(return {}, [], [], [], [],services/gateway_service.pyline 5410 in v1.0.11, 5548 onmain)._sync_gateway_catalog/_reconcile_gateway_catalogthen treat all existing tools as stale, delete them and calldb.expire(gateway)(line 6493 in v1.0.11, 6743 onmain).SessionLocalusesautoflush=False(db.pyline 582 / 585), sodb.expire(gateway)discards every attribute change of the update that was not flushed yet, including the newoauth_config. The finaldb.commit()writes onlyversion,updated_atand similar fields.
Suggested fix
- In
_reconcile_gateway_catalog, flush before expiring, or expire only the relationships (db.expire(gateway, ["tools", "resources", "prompts"])), so pending column changes survive. - In
update_gateway, skip the catalog sync and the stale cleanup when_initialize_gatewaydid not connect (authorization_code, or token exchange without a user token); otherwise every update deletes the discovered tools. - Regression test: PUT on an
authorization_codegateway with existing tools changesoauth_config.scopesand keeps the tools.
Workaround: send the PUT twice and refresh the tools afterwards.
Version: v1.0.11 (Docker image); the three code locations are unchanged on main (commit 9ea4dd73). We could not build an image from main in our environment to run the reproduction there. We found no related issue or PR.
We hit the same thing with token-exchange gateways. On v1.0.10 (the code is unchanged in v1.0.11 and on main), a PUT /gateways/{id} answered 200 and deleted all four tools of the gateway; we restored them from a database dump. _initialize_gateway also skips discovery for token-exchange when there is no user token, so the same path applies. By code reading one_time_auth is affected too, since the skip_stale_cleanup guards only cover authorization_code.
We also found the same db.expire(gateway) root cause on the refresh path, with a controlled reproduction: #7273. A refresh reports tools_updated, but the updates are lost when the same refresh also removes an item. Your first suggested fix (flush before the expire, or expire only the relationship collections) would close both.
For context: we carry a downstream patch that keeps the catalog when token-exchange discovery is skipped on update and reactivation. With an upstream fix we would drop it. Happy to test a fix branch.