-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathmcp-auth-flow.ts
More file actions
462 lines (410 loc) · 14.2 KB
/
mcp-auth-flow.ts
File metadata and controls
462 lines (410 loc) · 14.2 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
/**
* MCP Auth Flow
*
* High-level OAuth flow management using the MCP SDK's built-in auth functions.
*/
import {
auth as runSdkAuth,
UnauthorizedError,
} from "@modelcontextprotocol/sdk/client/auth.js"
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
import open from "open"
import { McpOAuthProvider, type McpOAuthConfig } from "./mcp-oauth-provider.ts"
import {
ensureCallbackServer,
waitForCallback,
cancelPendingCallback,
stopCallbackServer,
releaseCallbackServer,
} from "./mcp-callback-server.ts"
import {
getAuthForUrl,
isTokenExpired,
hasStoredTokens,
clearAllCredentials,
clearClientInfo,
clearTokens,
clearCodeVerifier,
updateOAuthState,
getOAuthState,
clearOAuthState,
type StoredTokens,
} from "./mcp-auth.ts"
import type { ServerEntry } from "./types.ts"
/** Auth status for a server */
export type AuthStatus = "authenticated" | "expired" | "not_authenticated"
// Track pending transports for auth completion
const pendingTransports = new Map<string, StreamableHTTPClientTransport>()
// Deduplicate concurrent authenticate() calls per server.
const pendingAuthentications = new Map<string, Promise<AuthStatus>>()
/**
* Generate a cryptographically secure random state parameter.
*/
function generateState(): string {
return Array.from(crypto.getRandomValues(new Uint8Array(32)))
.map((b) => b.toString(16).padStart(2, "0"))
.join("")
}
/**
* Extract OAuth configuration from a ServerEntry.
*/
export function extractOAuthConfig(definition: ServerEntry): McpOAuthConfig {
if (definition.oauth === false) {
return {}
}
const config: McpOAuthConfig = {}
if (definition.oauth?.grantType !== undefined) config.grantType = definition.oauth.grantType
if (definition.oauth?.clientId !== undefined) config.clientId = definition.oauth.clientId
if (definition.oauth?.clientSecret !== undefined) config.clientSecret = definition.oauth.clientSecret
if (definition.oauth?.scope !== undefined) config.scope = definition.oauth.scope
if (definition.oauth?.redirectUri !== undefined) {
if (typeof definition.oauth.redirectUri !== "string") {
throw new Error("OAuth redirectUri must be a string")
}
const redirectUri = definition.oauth.redirectUri.trim()
if (!redirectUri) {
throw new Error("OAuth redirectUri must not be empty")
}
config.redirectUri = redirectUri
}
if (definition.oauth?.clientName !== undefined) {
if (typeof definition.oauth.clientName !== "string") {
throw new Error("OAuth clientName must be a string")
}
const clientName = definition.oauth.clientName.trim()
if (!clientName) {
throw new Error("OAuth clientName must not be empty")
}
config.clientName = clientName
}
if (definition.oauth?.clientUri !== undefined) {
if (typeof definition.oauth.clientUri !== "string") {
throw new Error("OAuth clientUri must be a string")
}
const clientUri = definition.oauth.clientUri.trim()
if (!clientUri) {
throw new Error("OAuth clientUri must not be empty")
}
config.clientUri = clientUri
}
return config
}
function parseOAuthRedirectUri(redirectUri: string): { port: number; callbackHost: string; callbackPath: string } {
let url: URL
try {
url = new URL(redirectUri)
} catch (error) {
throw new Error(`Invalid OAuth redirectUri: ${redirectUri}`, { cause: error })
}
const hostname = url.hostname.toLowerCase()
const isLocalhost = hostname === "localhost" || hostname === "127.0.0.1" || hostname === "[::1]" || hostname === "::1"
if (url.protocol !== "http:" || !isLocalhost) {
throw new Error("OAuth redirectUri must be an http:// localhost or loopback URI")
}
if (url.username || url.password) {
throw new Error("OAuth redirectUri must not include username or password")
}
if (url.hash) {
throw new Error("OAuth redirectUri must not include a fragment")
}
if (!url.port) {
throw new Error("OAuth redirectUri must include an explicit numeric port")
}
const port = Number.parseInt(url.port, 10)
if (!Number.isInteger(port) || port <= 0 || port > 65535) {
throw new Error("OAuth redirectUri must include an explicit numeric port")
}
const callbackHost = hostname === "[::1]" ? "::1" : hostname
return { port, callbackHost, callbackPath: url.pathname }
}
/**
* Start OAuth authentication flow for a server.
* Returns the authorization URL when browser authorization is required.
*/
export async function startAuth(
serverName: string,
serverUrl: string,
definition?: ServerEntry
): Promise<{ authorizationUrl: string }> {
const config = definition ? extractOAuthConfig(definition) : {}
if (config.grantType === "client_credentials") {
const storedAuth = await getAuthForUrl(serverName, serverUrl)
if (storedAuth?.clientInfo && !storedAuth.tokens && !config.clientId) {
clearClientInfo(serverName)
clearCodeVerifier(serverName)
await clearOAuthState(serverName)
}
const authProvider = new McpOAuthProvider(serverName, serverUrl, config, {
onRedirect: async () => {
throw new Error("Browser redirect is not used for client_credentials flow")
},
})
const result = await runSdkAuth(authProvider, { serverUrl })
if (result !== "AUTHORIZED") {
throw new UnauthorizedError("Failed to authorize")
}
return { authorizationUrl: "" }
}
const redirectCallback = config.redirectUri !== undefined ? parseOAuthRedirectUri(config.redirectUri) : undefined
const oauthState = generateState()
try {
await ensureCallbackServer({
strictPort: Boolean(config.clientId) || config.redirectUri !== undefined,
oauthState,
reserveState: true,
...(redirectCallback ? { port: redirectCallback.port, callbackHost: redirectCallback.callbackHost, callbackPath: redirectCallback.callbackPath } : {}),
})
} catch (error) {
await clearOAuthState(serverName)
throw error
}
let capturedUrl: URL | undefined
const authProvider = new McpOAuthProvider(serverName, serverUrl, config, {
onRedirect: async (url) => {
capturedUrl = url
},
})
try {
const storedAuth = await getAuthForUrl(serverName, serverUrl)
if (storedAuth?.clientInfo && !config.clientId) {
if (!storedAuth.tokens) {
clearClientInfo(serverName)
clearCodeVerifier(serverName)
await clearOAuthState(serverName)
} else {
const redirectUris = storedAuth.clientInfo.redirectUris
if (!Array.isArray(redirectUris) || !redirectUris.includes(authProvider.redirectUrl ?? "")) {
clearClientInfo(serverName)
clearTokens(serverName)
clearCodeVerifier(serverName)
await clearOAuthState(serverName)
}
}
}
await updateOAuthState(serverName, oauthState, serverUrl)
const result = await runSdkAuth(authProvider, { serverUrl })
if (result === "AUTHORIZED") {
releaseCallbackServer(oauthState)
await clearOAuthState(serverName)
return { authorizationUrl: "" }
}
if (!capturedUrl) {
throw new UnauthorizedError("OAuth authorization URL was not provided")
}
pendingTransports.set(
serverName,
new StreamableHTTPClientTransport(new URL(serverUrl), { authProvider }),
)
return { authorizationUrl: capturedUrl.toString() }
} catch (error) {
releaseCallbackServer(oauthState)
await clearOAuthState(serverName)
throw error
}
}
/**
* Complete OAuth authentication with the authorization code.
*/
export async function completeAuth(
serverName: string,
authorizationCode: string
): Promise<AuthStatus> {
const transport = pendingTransports.get(serverName)
if (!transport) {
throw new Error(`No pending OAuth flow for server: ${serverName}`)
}
const oauthState = await getOAuthState(serverName)
try {
// Complete the auth using the transport's finishAuth method
await transport.finishAuth(authorizationCode)
return "authenticated"
} finally {
if (oauthState) {
releaseCallbackServer(oauthState)
}
await clearOAuthState(serverName)
pendingTransports.delete(serverName)
await transport.close().catch(() => {})
}
}
/**
* Perform the complete OAuth authentication flow for a server.
*
* @param serverName - The name of the MCP server
* @param serverUrl - The URL of the MCP server
* @param definition - The server definition (optional)
* @returns The final auth status
*/
export async function authenticate(
serverName: string,
serverUrl: string,
definition?: ServerEntry,
): Promise<AuthStatus> {
const inFlight = pendingAuthentications.get(serverName)
if (inFlight) {
return inFlight
}
const operation = (async (): Promise<AuthStatus> => {
// Start auth flow
const { authorizationUrl } = await startAuth(serverName, serverUrl, definition)
// If no auth URL needed, already authenticated
if (!authorizationUrl) {
return "authenticated"
}
// Get the state that was already generated and stored in startAuth()
const oauthState = await getOAuthState(serverName)
if (!oauthState) {
throw new Error("OAuth state not found - this should not happen")
}
// Register the callback BEFORE opening the browser
const callbackPromise = waitForCallback(oauthState)
try {
// Open browser
console.log(`MCP Auth: Opening browser for ${serverName}`)
try {
await open(authorizationUrl)
} catch (error) {
console.warn(`MCP Auth: Failed to open browser for ${serverName}`, { error })
throw new Error(
`Could not open browser. Please open this URL manually: ${authorizationUrl}`,
{ cause: error },
)
}
// Wait for callback
const code = await callbackPromise
// Validate state
const storedState = await getOAuthState(serverName)
if (storedState !== oauthState) {
await clearOAuthState(serverName)
throw new Error("OAuth state mismatch - potential CSRF attack")
}
await clearOAuthState(serverName)
// Complete the auth
return await completeAuth(serverName, code)
} catch (error) {
cancelPendingCallback(oauthState)
await clearOAuthState(serverName)
const pendingTransport = pendingTransports.get(serverName)
if (pendingTransport) {
pendingTransports.delete(serverName)
await pendingTransport.close().catch(() => {})
}
throw error
}
})()
pendingAuthentications.set(serverName, operation)
try {
return await operation
} finally {
if (pendingAuthentications.get(serverName) === operation) {
pendingAuthentications.delete(serverName)
}
}
}
/**
* Get a valid access token for a server, refreshing if necessary.
*
* @param serverName - The name of the MCP server
* @param serverUrl - The URL of the MCP server
* @returns The valid tokens or null if not authenticated
*/
export async function getValidToken(
serverName: string,
serverUrl: string,
): Promise<StoredTokens | null> {
// Check if we have valid tokens
const entry = await getAuthForUrl(serverName, serverUrl)
if (!entry?.tokens) {
return null
}
// Check expiration
const expired = await isTokenExpired(serverName)
if (expired === false) {
return entry.tokens
}
if (expired === true && entry.tokens.refreshToken) {
// Token is expired, try to refresh
console.log(`MCP Auth: Token expired for ${serverName}, attempting refresh`)
try {
// Create auth provider for token refresh
const authProvider = new McpOAuthProvider(serverName, serverUrl, {}, {
onRedirect: async () => {},
})
const clientInfo = await authProvider.clientInformation()
if (!clientInfo) {
console.log(`MCP Auth: No client info for refresh for ${serverName}`)
return null
}
const result = await runSdkAuth(authProvider, { serverUrl })
if (result !== "AUTHORIZED") {
return null
}
const refreshed = await getAuthForUrl(serverName, serverUrl)
return refreshed?.tokens ?? null
} catch (error) {
console.error(`MCP Auth: Token refresh failed for ${serverName}`, { error })
return null
}
}
// No expiration info or no refresh token, assume valid
return entry.tokens
}
/**
* Check the authentication status for a server.
*
* @param serverName - The name of the MCP server
* @returns The current auth status
*/
export async function getAuthStatus(serverName: string): Promise<AuthStatus> {
const hasTokens = await hasStoredTokens(serverName)
if (!hasTokens) return "not_authenticated"
const expired = await isTokenExpired(serverName)
return expired ? "expired" : "authenticated"
}
/**
* Remove all OAuth credentials for a server.
*
* @param serverName - The name of the MCP server
*/
export async function removeAuth(serverName: string): Promise<void> {
const oauthState = await getOAuthState(serverName)
if (oauthState) {
cancelPendingCallback(oauthState)
}
const pendingTransport = pendingTransports.get(serverName)
if (pendingTransport) {
pendingTransports.delete(serverName)
await pendingTransport.close().catch(() => {})
}
clearAllCredentials(serverName)
await clearOAuthState(serverName)
console.log(`MCP Auth: Removed credentials for ${serverName}`)
}
/**
* Check if OAuth is supported for a server configuration.
* OAuth is supported for HTTP servers unless explicitly disabled.
*
* @param definition - The server definition
* @returns True if OAuth is supported
*/
export function supportsOAuth(definition: ServerEntry): boolean {
// OAuth requires a URL
if (!definition.url) return false
// Explicitly disabled via auth: false or oauth: false
if (definition.auth === false) return false
if (definition.oauth === false) return false
// OAuth is enabled if auth is 'oauth' or not specified (auto-detect)
return definition.auth === "oauth" || definition.auth === undefined
}
/**
* Initialize the OAuth system on startup.
* OAuth callback binding is lazy and starts from startAuth() only.
*/
export async function initializeOAuth(): Promise<void> {}
/**
* Shutdown the OAuth system.
* Stops the callback server and cancels pending auths.
*/
export async function shutdownOAuth(): Promise<void> {
await stopCallbackServer()
}