Skip to content

Commit 7852c38

Browse files
max-stytchgeelen
authored andcommitted
Add --host parameter
1 parent 7eecc9c commit 7852c38

File tree

6 files changed

+32
-7
lines changed

6 files changed

+32
-7
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ To bypass authentication, or to emit custom headers on all requests to your remo
104104
]
105105
```
106106

107+
* To change which host `mcp-remote` registers as the OAuth callback URL (by default `127.0.0.1`), add the `--host` flag.
108+
109+
```json
110+
"args": [
111+
"mcp-remote",
112+
"https://remote.mcp.server/sse",
113+
"--host",
114+
"localhost"
115+
]
116+
```
117+
107118
* To allow HTTP connections in trusted private networks, add the `--allow-http` flag. Note: This should only be used in secure private networks where traffic cannot be intercepted.
108119

109120
```json

src/client.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ async function runClient(
3232
callbackPort: number,
3333
headers: Record<string, string>,
3434
transportStrategy: TransportStrategy = 'http-first',
35+
host: string,
3536
) {
3637
// Set up event emitter for auth flow
3738
const events = new EventEmitter()
@@ -46,6 +47,7 @@ async function runClient(
4647
const authProvider = new NodeOAuthClientProvider({
4748
serverUrl,
4849
callbackPort,
50+
host,
4951
clientName: 'MCP CLI Client',
5052
})
5153

@@ -152,8 +154,8 @@ async function runClient(
152154

153155
// Parse command-line arguments and run the client
154156
parseCommandLineArgs(process.argv.slice(2), 'Usage: npx tsx client.ts <https://server-url> [callback-port]')
155-
.then(({ serverUrl, callbackPort, headers, transportStrategy }) => {
156-
return runClient(serverUrl, callbackPort, headers, transportStrategy)
157+
.then(({ serverUrl, callbackPort, headers, transportStrategy, host }) => {
158+
return runClient(serverUrl, callbackPort, headers, transportStrategy, host)
157159
})
158160
.catch((error) => {
159161
console.error('Fatal error:', error)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class NodeOAuthClientProvider implements OAuthClientProvider {
3636
}
3737

3838
get redirectUrl(): string {
39-
return `http://localhost:${this.options.callbackPort}${this.callbackPath}`
39+
return `http://${this.options.host}:${this.options.callbackPort}${this.callbackPath}`;
4040
}
4141

4242
get clientMetadata() {

src/lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export interface OAuthProviderOptions {
88
serverUrl: string
99
/** Port for the OAuth callback server */
1010
callbackPort: number
11+
/** Desired hostname for the OAuth callback server */
12+
host: string
1113
/** Path for the OAuth callback endpoint */
1214
callbackPath?: string
1315
/** Directory to store OAuth credentials */

src/lib/utils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ async function findExistingClientPort(serverUrlHash: string): Promise<number | u
361361
return undefined
362362
}
363363

364-
const localhostRedirectUri = clientInfo.redirect_uris.map((uri) => new URL(uri)).find(({ hostname }) => hostname === 'localhost')
364+
const localhostRedirectUri = clientInfo.redirect_uris.map((uri) => new URL(uri)).find(({ hostname }) => hostname === 'localhost' || hostname === '127.0.0.1')
365365
if (!localhostRedirectUri) {
366366
throw new Error('Cannot find localhost callback URI from existing client information')
367367
}
@@ -449,6 +449,14 @@ export async function parseCommandLineArgs(args: string[], usage: string) {
449449
}
450450
}
451451

452+
// Parse host
453+
let host = '127.0.0.1' // Default
454+
const hostIndex = args.indexOf('--host')
455+
if (hostIndex !== -1 && hostIndex < args.length - 1) {
456+
host = args[hostIndex + 1]
457+
log(`Using callback hostname: ${host}`)
458+
}
459+
452460
if (!serverUrl) {
453461
log(usage)
454462
process.exit(1)
@@ -505,7 +513,7 @@ export async function parseCommandLineArgs(args: string[], usage: string) {
505513
})
506514
}
507515

508-
return { serverUrl, callbackPort, headers, transportStrategy }
516+
return { serverUrl, callbackPort, headers, transportStrategy, host }
509517
}
510518

511519
/**

src/proxy.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ async function runProxy(
3232
callbackPort: number,
3333
headers: Record<string, string>,
3434
transportStrategy: TransportStrategy = 'http-first',
35+
host: string,
3536
) {
3637
// Set up event emitter for auth flow
3738
const events = new EventEmitter()
@@ -46,6 +47,7 @@ async function runProxy(
4647
const authProvider = new NodeOAuthClientProvider({
4748
serverUrl,
4849
callbackPort,
50+
host,
4951
clientName: 'MCP CLI Proxy',
5052
})
5153

@@ -136,8 +138,8 @@ to the CA certificate file. If using claude_desktop_config.json, this might look
136138

137139
// Parse command-line arguments and run the proxy
138140
parseCommandLineArgs(process.argv.slice(2), 'Usage: npx tsx proxy.ts <https://server-url> [callback-port]')
139-
.then(({ serverUrl, callbackPort, headers, transportStrategy }) => {
140-
return runProxy(serverUrl, callbackPort, headers, transportStrategy)
141+
.then(({ serverUrl, callbackPort, headers, transportStrategy, host }) => {
142+
return runProxy(serverUrl, callbackPort, headers, transportStrategy, host)
141143
})
142144
.catch((error) => {
143145
log('Fatal error:', error)

0 commit comments

Comments
 (0)