@@ -12,10 +12,11 @@ export const REASON_TRANSPORT_FALLBACK = 'falling-back-to-alternate-transport'
12
12
// Transport strategy types
13
13
export type TransportStrategy = 'sse-only' | 'http-only' | 'sse-first' | 'http-first'
14
14
import { OAuthCallbackServerOptions } from './types'
15
- import { readJsonFile } from './mcp-auth-config'
15
+ import { getConfigFilePath , readJsonFile } from './mcp-auth-config'
16
16
import express from 'express'
17
17
import net from 'net'
18
18
import crypto from 'crypto'
19
+ import fs from 'fs/promises'
19
20
20
21
// Package version from package.json
21
22
export const MCP_REMOTE_VERSION = require ( '../../package.json' ) . version
@@ -459,14 +460,23 @@ export async function parseCommandLineArgs(args: string[], defaultPort: number,
459
460
460
461
// Use the specified port, or the existing client port or fallback to find an available one
461
462
const [ existingClientPort , availablePort ] = await Promise . all ( [ findExistingClientPort ( serverUrl ) , findAvailablePort ( defaultPort ) ] )
462
- const callbackPort = specifiedPort || existingClientPort || availablePort
463
+ let callbackPort : number
463
464
464
465
if ( specifiedPort ) {
465
- log ( `Using specified callback port: ${ callbackPort } ` )
466
+ if ( existingClientPort && specifiedPort !== existingClientPort ) {
467
+ log (
468
+ `Warning! Specified callback port of ${ specifiedPort } , which conflicts with existing client registration port ${ existingClientPort } . Deleting existing client data to force reregistration.` ,
469
+ )
470
+ await fs . rm ( getConfigFilePath ( getServerUrlHash ( serverUrl ) , 'client_info.json' ) )
471
+ }
472
+ log ( `Using specified callback port: ${ specifiedPort } ` )
473
+ callbackPort = specifiedPort
466
474
} else if ( existingClientPort ) {
467
475
log ( `Using existing client port: ${ existingClientPort } ` )
476
+ callbackPort = existingClientPort
468
477
} else {
469
- log ( `Using automatically selected callback port: ${ callbackPort } ` )
478
+ log ( `Using automatically selected callback port: ${ availablePort } ` )
479
+ callbackPort = availablePort
470
480
}
471
481
472
482
if ( Object . keys ( headers ) . length > 0 ) {
0 commit comments