Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
"dependencies": {
"@modelcontextprotocol/client": "workspace:^",
"ajv": "catalog:runtimeShared",
"open": "^11.0.0",
"zod": "catalog:runtimeShared"
},
"devDependencies": {
"@modelcontextprotocol/eslint-config": "workspace:^",
"@modelcontextprotocol/examples-shared": "workspace:^",
"@modelcontextprotocol/tsconfig": "workspace:^",
"@modelcontextprotocol/eslint-config": "workspace:^",
"@modelcontextprotocol/vitest-config": "workspace:^",
"tsdown": "catalog:devTools"
}
Expand Down
26 changes: 18 additions & 8 deletions examples/client/src/elicitationUrlExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// URL elicitation allows servers to prompt the end-user to open a URL in their browser
// to collect sensitive information.

import { exec } from 'node:child_process';
import { createServer } from 'node:http';
import { createInterface } from 'node:readline';

Expand All @@ -29,6 +28,7 @@ import {
UnauthorizedError,
UrlElicitationRequiredError
} from '@modelcontextprotocol/client';
import open from 'open';

import { InMemoryOAuthClientProvider } from './simpleOAuthClientProvider.js';

Expand Down Expand Up @@ -272,15 +272,25 @@ async function elicitationLoop(): Promise<void> {
}
}

async function openBrowser(url: string): Promise<void> {
const command = `open "${url}"`;
const ALLOWED_SCHEMES = new Set(['http:', 'https:']);

exec(command, error => {
if (error) {
console.error(`Failed to open browser: ${error.message}`);
console.log(`Please manually open: ${url}`);
async function openBrowser(url: string): Promise<void> {
try {
const parsed = new URL(url);
if (!ALLOWED_SCHEMES.has(parsed.protocol)) {
console.error(`Refusing to open URL with unsupported scheme '${parsed.protocol}': ${url}`);
return;
}
});
} catch {
console.error(`Invalid URL: ${url}`);
return;
}

try {
await open(url);
} catch {
console.log(`Please manually open: ${url}`);
}
}

/**
Expand Down
26 changes: 18 additions & 8 deletions examples/client/src/simpleOAuthClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env node

import { exec } from 'node:child_process';
import { createServer } from 'node:http';
import { createInterface } from 'node:readline';
import { URL } from 'node:url';
Expand All @@ -13,6 +12,7 @@ import {
StreamableHTTPClientTransport,
UnauthorizedError
} from '@modelcontextprotocol/client';
import open from 'open';

import { InMemoryOAuthClientProvider } from './simpleOAuthClientProvider.js';

Expand Down Expand Up @@ -49,17 +49,27 @@ class InteractiveOAuthClient {
/**
* Opens the authorization URL in the user's default browser
*/
private static readonly ALLOWED_SCHEMES = new Set(['http:', 'https:']);

private async openBrowser(url: string): Promise<void> {
console.log(`🌐 Opening browser for authorization: ${url}`);

const command = `open "${url}"`;

exec(command, error => {
if (error) {
console.error(`Failed to open browser: ${error.message}`);
console.log(`Please manually open: ${url}`);
try {
const parsed = new URL(url);
if (!InteractiveOAuthClient.ALLOWED_SCHEMES.has(parsed.protocol)) {
console.error(`Refusing to open URL with unsupported scheme '${parsed.protocol}': ${url}`);
return;
}
});
} catch {
console.error(`Invalid URL: ${url}`);
return;
}

try {
await open(url);
} catch {
console.log(`Please manually open: ${url}`);
}
}
/**
* Example OAuth callback handler - in production, use a more robust approach
Expand Down
96 changes: 96 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading