Skip to content

feat(ui): MCP tool refresh button - #3802

Merged
crivetimihai merged 4 commits into
mainfrom
3765-mcp-refresh-button
Apr 1, 2026
Merged

crivetimihai merged 4 commits into
mainfrom
3765-mcp-refresh-button

Conversation

@gcgoncalves

Copy link
Copy Markdown
Collaborator

✨ Feature / Enhancement PR

🔗 Epic / Issue

Closes #3765


🚀 Summary (1-2 sentences)

Adds buttons for refreshing an MCP server's tools

Screenshot 2026-03-20 at 22 29 34 Screenshot 2026-03-20 at 23 24 53 Screenshot 2026-03-20 at 23 25 18

🧪 Checks

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

📓 Notes (optional)

Piggybacks (partially) #3519, to prevent an overclutered actions section

flowchart TD
    A[Client] -->|POST /gateway/gateway_id/refresh/tools| B(MCPGateway)
    B -->|/tools/list| C[MCP]
    B -->|Response - HTMX| A
Loading

@gcgoncalves
gcgoncalves force-pushed the 3765-mcp-refresh-button branch from d9e97a1 to 0ef3f89 Compare March 23, 2026 15:35
@gcgoncalves
gcgoncalves marked this pull request as ready for review March 23, 2026 15:35
@gcgoncalves
gcgoncalves force-pushed the 3765-mcp-refresh-button branch 3 times, most recently from 42e28fd to 0f7bf73 Compare March 23, 2026 15:43
@gcgoncalves gcgoncalves changed the title [FEATURE][UI]: MCP tool refresh button feat(ui): MCP tool refresh button Mar 23, 2026
@gcgoncalves
gcgoncalves force-pushed the 3765-mcp-refresh-button branch 2 times, most recently from 1cc8717 to 5850a18 Compare March 24, 2026 09:27
@marekdano

Copy link
Copy Markdown
Collaborator

Reviewing - there is an issue with the tooltip for the Refresh from MCPs button, which is behind the Edit modal
Screenshot 2026-03-24 at 12 01 47

@gcgoncalves
gcgoncalves force-pushed the 3765-mcp-refresh-button branch from b80501d to 0fa8ac1 Compare March 24, 2026 13:27
@gcgoncalves

Copy link
Copy Markdown
Collaborator Author

Reviewing - there is an issue with the tooltip for the Refresh from MCPs button, which is behind the Edit modal

Addressed.

Screenshot 2026-03-24 at 13 28 45

@marekdano marekdano left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Summary:
This PR successfully implements the refresh tools feature and overflow menu, but has some implementation concerns that should be addressed before merging.

Issues to Address

Medium Priority:

  1. Fragile relationship loading check (mcpgateway/services/gateway_service.py:4160)
tools_rel = gateway.__dict__.get("tools")
gateway_dict["tool_count"] = len(tools_rel) if tools_rel is not None else 0
  • Using dict.get() is SQLAlchemy-internal and fragile
  • If selectinload is removed elsewhere, tool counts silently become 0 with no error
  • Fix: Use inspect(gateway).attrs["tools"].loaded_value (official SQLAlchemy API) or add a clear comment explaining why dict is used
  1. Test scoping inconsistency (tests/playwright/test_gateways.py:545)
activate_btn = first_row.locator('button:text-is("Activate")')
deactivate_btn = first_row.locator('button:has-text("Deactivate")')
  • After refactor, these buttons are inside dropdown but test still scopes to first_row
  • Fix: Change to dropdown.locator() for consistency with other assertions in same test

Low Priority / Polish:

  1. Defensive check could be clearer (mcpgateway/static/admin.js:~146)
  • typeof getSelectedGatewayIds === "function" check silently returns [] if function missing
  • Consider logging warning if function doesn't exist
  1. UX improvement opportunity
  • When all delta counts are 0, showing "0 added, 0 updated, 0 removed" is technically correct but could be friendlier
  • Consider "No changes detected" message for better UX (can be follow-up)
  1. Keyboard navigation gap
  • Overflow menu lacks Escape key to close and arrow key navigation
  • Minor a11y gap (can be follow-up issue)

What's Good

✅ Smart label toggling based on tool_count
✅ Proper eager loading with selectinload(DbGateway.tools)
✅ Good error handling in JavaScript
✅ Comprehensive test updates
✅ Proper accessibility attributes (role, aria-expanded)
✅ Well-written CHANGELOG
✅ No security concerns

@gcgoncalves

Copy link
Copy Markdown
Collaborator Author

Summary: This PR successfully implements the refresh tools feature and overflow menu, but has some implementation concerns that should be addressed before merging.
...

@marekdano Addressed.

@gcgoncalves
gcgoncalves requested a review from marekdano March 24, 2026 14:12

@vishu-bh vishu-bh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Change introduces a browser-triggered mutation on a non-admin API route (POST /gateways/{gateway_id}/tools/refresh) while the existing CSRF protection is scoped to admin_router. The new UI path calls the route with cookie credentials from mcpgateway/static/admin.js (mcpgateway/static/admin.js#L20848), but the route itself lives in mcpgateway/main.py (mcpgateway/main.py#L6573) rather than under the admin router where enforce_admin_csrf is applied (mcpgateway/admin.py (mcpgateway/admin.py#L1570)).

Under the default SameSite=Lax cookie setting this is probably not a classic arbitrary cross-site POST issue, but it still bypasses the app’s explicit CSRF boundary and becomes relevant in same-site cross-origin deployments or when COOKIE_SAMESITE=none is used. Would it make sense to add a small cookie-auth CSRF check on this route, while still allowing bearer-token API callers, so the admin UI action stays aligned with the existing CSRF model?

@gcgoncalves
gcgoncalves force-pushed the 3765-mcp-refresh-button branch 2 times, most recently from e2ac8c9 to c818a57 Compare March 24, 2026 14:44
@vishu-bh

Copy link
Copy Markdown
Collaborator

Also noticed a cosmetic behaviour for the action button in table.

Screen.Recording.2026-03-24.at.14.41.37.mov

@gcgoncalves
gcgoncalves force-pushed the 3765-mcp-refresh-button branch from c818a57 to 737b8ee Compare March 24, 2026 15:04
@gcgoncalves

Copy link
Copy Markdown
Collaborator Author

Also noticed a cosmetic behaviour for the action button in table.

Screen.Recording.2026-03-24.at.14.41.37.mov

@vishu-bh Addressed.

Screen.Recording.2026-03-24.at.15.03.34.mov

@gcgoncalves
gcgoncalves force-pushed the 3765-mcp-refresh-button branch from 737b8ee to 8f37574 Compare March 24, 2026 15:05
@gcgoncalves
gcgoncalves requested a review from vishu-bh March 24, 2026 15:05
@gcgoncalves
gcgoncalves force-pushed the 3765-mcp-refresh-button branch from 8f37574 to 9475ebb Compare March 24, 2026 16:36
@gcgoncalves
gcgoncalves force-pushed the 3765-mcp-refresh-button branch 3 times, most recently from da614e9 to 5fcb01c Compare April 1, 2026 13:18
marekdano
marekdano previously approved these changes Apr 1, 2026

@marekdano marekdano left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM 🚀

@gcgoncalves gcgoncalves added the release-fix Critical bugfix required for the release label Apr 1, 2026
@gcgoncalves
gcgoncalves force-pushed the 3765-mcp-refresh-button branch from 5fcb01c to 917405c Compare April 1, 2026 14:11
gcgoncalves and others added 2 commits April 1, 2026 17:42
Closes #3765
Closes #3519

Adds a Fetch/Refresh Tools action to the gateways table for all gateway
auth types, not just OAuth. The label reads "Fetch Tools" on first use
(no tools registered yet) and switches to "Refresh Tools" once tools
exist, using the existing POST /gateways/{id}/tools/refresh endpoint.
On success, a toast shows the delta counts (added / updated / removed)
and the table reloads via HTMX.

A matching "Refresh from MCPs" button is added to the virtual server
edit form so users can pull the latest tools from selected gateways
without leaving the modal.

To support the contextual label, tool_count is added to GatewayRead
(serialised as toolCount) and DbGateway.tools is eagerly loaded in
the gateways partial query to avoid N+1 queries.

As a piggyback improvement, the stacked action buttons in the gateways
table are replaced with an Alpine.js overflow menu (...), housing all
actions including the new refresh button.

Signed-off-by: Gabriel Costa <[email protected]>
Cover the new tool_count field added in feat(ui): MCP tool refresh
button. Tests cover:
- GatewayRead.tool_count default (0) and explicit value serialization
- convert_gateway_to_read populating tool_count from eagerly-loaded
  tools relationship, empty tools list, and unloaded relationship

Signed-off-by: Mihai Criveti <[email protected]>
crivetimihai
crivetimihai previously approved these changes Apr 1, 2026

@crivetimihai crivetimihai left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Review Summary

Clean implementation — rebased onto main, resolved .secrets.baseline conflict, and added differential tests.

What was reviewed

  • Security: Backend endpoint gated by @require_permission("gateways.update"), template gates button with can_modify, tojson_attr escapes values properly (XSS-safe). No new auth bypass vectors.
  • Logic: JS correctly reads camelCase response fields from GatewayRefreshResponse. Promise.allSettled handles partial failures across multiple gateways. HTMX table reload updates button labels correctly.
  • Performance: selectinload(DbGateway.tools) is consistent with existing patterns (e.g. selectinload(DbServer.tools)).
  • Consistency: Overflow menu pattern follows Alpine.js conventions. overflow-visible change is needed so dropdown isn't clipped.

Changes made during review

  1. Rebased onto main (resolved .secrets.baseline merge conflict)
  2. Added 5 unit tests for 100% differential coverage of new Python code:
    • GatewayRead.tool_count default value and camelCase serialization
    • convert_gateway_to_read populating tool_count from eagerly-loaded tools, empty list, and unloaded relationship
  3. Updated .secrets.baseline for pre-existing false positives

Minor notes (non-blocking)

  • The old fetchToolsForGateway() function in admin.js remains and is still referenced in the hidden inline gateway table in admin.html (line 5333). Could be cleaned up in a follow-up.

@crivetimihai

Copy link
Copy Markdown
Member

Added ecffd51c0: fix(ui): preserve table state on gateway tool refresh

The original htmx.ajax reload used a bare /admin/gateways/partial URL, which reset pagination, search query, tag filters, and include_inactive state. Now uses buildTableUrl("gateways", ...) to carry the current UI state through the reload — matching the pattern used by the initial x-init load and the inactive-toggle checkbox.

Use buildTableUrl to carry current pagination, search query, tag
filter, and include_inactive state when reloading the gateways
partial after a tool refresh, preventing loss of user context.

Signed-off-by: Mihai Criveti <[email protected]>
@crivetimihai
crivetimihai force-pushed the 3765-mcp-refresh-button branch from ecffd51 to 9a8690b Compare April 1, 2026 17:10
@crivetimihai

Copy link
Copy Markdown
Member

Updated 9a8690b10: the table reload now also preserves the active team_id scope via getCurrentTeamId(), preventing the refresh from loading gateways from a different team context.

crivetimihai
crivetimihai previously approved these changes Apr 1, 2026

@crivetimihai crivetimihai left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Final Review — Playwright-verified

Rebased, reviewed, tested, and verified via Playwright against localhost:8080.

Playwright test results

Test Result
Overflow menu opens with all actions (Test, Refresh Tools, View, Edit, Deactivate, Delete) Pass
"Refresh Tools" label when gateway has tools / "Fetch Tools" when toolCount=0 Pass
Click "Refresh Tools" — calls API, reloads table with preserved state, zero JS errors Pass
"Refresh from MCPs" button visible in virtual server edit modal Pass
"Refresh from MCPs" with no gateway selected — shows error toast Pass
"Refresh from MCPs" with gateway selected — refreshes and reloads tools list Pass
Zero console errors throughout all operations Pass

Changes made during review

  1. Rebased onto main (resolved .secrets.baseline merge conflict)
  2. Added 5 unit tests for 100% differential coverage of new Python code
  3. Fixed table reload to preserve pagination, search, tag filter, include_inactive, and team_id scope via buildTableUrl + getCurrentTeamId()

…dGateways

Address three issues found by Codex review:

1. Filter out REST/A2A sentinel ("null") from gateway IDs before posting
   to /gateways/{id}/tools/refresh, preventing spurious 404 failures.

2. Replace broken htmx.trigger(container, "load") with a call to
   reloadAssociatedItems() which uses htmx.ajax and correctly targets
   the active form container (edit modal vs create form).

3. Fix create-form reload targeting the wrong (hidden edit-modal)
   container by delegating to reloadAssociatedItems() which checks
   isEditModalOpen before choosing the container.

Signed-off-by: Mihai Criveti <[email protected]>
@crivetimihai

crivetimihai commented Apr 1, 2026 •

Copy link
Copy Markdown
Member

code review fixes — c347e6df6

Addressed all three issues from the code code review:

1. REST/A2A sentinel causes spurious 404 (non-blocking → fixed)
getSelectedGatewayIds() returns "null" for the REST/A2A checkbox, which was sent as POST /gateways/null/tools/refresh → 404. Now filtered out before issuing requests.

2. Tools list not reloading after refresh (blocking → fixed)
htmx.trigger(container, "load") did not re-trigger the HTMX fetch (confirmed via Playwright network capture — zero tools/partial requests after click). Replaced with a call to the existing reloadAssociatedItems() function which uses htmx.ajax("GET", ...) and properly reloads tools, resources, and prompts.

3. Create-form refresh targets wrong container (blocking → fixed)
safeGetElement("edit-server-tools", true) always finds the hidden edit-modal container (it exists in the DOM even when hidden), so the create-form's #associatedTools was never targeted. reloadAssociatedItems() correctly checks !editModal.classList.contains("hidden") to pick the active container.

@crivetimihai crivetimihai left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approved after addressing all code review findings. Four commits total:

  1. 2596ac582 — Original feature (rebased onto main)
  2. e2e37402b — Differential tests for tool_count
  3. 9a8690b10 — Preserve table state (pagination, search, tags, team_id) on refresh
  4. c347e6df6 — Fix tools reload (htmx.trigger→reloadAssociatedItems), create-form container targeting, and REST/A2A null sentinel filtering

@crivetimihai crivetimihai left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Final approval — all fixes verified against live deployment

Re-tested all three code findings against freshly deployed localhost:8080 with Playwright network capture:

Test Result
Overflow menu + Refresh Tools + table reload preserving state PASS
Edit modal "Refresh from MCPs" → tools list reloads via reloadAssociatedItems() PASS (previously broken)
REST/A2A null sentinel filtered — shows friendly error PASS (previously sent POST /gateways/null/tools/refresh)
Create-form targets correct #associatedTools container PASS (previously targeted hidden #edit-server-tools)
Zero JS console errors across all operations PASS

@crivetimihai

crivetimihai commented Apr 1, 2026 •

Copy link
Copy Markdown
Member

All three code review findings verified fixed against fresh deployment at localhost:8080 via Playwright with network capture. Tools list now properly reloads after refresh, REST/A2A sentinel is filtered, and create-form targets the correct container. Zero console errors. Approved.

@crivetimihai
crivetimihai merged commit 6c567f7 into main Apr 1, 2026
36 checks passed
@crivetimihai
crivetimihai deleted the 3765-mcp-refresh-button branch April 1, 2026 20:38
crivetimihai added a commit that referenced this pull request Apr 3, 2026
The delete_gateway_by_url method was missing the _open_action_dropdown
call added by the previous commit. Since PR #3802 moved all action
buttons (including Delete) inside an Alpine.js dropdown menu, the
delete button is not directly clickable without first opening the menu.

Closes #3982

Signed-off-by: Mihai Criveti <[email protected]>
crivetimihai added a commit that referenced this pull request Apr 3, 2026
…ject

Three test files directly click gateway action buttons without going
through the GatewaysPage page object, so they were also broken by
PR #3802's move to the Alpine.js overflow menu:

- test_rbac_permissions.py: test_admin_delete_gateway bypassed page
  object to inspect HTTP response status directly
- test_admin_url_context.py: _get_delete_gateway_btn helper used by
  multiple URL-context tests and iframe delete tests
- test_iframe_embedding_security.py: delete and toggle tests operated
  directly on iframe FrameLocator

Each now opens the actions dropdown before clicking the menu item.

Signed-off-by: Mihai Criveti <[email protected]>
crivetimihai added a commit that referenced this pull request Apr 3, 2026
* fix: Fix playwright gateway action tests

Signed-off-by: Gabriel Costa <[email protected]>

* fix: add missing _open_action_dropdown call in delete_gateway_by_url

The delete_gateway_by_url method was missing the _open_action_dropdown
call added by the previous commit. Since PR #3802 moved all action
buttons (including Delete) inside an Alpine.js dropdown menu, the
delete button is not directly clickable without first opening the menu.

Closes #3982

Signed-off-by: Mihai Criveti <[email protected]>

* fix: open action dropdown in remaining test files that bypass page object

Three test files directly click gateway action buttons without going
through the GatewaysPage page object, so they were also broken by
PR #3802's move to the Alpine.js overflow menu:

- test_rbac_permissions.py: test_admin_delete_gateway bypassed page
  object to inspect HTTP response status directly
- test_admin_url_context.py: _get_delete_gateway_btn helper used by
  multiple URL-context tests and iframe delete tests
- test_iframe_embedding_security.py: delete and toggle tests operated
  directly on iframe FrameLocator

Each now opens the actions dropdown before clicking the menu item.

Signed-off-by: Mihai Criveti <[email protected]>

---------

Signed-off-by: Gabriel Costa <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
Co-authored-by: Mihai Criveti <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request release-fix Critical bugfix required for the release SHOULD P2: Important but not vital; high-value items that are not crucial for the immediate release ui User Interface wxo wxo integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE][UI]: MCP tool refresh button

5 participants