Skip to content

Commit 8ab596d

Browse files
committed
test(mcp): exercise profile refresh through the SDK client
Let the SDK own transport callbacks and response correlation in the real profile-refresh fixture. Cover initialization and ping responses while keeping optional SSE probes separate from RPC POST ordering. Fix the hosted lint-suppression gate without expanding its allowlist, changing process deadlines, or adding a production test seam.
1 parent 6e08568 commit 8ab596d

1 file changed

Lines changed: 48 additions & 20 deletions

File tree

‎src/agents/mcp-auth-profile.integration.test-support.ts‎

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,36 @@ async function createHttpFixture() {
145145
}
146146
requests.push({ method: request.method, headers: request.headers, body });
147147
order.push("mcp");
148-
const message = body ? (JSON.parse(body) as { id?: number }) : undefined;
148+
const message = body
149+
? (JSON.parse(body) as {
150+
id?: number;
151+
method?: string;
152+
params?: { protocolVersion: string };
153+
})
154+
: undefined;
155+
// This JSON-only endpoint declines the SDK's optional SSE stream.
156+
if (request.method === "GET" && request.headers.accept === "text/event-stream") {
157+
response.statusCode = 405;
158+
response.setHeader("allow", "POST");
159+
response.end();
160+
return;
161+
}
162+
if (message?.method && message.id === undefined) {
163+
response.statusCode = 202;
164+
response.end();
165+
return;
166+
}
167+
const result =
168+
message?.method === "initialize"
169+
? {
170+
protocolVersion: message.params?.protocolVersion,
171+
capabilities: {},
172+
serverInfo: { name: "mcp-proof", version: "1.0.0" },
173+
}
174+
: {};
149175
response.end(
150176
JSON.stringify(
151-
message?.id === undefined ? { ok: true } : { jsonrpc: "2.0", id: message.id, result: {} },
177+
message?.id === undefined ? { ok: true } : { jsonrpc: "2.0", id: message.id, result },
152178
),
153179
);
154180
})().catch((error: unknown) => {
@@ -327,6 +353,7 @@ async function runRefreshScenario(root: string): Promise<void> {
327353
const { loadPersistedAuthProfileStore } = await import("./auth-profiles/persisted.js");
328354
const { resolveMcpBearerBundleConfig, withMcpAuthProfileBearer } =
329355
await import("./mcp-auth-profile.js");
356+
const { Client } = await import("@modelcontextprotocol/sdk/client/index.js");
330357
const { StreamableHTTPClientTransport } =
331358
await import("@modelcontextprotocol/sdk/client/streamableHttp.js");
332359
const endpoint = await createHttpFixture();
@@ -370,26 +397,27 @@ async function runRefreshScenario(root: string): Promise<void> {
370397
fetch: wrapped,
371398
requestInit: { headers: { "X-Request": "keep", Authorization: "Bearer stale-sdk-not-real" } },
372399
});
373-
const received: unknown[] = [];
374-
// MCP transports use callback properties, not EventTarget listeners.
375-
// oxlint-disable-next-line unicorn/prefer-add-event-listener
376-
transport.onmessage = (message) => received.push(message);
400+
const client = new Client({ name: "mcp-auth-proof", version: "1.0.0" });
401+
// Optional SSE GETs are independent of the ordered RPC POSTs under test.
402+
const mcpPosts = () => endpoint.requests.filter((request) => request.method === "POST");
377403
try {
378-
await transport.start();
379-
await transport.send({ jsonrpc: "2.0", id: 1, method: "ping" });
380-
await transport.send({ jsonrpc: "2.0", id: 2, method: "ping" });
404+
await client.connect(transport);
405+
assert.deepEqual(await client.ping(), {});
406+
assert.deepEqual(await client.ping(), {});
381407
assert.deepEqual(endpoint.refreshInputs, ["stored-refresh-not-real"]);
382-
assert.deepEqual(endpoint.order, ["refresh", "mcp", "mcp"]);
383-
assert.deepEqual(received, [
384-
{ jsonrpc: "2.0", id: 1, result: {} },
385-
{ jsonrpc: "2.0", id: 2, result: {} },
386-
]);
408+
assert.equal(endpoint.order[0], "refresh");
409+
assert.deepEqual(
410+
mcpPosts().map((request) => (JSON.parse(request.body) as { method: string }).method),
411+
["initialize", "notifications/initialized", "ping", "ping"],
412+
);
387413
for (const request of endpoint.requests) {
388-
assert.equal(request.method, "POST");
389414
assert.equal(request.headers.authorization, "Bearer rotated-access-not-real");
390415
assert.equal(request.headers["x-trace"], "keep");
391416
assert.equal(request.headers["x-request"], "keep");
392-
assert.equal(request.headers.accept, "application/json, text/event-stream");
417+
assert.equal(
418+
request.headers.accept,
419+
request.method === "POST" ? "application/json, text/event-stream" : "text/event-stream",
420+
);
393421
}
394422
const stored = loadPersistedAuthProfileStore(fixture.agentDir)?.profiles[STORED_PROFILE];
395423
assert(stored?.type === "oauth");
@@ -440,9 +468,9 @@ async function runRefreshScenario(root: string): Promise<void> {
440468
}
441469
seed();
442470
endpoint.behavior.rejectRefresh = true;
443-
const sent = endpoint.requests.length;
471+
const sent = mcpPosts().length;
444472
await assert.rejects(
445-
() => transport.send({ jsonrpc: "2.0", id: 3, method: "ping" }),
473+
() => client.ping(),
446474
(error: unknown) => {
447475
assert(error instanceof Error);
448476
assert.match(error.message, /fixture refresh denied/u);
@@ -451,11 +479,11 @@ async function runRefreshScenario(root: string): Promise<void> {
451479
return true;
452480
},
453481
);
454-
assert.equal(endpoint.requests.length, sent);
482+
assert.equal(mcpPosts().length, sent);
455483
assert(providerEvents.some((event) => event.kind === "refresh"));
456484
assert(providerEvents.some((event) => event.kind === "refreshed"));
457485
} finally {
458-
await transport.close();
486+
await client.close();
459487
await endpoint.close();
460488
}
461489
}

0 commit comments

Comments
 (0)