-
Notifications
You must be signed in to change notification settings - Fork 592
feat: allow securing SSE transport with secret #323
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
Open
Skn0tt
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
Skn0tt:sse-secret
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,22 +19,31 @@ import { spawn } from 'node:child_process'; | |
import path from 'node:path'; | ||
import { test as baseTest } from './fixtures.js'; | ||
import { expect } from 'playwright/test'; | ||
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; | ||
import { Client } from '@modelcontextprotocol/sdk/client/index.js'; | ||
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'; | ||
|
||
// NOTE: Can be removed when we drop Node.js 18 support and changed to import.meta.filename. | ||
const __filename = url.fileURLToPath(import.meta.url); | ||
|
||
const test = baseTest.extend<{ serverEndpoint: string }>({ | ||
const test = baseTest.extend<{ serverEndpoint: URL }>({ | ||
serverEndpoint: async ({}, use) => { | ||
const cp = spawn('node', [path.join(path.dirname(__filename), '../cli.js'), '--port', '0'], { stdio: 'pipe' }); | ||
const cp = spawn('node', [path.join(path.dirname(__filename), '../cli.js'), '--port', '0', '--secret', 'mySecretValue'], { stdio: 'pipe' }); | ||
try { | ||
let stdout = ''; | ||
const url = await new Promise<string>(resolve => cp.stdout?.on('data', data => { | ||
const url = await new Promise<URL>(resolve => cp.stdout?.on('data', data => { | ||
stdout += data.toString(); | ||
const match = stdout.match(/Listening on (http:\/\/.*)/); | ||
if (match) | ||
resolve(match[1]); | ||
if (match) { | ||
const baseURL = new URL(match[1]); | ||
baseURL.searchParams.set('secret', 'mySecretValue'); | ||
resolve(baseURL); | ||
} | ||
})); | ||
|
||
cp.stderr.pipe(process.stderr); | ||
cp.stdout.pipe(process.stdout); | ||
|
||
await use(url); | ||
} finally { | ||
cp.kill(); | ||
|
@@ -43,22 +52,32 @@ const test = baseTest.extend<{ serverEndpoint: string }>({ | |
}); | ||
|
||
test('sse transport', async ({ serverEndpoint }) => { | ||
// need dynamic import b/c of some ESM nonsense | ||
const { SSEClientTransport } = await import('@modelcontextprotocol/sdk/client/sse.js'); | ||
const { Client } = await import('@modelcontextprotocol/sdk/client/index.js'); | ||
const transport = new SSEClientTransport(new URL(serverEndpoint)); | ||
const transport = new SSEClientTransport(serverEndpoint); | ||
const client = new Client({ name: 'test', version: '1.0.0' }); | ||
await client.connect(transport); | ||
await client.ping(); | ||
}); | ||
|
||
test('sse transport auth', async ({ serverEndpoint }) => { | ||
serverEndpoint.searchParams.delete('secret'); | ||
const transport = new SSEClientTransport(serverEndpoint); | ||
const client = new Client({ name: 'test', version: '1.0.0' }); | ||
await expect(() => client.connect(transport)).rejects.toThrow(/403/); | ||
}); | ||
|
||
test('streamable http transport', async ({ serverEndpoint }) => { | ||
// need dynamic import b/c of some ESM nonsense | ||
const { StreamableHTTPClientTransport } = await import('@modelcontextprotocol/sdk/client/streamableHttp.js'); | ||
const { Client } = await import('@modelcontextprotocol/sdk/client/index.js'); | ||
const transport = new StreamableHTTPClientTransport(new URL('/mcp', serverEndpoint)); | ||
serverEndpoint.pathname = '/mcp'; | ||
const transport = new StreamableHTTPClientTransport(serverEndpoint); | ||
Comment on lines
+62
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Modifying the 'serverEndpoint' URL directly may cause unintended side-effects in subsequent tests if the same instance is shared; consider cloning the URL instance to isolate changes. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
const client = new Client({ name: 'test', version: '1.0.0' }); | ||
await client.connect(transport); | ||
await client.ping(); | ||
expect(transport.sessionId, 'has session support').toBeDefined(); | ||
}); | ||
|
||
test('streamable http transport auth', async ({ serverEndpoint }) => { | ||
serverEndpoint.pathname = '/mcp'; | ||
serverEndpoint.searchParams.delete('secret'); | ||
const transport = new StreamableHTTPClientTransport(serverEndpoint); | ||
const client = new Client({ name: 'test', version: '1.0.0' }); | ||
await expect(() => client.connect(transport)).rejects.toThrow(/403/); | ||
}); |
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.
Users are bad at managing secrets. I think we should generate endpoint based on crypto-secure guid by default. And allow user to override it with full
--endpoint
. So they'll say npx @playwright/mcp --server http://localhost:0/my-secret for manual control, wdyt? We'll add path to certs later for tls.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.
Absolutely, but let's do that in a follow-up.
Full endpoint control will bring us trouble down the road when adopting MCP OAuth, which has some well-known paths.
--secret
is enough to solve the problem at hand.I don't think TLS is our business, would prefer the user to setup a reverse proxy for this. Imagine we implement TLS, and the next thing people ask for is automated Let's Encrypt provisioning.
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.
In that case we can just wait - we can't expect users to opt into the --secret.