Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions server/connector-sync.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 1 addition & 1 deletion server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
9 changes: 9 additions & 0 deletions server/mcp-oauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions server/mcp-oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading