Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 91
[auth] Scenarios for scope selection#36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
e37c97faa16772ef31f4053ae69070fcc3a194fbae7f94fed8b378be8d24f02ab7ecd5258d859778c933e7185ff7bab0bbec554f58961318ee9015eFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #!/usr/bin/env node | ||
| import { Client } from '@modelcontextprotocol/sdk/client/index.js'; | ||
| import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; | ||
| import { | ||
| auth, | ||
| UnauthorizedError | ||
| } from '@modelcontextprotocol/sdk/client/auth.js'; | ||
| import type { FetchLike } from '@modelcontextprotocol/sdk/shared/transport.js'; | ||
| import { withOAuthRetry } from './helpers/withOAuthRetry.js'; | ||
| import { ConformanceOAuthProvider } from './helpers/ConformanceOAuthProvider.js'; | ||
| import { runAsCli } from './helpers/cliRunner.js'; | ||
| import { logger } from './helpers/logger.js'; | ||
| /** | ||
| * Broken client that always uses root-based PRM discovery. | ||
| * BUG: Ignores the resource_metadata URL from WWW-Authenticate header. | ||
| */ | ||
| export async function runClient(serverUrl: string): Promise<void> { | ||
| const handle401Broken = async ( | ||
| response: Response, | ||
| provider: ConformanceOAuthProvider, | ||
| next: FetchLike, | ||
| serverUrl: string | URL | ||
| ): Promise<void> => { | ||
| // BUG: Use root-based PRM discovery exclusively | ||
| const resourceMetadataUrl = new URL( | ||
| '/.well-known/oauth-protected-resource', | ||
| typeof serverUrl === 'string' ? serverUrl : serverUrl.origin | ||
| ); | ||
| let result = await auth(provider, { | ||
| serverUrl, | ||
| resourceMetadataUrl, | ||
| fetchFn: next | ||
| }); | ||
| if (result === 'REDIRECT') { | ||
| const authorizationCode = await provider.getAuthCode(); | ||
| result = await auth(provider, { | ||
| serverUrl, | ||
| resourceMetadataUrl, | ||
| authorizationCode, | ||
| fetchFn: next | ||
| }); | ||
| if (result !== 'AUTHORIZED') { | ||
| throw new UnauthorizedError( | ||
| `Authentication failed with result: ${result}` | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
| const client = new Client( | ||
| { name: 'test-auth-client-broken', version: '1.0.0' }, | ||
| { capabilities: {} } | ||
| ); | ||
| const oauthFetch = withOAuthRetry( | ||
| 'test-auth-client-broken', | ||
| new URL(serverUrl), | ||
| handle401Broken | ||
| )(fetch); | ||
| const transport = new StreamableHTTPClientTransport(new URL(serverUrl), { | ||
| fetch: oauthFetch | ||
| }); | ||
| await client.connect(transport); | ||
| logger.debug('✅ Successfully connected to MCP server'); | ||
| await client.listTools(); | ||
| logger.debug('✅ Successfully listed tools'); | ||
| await transport.close(); | ||
| logger.debug('✅ Connection closed successfully'); | ||
| } | ||
| runAsCli(runClient, import.meta.url, 'auth-test-bad-prm <server-url>'); |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| #!/usr/bin/env node | ||
| import { Client } from '@modelcontextprotocol/sdk/client/index.js'; | ||
| import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; | ||
| import { | ||
| auth, | ||
| extractWWWAuthenticateParams, | ||
| UnauthorizedError | ||
| } from '@modelcontextprotocol/sdk/client/auth.js'; | ||
| import type { FetchLike } from '@modelcontextprotocol/sdk/shared/transport.js'; | ||
| import { withOAuthRetry } from './helpers/withOAuthRetry.js'; | ||
| import { ConformanceOAuthProvider } from './helpers/ConformanceOAuthProvider.js'; | ||
| import { runAsCli } from './helpers/cliRunner.js'; | ||
| import { logger } from './helpers/logger.js'; | ||
| /** | ||
| * Broken client that only responds to 401, not 403. | ||
| * BUG: Ignores 403 responses which are used for step-up auth. | ||
| */ | ||
| export async function runClient(serverUrl: string): Promise<void> { | ||
| const handle401Broken = async ( | ||
| response: Response, | ||
| provider: ConformanceOAuthProvider, | ||
| next: FetchLike, | ||
| serverUrl: string | URL | ||
| ): Promise<void> => { | ||
| // BUG: Only respond to 401, not 403 | ||
| if (response.status !== 401) { | ||
| return; | ||
| } | ||
| const { resourceMetadataUrl, scope } = | ||
| extractWWWAuthenticateParams(response); | ||
| let result = await auth(provider, { | ||
| serverUrl, | ||
| resourceMetadataUrl, | ||
| scope, | ||
| fetchFn: next | ||
| }); | ||
| if (result === 'REDIRECT') { | ||
| const authorizationCode = await provider.getAuthCode(); | ||
| result = await auth(provider, { | ||
| serverUrl, | ||
| resourceMetadataUrl, | ||
| scope, | ||
| authorizationCode, | ||
| fetchFn: next | ||
| }); | ||
| if (result !== 'AUTHORIZED') { | ||
| throw new UnauthorizedError( | ||
| `Authentication failed with result: ${result}` | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
| const client = new Client( | ||
| { name: 'test-auth-client-broken', version: '1.0.0' }, | ||
| { capabilities: {} } | ||
| ); | ||
| const oauthFetch = withOAuthRetry( | ||
| 'test-auth-client-broken', | ||
| new URL(serverUrl), | ||
| handle401Broken | ||
| )(fetch); | ||
| const transport = new StreamableHTTPClientTransport(new URL(serverUrl), { | ||
| fetch: oauthFetch | ||
| }); | ||
| await client.connect(transport); | ||
| logger.debug('✅ Successfully connected to MCP server'); | ||
| await client.listTools(); | ||
| logger.debug('✅ Successfully listed tools'); | ||
| // Call tool to trigger step-up auth | ||
| await client.callTool({ name: 'test-tool', arguments: {} }); | ||
| logger.debug('✅ Successfully called tool'); | ||
| await transport.close(); | ||
| logger.debug('✅ Connection closed successfully'); | ||
| } | ||
| runAsCli(runClient, import.meta.url, 'auth-test-ignore-403 <server-url>'); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #!/usr/bin/env node | ||
| import { Client } from '@modelcontextprotocol/sdk/client/index.js'; | ||
| import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; | ||
| import { | ||
| auth, | ||
| extractWWWAuthenticateParams, | ||
| UnauthorizedError | ||
| } from '@modelcontextprotocol/sdk/client/auth.js'; | ||
| import type { FetchLike } from '@modelcontextprotocol/sdk/shared/transport.js'; | ||
| import { withOAuthRetry } from './helpers/withOAuthRetry.js'; | ||
| import { ConformanceOAuthProvider } from './helpers/ConformanceOAuthProvider.js'; | ||
| import { runAsCli } from './helpers/cliRunner.js'; | ||
| import { logger } from './helpers/logger.js'; | ||
| /** | ||
| * Broken client that ignores the scope from WWW-Authenticate header. | ||
| * BUG: Doesn't pass the scope parameter from the 401 response. | ||
| */ | ||
| export async function runClient(serverUrl: string): Promise<void> { | ||
| const handle401Broken = async ( | ||
| response: Response, | ||
| provider: ConformanceOAuthProvider, | ||
| next: FetchLike, | ||
| serverUrl: string | URL | ||
| ): Promise<void> => { | ||
| // BUG: Don't read the scope from the header | ||
| const { resourceMetadataUrl } = extractWWWAuthenticateParams(response); | ||
| let result = await auth(provider, { | ||
| serverUrl, | ||
| resourceMetadataUrl, | ||
| // scope deliberately omitted | ||
| fetchFn: next | ||
| }); | ||
| if (result === 'REDIRECT') { | ||
| const authorizationCode = await provider.getAuthCode(); | ||
| result = await auth(provider, { | ||
| serverUrl, | ||
| resourceMetadataUrl, | ||
| authorizationCode, | ||
| fetchFn: next | ||
| }); | ||
| if (result !== 'AUTHORIZED') { | ||
| throw new UnauthorizedError( | ||
| `Authentication failed with result: ${result}` | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
| const client = new Client( | ||
| { name: 'test-auth-client-broken', version: '1.0.0' }, | ||
| { capabilities: {} } | ||
| ); | ||
| const oauthFetch = withOAuthRetry( | ||
| 'test-auth-client-broken', | ||
| new URL(serverUrl), | ||
| handle401Broken | ||
| )(fetch); | ||
| const transport = new StreamableHTTPClientTransport(new URL(serverUrl), { | ||
| fetch: oauthFetch | ||
| }); | ||
| await client.connect(transport); | ||
| logger.debug('✅ Successfully connected to MCP server'); | ||
| await client.listTools(); | ||
| logger.debug('✅ Successfully listed tools'); | ||
| await transport.close(); | ||
| logger.debug('✅ Connection closed successfully'); | ||
| } | ||
| runAsCli(runClient, import.meta.url, 'auth-test-ignore-scope <server-url>'); |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more of a style nit: all these "broken" examples look very similar (identical?) except for the specific way in which they're "broken".
Could consider having a single implementation and having the specific way it's actually broken be a param to
createBrokenHandler, a higher level function that creates thehandle401broken.Could make this less verbose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potentially the well-behaved client could also share the same implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we prefer explicitness over conciseness though for conformance tests, so definitely just a thought 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed on the repetitiveness, i've been resisting the urge to de-dupe too aggressively in order to let abstractions fall out from a few more iterations on tests before committing to one.