-
Notifications
You must be signed in to change notification settings - Fork 1.5k
chore(vscode): add vscode mcp factory #868
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
Changes from 8 commits
e884b3a
001fa6f
0d0783b
0741b8b
9d17572
f8a61de
39c3848
7a814d5
cc61b67
affe1d7
98fef06
1ff80f8
5a0cfb9
bcbc2fe
14b931d
fcd953c
35c464e
da5b0c6
ee59735
21e0396
922002e
d12b5aa
74e3ab5
dc149c1
76ba7f7
24ed62e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { Client } from '@modelcontextprotocol/sdk/client/index.js'; | ||
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'; | ||
import { FullConfig } from '../config.js'; | ||
import { ClientFactory } from '../mcp/proxyBackend.js'; | ||
import { Server } from '../mcp/server.js'; | ||
|
||
class VSCodeClientFactory implements ClientFactory { | ||
name = 'vscode'; | ||
description = 'Connect to a browser running in the Playwright VS Code extension'; | ||
|
||
constructor(private readonly _config: FullConfig) {} | ||
|
||
async create(server: Server, options: any): Promise<Client> { | ||
if (typeof options.connectionString !== 'string') | ||
throw new Error('Missing options.connectionString'); | ||
if (typeof options.lib !== 'string') | ||
throw new Error('Missing options.library'); | ||
|
||
const client = new Client(server.getClientVersion()!); | ||
await client.connect(new StdioClientTransport({ | ||
command: process.execPath, | ||
cwd: process.cwd(), | ||
args: [ | ||
new URL('./main.js', import.meta.url).pathname, | ||
JSON.stringify(this._config), | ||
options.connectionString, | ||
options.lib, | ||
], | ||
})); | ||
await client.ping(); | ||
Skn0tt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return client; | ||
} | ||
} | ||
|
||
export function createVSCodeClientFactory(config: FullConfig): ClientFactory { | ||
return new VSCodeClientFactory(config); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; | ||
import { BrowserContext } from 'playwright-core'; | ||
import { FullConfig } from '../config.js'; | ||
import * as mcpServer from '../mcp/server.js'; | ||
import { BrowserServerBackend } from '../browserServerBackend.js'; | ||
import { BrowserContextFactory, ClientInfo } from '../browserContextFactory.js'; | ||
|
||
const config: FullConfig = JSON.parse(process.argv[2]); | ||
Skn0tt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const connectionString = new URL(process.argv[3]); | ||
const lib = process.argv[4]; | ||
|
||
const playwright = await import(lib).then(mod => mod.default ?? mod) as typeof import('playwright'); | ||
|
||
class VSCodeBrowserContextFactory implements BrowserContextFactory { | ||
name = 'vscode'; | ||
description = 'Connect to a browser running in the Playwright VS Code extension'; | ||
|
||
async createContext(clientInfo: ClientInfo, abortSignal: AbortSignal): Promise<{ browserContext: BrowserContext; close: () => Promise<void>; }> { | ||
connectionString.searchParams.set('launch-options', JSON.stringify({ | ||
...config.browser.launchOptions, | ||
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. nit: why mix them up rather than keep in separate fields? 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. because that's how |
||
...config.browser.contextOptions, | ||
userDataDir: config.browser.userDataDir, | ||
})); | ||
|
||
const browser = await playwright.chromium.connect(connectionString.toString()); | ||
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. Is it supposed to work with WebKit and Firefox? 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. yes, I made that clearer in the code |
||
|
||
const context = browser.contexts()[0] ?? await browser.newContext(config.browser.contextOptions); | ||
|
||
return { | ||
browserContext: context, | ||
close: async () => { | ||
await browser.close(); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
await mcpServer.connect( | ||
() => new BrowserServerBackend(config, new VSCodeBrowserContextFactory()), | ||
new StdioServerTransport(), | ||
false | ||
); |
Uh oh!
There was an error while loading. Please reload this page.