-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathutils.ts
More file actions
1024 lines (902 loc) · 36 KB
/
Copy pathutils.ts
File metadata and controls
1024 lines (902 loc) · 36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { OAuthClientProvider, UnauthorizedError } from '@modelcontextprotocol/sdk/client/auth.js'
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
import { StreamableHTTPClientTransport, StreamableHTTPError } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
import { OAuthError } from '@modelcontextprotocol/sdk/server/auth/errors.js'
import { OAuthClientInformationFull, OAuthClientInformationFullSchema } from '@modelcontextprotocol/sdk/shared/auth.js'
import { OAuthCallbackServerOptions, StaticOAuthClientInformationFull, StaticOAuthClientMetadata } from './types'
import { getConfigDir, getConfigFilePath, readJsonFile } from './mcp-auth-config'
import {
discoverProtectedResourceMetadata,
parseWWWAuthenticateHeader,
getAuthorizationServerUrl,
type ProtectedResourceMetadata,
} from './protected-resource-metadata'
import { fetchAuthorizationServerMetadata, type AuthorizationServerMetadata } from './authorization-server-metadata'
import express from 'express'
import net from 'net'
import crypto from 'crypto'
import fs from 'fs'
import { readFile, rm } from 'fs/promises'
import path from 'path'
import { version as MCP_REMOTE_VERSION } from '../../package.json'
import { EnvHttpProxyAgent, fetch, Headers, RequestInit, setGlobalDispatcher } from 'undici'
// Global type declaration for typescript
declare global {
var currentServerUrlHash: string | undefined
}
// Connection constants
export const REASON_AUTH_NEEDED = 'authentication-needed'
export const REASON_TRANSPORT_FALLBACK = 'falling-back-to-alternate-transport'
// Transport strategy types
export type TransportStrategy = 'sse-only' | 'http-only' | 'sse-first' | 'http-first'
export { MCP_REMOTE_VERSION }
const pid = process.pid
// Global debug flag
export let DEBUG = false
export let SILENT = false
// Helper function for timestamp formatting
function getTimestamp(): string {
const now = new Date()
return now.toISOString()
}
// Debug logging function
export function debugLog(message: string, ...args: any[]) {
if (!DEBUG) return
const serverUrlHash = global.currentServerUrlHash
if (!serverUrlHash) {
console.error('[DEBUG LOG ERROR] global.currentServerUrlHash is not set. Cannot write debug log.')
return
}
try {
// Format with timestamp and PID
const formattedMessage = `[${getTimestamp()}][${pid}] ${message}`
// Log to console
console.error(formattedMessage, ...args)
// Ensure config directory exists
const configDir = getConfigDir()
fs.mkdirSync(configDir, { recursive: true })
// Append to log file
const logPath = path.join(configDir, `${serverUrlHash}_debug.log`)
const logMessage = `${formattedMessage} ${args.map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg))).join(' ')}\n`
fs.appendFileSync(logPath, logMessage, { encoding: 'utf8' })
} catch (error) {
// Fallback to console if file logging fails
console.error(`[DEBUG LOG ERROR] ${error}`)
}
}
export function log(str: string, ...rest: unknown[]) {
if (!SILENT) {
// Using stderr so that it doesn't interfere with stdout
console.error(`[${pid}] ${str}`, ...rest)
}
// If debug mode is on, also log to debug file
debugLog(str, ...rest)
}
type Message = any
const MESSAGE_BLOCKED = Symbol('MessageBlocked')
const isMessageBlocked = (value: any): value is typeof MESSAGE_BLOCKED => value === MESSAGE_BLOCKED
export function createMessageTransformer({
transformRequestFunction,
transformResponseFunction,
}: {
transformRequestFunction?: null | ((request: Message) => Message | typeof MESSAGE_BLOCKED)
transformResponseFunction?: null | ((request: Message, response: Message) => Message)
} = {}) {
const pendingRequests = new Map<string, Message>()
const interceptRequest = (message: Message) => {
const messageId = message.id
if (!messageId) return message
pendingRequests.set(messageId, message)
return transformRequestFunction?.(message) ?? message
}
const interceptResponse = (message: Message) => {
const messageId = message.id
if (!messageId) return message
const originalRequest = pendingRequests.get(messageId)
if (!originalRequest) return message
pendingRequests.delete(messageId)
return transformResponseFunction?.(originalRequest, message) ?? message
}
return {
interceptRequest,
interceptResponse,
}
}
/**
* Creates a bidirectional proxy between two transports
* @param params The transport connections to proxy between
*/
export function mcpProxy({
transportToClient,
transportToServer,
ignoredTools = [],
}: {
transportToClient: Transport
transportToServer: Transport
ignoredTools?: string[]
}) {
let transportToClientClosed = false
let transportToServerClosed = false
const messageTransformer = createMessageTransformer({
transformRequestFunction: (request: Message) => {
// Block tools/call for ignored tools
if (request.method === 'tools/call' && request.params?.name) {
const toolName = request.params.name
if (!shouldIncludeTool(ignoredTools, toolName)) {
// Send error response back to client immediately
const errorResponse = {
jsonrpc: '2.0' as const,
id: request.id,
error: {
code: -32603,
message: `Tool "${toolName}" is not available`,
},
}
transportToClient.send(errorResponse).catch(onClientError)
// Return symbol to indicate this request should not be forwarded
return MESSAGE_BLOCKED
}
}
return request
},
transformResponseFunction: (req: Message, res: Message) => {
if (req.method === 'tools/list') {
return {
...res,
result: {
...res.result,
tools: res.result.tools.filter((tool: any) => shouldIncludeTool(ignoredTools, tool.name)),
},
}
}
return res
},
})
transportToClient.onmessage = (_message) => {
// TODO: fix types
const message = messageTransformer.interceptRequest(_message as any)
// If interceptor returns MESSAGE_BLOCKED, don't forward the message
if (isMessageBlocked(message)) {
return
}
log('[Local→Remote]', message.method || message.id)
debugLog('Local → Remote message', {
method: message.method,
id: message.id,
params: message.params ? JSON.stringify(message.params).substring(0, 500) : undefined,
})
if (message.method === 'initialize') {
const { clientInfo } = message.params
if (clientInfo) clientInfo.name = `${clientInfo.name} (via mcp-remote ${MCP_REMOTE_VERSION})`
log(JSON.stringify(message, null, 2))
debugLog('Initialize message with modified client info', { clientInfo })
}
transportToServer.send(message).catch(onServerError)
}
transportToServer.onmessage = (_message) => {
// TODO: fix types
const message = messageTransformer.interceptResponse(_message as any)
log('[Remote→Local]', message.method || message.id)
debugLog('Remote → Local message', {
method: message.method,
id: message.id,
result: message.result ? 'result-present' : undefined,
error: message.error,
})
transportToClient.send(message).catch(onClientError)
}
transportToClient.onclose = () => {
if (transportToServerClosed) {
return
}
transportToClientClosed = true
debugLog('Local transport closed, closing remote transport')
transportToServer.close().catch(onServerError)
}
transportToServer.onclose = () => {
if (transportToClientClosed) {
return
}
transportToServerClosed = true
debugLog('Remote transport closed, closing local transport')
transportToClient.close().catch(onClientError)
}
transportToClient.onerror = onClientError
transportToServer.onerror = onServerError
function onClientError(error: Error) {
log('Error from local client:', error)
debugLog('Error from local client', { stack: error.stack })
}
function onServerError(error: Error) {
log('Error from remote server:', error)
debugLog('Error from remote server', { stack: error.stack })
}
}
/**
* Result of OAuth server discovery
*/
export interface OAuthServerDiscoveryResult {
/** The URL of the authorization server to use for OAuth */
authorizationServerUrl: string
/** Authorization server metadata (if successfully fetched) */
authorizationServerMetadata?: AuthorizationServerMetadata
/** Protected resource metadata (if discovered) */
protectedResourceMetadata?: ProtectedResourceMetadata
/** Scope extracted from WWW-Authenticate header */
wwwAuthenticateScope?: string
}
/**
* Probes the MCP server to discover the authorization server via Protected Resource Metadata.
*
* This implements the MCP Authorization Server Discovery flow:
* 1. Make a request to the MCP server
* 2. If we get a 401, extract the WWW-Authenticate header
* 3. Use the resource_metadata URL from the header (if present) or well-known URIs
* 4. Fetch Protected Resource Metadata to get the authorization server URL
* 5. Fetch Authorization Server Metadata from the discovered server
*
* @param serverUrl The MCP server URL
* @param headers Optional headers to include in the probe request
* @returns Discovery result with authorization server URL and metadata
*/
export async function discoverOAuthServerInfo(
serverUrl: string,
headers: Record<string, string> = {},
): Promise<OAuthServerDiscoveryResult> {
debugLog('Starting OAuth server discovery', { serverUrl })
let wwwAuthenticateHeader: string | undefined
let wwwAuthenticateScope: string | undefined
// Step 1: Probe the MCP server to get WWW-Authenticate header
try {
debugLog('Probing MCP server for WWW-Authenticate header')
const response = await fetch(serverUrl, {
method: 'GET',
headers: {
...headers,
Accept: 'application/json, text/event-stream',
},
signal: AbortSignal.timeout(10000),
})
// If we get a successful response, the server doesn't require auth
// Fall back to using serverUrl as authorization server
if (response.ok) {
debugLog('Server responded OK without auth, using server URL as authorization server')
const authServerMetadata = await fetchAuthorizationServerMetadata(serverUrl)
return {
authorizationServerUrl: serverUrl,
authorizationServerMetadata: authServerMetadata,
}
}
// Check for 401 Unauthorized
if (response.status === 401) {
wwwAuthenticateHeader = response.headers.get('WWW-Authenticate') || undefined
debugLog('Received 401 with WWW-Authenticate header', {
hasHeader: !!wwwAuthenticateHeader,
header: wwwAuthenticateHeader,
})
// Parse scope from WWW-Authenticate header if present
if (wwwAuthenticateHeader) {
const params = parseWWWAuthenticateHeader(wwwAuthenticateHeader)
wwwAuthenticateScope = params.scope
}
}
} catch (error) {
debugLog('Error probing MCP server', {
error: error instanceof Error ? error.message : String(error),
})
// Continue with discovery even if probe fails
}
// Step 2: Discover Protected Resource Metadata
const protectedResourceMetadata = await discoverProtectedResourceMetadata(serverUrl, wwwAuthenticateHeader)
// Step 3: Determine authorization server URL
let authorizationServerUrl: string
if (protectedResourceMetadata) {
const discoveredUrl = getAuthorizationServerUrl(protectedResourceMetadata)
if (discoveredUrl) {
authorizationServerUrl = discoveredUrl
debugLog('Using authorization server from Protected Resource Metadata', {
authorizationServerUrl,
})
} else {
// PRM found but no authorization_servers - fall back to server URL
authorizationServerUrl = serverUrl
debugLog('PRM found but no authorization_servers, falling back to server URL')
}
} else {
// No PRM found - fall back to server URL (current behavior)
authorizationServerUrl = serverUrl
debugLog('No Protected Resource Metadata found, falling back to server URL as authorization server')
}
// Step 4: Fetch Authorization Server Metadata
const authorizationServerMetadata = await fetchAuthorizationServerMetadata(authorizationServerUrl)
return {
authorizationServerUrl,
authorizationServerMetadata,
protectedResourceMetadata,
wwwAuthenticateScope,
}
}
/**
* Type for the auth initialization function
*/
export type AuthInitializer = () => Promise<{
waitForAuthCode: () => Promise<string>
skipBrowserAuth: boolean
}>
/**
* Creates and connects to a remote server with OAuth authentication
* @param client The client to connect with
* @param serverUrl The URL of the remote server
* @param authProvider The OAuth client provider
* @param headers Additional headers to send with the request
* @param authInitializer Function to initialize authentication when needed
* @param transportStrategy Strategy for selecting transport type ('sse-only', 'http-only', 'sse-first', 'http-first')
* @param recursionReasons Set of reasons for recursive calls (internal use)
* @returns The connected transport
*/
export async function connectToRemoteServer(
client: Client | null,
serverUrl: string,
authProvider: OAuthClientProvider,
headers: Record<string, string>,
authInitializer: AuthInitializer,
transportStrategy: TransportStrategy = 'http-first',
recursionReasons: Set<string> = new Set(),
): Promise<Transport> {
log(`[${pid}] Connecting to remote server: ${serverUrl}`)
const url = new URL(serverUrl)
// Create transport with eventSourceInit to pass Authorization header if present
const eventSourceInit = {
fetch: (url: string | URL, init?: RequestInit) => {
return Promise.resolve(authProvider?.tokens?.()).then((tokens) =>
fetch(url, {
...init,
headers: {
...(init?.headers instanceof Headers
? Object.fromEntries(init?.headers.entries())
: (init?.headers as Record<string, string>) || {}),
...headers,
...(tokens?.access_token ? { Authorization: `Bearer ${tokens.access_token}` } : {}),
Accept: 'text/event-stream',
} as Record<string, string>,
}),
)
},
}
log(`Using transport strategy: ${transportStrategy}`)
// Determine if we should attempt to fallback on error
// Choose transport based on user strategy and recursion history
const shouldAttemptFallback = transportStrategy === 'http-first' || transportStrategy === 'sse-first'
// Create transport instance based on the strategy
const sseTransport = transportStrategy === 'sse-only' || transportStrategy === 'sse-first'
const transport = sseTransport
? new SSEClientTransport(url, {
authProvider,
requestInit: { headers },
eventSourceInit,
})
: new StreamableHTTPClientTransport(url, {
authProvider,
requestInit: { headers },
})
try {
debugLog('Attempting to connect to remote server', { sseTransport })
if (client) {
debugLog('Connecting client to transport')
await client.connect(transport)
} else {
debugLog('Starting transport directly')
await transport.start()
if (!sseTransport) {
// Extremely hacky, but we didn't actually send a request when calling transport.start() above, so we don't
// know if we're even talking to an HTTP server. But if we forced that now we'd get an error later saying that
// the client is already connected. So let's just create a one-off client to make a single request and figure
// out if we're actually talking to an HTTP server or not.
debugLog('Creating test transport for HTTP-only connection test')
const testTransport = new StreamableHTTPClientTransport(url, { authProvider, requestInit: { headers } })
const testClient = new Client({ name: 'mcp-remote-fallback-test', version: '0.0.0' }, { capabilities: {} })
await testClient.connect(testTransport)
}
}
log(`Connected to remote server using ${transport.constructor.name}`)
return transport
} catch (error: any) {
// Check if it's a protocol error and we should attempt fallback
// StreamableHTTPError has a `code` property with the HTTP status code
const isStreamableHTTPError = error instanceof StreamableHTTPError
const httpStatusCode = isStreamableHTTPError ? error.code : null
const shouldFallbackOnError =
shouldAttemptFallback &&
error instanceof Error &&
(httpStatusCode === 404 ||
httpStatusCode === 405 ||
error.message.includes('405') ||
error.message.includes('Method Not Allowed') ||
error.message.includes('404') ||
error.message.includes('Not Found'))
if (shouldFallbackOnError) {
log(`Received error (status ${httpStatusCode ?? 'unknown'}): ${error.message}`)
// If we've already tried falling back once, throw an error
if (recursionReasons.has(REASON_TRANSPORT_FALLBACK)) {
const errorMessage = `Already attempted transport fallback. Giving up.`
log(errorMessage)
throw new Error(errorMessage)
}
log(`Recursively reconnecting for reason: ${REASON_TRANSPORT_FALLBACK}`)
// Add to recursion reasons set
recursionReasons.add(REASON_TRANSPORT_FALLBACK)
// Recursively call connectToRemoteServer with the updated recursion tracking
return connectToRemoteServer(
client,
serverUrl,
authProvider,
headers,
authInitializer,
sseTransport ? 'http-only' : 'sse-only',
recursionReasons,
)
} else if (error instanceof UnauthorizedError || (error instanceof Error && error.message.includes('Unauthorized'))) {
log('Authentication required. Initializing auth...')
debugLog('Authentication error detected', {
errorCode: error instanceof OAuthError ? error.errorCode : undefined,
errorMessage: error.message,
stack: error.stack,
})
// Initialize authentication on-demand
debugLog('Calling authInitializer to start auth flow')
const { waitForAuthCode, skipBrowserAuth } = await authInitializer()
if (skipBrowserAuth) {
log('Authentication required but skipping browser auth - using shared auth')
} else {
log('Authentication required. Waiting for authorization...')
}
// Wait for the authorization code from the callback
debugLog('Waiting for auth code from callback server')
const code = await waitForAuthCode()
debugLog('Received auth code from callback server')
try {
log('Completing authorization...')
await transport.finishAuth(code)
debugLog('Authorization completed successfully')
if (recursionReasons.has(REASON_AUTH_NEEDED)) {
const errorMessage = `Already attempted reconnection for reason: ${REASON_AUTH_NEEDED}. Giving up.`
log(errorMessage)
debugLog('Already attempted auth reconnection, giving up', {
recursionReasons: Array.from(recursionReasons),
})
throw new Error(errorMessage)
}
// Track this reason for recursion
recursionReasons.add(REASON_AUTH_NEEDED)
log(`Recursively reconnecting for reason: ${REASON_AUTH_NEEDED}`)
debugLog('Recursively reconnecting after auth', { recursionReasons: Array.from(recursionReasons) })
// Recursively call connectToRemoteServer with the updated recursion tracking
return connectToRemoteServer(client, serverUrl, authProvider, headers, authInitializer, transportStrategy, recursionReasons)
} catch (authError: any) {
log('Authorization error:', authError)
debugLog('Authorization error during finishAuth', {
errorMessage: authError.message,
stack: authError.stack,
})
throw authError
}
} else {
log('Connection error:', error)
debugLog('Connection error', {
errorMessage: error.message,
stack: error.stack,
transportType: transport.constructor.name,
})
throw error
}
}
}
/**
* Sets up an Express server to handle OAuth callbacks
* @param options The server options
* @returns An object with the server, authCode, and waitForAuthCode function
*/
export function setupOAuthCallbackServerWithLongPoll(options: OAuthCallbackServerOptions) {
let authCode: string | null = null
const app = express()
// Create a promise to track when auth is completed
let authCompletedResolve: (code: string) => void
const authCompletedPromise = new Promise<string>((resolve) => {
authCompletedResolve = resolve
})
// Long-polling endpoint
app.get('/wait-for-auth', (req, res) => {
if (authCode) {
// Auth already completed - just return 200 without the actual code
// Secondary instances will read tokens from disk
log('Auth already completed, returning 200')
res.status(200).send('Authentication completed')
return
}
if (req.query.poll === 'false') {
log('Client requested no long poll, responding with 202')
res.status(202).send('Authentication in progress')
return
}
// Long poll - wait for up to 30 seconds
const longPollTimeout = setTimeout(() => {
log('Long poll timeout reached, responding with 202')
res.status(202).send('Authentication in progress')
}, options.authTimeoutMs || 30000)
// If auth completes while we're waiting, send the response immediately
authCompletedPromise
.then(() => {
clearTimeout(longPollTimeout)
if (!res.headersSent) {
log('Auth completed during long poll, responding with 200')
res.status(200).send('Authentication completed')
}
})
.catch(() => {
clearTimeout(longPollTimeout)
if (!res.headersSent) {
log('Auth failed during long poll, responding with 500')
res.status(500).send('Authentication failed')
}
})
})
// OAuth callback endpoint
app.get(options.path, (req, res) => {
const code = req.query.code as string | undefined
if (!code) {
res.status(400).send('Error: No authorization code received')
return
}
authCode = code
log('Auth code received, resolving promise')
authCompletedResolve(code)
res.send(`
Authorization successful!
You may close this window and return to the CLI.
<script>
// If this is a non-interactive session (no manual approval step was required) then
// this should automatically close the window. If not, this will have no effect and
// the user will see the message above.
window.close();
</script>
`)
// Notify main flow that auth code is available
options.events.emit('auth-code-received', code)
})
const server = app.listen(options.port, '127.0.0.1', () => {
log(`OAuth callback server running at http://127.0.0.1:${options.port}`)
})
const waitForAuthCode = (): Promise<string> => {
return new Promise((resolve) => {
if (authCode) {
resolve(authCode)
return
}
options.events.once('auth-code-received', (code) => {
resolve(code)
})
})
}
return { server, authCode, waitForAuthCode, authCompletedPromise }
}
/**
* Sets up an Express server to handle OAuth callbacks
* @param options The server options
* @returns An object with the server, authCode, and waitForAuthCode function
*/
export function setupOAuthCallbackServer(options: OAuthCallbackServerOptions) {
const { server, authCode, waitForAuthCode } = setupOAuthCallbackServerWithLongPoll(options)
return { server, authCode, waitForAuthCode }
}
async function findExistingClientPort(serverUrlHash: string): Promise<number | undefined> {
const clientInfo = await readJsonFile<OAuthClientInformationFull>(serverUrlHash, 'client_info.json', OAuthClientInformationFullSchema)
if (!clientInfo) {
return undefined
}
const localhostRedirectUri = clientInfo.redirect_uris
.map((uri) => new URL(uri))
.find(({ hostname }) => hostname === 'localhost' || hostname === '127.0.0.1')
if (!localhostRedirectUri) {
throw new Error('Cannot find localhost callback URI from existing client information')
}
return parseInt(localhostRedirectUri.port)
}
function calculateDefaultPort(serverUrlHash: string): number {
// Convert the first 4 bytes of the serverUrlHash into a port offset
const offset = parseInt(serverUrlHash.substring(0, 4), 16)
// Pick a consistent but random-seeming port from 3335 to 49151
return 3335 + (offset % 45816)
}
/**
* Finds an available port on the local machine
* @param preferredPort Optional preferred port to try first
* @returns A promise that resolves to an available port number
*/
export async function findAvailablePort(preferredPort?: number): Promise<number> {
return new Promise((resolve, reject) => {
const server = net.createServer()
server.on('error', (err: NodeJS.ErrnoException) => {
if (err.code === 'EADDRINUSE') {
// If preferred port is in use, get a random port
server.listen(0)
} else {
reject(err)
}
})
server.on('listening', () => {
const { port } = server.address() as net.AddressInfo
server.close(() => {
resolve(port)
})
})
// Try preferred port first, or get a random port
server.listen(preferredPort || 0)
})
}
/**
* Parses command line arguments for MCP clients and proxies
* @param args Command line arguments
* @param usage Usage message to show on error
* @returns A promise that resolves to an object with parsed serverUrl, callbackPort and headers
*/
export async function parseCommandLineArgs(args: string[], usage: string) {
// Process headers
const headers: Record<string, string> = {}
let i = 0
while (i < args.length) {
if (args[i] === '--header' && i < args.length - 1) {
const value = args[i + 1]
const match = value.match(/^([A-Za-z0-9_-]+):\s*(.*)$/)
if (match) {
headers[match[1]] = match[2]
} else {
log(`Warning: ignoring invalid header argument: ${value}`)
}
args.splice(i, 2)
// Do not increment i, as the array has shifted
continue
}
i++
}
const serverUrl = args[0]
const specifiedPort = args[1] ? parseInt(args[1]) : undefined
const allowHttp = args.includes('--allow-http')
// Check for debug flag
const debug = args.includes('--debug')
if (debug) {
DEBUG = true
log('Debug mode enabled - detailed logs will be written to ~/.mcp-auth/')
}
// Check for silent flag
const silent = args.includes('--silent')
if (silent) {
SILENT = true
log('Silent mode enabled - stderr output will be suppressed, except when --debug is also enabled')
}
const enableProxy = args.includes('--enable-proxy')
if (enableProxy) {
// Use env proxy
setGlobalDispatcher(new EnvHttpProxyAgent())
log('HTTP proxy support enabled - using system HTTP_PROXY/HTTPS_PROXY environment variables')
}
// Parse transport strategy
let transportStrategy: TransportStrategy = 'http-first' // Default
const transportIndex = args.indexOf('--transport')
if (transportIndex !== -1 && transportIndex < args.length - 1) {
const strategy = args[transportIndex + 1]
if (strategy === 'sse-only' || strategy === 'http-only' || strategy === 'sse-first' || strategy === 'http-first') {
transportStrategy = strategy as TransportStrategy
log(`Using transport strategy: ${transportStrategy}`)
} else {
log(`Warning: Ignoring invalid transport strategy: ${strategy}. Valid values are: sse-only, http-only, sse-first, http-first`)
}
}
// Parse host
let host = 'localhost' // Default
const hostIndex = args.indexOf('--host')
if (hostIndex !== -1 && hostIndex < args.length - 1) {
host = args[hostIndex + 1]
log(`Using callback hostname: ${host}`)
}
let staticOAuthClientMetadata: StaticOAuthClientMetadata = null
const staticOAuthClientMetadataIndex = args.indexOf('--static-oauth-client-metadata')
if (staticOAuthClientMetadataIndex !== -1 && staticOAuthClientMetadataIndex < args.length - 1) {
const staticOAuthClientMetadataArg = args[staticOAuthClientMetadataIndex + 1]
if (staticOAuthClientMetadataArg.startsWith('@')) {
const filePath = staticOAuthClientMetadataArg.slice(1)
staticOAuthClientMetadata = JSON.parse(await readFile(filePath, 'utf8'))
log(`Using static OAuth client metadata from file: ${filePath}`)
} else {
staticOAuthClientMetadata = JSON.parse(staticOAuthClientMetadataArg)
log(`Using static OAuth client metadata from string`)
}
}
// parse static OAuth client information, if provided
// defaults to OAuth dynamic client registration
let staticOAuthClientInfo: StaticOAuthClientInformationFull = null
const staticOAuthClientInfoIndex = args.indexOf('--static-oauth-client-info')
if (staticOAuthClientInfoIndex !== -1 && staticOAuthClientInfoIndex < args.length - 1) {
const staticOAuthClientInfoArg = args[staticOAuthClientInfoIndex + 1]
if (staticOAuthClientInfoArg.startsWith('@')) {
const filePath = staticOAuthClientInfoArg.slice(1)
staticOAuthClientInfo = JSON.parse(await readFile(filePath, 'utf8'))
log(`Using static OAuth client information from file: ${filePath}`)
} else {
staticOAuthClientInfo = JSON.parse(staticOAuthClientInfoArg)
log(`Using static OAuth client information from string`)
}
}
// Parse resource to authorize
let authorizeResource = '' // Default
const resourceIndex = args.indexOf('--resource')
if (resourceIndex !== -1 && resourceIndex < args.length - 1) {
authorizeResource = args[resourceIndex + 1]
log(`Using authorize resource: ${authorizeResource}`)
}
// Parse ignored tools
const ignoredTools: string[] = []
let j = 0
while (j < args.length) {
if (args[j] === '--ignore-tool' && j < args.length - 1) {
const toolName = args[j + 1]
ignoredTools.push(toolName)
log(`Ignoring tool: ${toolName}`)
args.splice(j, 2)
// Do not increment j, as the array has shifted
continue
}
j++
}
// Parse auth timeout
let authTimeoutMs = 30000 // Default 30 seconds
const authTimeoutIndex = args.indexOf('--auth-timeout')
if (authTimeoutIndex !== -1 && authTimeoutIndex < args.length - 1) {
const timeoutSeconds = parseInt(args[authTimeoutIndex + 1], 10)
if (!isNaN(timeoutSeconds) && timeoutSeconds > 0) {
authTimeoutMs = timeoutSeconds * 1000
log(`Using auth callback timeout: ${timeoutSeconds} seconds`)
} else {
log(`Warning: Ignoring invalid auth timeout value: ${args[authTimeoutIndex + 1]}. Must be a positive number.`)
}
}
if (!serverUrl) {
log(usage)
process.exit(1)
}
const url = new URL(serverUrl)
const isLocalhost = (url.hostname === 'localhost' || url.hostname === '127.0.0.1') && url.protocol === 'http:'
if (!(url.protocol == 'https:' || isLocalhost || allowHttp)) {
log('Error: Non-HTTPS URLs are only allowed for localhost or when --allow-http flag is provided')
log(usage)
process.exit(1)
}
// Calculate hash with all parsed parameters for cache isolation
const serverUrlHash = getServerUrlHash(serverUrl, authorizeResource, headers)
// Set server hash globally for debug logging
global.currentServerUrlHash = serverUrlHash
debugLog(`Starting mcp-remote with server URL: ${serverUrl}`)
const defaultPort = calculateDefaultPort(serverUrlHash)
// Use the specified port, or the existing client port or fallback to find an available one
const [existingClientPort, availablePort] = await Promise.all([findExistingClientPort(serverUrlHash), findAvailablePort(defaultPort)])
let callbackPort: number
if (specifiedPort) {
if (existingClientPort && specifiedPort !== existingClientPort) {
log(
`Warning! Specified callback port of ${specifiedPort}, which conflicts with existing client registration port ${existingClientPort}. Deleting existing client data to force reregistration.`,
)
await rm(getConfigFilePath(serverUrlHash, 'client_info.json'))
}
log(`Using specified callback port: ${specifiedPort}`)
callbackPort = specifiedPort
} else if (existingClientPort) {
log(`Using existing client port: ${existingClientPort}`)
callbackPort = existingClientPort
} else {
log(`Using automatically selected callback port: ${availablePort}`)
callbackPort = availablePort
}
if (Object.keys(headers).length > 0) {
log(`Using custom header names: ${Object.keys(headers).join(', ')}`)
}
// Replace environment variables in headers
// example `Authorization: Bearer ${TOKEN}` will read process.env.TOKEN
for (const [key, value] of Object.entries(headers)) {
headers[key] = value.replace(/\$\{([^}]+)}/g, (match, envVarName) => {
const envVarValue = process.env[envVarName]
if (envVarValue !== undefined) {
log(`Replacing ${match} with environment value in header '${key}'`)
return envVarValue
} else {
log(`Warning: Environment variable '${envVarName}' not found for header '${key}'.`)
return ''
}
})
}
return {
serverUrl,
callbackPort,
headers,
transportStrategy,
host,
debug,
staticOAuthClientMetadata,
staticOAuthClientInfo,
authorizeResource,
ignoredTools,
authTimeoutMs,
serverUrlHash,
}
}
/**
* Sets up signal handlers for graceful shutdown
* @param cleanup Cleanup function to run on shutdown
*/
export function setupSignalHandlers(cleanup: () => Promise<void>) {
process.on('SIGINT', async () => {
log('\nShutting down...')
await cleanup()
process.exit(0)
})
// Keep the process alive
process.stdin.resume()
process.stdin.on('end', async () => {
log('\nShutting down...')
await cleanup()
process.exit(0)
})
}
/**
* Generates a hash for the server URL configuration
* Includes resource and headers to isolate OAuth sessions per unique
* server configuration (fixes #25: multi-instance support)
* @param serverUrl The server URL
* @param authorizeResource Optional resource parameter for OAuth
* @param headers Optional custom headers
* @returns MD5 hash of the configuration
*/
export function getServerUrlHash(serverUrl: string, authorizeResource?: string, headers?: Record<string, string>): string {
// Include resource and headers in hash to isolate OAuth sessions
// per unique server configuration (fixes #25)
const parts = [serverUrl]
if (authorizeResource) parts.push(authorizeResource)
if (headers && Object.keys(headers).length > 0) {
const sortedKeys = Object.keys(headers).sort()
parts.push(JSON.stringify(headers, sortedKeys))
}
return crypto.createHash('md5').update(parts.join('|')).digest('hex')
}
/**
* Converts a glob pattern to a regular expression
* @param pattern The glob pattern (e.g., "create*", "*account")
* @returns The corresponding regular expression
*/
function patternToRegex(pattern: string): RegExp {
// Split by asterisks, escape each part, then join with .*
const parts = pattern.split('*')
const escapedParts = parts.map((part) => part.replace(/\W/g, '\\$&'))
const regexPattern = escapedParts.join('.*')
// Match the entire string from start to end, case-insensitive
return new RegExp(`^${regexPattern}$`, 'i')