Skip to content

fix(runtime): index approved tools immediately after approval + upstream refresh op - #876

Merged
Dumbris merged 5 commits into
mainfrom
fix/873-reindex-on-approval
Jul 21, 2026
Merged

Dumbris merged 5 commits into
mainfrom
fix/873-reindex-on-approval

Conversation

@Dumbris

@Dumbris Dumbris commented Jul 21, 2026

Copy link
Copy Markdown
Member

Fixes #873

ApproveTools/ApproveAllTools only wrote approval records — indexing happens exclusively in applyDifferentialToolUpdate, reached only from discovery. So a just-approved tool was immediately callable but invisible to retrieve_tools/describe_tool until the next sweep (default 5 min; indefinitely with sweeps disabled). Reproduced live with a rug-pull test server.

Change

  • reindexServerToolsAfterApprovalChange (async, AppContext-scoped) runs after ApproveTools/BlockTools/SetToolEnabled changes: takes the last-good tool snapshot (now also populated by the single-server discovery path) and re-runs applyDifferentialToolUpdate — so profile indexes and the spec-085 signature cache stay consistent for free, and block/disable now removes tools promptly too.
  • Security guard (mandatory): the reindex bails unless the server is enabled and not quarantined — applyDifferentialToolUpdate itself never checks server quarantine, so an unguarded call would index a quarantined server's withheld tool descriptions. DiscoverAndIndexToolsForServer got the same guard (review found the new refresh op could otherwise index a quarantined server, and was reachable by non-admin agent tokens — both closed).
  • Operator recovery (issue's ask): new upstream_servers operation refresh (pure rediscover+reindex, no security-state change) + REST POST /api/v1/servers/{id}/refresh; the operation enum also now advertises the previously-undocumented enable/disable/restart.
  • Spec-085 golden snapshot updated surgically for the enum change (delta assertions preserved).

Verification

  • Live app (rug-pull server, tool_discovery_interval=30m so no sweep could mask the result): pending tool invisible → approve_all_tools → visible to retrieve_tools/describe_tool within seconds; refresh op works via MCP and REST; quarantined-server probe confirms tools stay withheld.
  • New tests: approve→indexed, block→removed, disable→removed, quarantined-server-not-indexed (security), full quarantine-flow reindex. go test -race green, API E2E green, lint clean.

Draft: @neylwalecki mentioned having a patch for this — happy to reconcile.

Dumbris added 3 commits July 21, 2026 09:15
…eam refresh op (#873)

Approval mutations (ApproveTools/ApproveAllTools/BlockTools/SetToolEnabled and
their bulk variants) only wrote BBolt records and emitted events. Indexing
happened solely in applyDifferentialToolUpdate, reached from discovery (boot
sweep, 5-min periodic sweep, connect, tools/list_changed) — never from approval.
So an approved tool was callable immediately (call-time gates read BBolt live)
but invisible to retrieve_tools/describe_tool until the next sweep; blocked/
disabled tools lingered in the index just as long.

Add reindexServerToolsAfterApprovalChange, triggered async from the approval/
block/enable paths, which re-runs applyDifferentialToolUpdate against the last-
good tool snapshot. A SECURITY-CRITICAL guard returns early unless the server
exists, is enabled, and is NOT quarantined — applyDifferentialToolUpdate does
not itself withhold a quarantined server's tools, so an unguarded call would
index descriptions the search-side quarantine model deliberately withholds.
DiscoverAndIndexToolsForServer now also populates lastGoodTools so single-server
connects have a snapshot; the reindex falls back to a full rediscover otherwise.

Add operator recovery op: upstream_servers "refresh" (pure rediscover+reindex,
no security-state change) and REST POST /api/v1/servers/{id}/refresh (alias of
discover-tools). Also fix pre-existing enum drift: enable/disable/restart were
dispatched but not advertised in the operation enum — now listed alongside
refresh. refresh is intentionally left out of the agent-token block list (it
mutates no config/security state).

The unquarantine path already schedules a full reindex via
HandleUpstreamServerChange, so the extra differential pass from
approveBaselineToolsForServer is a cheap no-op (and a no-op under the guard while
the server is still quarantined).
… regen golden (#873)

The upstream_servers 'refresh' op (and the reactive discovery callbacks)
reach DiscoverAndIndexToolsForServer, which fed applyDifferentialToolUpdate
with no quarantine/enabled guard — the same gap reindexServerToolsAfterApprovalChange
already defends. A refresh on a still-connected quarantined server therefore
re-indexed its (possibly TPA-poisoned) tool descriptions into retrieve_tools/
describe_tool. Extract serverEligibleForIndexing (exists+enabled+!quarantined)
and gate DiscoverAndIndexToolsForServer on it; unquarantine reindexes via the
full sweep, so newly-trusted servers are unaffected.

Also block 'refresh' for non-admin agent tokens (defense-in-depth, matching
enable/disable/restart), and regenerate the Spec-085 pre-feature golden's
upstream_servers entry for the intended enable/disable/restart/refresh enum +
description change so TestMenuSurface_ExactDeltaFromPreFeature passes.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

Deploying mcpproxy-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: e1ee5e4
Status: ✅  Deploy successful!
Preview URL: https://b1e1f46c.mcpproxy-docs.pages.dev
Branch Preview URL: https://fix-873-reindex-on-approval.mcpproxy-docs.pages.dev

View logs

@codecov-commenter

codecov-commenter commented Jul 21, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 55.37190% with 54 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/runtime/lifecycle.go 48.57% 18 Missing ⚠️
internal/server/mcp.go 14.28% 18 Missing ⚠️
internal/runtime/tool_quarantine.go 66.66% 8 Missing and 4 partials ⚠️
internal/httpapi/server.go 82.14% 4 Missing and 1 partial ⚠️
internal/server/server.go 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: fix/873-reindex-on-approval

Available Artifacts

  • archive-darwin-amd64 (28 MB)
  • archive-darwin-arm64 (25 MB)
  • archive-linux-amd64 (16 MB)
  • archive-linux-arm64 (15 MB)
  • archive-windows-amd64 (28 MB)
  • archive-windows-arm64 (25 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (22 MB)
  • installer-dmg-darwin-arm64 (20 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 29826387029 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

Dumbris added 2 commits July 21, 2026 14:09
…empty refresh (#873)

Codex cross-review follow-ups on PR #876:

1. REST POST /servers/{id}/refresh accepted agent tokens while the MCP
   upstream_servers 'refresh' op blocks them. Gate the REST alias with the
   strictest sibling pattern (admin-only), matching handleSetToolEnabled, so
   agents cannot bypass the MCP restriction over HTTP.

2. TOCTOU: single-server discovery and the approval-driven reindex checked
   eligibility before the seconds-wide ListTools / snapshot diff, so a server
   quarantined mid-flight could have its tools written back into the index after
   quarantine already deleted them. Re-check eligibility immediately before the
   index write and bail if ineligible (a microsecond window remains, documented).

3. A successful discovery returning zero tools exited early without reconciling
   the index or last-good snapshot, so an explicit refresh reported success while
   stale tools stayed searchable and a later approval reindex could resurface
   them. Split the entry point: reactive discovery stays lenient (transient empty
   ignored); the explicit refresh/discover path (RefreshServerTools) treats zero
   tools as authoritative, clearing stale entries and the snapshot.

Regenerated oas/ for the new 403 response on the refresh endpoint.
…-tools (#873)

Codex round 2 on PR #876:

1. The discovery sweep (discoverAndIndexTools) still bypassed the eligibility
   guard at both index-write sites. The upstream manager already skips
   quarantined/disabled servers when listing, but the last-good fallback loop
   iterates all known servers and reapplies a connected server's snapshot — and
   a quarantined server stays connected keeping its pre-quarantine snapshot, so
   the sweep QuarantineServer triggers right after deleting the server's index
   entries would restore them. Route both sweep writes through a shared
   applyServerDiffIfEligible helper that re-checks eligibility immediately before
   the write.

2. REST POST /servers/{id}/discover-tools was still agent-token-accessible and
   is an alias reaching the same authoritative RefreshServerTools path, bypassing
   the /refresh admin gate. Gate it with the identical IsAdmin() check.

Regenerated oas/ for the new 403 on discover-tools.
@Dumbris

Dumbris commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

Cross-model review (Codex): clean after 3 rounds. R1: REST /refresh agent-token gate, quarantine TOCTOU, zero-tool refresh semantics. R2: eligibility guard on both full-sweep index writes (closing the quarantine→sweep-restores-tools path) and the same admin gate on /discover-tools. R3: ACCEPT. CI green except the main-broken CodeQL canary (Analyze (go), not required). Note: other pre-existing REST mutating endpoints (enable/disable/restart/quarantine) also lack agent-token gating — deserves a separate consistency pass.

@Dumbris
Dumbris marked this pull request as ready for review July 21, 2026 13:32
@Dumbris
Dumbris merged commit edf4866 into main Jul 21, 2026
37 of 38 checks passed
@Dumbris
Dumbris deleted the fix/873-reindex-on-approval branch July 21, 2026 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Approved tools should become discoverable without another discovery pass

2 participants