Skip to content

The agent that grows with you

fix(mcp): seed OAuth disk-watch baseline before reload

LeonSGP43374ca053875d
diff --git a/tests/tools/test_mcp_oauth_integration.py b/tests/tools/test_mcp_oauth_integration.py
index 8489548196b..d5ac922c080 100644
--- a/tests/tools/test_mcp_oauth_integration.py
+++ b/tests/tools/test_mcp_oauth_integration.py
@@ -77,7 +77,9 @@ async def test_external_refresh_picked_up_without_restart(tmp_path, monkeypatch)
# automatically via the HermesMCPOAuthProvider.async_auth_flow
# pre-hook on the first real request, but we exercise it directly
# here for test determinism).
- await mgr.invalidate_if_disk_changed("srv")
+ baseline_changed = await mgr.invalidate_if_disk_changed("srv")
+ assert baseline_changed is False
+ assert provider._initialized is True
# EXTERNAL PROCESS: cron rewrites the tokens file with fresh creds.
# The old refresh_token has been consumed by this external exchange.
diff --git a/tests/tools/test_mcp_oauth_manager.py b/tests/tools/test_mcp_oauth_manager.py
index f0063446287..78f7ac6aa2a 100644
--- a/tests/tools/test_mcp_oauth_manager.py
+++ b/tests/tools/test_mcp_oauth_manager.py
@@ -77,7 +77,7 @@ def _set_interactive_stdin(monkeypatch, *, is_tty: bool = True) -> None:
@pytest.mark.asyncio
async def test_disk_watch_invalidates_on_mtime_change(tmp_path, monkeypatch):
- """When the tokens file mtime changes, provider._initialized flips False.
+ """When the tokens file mtime changes after baseline, provider reloads.
This is the behaviour Claude Code ships as
invalidateOAuthCacheIfDiskChanged (CC-1096 / GH#24317) and is the core
@@ -99,10 +99,14 @@ async def test_disk_watch_invalidates_on_mtime_change(tmp_path, monkeypatch):
mgr = MCPOAuthManager()
provider = mgr.get_or_build_provider("srv", "https://example.com/mcp", None)
assert provider is not None
+ await provider._initialize()
+ assert provider._initialized is True
- # First call: records mtime (zero -> real) -> returns True
+ # First call only records the baseline mtime. Reloading here would reset
+ # the provider during its first auth handshake.
changed1 = await mgr.invalidate_if_disk_changed("srv")
- assert changed1 is True
+ assert changed1 is False
+ assert provider._initialized is True
# No file change -> False
changed2 = await mgr.invalidate_if_disk_changed("srv")
diff --git a/tools/mcp_oauth_manager.py b/tools/mcp_oauth_manager.py
index bf9507f4602..e40ff4663d2 100644
--- a/tools/mcp_oauth_manager.py
+++ b/tools/mcp_oauth_manager.py
@@ -355,6 +355,11 @@ class MCPOAuthManager:
if mtime_ns == entry.last_mtime_ns:
return False
old, entry.last_mtime_ns = entry.last_mtime_ns, mtime_ns
+ if old == 0:
+ # First observation only seeds the baseline. Forcing a
+ # reload here can reset the provider in the middle of its
+ # first auth handshake and tear down HTTP MCP discovery.
+ return False
# `_initialized` is private SDK API but stable across the pinned versions (>=1.26.0).
if hasattr(entry.provider, "_initialized"):
entry.provider._initialized = False # noqa: SLF001