mcp-context-forge icon indicating copy to clipboard operation
mcp-context-forge copied to clipboard

`PUT /gateways/{id}` on an `authorization_code` gateway with discovered tools returns 200 but drops all changes and deletes the tools

Open wolfgang-kriesel opened this issue 2 months ago • 1 comments

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)

  1. 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.
  2. Discover its tools: POST /gateways/{id}/tools/refresh (with X-Upstream-Authorization for the upstream server). The gateway now has one tool.
  3. PUT /gateways/{id} with oauth_config.scopes = ["a", "b", "offline_access"] and a new description → 200, but the response already shows the old scopes and description.
  4. GET /gateways/{id}, GET /gateways and the authorize URL (/oauth/authorize/{id} → scope=a offline_access) still show the old values; GET /tools shows no tool of the gateway any more.
  5. 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):

  1. update_gateway re-initialises the gateway via _initialize_gateway, which returns without connecting for authorization_code (return {}, [], [], [], [], services/gateway_service.py line 5410 in v1.0.11, 5548 on main).
  2. _sync_gateway_catalog / _reconcile_gateway_catalog then treat all existing tools as stale, delete them and call db.expire(gateway) (line 6493 in v1.0.11, 6743 on main).
  3. SessionLocal uses autoflush=False (db.py line 582 / 585), so db.expire(gateway) discards every attribute change of the update that was not flushed yet, including the new oauth_config. The final db.commit() writes only version, updated_at and 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_gateway did not connect (authorization_code, or token exchange without a user token); otherwise every update deletes the discovered tools.
  • Regression test: PUT on an authorization_code gateway with existing tools changes oauth_config.scopes and 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.

wolfgang-kriesel avatar Oct 06 '26 06:10 wolfgang-kriesel

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.

vgunjic avatar Oct 19 '26 16:10 vgunjic