fix(mcp-oauth): robust port binding for callback server

Use port 0 fallback when findAvailablePort fails, read the actual bound
port from server.port. Tests refactored to use mock server when real
socket binding is unavailable in CI.

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
YeonGyu-Kim
2026-03-25 11:44:11 +09:00
parent 42fb2548d6
commit 78a3e985be
2 changed files with 93 additions and 36 deletions
+4 -3
View File
@@ -39,7 +39,7 @@ export async function findAvailablePort(startPort: number = DEFAULT_PORT): Promi
}
export async function startCallbackServer(startPort: number = DEFAULT_PORT): Promise<CallbackServer> {
const port = await findAvailablePort(startPort)
const requestedPort = await findAvailablePort(startPort).catch(() => 0)
let resolveCallback: ((result: OAuthCallbackResult) => void) | null = null
let rejectCallback: ((error: Error) => void) | null = null
@@ -55,7 +55,7 @@ export async function startCallbackServer(startPort: number = DEFAULT_PORT): Pro
}, TIMEOUT_MS)
const server = Bun.serve({
port,
port: requestedPort,
hostname: "127.0.0.1",
fetch(request: Request): Response {
const url = new URL(request.url)
@@ -93,9 +93,10 @@ export async function startCallbackServer(startPort: number = DEFAULT_PORT): Pro
})
},
})
const activePort = server.port ?? requestedPort
return {
port,
port: activePort,
waitForCallback: () => callbackPromise,
close: () => {
clearTimeout(timeoutId)