|
1 | | -import pkceChallenge from "pkce-challenge"; |
| 1 | +import { OAuthClientProvider } from "@modelcontextprotocol/sdk/client/auth.js"; |
| 2 | +import { |
| 3 | + OAuthClientInformationSchema, |
| 4 | + OAuthClientInformation, |
| 5 | + OAuthTokens, |
| 6 | + OAuthTokensSchema, |
| 7 | +} from "@modelcontextprotocol/sdk/shared/auth.js"; |
2 | 8 | import { SESSION_KEYS } from "./constants"; |
3 | | -import { z } from "zod"; |
4 | 9 |
|
5 | | -export const OAuthMetadataSchema = z.object({ |
6 | | - authorization_endpoint: z.string(), |
7 | | - token_endpoint: z.string(), |
8 | | -}); |
9 | | - |
10 | | -export type OAuthMetadata = z.infer<typeof OAuthMetadataSchema>; |
11 | | - |
12 | | -export const OAuthTokensSchema = z.object({ |
13 | | - access_token: z.string(), |
14 | | - refresh_token: z.string().optional(), |
15 | | - expires_in: z.number().optional(), |
16 | | -}); |
17 | | - |
18 | | -export type OAuthTokens = z.infer<typeof OAuthTokensSchema>; |
19 | | - |
20 | | -export async function discoverOAuthMetadata( |
21 | | - serverUrl: string, |
22 | | -): Promise<OAuthMetadata> { |
23 | | - try { |
24 | | - const url = new URL("/.well-known/oauth-authorization-server", serverUrl); |
25 | | - const response = await fetch(url.toString()); |
26 | | - |
27 | | - if (response.ok) { |
28 | | - const metadata = await response.json(); |
29 | | - const validatedMetadata = OAuthMetadataSchema.parse({ |
30 | | - authorization_endpoint: metadata.authorization_endpoint, |
31 | | - token_endpoint: metadata.token_endpoint, |
32 | | - }); |
33 | | - return validatedMetadata; |
34 | | - } |
35 | | - } catch (error) { |
36 | | - console.warn("OAuth metadata discovery failed:", error); |
| 10 | +class InspectorOAuthClientProvider implements OAuthClientProvider { |
| 11 | + get redirectUrl() { |
| 12 | + return window.location.origin + "/oauth/callback"; |
37 | 13 | } |
38 | 14 |
|
39 | | - // Fall back to default endpoints |
40 | | - const baseUrl = new URL(serverUrl); |
41 | | - const defaultMetadata = { |
42 | | - authorization_endpoint: new URL("/authorize", baseUrl).toString(), |
43 | | - token_endpoint: new URL("/token", baseUrl).toString(), |
44 | | - }; |
45 | | - return OAuthMetadataSchema.parse(defaultMetadata); |
46 | | -} |
47 | | - |
48 | | -export async function startOAuthFlow(serverUrl: string): Promise<string> { |
49 | | - // Generate PKCE challenge |
50 | | - const challenge = await pkceChallenge(); |
51 | | - const codeVerifier = challenge.code_verifier; |
52 | | - const codeChallenge = challenge.code_challenge; |
53 | | - |
54 | | - // Store code verifier for later use |
55 | | - sessionStorage.setItem(SESSION_KEYS.CODE_VERIFIER, codeVerifier); |
56 | | - |
57 | | - // Discover OAuth endpoints |
58 | | - const metadata = await discoverOAuthMetadata(serverUrl); |
| 15 | + get clientMetadata() { |
| 16 | + return { |
| 17 | + redirect_uris: [this.redirectUrl], |
| 18 | + token_endpoint_auth_method: "none", |
| 19 | + grant_types: ["authorization_code", "refresh_token"], |
| 20 | + response_types: ["code"], |
| 21 | + client_name: "MCP Inspector", |
| 22 | + client_uri: "https://github.com/modelcontextprotocol/inspector", |
| 23 | + }; |
| 24 | + } |
59 | 25 |
|
60 | | - // Build authorization URL |
61 | | - const authUrl = new URL(metadata.authorization_endpoint); |
62 | | - authUrl.searchParams.set("response_type", "code"); |
63 | | - authUrl.searchParams.set("code_challenge", codeChallenge); |
64 | | - authUrl.searchParams.set("code_challenge_method", "S256"); |
65 | | - authUrl.searchParams.set( |
66 | | - "redirect_uri", |
67 | | - window.location.origin + "/oauth/callback", |
68 | | - ); |
| 26 | + async clientInformation() { |
| 27 | + const value = sessionStorage.getItem(SESSION_KEYS.CLIENT_INFORMATION); |
| 28 | + if (!value) { |
| 29 | + return undefined; |
| 30 | + } |
69 | 31 |
|
70 | | - return authUrl.toString(); |
71 | | -} |
| 32 | + return await OAuthClientInformationSchema.parseAsync(JSON.parse(value)); |
| 33 | + } |
72 | 34 |
|
73 | | -export async function handleOAuthCallback( |
74 | | - serverUrl: string, |
75 | | - code: string, |
76 | | -): Promise<OAuthTokens> { |
77 | | - // Get stored code verifier |
78 | | - const codeVerifier = sessionStorage.getItem(SESSION_KEYS.CODE_VERIFIER); |
79 | | - if (!codeVerifier) { |
80 | | - throw new Error("No code verifier found"); |
| 35 | + saveClientInformation(clientInformation: OAuthClientInformation) { |
| 36 | + sessionStorage.setItem( |
| 37 | + SESSION_KEYS.CLIENT_INFORMATION, |
| 38 | + JSON.stringify(clientInformation), |
| 39 | + ); |
81 | 40 | } |
82 | 41 |
|
83 | | - // Discover OAuth endpoints |
84 | | - const metadata = await discoverOAuthMetadata(serverUrl); |
85 | | - // Exchange code for tokens |
86 | | - const response = await fetch(metadata.token_endpoint, { |
87 | | - method: "POST", |
88 | | - headers: { |
89 | | - "Content-Type": "application/x-www-form-urlencoded", |
90 | | - }, |
91 | | - body: new URLSearchParams({ |
92 | | - grant_type: "authorization_code", |
93 | | - code, |
94 | | - code_verifier: codeVerifier, |
95 | | - redirect_uri: window.location.origin + "/oauth/callback", |
96 | | - }), |
97 | | - }); |
| 42 | + async tokens() { |
| 43 | + const tokens = sessionStorage.getItem(SESSION_KEYS.TOKENS); |
| 44 | + if (!tokens) { |
| 45 | + return undefined; |
| 46 | + } |
98 | 47 |
|
99 | | - if (!response.ok) { |
100 | | - throw new Error("Token exchange failed"); |
| 48 | + return await OAuthTokensSchema.parseAsync(JSON.parse(tokens)); |
101 | 49 | } |
102 | 50 |
|
103 | | - const tokens = await response.json(); |
104 | | - return OAuthTokensSchema.parse(tokens); |
105 | | -} |
| 51 | + saveTokens(tokens: OAuthTokens) { |
| 52 | + sessionStorage.setItem(SESSION_KEYS.TOKENS, JSON.stringify(tokens)); |
| 53 | + } |
106 | 54 |
|
107 | | -export async function refreshAccessToken( |
108 | | - serverUrl: string, |
109 | | -): Promise<OAuthTokens> { |
110 | | - const refreshToken = sessionStorage.getItem(SESSION_KEYS.REFRESH_TOKEN); |
111 | | - if (!refreshToken) { |
112 | | - throw new Error("No refresh token available"); |
| 55 | + redirectToAuthorization(authorizationUrl: URL) { |
| 56 | + window.location.href = authorizationUrl.href; |
113 | 57 | } |
114 | 58 |
|
115 | | - const metadata = await discoverOAuthMetadata(serverUrl); |
| 59 | + saveCodeVerifier(codeVerifier: string) { |
| 60 | + sessionStorage.setItem(SESSION_KEYS.CODE_VERIFIER, codeVerifier); |
| 61 | + } |
116 | 62 |
|
117 | | - const response = await fetch(metadata.token_endpoint, { |
118 | | - method: "POST", |
119 | | - headers: { |
120 | | - "Content-Type": "application/x-www-form-urlencoded", |
121 | | - }, |
122 | | - body: new URLSearchParams({ |
123 | | - grant_type: "refresh_token", |
124 | | - refresh_token: refreshToken, |
125 | | - }), |
126 | | - }); |
| 63 | + codeVerifier() { |
| 64 | + const verifier = sessionStorage.getItem(SESSION_KEYS.CODE_VERIFIER); |
| 65 | + if (!verifier) { |
| 66 | + throw new Error("No code verifier saved for session"); |
| 67 | + } |
127 | 68 |
|
128 | | - if (!response.ok) { |
129 | | - throw new Error("Token refresh failed"); |
| 69 | + return verifier; |
130 | 70 | } |
131 | | - |
132 | | - const tokens = await response.json(); |
133 | | - return OAuthTokensSchema.parse(tokens); |
134 | 71 | } |
| 72 | + |
| 73 | +export const authProvider = new InspectorOAuthClientProvider(); |
0 commit comments