Skip to content

fix(mcp): keep last-known-good tools instead of flashing red on a transient failure#5839

Merged
waleedlatif1 merged 2 commits into
stagingfrom
fix/mcp-keep-last-known-tools
Jul 22, 2026
Merged

fix(mcp): keep last-known-good tools instead of flashing red on a transient failure#5839
waleedlatif1 merged 2 commits into
stagingfrom
fix/mcp-keep-last-known-tools

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

A connected MCP server with cached tools was showing red "Failed to discover MCP tools" on a single transient discovery probe — even though the backend stored status was connected with a valid tool count (verified in the DB while the UI showed red). It made healthy servers look like they "fail all the time" as probes oscillated against the origin's intermittent stalls.

Two coordinated client-side display fixes:

  • useMcpToolsQuery no longer discards React Query's retained data on error — it kept the last successful tools around and we were throwing them away (if (result.data && !result.isError)if (result.data)). The per-server error still rides in toolsStateByServer.
  • showDiscoveryError only hard-reds when there are genuinely no tools to show (tools.length === 0). A populated, connected server stays on its tool count through a transient blip; a persistent failure still reads as failed via the stored connectionStatus (gated by MAX_CONSECUTIVE_FAILURES).

Validated against primary sources before changing (I'd over-corrected this exact condition in #5829): the MCP spec's discover-once + tools/list_changed model, plus the actual behavior of Claude Code (which shipped this same fix as a bug removal — a transient failure no longer flags a healthy server), LibreChat (returns already-fetched tools rather than discarding; guards against overwriting with empty), OpenCode (if (!listed) return, leaves tools intact), and VS Code (persistent tool cache). None blanks a populated server on a transient tools/list failure over a live connection. This supersedes the #5829 over-correction.

Type of Change

  • Bug fix (UX / correctness of display)

Testing

  • New useMcpToolsQuery test: last-known-good tools survive a failing refetch (with the per-server error still exposed). 16 MCP query + settings tests green; tsc + biome clean.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

…nsient failure

A connected server with cached tools was turning red 'Failed to discover' on a
single transient discovery probe blip, even though the stored status stayed
connected with tools — because useMcpToolsQuery dropped React Query's retained
data on error, and the row's showDiscoveryError fired over a populated server.

Now the aggregate keeps last-known-good tools across a failed refetch, and the
row only hard-reds when there are genuinely no tools to show. A persistent
failure still surfaces via the stored connectionStatus (gated by
MAX_CONSECUTIVE_FAILURES), matching how Claude Code / LibreChat / OpenCode /
VS Code handle a transient tools/list failure on a live connection.

Validated against those clients' source + the MCP spec before changing; this
supersedes the #5829 over-correction that showed the error even when tools
existed.
@vercel

vercel Bot commented Jul 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Jul 22, 2026 6:16am

Request Review

@cursor

cursor Bot commented Jul 22, 2026

Copy link
Copy Markdown

PR Summary

Low Risk
Client-side MCP display and query aggregation only; no auth or API contract changes, with new unit tests locking behavior.

Overview
Fixes MCP settings and workflow tooling flashing red or dropping tools when a connected server’s latest tools/list probe fails but the backend still reports connected.

useMcpToolsQuery now keeps React Query’s retained per-server tool lists through a failed refetch while stored connectionStatus stays healthy, and only removes them from the aggregate once status is error or disconnected. Per-server errors still surface via toolsStateByServer.

MCP server list rows only treat a discovery error as a hard failure (showDiscoveryError) when there are no tools to show; servers with a last-known tool count keep the normal label until persistent failure updates status.

Tests cover transient refetch failure (tools preserved + error recorded) and persistent failure (stale tools cleared after list refresh shows error).

Reviewed by Cursor Bugbot for commit b65d49c. Configure here.

@greptile-apps

greptile-apps Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR keeps cached MCP tools visible during transient discovery failures. The main changes are:

  • Preserve React Query's last successful tool data after a failed refetch.
  • Show discovery errors only when no cached tools are available.
  • Remove cached tools from the aggregate when the stored connection status indicates failure.
  • Add tests for transient and persistent discovery failures.

Confidence Score: 5/5

No additional blocking issue qualifies for this follow-up review.

  • The latest changes address the previously reported stale-tool behavior.
  • No separate production failure requiring another comment was identified.

Important Files Changed

Filename Overview
apps/sim/hooks/queries/mcp.ts Preserves cached tools during transient failures and filters them using the stored server status.
apps/sim/app/workspace/[workspaceId]/settings/components/mcp/mcp.tsx Keeps the tool count visible when a transient discovery error occurs and cached tools remain.
apps/sim/hooks/queries/mcp.test.tsx Adds coverage for retaining cached tools after a failed refetch and dropping them after a stored failure status.

Reviews (2): Last reviewed commit: "fix(mcp): drop stale tools once a server..." | Re-trigger Greptile

Comment thread apps/sim/hooks/queries/mcp.ts Outdated
… on transient

Sharpens the last-known-good behavior with the transient-vs-persistent distinction
the reference clients make: keep cached tools through a transient discovery failure
(stored status still healthy) so a populated server doesn't blank, but drop them
once the stored connectionStatus crosses its failure threshold to error/disconnected
— so the shared aggregate the workflow editor consumes stops offering a dead
server's stale tool schemas.
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit b65d49c. Configure here.

@waleedlatif1
waleedlatif1 merged commit c708add into staging Jul 22, 2026
20 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/mcp-keep-last-known-tools branch July 22, 2026 06:29
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.

1 participant