Skip to content

Commit 6d2aa68

Browse files
authored
Add resource parameter to authorize URL (#112)
* feat: add resource flag to proxy command * feat: append resource parameter to authorize URL * fix: authorize resource used and logged * feat: pass resource to OAuth client * refactor: lint
1 parent 54f03e0 commit 6d2aa68

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

src/lib/node-oauth-client-provider.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class NodeOAuthClientProvider implements OAuthClientProvider {
2626
private softwareVersion: string
2727
private staticOAuthClientMetadata: StaticOAuthClientMetadata
2828
private staticOAuthClientInfo: StaticOAuthClientInformationFull
29+
private authorizeResource: string | undefined
2930
private _state: string
3031

3132
/**
@@ -41,6 +42,7 @@ export class NodeOAuthClientProvider implements OAuthClientProvider {
4142
this.softwareVersion = options.softwareVersion || MCP_REMOTE_VERSION
4243
this.staticOAuthClientMetadata = options.staticOAuthClientMetadata
4344
this.staticOAuthClientInfo = options.staticOAuthClientInfo
45+
this.authorizeResource = options.authorizeResource
4446
this._state = randomUUID()
4547
}
4648

@@ -168,6 +170,10 @@ export class NodeOAuthClientProvider implements OAuthClientProvider {
168170
* @param authorizationUrl The URL to redirect to
169171
*/
170172
async redirectToAuthorization(authorizationUrl: URL): Promise<void> {
173+
if (this.authorizeResource) {
174+
authorizationUrl.searchParams.set('resource', this.authorizeResource)
175+
}
176+
171177
log(`\nPlease authorize this client by visiting:\n${authorizationUrl.toString()}\n`)
172178

173179
if (DEBUG) debugLog('Redirecting to authorization URL', authorizationUrl.toString())

src/lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export interface OAuthProviderOptions {
2727
staticOAuthClientMetadata?: StaticOAuthClientMetadata
2828
/** Static OAuth client information to use instead of OAuth registration */
2929
staticOAuthClientInfo?: StaticOAuthClientInformationFull
30+
/** Resource parameter to send to the authorization server */
31+
authorizeResource?: string
3032
}
3133

3234
/**

src/lib/utils.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,14 @@ export async function parseCommandLineArgs(args: string[], usage: string) {
609609
}
610610
}
611611

612+
// Parse resource to authorize
613+
let authorizeResource = '' // Default
614+
const resourceIndex = args.indexOf('--resource')
615+
if (resourceIndex !== -1 && resourceIndex < args.length - 1) {
616+
authorizeResource = args[resourceIndex + 1]
617+
log(`Using authorize resource: ${authorizeResource}`)
618+
}
619+
612620
if (!serverUrl) {
613621
log(usage)
614622
process.exit(1)
@@ -673,7 +681,17 @@ export async function parseCommandLineArgs(args: string[], usage: string) {
673681
})
674682
}
675683

676-
return { serverUrl, callbackPort, headers, transportStrategy, host, debug, staticOAuthClientMetadata, staticOAuthClientInfo }
684+
return {
685+
serverUrl,
686+
callbackPort,
687+
headers,
688+
transportStrategy,
689+
host,
690+
debug,
691+
staticOAuthClientMetadata,
692+
staticOAuthClientInfo,
693+
authorizeResource,
694+
}
677695
}
678696

679697
/**

src/proxy.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ async function runProxy(
3535
host: string,
3636
staticOAuthClientMetadata: StaticOAuthClientMetadata,
3737
staticOAuthClientInfo: StaticOAuthClientInformationFull,
38+
authorizeResource: string,
3839
) {
3940
// Set up event emitter for auth flow
4041
const events = new EventEmitter()
@@ -53,6 +54,7 @@ async function runProxy(
5354
clientName: 'MCP CLI Proxy',
5455
staticOAuthClientMetadata,
5556
staticOAuthClientInfo,
57+
authorizeResource,
5658
})
5759

5860
// Create the STDIO transport for local connections
@@ -142,9 +144,30 @@ to the CA certificate file. If using claude_desktop_config.json, this might look
142144

143145
// Parse command-line arguments and run the proxy
144146
parseCommandLineArgs(process.argv.slice(2), 'Usage: npx tsx proxy.ts <https://server-url> [callback-port] [--debug]')
145-
.then(({ serverUrl, callbackPort, headers, transportStrategy, host, debug, staticOAuthClientMetadata, staticOAuthClientInfo }) => {
146-
return runProxy(serverUrl, callbackPort, headers, transportStrategy, host, staticOAuthClientMetadata, staticOAuthClientInfo)
147-
})
147+
.then(
148+
({
149+
serverUrl,
150+
callbackPort,
151+
headers,
152+
transportStrategy,
153+
host,
154+
debug,
155+
staticOAuthClientMetadata,
156+
staticOAuthClientInfo,
157+
authorizeResource,
158+
}) => {
159+
return runProxy(
160+
serverUrl,
161+
callbackPort,
162+
headers,
163+
transportStrategy,
164+
host,
165+
staticOAuthClientMetadata,
166+
staticOAuthClientInfo,
167+
authorizeResource,
168+
)
169+
},
170+
)
148171
.catch((error) => {
149172
log('Fatal error:', error)
150173
process.exit(1)

0 commit comments

Comments
 (0)