Skip to content

Conversation

@kevalmahajan
Copy link
Member

@kevalmahajan kevalmahajan commented Jan 7, 2026

✨ Feature / Enhancement PR

πŸ”— Epic / Issue

Closes #1910, #172


πŸš€ Summary (1-2 sentences)

Added configurable AUTO_REFRESH_SERVERS feature that automatically syncs gateway tools, resources, and prompts with the database during health checks. When enabled, the system detects changes from MCP servers and performs add/update/delete operations only when necessary.

The tools, prompts, and resources are compared against those stored in the database, and any required add, update, or delete operations are performed.


πŸ”§ Usage

Enable via environment variable:

AUTO_REFRESH_SERVERS=true
  1. HEALTH_CHECK_INTERVAL=30 # Optional: shorter interval for testing
  2. Add an mcp server with 3 tools in the gateway.
  3. Update the mcp server by reducing the number of tools or adding some tools.
  4. Wait for the health check and notice that the new tools are automatically refreshed in the DB.
  5. Same applies for prompts and resources as well.

πŸ§ͺ Checks

  • make lint passes
  • make test passes
  • CHANGELOG updated (if user-facing)

πŸ““ Notes (optional)

flowchart TD
    A[Health Check Loop] -->|Every HEALTH_CHECK_INTERVAL| B{Gateway Healthy?}
    B -->|No| C[Handle Failure]
    B -->|Yes| D{AUTO_REFRESH_SERVERS?}
    D -->|No| E[Update last_seen]
    D -->|Yes| F[Fetch tools from MCP Server]
    F --> G{Any Changes?}
    G -->|No| E
    G -->|Yes| H[Sync DB: Add/Update/Delete]
    H --> I[Invalidate Caches]
    I --> E
Loading
  • The feature reuses existing _update_or_create_tools/resources/prompts helper methods
  • Uses fresh_db_session() to avoid holding DB connections during HTTP calls to MCP servers
  • SQLAlchemy's db.dirty tracking detects in-place updates to determine if commit is needed
  • Tool lookup cache is invalidated per-gateway for efficiency

@kevalmahajan kevalmahajan changed the title configurable auto refresh gateways configurable auto refresh for automatic tools/prompts/resources sync during health checks Jan 7, 2026
@kevalmahajan kevalmahajan requested a review from madhav165 January 7, 2026 11:21
@crivetimihai crivetimihai added the wxo wxo integration label Jan 8, 2026
@crivetimihai crivetimihai self-assigned this Jan 8, 2026
@crivetimihai
Copy link
Member

Thanks for the PR β€” it covers health-check auto-refresh. For REQ-002 we still need manual refresh API, per-gateway intervals, list_changed event handling, and full tool spec sync (annotations/outputSchema). Tracking those gaps in #1984.

@crivetimihai crivetimihai force-pushed the 1910_autorefresh_gateways branch 2 times, most recently from 91499e8 to eca342c Compare January 9, 2026 01:14
…sync

Add configurable feature to automatically sync gateway tools, resources,
and prompts with the database during health checks. When enabled:

- Fetches latest tools/resources/prompts from MCP servers
- Compares against database state
- Performs add/update/delete operations only when changes detected
- Tracks updates per-type (tools/resources/prompts) for precise cache invalidation
- Only deletes MCP-discovered items (health_check, oauth, federation, update, MCP)
- Preserves user-created entries (api, ui, None/legacy) during cleanup
- Allows empty server responses to clear stale items (except for auth_code OAuth)
- Passes pre-authenticated headers from health check to avoid duplicate OAuth token fetch
- Invalidates relevant caches per-gateway for efficiency
- Uses fresh_db_session() to avoid holding DB connections during HTTP calls

Configuration:
- AUTO_REFRESH_SERVERS=false (disabled by default)
- Reduces load on MCP servers when disabled

Closes #1910, #172

Signed-off-by: Mihai Criveti <[email protected]>
@crivetimihai crivetimihai force-pushed the 1910_autorefresh_gateways branch from eca342c to 5b0ba9b Compare January 9, 2026 01:21
@crivetimihai
Copy link
Member

Code Review Changes

Rebased onto main, squashed 7 commits into 1, and addressed the following issues:

Fixes Applied

Issue Severity Fix
Cache invalidation for resource/prompt updates High Added per-type update tracking (tools_updated, resources_updated, prompts_updated) instead of global db.dirty count. Now properly invalidates each cache type when updates occur.
Empty server clearing stale rows Medium Changed logic to only skip empty responses for authorization_code OAuth gateways (incomplete auth flow). Non-auth_code gateways with empty responses now properly clear stale items.
Double OAuth token requests Medium (perf) Added pre_auth_headers parameter to _initialize_gateway and _refresh_gateway_tools_resources_prompts. Health check now passes pre-authenticated headers to avoid duplicate token fetch.
Deletion scope too broad Medium Narrowed mcp_created_via_values to only actual MCP discovery sources: {"MCP", "federation", "health_check", "oauth", "update"}. User-created entries ("api", "ui", None/legacy) are now preserved.
Missing tests Low Added 9 new unit tests in test_gateway_auto_refresh.py covering auto-refresh paths, cache invalidation, pre-auth headers, and deletion scope.

Key Implementation Details

  • Per-type dirty tracking: Tracks db.dirty objects before/after _update_or_create_* calls to count updates per type (tools/resources/prompts)
  • Selective cache invalidation: Only invalidates caches for types that actually changed (add/remove/update)
  • Safe deletion scope: Only removes items with created_via in known MCP discovery sources, preserving user-created entries
  • OAuth token reuse: Passes pre_auth_headers from health check to _initialize_gateway to avoid fetching tokens twice per health check cycle

Files Changed

  • mcpgateway/services/gateway_service.py - Core implementation fixes
  • tests/unit/mcpgateway/services/test_gateway_auto_refresh.py - New test file (9 tests)

All tests pass.

Copy link
Member

@crivetimihai crivetimihai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved - created [FEATURE REQUEST]: Full tool list/spec refresh (polling + API + list_changed) #1984 for remaining features

@crivetimihai crivetimihai merged commit fd0ef33 into main Jan 9, 2026
52 checks passed
@crivetimihai crivetimihai deleted the 1910_autorefresh_gateways branch January 9, 2026 01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wxo wxo integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: Support re-discovery / refresh of tools for already registered MCP gateways

3 participants