From b9ec3d269558b7c27c6a6ea870bfc987515d3d5c Mon Sep 17 00:00:00 2001 From: colorlesspilgrimage Date: Tue, 1 Dec 2026 03:03:08 -0600 Subject: [PATCH] connectors: a lent token that ran out marks the connector needs-sign-in, and a finished sign-in stamps its time for connector sync A place borrowing a connector's token never refreshes it. When that token ran out, refresh() returned null and left the entry as is, so every caller on the place only heard that access was unavailable, with nothing to tell the person or a bot to reconnect. It now marks the connector needs-sign-in, which borrow() already lets the holder's next token replace. Connector sync decides which sign-in is newer from times.signedIn, but nothing called sync.signedIn() when a sign-in completed, so the time was only the first moment a listing saw the sign-in. The sign-in completion callback now stamps it. Tests: a lent token that ran out marks needs-sign-in and takes the holder's next token (failed before, passes now); signedIn() restamps a holder's signedInAt. Co-Authored-By: Claude Sonnet 5.5 --- server/connector-sync.test.ts | 34 ++++++++++++++++++++++++++++++++++ server/index.ts | 2 +- server/mcp-oauth.test.ts | 9 +++++++++ server/mcp-oauth.ts | 4 ++-- 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 server/connector-sync.test.ts diff --git a/server/connector-sync.test.ts b/server/connector-sync.test.ts new file mode 100644 index 0000000000..029f979761 --- /dev/null +++ b/server/connector-sync.test.ts @@ -0,0 +1,34 @@ +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; + +import { createConnectorSync } from "./connector-sync.ts"; + +const URL = "https://connect.composio.example/mcp"; +let dir: string; +let clock: number; + +beforeEach(() => { dir = mkdtempSync(join(tmpdir(), "connector-sync-")); clock = 1_000; }); +afterEach(() => rmSync(dir, { recursive: true, force: true })); + +function holderSync() { + return createConnectorSync({ + dataDir: dir, + config: () => ({ mcpServers: { notion: { type: "http", url: URL, headers: {}, enabled: true } } }) as never, + mcpConfig: {} as never, + mcpOAuth: { syncRole: () => ({ role: "holder", access: "a1" }) } as never, + place: { id: "here", label: "Here" }, + now: () => clock, + }); +} + +describe("connector sync sign-in times", () => { + it("stamps a sign-in made here when it is made, not when sync first sees the connector", () => { + const sync = holderSync(); + expect(sync.list().servers[0]!.signIn.signedInAt).toBe(1_000); + clock = 5_000; + sync.signedIn(URL); + expect(sync.list().servers[0]!.signIn.signedInAt).toBe(5_000); + }); +}); diff --git a/server/index.ts b/server/index.ts index 224918a35a..e82171deca 100644 --- a/server/index.ts +++ b/server/index.ts @@ -1393,7 +1393,7 @@ let dashboardReporterRef: { syncChoice(): void; status(): { url: string | null } const mcpOAuth = new McpOAuthManager({ file: join(DATA_DIR, "mcp-oauth.json"), isOwnerLive: (owner) => owner === "loopback" || sessions.isLive(owner) || isCardOwnerLive(store, owner), - onSettled: (name, phase, owner) => { mcpSignInCards.settled(name, phase, owner); if (phase === "succeeded") { reconnectResume.reconnected(name); if (composioConnectServer(cfg.mcpServers)?.name === name) broadcast({ kind: "config", ...configStatus() }); } }, + onSettled: (name, phase, owner) => { mcpSignInCards.settled(name, phase, owner); if (phase === "succeeded") { const signedInTo = cfg.mcpServers?.[name]; const signedInUrl = signedInTo === undefined ? null : parseStoredMcpServer(name, signedInTo); if (signedInUrl?.ok && isRemoteMcpServer(signedInUrl.server)) connectorSyncRef?.signedIn(signedInUrl.server.url); reconnectResume.reconnected(name); if (composioConnectServer(cfg.mcpServers)?.name === name) broadcast({ kind: "config", ...configStatus() }); } }, // Read the registered app when needed; never copy its secret into the token store. clients: (name, url) => { const raw = cfg.mcpServers?.[name]; diff --git a/server/mcp-oauth.test.ts b/server/mcp-oauth.test.ts index 83c7bbdf49..1863e0f90e 100644 --- a/server/mcp-oauth.test.ts +++ b/server/mcp-oauth.test.ts @@ -204,6 +204,15 @@ describe("McpOAuthManager tokens", () => { expect(manager.authState("docs", mcp.url)).toBe("needs-sign-in"); }); + it("marks a lent token that ran out as needing sign-in, and takes the holder's next one", async () => { + await setup(); + manager.borrow("docs", mcp.url, { access: "lent-old", expiresAt: Date.now() - 1_000, from: "This computer", grantAt: 1 }); + expect(await manager.accessToken("docs", mcp.url)).toBeNull(); + expect(manager.authState("docs", mcp.url)).toBe("needs-sign-in"); + expect(manager.borrow("docs", mcp.url, { access: "lent-new", expiresAt: Date.now() + 3_600_000, from: "This computer", grantAt: 2 })).toBe("borrowed"); + expect(await manager.accessToken("docs", mcp.url)).toBe("lent-new"); + }); + it("never hands a token to a different URL", async () => { await signedIn(); expect(await manager.accessToken("docs", "https://elsewhere.example.com/mcp")).toBeNull(); diff --git a/server/mcp-oauth.ts b/server/mcp-oauth.ts index 37ad6f5958..bca1d7427c 100644 --- a/server/mcp-oauth.ts +++ b/server/mcp-oauth.ts @@ -459,8 +459,8 @@ export class McpOAuthManager { const client = this.recordClient(name, record); if (!tokens.refresh || !record.tokenEndpoint || !client) { if (stillValid) return tokens.access; - // a lent token that ran out waits for the next one from its holder: it was never this place's to refresh - if (record.lent) return null; + // a lent token is never this place's to refresh: the holder's next one replaces this entry (borrow), which + // needs-sign-in lets through, and until then callers hear that this place has no access this.markNeedsSignIn(name, record.url); return null; }