fix(mcp): refresh expired OAuth tokens automatically (#5573)
* fix(mcp): refresh expired OAuth tokens * fix(mcp): bind refreshed tokens to OAuth issuer * fix(mcp): activate OAuth tools before probe cleanup * fix(webui): preserve failed MCP OAuth status * fix(mcp): preserve OAuth refresh across discovery retries * test(mcp): align OAuth issuer fixture with SDK validation
This commit is contained in:
1 parent
6a8647cfb6
commit
55093dd75b
8 files changed
+1174
-60
No files matched your search
@@ -21,6 +21,18 @@ class _Connection:
|
||||
self.closed = True
|
||||
|
||||
|
||||
class _BlockingConnection:
|
||||
def __init__(self) -> None:
|
||||
self.close_started = asyncio.Event()
|
||||
self.allow_close = asyncio.Event()
|
||||
self.closed = False
|
||||
|
||||
async def aclose(self) -> None:
|
||||
self.close_started.set()
|
||||
await self.allow_close.wait()
|
||||
self.closed = True
|
||||
|
||||
|
||||
def _config() -> MCPServerConfig:
|
||||
return MCPServerConfig(
|
||||
type="streamableHttp",
|
||||
@@ -107,6 +119,62 @@ async def test_browser_flow_retries_current_server_and_ignores_unrelated_reload_
|
||||
assert connection.closed is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_browser_flow_activates_tools_before_probe_cleanup_completes(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
manager = McpOAuthManager()
|
||||
connection = _BlockingConnection()
|
||||
reload_started = asyncio.Event()
|
||||
|
||||
monkeypatch.setattr(
|
||||
"nanobot.webui.mcp_oauth_api.validate_url_target",
|
||||
lambda _url: (True, ""),
|
||||
)
|
||||
|
||||
async def connect(_servers, _registry, *, oauth_handlers):
|
||||
handlers = oauth_handlers["linear"]
|
||||
await handlers.redirect_handler(
|
||||
"https://accounts.example.com/authorize?client_id=test&state=blocked-close"
|
||||
)
|
||||
await handlers.callback_handler()
|
||||
return {"linear": connection}
|
||||
|
||||
async def reload_mcp() -> dict[str, object]:
|
||||
reload_started.set()
|
||||
return {
|
||||
"ok": True,
|
||||
"requires_restart": False,
|
||||
"connected": ["linear"],
|
||||
"failed": [],
|
||||
}
|
||||
|
||||
monkeypatch.setattr("nanobot.webui.mcp_oauth_api.connect_mcp_servers", connect)
|
||||
|
||||
started = await manager.start(
|
||||
"linear",
|
||||
_config(),
|
||||
"https://agent.example.com/auth/mcp/callback",
|
||||
reload_mcp=reload_mcp,
|
||||
)
|
||||
manager.submit_callback(state="blocked-close", code="oauth-code", error=None)
|
||||
|
||||
try:
|
||||
await asyncio.wait_for(reload_started.wait(), timeout=0.5)
|
||||
await asyncio.wait_for(connection.close_started.wait(), timeout=0.5)
|
||||
result = await manager.status(started["flow_id"])
|
||||
assert result["status"] == "connected"
|
||||
assert connection.closed is False
|
||||
finally:
|
||||
connection.allow_close.set()
|
||||
|
||||
for _ in range(10):
|
||||
await asyncio.sleep(0)
|
||||
if connection.closed:
|
||||
break
|
||||
assert connection.closed is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_remote_http_flow_accepts_a_pasted_loopback_callback(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
|
||||
@@ -184,6 +184,17 @@ async def test_oauth_preset_is_one_click_configured_after_token_storage(
|
||||
row = next(item for item in healthy["presets"] if item["name"] == "xmind")
|
||||
assert row["runtime_status"] == "connected"
|
||||
|
||||
await MCPOAuthStorage("xmind", cfg.url).clear_tokens()
|
||||
refresh_failed = mcp_presets_payload(runtime_status={"xmind": "failed"})
|
||||
row = next(item for item in refresh_failed["presets"] if item["name"] == "xmind")
|
||||
assert row["configured"] is False
|
||||
assert row["status"] == "authorization_required"
|
||||
assert row["runtime_status"] == "failed"
|
||||
|
||||
stale_connected = mcp_presets_payload(runtime_status={"xmind": "connected"})
|
||||
row = next(item for item in stale_connected["presets"] if item["name"] == "xmind")
|
||||
assert "runtime_status" not in row
|
||||
|
||||
mcp_presets_action("remove", {"name": ["xmind"]})
|
||||
assert await MCPOAuthStorage("xmind", cfg.url).get_tokens() is None
|
||||
|
||||
|
||||
Reference in new issue
Block a user