Merge 564983fea5
into 504aa26761
This commit is contained in:
commit
186bf2a288
3 changed files with 11 additions and 14 deletions
|
@ -28,9 +28,6 @@ async function runClient(serverUrl: string, callbackPort: number, headers: Recor
|
||||||
// Get the server URL hash for lockfile operations
|
// Get the server URL hash for lockfile operations
|
||||||
const serverUrlHash = getServerUrlHash(serverUrl)
|
const serverUrlHash = getServerUrlHash(serverUrl)
|
||||||
|
|
||||||
// Coordinate authentication with other instances
|
|
||||||
const { server, waitForAuthCode, skipBrowserAuth } = await coordinateAuth(serverUrlHash, callbackPort, events)
|
|
||||||
|
|
||||||
// Create the OAuth client provider
|
// Create the OAuth client provider
|
||||||
const authProvider = new NodeOAuthClientProvider({
|
const authProvider = new NodeOAuthClientProvider({
|
||||||
serverUrl,
|
serverUrl,
|
||||||
|
@ -38,6 +35,10 @@ async function runClient(serverUrl: string, callbackPort: number, headers: Recor
|
||||||
clientName: 'MCP CLI Client',
|
clientName: 'MCP CLI Client',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Coordinate authentication with other instances
|
||||||
|
const hasSavedTokens = !!(await authProvider.tokens())
|
||||||
|
const { server, waitForAuthCode, skipBrowserAuth } = await coordinateAuth(serverUrlHash, callbackPort, events, hasSavedTokens)
|
||||||
|
|
||||||
// If auth was completed by another instance, just log that we'll use the auth from disk
|
// If auth was completed by another instance, just log that we'll use the auth from disk
|
||||||
if (skipBrowserAuth) {
|
if (skipBrowserAuth) {
|
||||||
log('Authentication was completed by another instance - will use tokens from disk...')
|
log('Authentication was completed by another instance - will use tokens from disk...')
|
||||||
|
|
|
@ -25,13 +25,6 @@ export async function isPidRunning(pid: number): Promise<boolean> {
|
||||||
* @returns True if the lockfile is valid, false otherwise
|
* @returns True if the lockfile is valid, false otherwise
|
||||||
*/
|
*/
|
||||||
export async function isLockValid(lockData: LockfileData): Promise<boolean> {
|
export async function isLockValid(lockData: LockfileData): Promise<boolean> {
|
||||||
// Check if the lockfile is too old (over 30 minutes)
|
|
||||||
const MAX_LOCK_AGE = 30 * 60 * 1000 // 30 minutes
|
|
||||||
if (Date.now() - lockData.timestamp > MAX_LOCK_AGE) {
|
|
||||||
log('Lockfile is too old')
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the process is still running
|
// Check if the process is still running
|
||||||
if (!(await isPidRunning(lockData.pid))) {
|
if (!(await isPidRunning(lockData.pid))) {
|
||||||
log('Process from lockfile is not running')
|
log('Process from lockfile is not running')
|
||||||
|
@ -93,12 +86,14 @@ export async function waitForAuthentication(port: number): Promise<boolean> {
|
||||||
* @param serverUrlHash The hash of the server URL
|
* @param serverUrlHash The hash of the server URL
|
||||||
* @param callbackPort The port to use for the callback server
|
* @param callbackPort The port to use for the callback server
|
||||||
* @param events The event emitter to use for signaling
|
* @param events The event emitter to use for signaling
|
||||||
|
* @param hasSavedTokens Whether the client has saved tokens
|
||||||
* @returns An object with the server, waitForAuthCode function, and a flag indicating if browser auth can be skipped
|
* @returns An object with the server, waitForAuthCode function, and a flag indicating if browser auth can be skipped
|
||||||
*/
|
*/
|
||||||
export async function coordinateAuth(
|
export async function coordinateAuth(
|
||||||
serverUrlHash: string,
|
serverUrlHash: string,
|
||||||
callbackPort: number,
|
callbackPort: number,
|
||||||
events: EventEmitter,
|
events: EventEmitter,
|
||||||
|
hasSavedTokens: boolean,
|
||||||
): Promise<{ server: Server; waitForAuthCode: () => Promise<string>; skipBrowserAuth: boolean }> {
|
): Promise<{ server: Server; waitForAuthCode: () => Promise<string>; skipBrowserAuth: boolean }> {
|
||||||
// Check for a lockfile (disabled on Windows for the time being)
|
// Check for a lockfile (disabled on Windows for the time being)
|
||||||
const lockData = process.platform === 'win32' ? null : await checkLockfile(serverUrlHash)
|
const lockData = process.platform === 'win32' ? null : await checkLockfile(serverUrlHash)
|
||||||
|
@ -109,7 +104,7 @@ export async function coordinateAuth(
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Try to wait for the authentication to complete
|
// Try to wait for the authentication to complete
|
||||||
const authCompleted = await waitForAuthentication(lockData.port)
|
const authCompleted = hasSavedTokens || (await waitForAuthentication(lockData.port))
|
||||||
if (authCompleted) {
|
if (authCompleted) {
|
||||||
log('Authentication completed by another instance')
|
log('Authentication completed by another instance')
|
||||||
|
|
||||||
|
|
|
@ -25,9 +25,6 @@ async function runProxy(serverUrl: string, callbackPort: number, headers: Record
|
||||||
// Get the server URL hash for lockfile operations
|
// Get the server URL hash for lockfile operations
|
||||||
const serverUrlHash = getServerUrlHash(serverUrl)
|
const serverUrlHash = getServerUrlHash(serverUrl)
|
||||||
|
|
||||||
// Coordinate authentication with other instances
|
|
||||||
const { server, waitForAuthCode, skipBrowserAuth } = await coordinateAuth(serverUrlHash, callbackPort, events)
|
|
||||||
|
|
||||||
// Create the OAuth client provider
|
// Create the OAuth client provider
|
||||||
const authProvider = new NodeOAuthClientProvider({
|
const authProvider = new NodeOAuthClientProvider({
|
||||||
serverUrl,
|
serverUrl,
|
||||||
|
@ -35,6 +32,10 @@ async function runProxy(serverUrl: string, callbackPort: number, headers: Record
|
||||||
clientName: 'MCP CLI Proxy',
|
clientName: 'MCP CLI Proxy',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Coordinate authentication with other instances
|
||||||
|
const hasSavedTokens = !!(await authProvider.tokens())
|
||||||
|
const { server, waitForAuthCode, skipBrowserAuth } = await coordinateAuth(serverUrlHash, callbackPort, events, hasSavedTokens)
|
||||||
|
|
||||||
// If auth was completed by another instance, just log that we'll use the auth from disk
|
// If auth was completed by another instance, just log that we'll use the auth from disk
|
||||||
if (skipBrowserAuth) {
|
if (skipBrowserAuth) {
|
||||||
log('Authentication was completed by another instance - will use tokens from disk')
|
log('Authentication was completed by another instance - will use tokens from disk')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue