Skip to content

Commit 09c80d6

Browse files
Answer late MCP OAuth callback requests
After the callback wait ends, the listener ignored a new request and sent no response. The connection stayed open, so stopCallbackServer() could not finish. This delayed close(), the OAuth error, and a retry. - Send 404 to a request that arrives after the callback wait ends. - Add a test that uses a real loopback listener.
1 parent f907cbe commit 09c80d6

2 files changed

Lines changed: 85 additions & 1 deletion

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import * as net from "net"
2+
3+
import { startCallbackServer, stopCallbackServer } from "../callbackServer"
4+
5+
// This spec uses a real loopback listener. callbackServer.spec.ts mocks the http module.
6+
const STATE = "listener-state"
7+
const RESPONSE_WAIT_MS = 2_000
8+
9+
/** Opens a raw keep-alive connection, so that a request can reach the listener after the callback. */
10+
async function connectClient(port: number) {
11+
const socket = net.connect(port, "127.0.0.1")
12+
let received = ""
13+
socket.setEncoding("utf8")
14+
socket.on("data", (chunk: string) => {
15+
received += chunk
16+
})
17+
socket.on("error", () => {
18+
// The server can close the connection when it stops. The assertions check the responses.
19+
})
20+
await new Promise<void>((resolve, reject) => {
21+
socket.once("connect", resolve)
22+
socket.once("error", reject)
23+
})
24+
return {
25+
send(path: string) {
26+
socket.write(`GET ${path} HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: keep-alive\r\n\r\n`)
27+
},
28+
/** Waits until the client has received one response, and returns its status line. */
29+
async waitForResponse(): Promise<string | undefined> {
30+
let status: string | undefined
31+
await vi.waitFor(
32+
() => {
33+
status = received.match(/^HTTP\/1\.1 \d{3}/m)?.[0]
34+
expect(status).toBeDefined()
35+
},
36+
{ timeout: RESPONSE_WAIT_MS },
37+
)
38+
return status
39+
},
40+
destroy: () => socket.destroy(),
41+
}
42+
}
43+
44+
describe("startCallbackServer with a real listener", () => {
45+
it("should answer a request that arrives after the callback wait ends, so that the server can stop", async () => {
46+
const { server, port, result, cancel } = await startCallbackServer(0, STATE)
47+
const outcome = result.catch((error: unknown) => error)
48+
const client = await connectClient(port)
49+
let stopped = false
50+
try {
51+
// The listener no longer waits for a callback, but the connection is still open.
52+
cancel()
53+
expect(await outcome).toEqual(new Error("Callback cancelled"))
54+
55+
client.send(`/callback?code=late-code&state=${STATE}`)
56+
57+
expect(await client.waitForResponse()).toBe("HTTP/1.1 404")
58+
await expect(stopCallbackServer(server, cancel)).resolves.toBeUndefined()
59+
stopped = true
60+
} finally {
61+
client.destroy()
62+
if (!stopped) server.close()
63+
}
64+
})
65+
66+
it("should accept a callback and then stop", async () => {
67+
const { server, port, result, cancel } = await startCallbackServer(0, STATE)
68+
const client = await connectClient(port)
69+
try {
70+
client.send(`/callback?code=first-code&state=${STATE}`)
71+
72+
await expect(result).resolves.toMatchObject({ code: "first-code", state: STATE })
73+
expect(await client.waitForResponse()).toBe("HTTP/1.1 200")
74+
await expect(stopCallbackServer(server, cancel)).resolves.toBeUndefined()
75+
} finally {
76+
client.destroy()
77+
}
78+
})
79+
})

‎src/services/mcp/utils/callbackServer.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ export function startCallbackServer(
8484
}
8585

8686
server.on("request", (req: any, res: any) => {
87-
if (resolved) return
87+
if (resolved) {
88+
// Answer late requests so that their connections can close when the server stops.
89+
res.writeHead(404)
90+
res.end("Not found")
91+
return
92+
}
8893

8994
const url = new URL(req.url || "", `http://localhost:${actualPort}`)
9095
const pathname = url.pathname

0 commit comments

Comments
 (0)