-
Notifications
You must be signed in to change notification settings - Fork 10
Add sep-835 limit retry scenario, add suite options to server command and update pending scenarios #49
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
Merged
Merged
Add sep-835 limit retry scenario, add suite options to server command and update pending scenarios #49
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cf2ef3b
lefthook: move deps installs to optional local change
pcarleton a011765
add test scenario for infinite retry
pcarleton 3ef0762
Add broken client for retry limit negative test and fix lefthook form…
pcarleton d89370e
fix www-authenticate in scope scenario
pcarleton deadcf7
add suites and shift pending to reflect current pending
pcarleton f19de47
lint
pcarleton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
examples/clients/typescript/auth-test-no-retry-limit.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| #!/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 type { Middleware } from '@modelcontextprotocol/sdk/client/middleware.js'; | ||
| import { ConformanceOAuthProvider } from './helpers/ConformanceOAuthProvider'; | ||
| import { runAsCli } from './helpers/cliRunner'; | ||
| import { logger } from './helpers/logger'; | ||
|
|
||
| /** | ||
| * Broken client that retries auth infinitely without any retry limit. | ||
| * BUG: Does not implement retry limits, causing infinite auth loops. | ||
| */ | ||
|
|
||
| const withOAuthRetryNoLimit = ( | ||
| clientName: string, | ||
| baseUrl?: string | URL | ||
| ): Middleware => { | ||
| const provider = new ConformanceOAuthProvider( | ||
| 'http://localhost:3000/callback', | ||
| { | ||
| client_name: clientName, | ||
| redirect_uris: ['http://localhost:3000/callback'] | ||
| } | ||
| ); | ||
|
|
||
| return (next: FetchLike) => { | ||
| return async ( | ||
| input: string | URL, | ||
| init?: RequestInit | ||
| ): Promise<Response> => { | ||
| const makeRequest = async (): Promise<Response> => { | ||
| const headers = new Headers(init?.headers); | ||
| const tokens = await provider.tokens(); | ||
| if (tokens) { | ||
| headers.set('Authorization', `Bearer ${tokens.access_token}`); | ||
| } | ||
| return await next(input, { ...init, headers }); | ||
| }; | ||
|
|
||
| let response = await makeRequest(); | ||
|
|
||
| // BUG: No retry limit - keeps retrying on every 401/403 | ||
| while (response.status === 401 || response.status === 403) { | ||
| const serverUrl = | ||
| baseUrl || | ||
| (typeof input === 'string' ? new URL(input).origin : input.origin); | ||
|
|
||
| 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}` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| response = await makeRequest(); | ||
| } | ||
|
|
||
| return response; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| export async function runClient(serverUrl: string): Promise<void> { | ||
| const client = new Client( | ||
| { name: 'test-auth-client-no-retry-limit', version: '1.0.0' }, | ||
| { capabilities: {} } | ||
| ); | ||
|
|
||
| const oauthFetch = withOAuthRetryNoLimit( | ||
| 'test-auth-client-no-retry-limit', | ||
| new URL(serverUrl) | ||
| )(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-no-retry-limit <server-url>'); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this file intentionally included?