Skip to content

fix: defer SetClientTools until after DB persistence to prevent runtime/DB state drift - #5972

Merged
Pratham-Mishra04 merged 1 commit into
devfrom
08-08-fix_persist_discovered_tools_before_triggering_cluster_propagation_in_verify-headers_verify-exchange_to_close_a_stale-read_race
Aug 8, 2026
Merged

fix: defer SetClientTools until after DB persistence to prevent runtime/DB state drift#5972
Pratham-Mishra04 merged 1 commit into
devfrom
08-08-fix_persist_discovered_tools_before_triggering_cluster_propagation_in_verify-headers_verify-exchange_to_close_a_stale-read_race

Conversation

@Pratham-Mishra04

Copy link
Copy Markdown
Collaborator

Summary

SetClientTools was being called before the discovered tools were persisted to the database. This created a window where the runtime had tools live but the DB row did not, causing state drift between the two. If persistence then failed, the runtime would be serving tools that the DB had no record of, making retries and restarts inconsistent.

Changes

  • Moved SetClientTools to run strictly after UpdateMCPClientConfig succeeds in both verifyMCPClientHeaders and verifyMCPClientExchange
  • When persistence fails, the runtime client is now activated but has no tools live — matching the empty DB state — so both sides agree rather than drifting
  • Updated comments and error messages to accurately reflect the new partial-failure behavior: "no tools live yet, retry to re-run verification" rather than "runtime and database state have drifted"
  • The ordering guarantee matters because SetClientTools may propagate updates to other observers of the DB row; it must run after the write lands to avoid a propagated read racing ahead of the write

Type of change

  • Bug fix
  • Feature
  • Refactor
  • Documentation
  • Chore/CI

Affected areas

  • Core (Go)
  • Transports (HTTP)
  • Providers/Integrations
  • Plugins
  • UI (React)
  • Docs

How to test

Trigger MCP client verification via both the headers and exchange endpoints. Simulate a DB persistence failure after activation and confirm that the runtime client has no tools live (matching the empty DB row) rather than serving tools that were never persisted. Confirm that retrying the endpoint re-runs verification and correctly persists and activates tools.

go test ./...

Breaking changes

  • Yes
  • No

Security considerations

None.

Checklist

  • I read docs/contributing/README.md and followed the guidelines
  • I added/updated tests where appropriate
  • I updated documentation where needed
  • I verified builds succeed (Go and UI)
  • I verified the CI pipeline passes locally if applicable

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

This was referenced Aug 8, 2026

Pratham-Mishra04 commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Improved tool discovery reliability during header and token-exchange verification.
    • Prevented runtime tool state from diverging when persistence fails.
    • Added clearer partial-success messages, including guidance to retry after failures.

Walkthrough

Header and token-exchange verification now activate MCP clients before tool persistence. Runtime tools are applied only after database persistence succeeds. Persistence errors report aligned state and support clean retries.

Changes

MCP tool synchronization

Layer / File(s) Summary
Header verification sequencing
transports/bifrost-http/handlers/mcp.go
Header verification activates the client before persistence. SetClientTools runs only after a successful database update. Persistence errors describe the empty live-tool state and retry behavior.
Token-exchange verification sequencing
transports/bifrost-http/handlers/mcp.go
Token-exchange verification removes the pre-persistence SetClientTools call. Persistence errors report aligned runtime and database tool state. Tools apply after successful persistence.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • maximhq/bifrost#3874: Extends the MCP header-verification flow with client activation, tool persistence, and runtime state updates.
  • maximhq/bifrost#5717: Modifies MCP verification and tool persistence sequencing in the same handler.
  • maximhq/bifrost#5724: Introduces the token-exchange verification flow refined by this change.

Suggested reviewers: akshaydeo, danpiths

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the main ordering change that prevents runtime and database tool state drift.
Description check ✅ Passed The description covers the purpose, changes, scope, testing guidance, breaking changes, security, and checklist sections; omitted optional sections are non-critical.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 08-08-fix_persist_discovered_tools_before_triggering_cluster_propagation_in_verify-headers_verify-exchange_to_close_a_stale-read_race

Comment @coderabbitai help to get the list of available commands.

Pratham-Mishra04 commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator Author

Merge activity

  • Aug 8, 10:33 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Aug 8, 11:43 AM UTC: Graphite rebased this pull request as part of a merge.
  • Aug 8, 11:44 AM UTC: @Pratham-Mishra04 merged this pull request with Graphite.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@transports/bifrost-http/handlers/mcp.go`:
- Around line 496-516: Reconcile persisted tools after UpdateMCPClientConfig
errors in both verification paths at
transports/bifrost-http/handlers/mcp.go:496-516 and
transports/bifrost-http/handlers/mcp.go:670-690: reload the client, and when
persisted tools and DiscoveredToolNameMapping exist, pass them to SetClientTools
before completing the error flow. In the header replay-success branch, hydrate
runtime tools before returning. Do not rely on UpdateClient to copy discovered
tools.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 0fd298bc-d2ba-48ff-9090-bf6b55c51585

📥 Commits

Reviewing files that changed from the base of the PR and between 425d315 and cbcaf6b.

📒 Files selected for processing (1)
  • transports/bifrost-http/handlers/mcp.go

Comment on lines 496 to +516
if err := h.store.ConfigStore.UpdateMCPClientConfig(ctx, clientConfig.ID, updateReq); err != nil {
// NOTE: Partial success; the runtime client is connected and serving
// but the DB row still has no discovered tools, so runtime and
// database state have drifted. The replay guard reads DiscoveredTools
// from the DB, so retrying this endpoint re-runs verification and
// reconverges the two; a restart re-parks the client in
// pending_verification.
// NOTE: Partial success; the runtime client is activated (Healthy)
// but — since SetClientTools runs below, after this succeeds — has
// no tools live either, so runtime and DB agree (both empty) rather
// than drifting. The replay guard reads DiscoveredTools from the
// DB, so retrying this endpoint re-runs verification cleanly; a
// restart re-parks the client in pending_verification.
logger.Error(fmt.Sprintf(
"[PARTIAL SUCCESS] MCP client %s was activated after headers verification but persisting discovered tools failed: %v. "+
"Runtime and database state have drifted: the client is connected now but will return to pending_verification on restart. "+
"Retry this endpoint to re-verify and persist.",
"The client is connected but has no tools live yet; retry this endpoint to re-run verification and persist.",
clientConfig.ID, err,
))
SendError(ctx, fasthttp.StatusInternalServerError, fmt.Sprintf("Client activated but discovered tools could not be persisted, so runtime and database state have drifted until re-verified. Retry this endpoint to re-run verification and persist: %v", err))
SendError(ctx, fasthttp.StatusInternalServerError, fmt.Sprintf("Client activated but discovered tools could not be persisted. Retry this endpoint to re-run verification and persist: %v", err))
return
}
// Set discovered tools on the client after persistence succeeds — not
// before. SetClientTools may propagate this update to other observers
// of the DB row (deployment-specific), so it must run strictly after
// the write above lands — otherwise a propagated update could be read
// back before the row it depends on was actually written.
h.mcpManager.SetClientTools(clientConfig.ID, tools, toolNameMapping)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -eu
printf '%s\n' '--- candidate files ---'
fd -i 'mcp|config.*store|store' . | head -200
printf '%s\n' '--- relevant symbols ---'
rg -n --glob '*.go' 'UpdateMCPClientConfig|SetClientTools|DiscoveredTools|pending_verification|replay' .
printf '%s\n' '--- handler outline ---'
ast-grep outline transports/bifrost-http/handlers/mcp.go

Repository: maximhq/bifrost

Length of output: 50371


🏁 Script executed:

#!/bin/bash
set -eu
printf '%s\n' '--- handler sections ---'
sed -n '400,560p' transports/bifrost-http/handlers/mcp.go
sed -n '600,730p' transports/bifrost-http/handlers/mcp.go
printf '%s\n' '--- persistence implementations and interfaces ---'
rg -n -C 8 --glob '*.go' 'func .*UpdateMCPClientConfig|UpdateMCPClientConfig\(' .
printf '%s\n' '--- runtime tool implementations ---'
rg -n -C 10 --glob '*.go' 'func .*SetClientTools|SetClientTools\(' .
printf '%s\n' '--- replay branches ---'
rg -n -C 15 --glob '*.go' 'DiscoveredTools != nil|DiscoveredTools == nil|DiscoveredTools' transports/bifrost-http/handlers

Repository: maximhq/bifrost

Length of output: 50372


🏁 Script executed:

#!/bin/bash
set -eu
sed -n '400,560p' transports/bifrost-http/handlers/mcp.go
sed -n '600,730p' transports/bifrost-http/handlers/mcp.go
rg -n -C 8 --glob '*.go' 'func .*UpdateMCPClientConfig|UpdateMCPClientConfig\(' .
rg -n -C 10 --glob '*.go' 'func .*SetClientTools|SetClientTools\(' .
rg -n -C 15 --glob '*.go' 'DiscoveredTools != nil|DiscoveredTools == nil|DiscoveredTools' transports/bifrost-http/handlers

Repository: maximhq/bifrost

Length of output: 50372


🏁 Script executed:

#!/bin/bash
set -eu
printf '%s\n' '--- UpdateMCPClientConfig implementation ---'
sed -n '2241,2355p' framework/configstore/rdb.go
printf '%s\n' '--- MCP client handler entry and replay guards ---'
sed -n '250,430p' transports/bifrost-http/handlers/mcp.go
printf '%s\n' '--- runtime activation/update helper references ---'
rg -n -C 12 --glob '*.go' 'func .*updateMCPClientWithRetry|updateMCPClientWithRetry|func .*UpdateClient|func .*AddClient' transports/bifrost-http/handlers/mcp.go core/mcp/clientmanager.go
printf '%s\n' '--- SetClientTools implementation ---'
sed -n '1020,1095p' core/mcp/clientmanager.go
printf '%s\n' '--- focused tests about persistence failures/retries ---'
rg -n -C 12 --glob '*_test.go' 'persist|persistence|UpdateMCPClientConfig|verify-headers|verify-exchange|SetClientTools|pending_verification' transports/bifrost-http/handlers framework/configstore core/mcp | head -500

Repository: maximhq/bifrost

Length of output: 50371


🏁 Script executed:

#!/bin/bash
set -eu
sed -n '2241,2355p' framework/configstore/rdb.go
sed -n '250,430p' transports/bifrost-http/handlers/mcp.go
rg -n -C 12 --glob '*.go' 'func .*updateMCPClientWithRetry|updateMCPClientWithRetry|func .*UpdateClient|func .*AddClient' transports/bifrost-http/handlers/mcp.go core/mcp/clientmanager.go
sed -n '1020,1095p' core/mcp/clientmanager.go
rg -n -C 12 --glob '*_test.go' 'persist|persistence|UpdateMCPClientConfig|verify-headers|verify-exchange|SetClientTools|pending_verification' transports/bifrost-http/handlers framework/configstore core/mcp | head -500

Repository: maximhq/bifrost

Length of output: 50371


🏁 Script executed:

#!/bin/bash
set -eu
printf '%s\n' '--- remainder of database transaction ---'
sed -n '2350,2415p' framework/configstore/rdb.go
printf '%s\n' '--- UpdateClient complete body ---'
sed -n '1329,1465p' core/mcp/clientmanager.go
printf '%s\n' '--- AddClient tool/state initialization ---'
sed -n '461,690p' core/mcp/clientmanager.go
printf '%s\n' '--- exact handler replay and activation lines ---'
nl -ba transports/bifrost-http/handlers/mcp.go | sed -n '425,525p'
nl -ba transports/bifrost-http/handlers/mcp.go | sed -n '615,705p'
printf '%s\n' '--- focused persistence tests ---'
rg -n --glob '*_test.go' 'UpdateMCPClientConfig|DiscoveredTools|verifyMCPClient|verify-headers|verify-exchange' framework/configstore/rdb_mcp_tools_test.go framework/configstore/rdb_mcp_admin_auth_mode_test.go transports/bifrost-http/handlers core/mcp | head -250

Repository: maximhq/bifrost

Length of output: 22574


🏁 Script executed:

#!/bin/bash
set -eu
sed -n '2350,2415p' framework/configstore/rdb.go
sed -n '1329,1465p' core/mcp/clientmanager.go
sed -n '461,690p' core/mcp/clientmanager.go
nl -ba transports/bifrost-http/handlers/mcp.go | sed -n '425,525p'
nl -ba transports/bifrost-http/handlers/mcp.go | sed -n '615,705p'
rg -n --glob '*_test.go' 'UpdateMCPClientConfig|DiscoveredTools|verifyMCPClient|verify-headers|verify-exchange' framework/configstore/rdb_mcp_tools_test.go framework/configstore/rdb_mcp_admin_auth_mode_test.go transports/bifrost-http/handlers core/mcp | head -250

Repository: maximhq/bifrost

Length of output: 22367


🏁 Script executed:

#!/bin/bash
set -eu
sed -n '2400,2475p' framework/configstore/rdb.go
sed -n '1400,1555p' core/mcp/clientmanager.go
sed -n '430,530p' transports/bifrost-http/handlers/mcp.go
sed -n '620,705p' transports/bifrost-http/handlers/mcp.go
python3 - <<'PY'
from pathlib import Path
handler = Path("transports/bifrost-http/handlers/mcp.go").read_text()
manager = Path("core/mcp/clientmanager.go").read_text()
rdb = Path("framework/configstore/rdb.go").read_text()

checks = {
    "update_client_does_not_copy_discovered_tools": "DiscoveredTools:" not in manager[manager.index("func (m *MCPManager) UpdateClient"):manager.index("func (m *MCPManager) UpdateClientCredentials")],
    "set_tools_sets_healthy": "client.State = schemas.MCPConnectionStateHealthy" in manager[manager.index("func (m *MCPManager) SetClientTools"):manager.index("func (m *MCPManager) RemoveClient")],
    "rdb_transaction": "return s.DB().Transaction(func(tx *gorm.DB) error" in rdb[rdb.index("func (s *RDBConfigStore) UpdateMCPClientConfig"):],
}
for name, value in checks.items():
    print(f"{name}={value}")
PY

Repository: maximhq/bifrost

Length of output: 21646


Reconcile the database result after UpdateMCPClientConfig errors.

A returned transaction error does not prove that the write rolled back. Reload the client and call SetClientTools with persisted tools and DiscoveredToolNameMapping when they exist. Apply this to both verification paths. In the header replay-success branch, hydrate the runtime tools before returning.

UpdateClient does not copy discovered tools, so the runtime can remain without tools while the database contains them.

📍 Affects 1 file
  • transports/bifrost-http/handlers/mcp.go#L496-L516 (this comment)
  • transports/bifrost-http/handlers/mcp.go#L670-L690
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@transports/bifrost-http/handlers/mcp.go` around lines 496 - 516, Reconcile
persisted tools after UpdateMCPClientConfig errors in both verification paths at
transports/bifrost-http/handlers/mcp.go:496-516 and
transports/bifrost-http/handlers/mcp.go:670-690: reload the client, and when
persisted tools and DiscoveredToolNameMapping exist, pass them to SetClientTools
before completing the error flow. In the header replay-success branch, hydrate
runtime tools before returning. Do not rely on UpdateClient to copy discovered
tools.

Source: Path instructions

@Pratham-Mishra04
Pratham-Mishra04 changed the base branch from 08-07-feat_persist_and_resync_mcp_tool_discoveries_uniformly_across_all_client_types_via_a_hash-gated_core_callback to graphite-base/5972 August 8, 2026 11:39
@Pratham-Mishra04
Pratham-Mishra04 changed the base branch from graphite-base/5972 to dev August 8, 2026 11:41
@Pratham-Mishra04
Pratham-Mishra04 requested a review from a team as a code owner August 8, 2026 11:41
…n verify-headers/verify-exchange to close a stale-read race
@Pratham-Mishra04
Pratham-Mishra04 force-pushed the 08-08-fix_persist_discovered_tools_before_triggering_cluster_propagation_in_verify-headers_verify-exchange_to_close_a_stale-read_race branch from cbcaf6b to 1341ebc Compare August 8, 2026 11:42
@Pratham-Mishra04
Pratham-Mishra04 merged commit 2d06da6 into dev Aug 8, 2026
14 checks passed
@Pratham-Mishra04
Pratham-Mishra04 deleted the 08-08-fix_persist_discovered_tools_before_triggering_cluster_propagation_in_verify-headers_verify-exchange_to_close_a_stale-read_race branch August 8, 2026 11:44
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.

2 participants