返回源码地图

packages/experimental/browser-use-playwright-mcp/src/index.ts

main snapshot · da00f7f5358f · 正文引用章节 12;完整原文可核对,不声称全文件人工逐行审计

完整原文供逐行核对;页面收录不代表每行都经过人工语义审核。MIT 许可见 许可证。

1/** Chromium browser tools from the pinned Playwright MCP server. @module */
2
3import { dirname, join } from 'node:path'
4import { fileURLToPath } from 'node:url'
5import type { Context } from '@deepseek-ai/cordis'
6import { BrowserMcpConfig, mountSessionMcp, validateBrowserMcpConfig } from '@deepseek-ai/dsh-experimental-browser-use-runtime/mcp'
7
8/** Cordis identity for the Playwright MCP browser provider. */
9export const name = 'experimental-browser-use-playwright-mcp'
10
11/** Services required for scoped MCP startup and prompt readiness checks. */
12export const inject = ['browserUse', 'agents', 'tools', 'systemPrompt']
13
14/** Fixed Chromium launch or existing-browser attachment settings. */
15export type Config = BrowserMcpConfig
16
17/** Validate the launch or attachment configuration before activation. */
18export const Config: typeof BrowserMcpConfig = BrowserMcpConfig
19
20/**
21 * Expose Playwright's upstream tools in each live Session's scope.
22 * The pinned npm server runs under the current Node executable; browser state is not persisted by DSH.
23 * @param ctx - provider context supplying browser use, Agents, and tools.
24 * @param config - validated browser choice and optional tool timeout.
25 */
26export function apply(ctx: Context, config: Config): void {
27 validateBrowserMcpConfig(config)
28 const cli = join(dirname(fileURLToPath(import.meta.resolve('@playwright/mcp/package.json'))), 'cli.js')
29 // Upstream environment options can otherwise replace the configured browser
30 // mode or import an unrelated profile. Empty values mean absent to its parser.
31 const env = Object.fromEntries(Object.keys(process.env)
32 .filter(key => key.toUpperCase().startsWith('PLAYWRIGHT_MCP_'))
33 .map(key => [key, '']))
34 const args = [cli, '--browser', 'chromium']
35 if (config.mode === 'attach') {
36 args.push('--cdp-endpoint', config.endpoint)
37 } else {
38 args.push('--isolated')
39 if (config.headless) args.push('--headless')
40 if (config.executablePath !== undefined) args.push('--executable-path', config.executablePath)
41 }
42 mountSessionMcp(ctx, {
43 name: 'playwright-mcp',
44 exclusive: config.mode === 'attach',
45 command: process.execPath,
46 args,
47 env,
48 ...config.toolCallTimeoutMs === undefined ? {} : { toolCallTimeoutMs: config.toolCallTimeoutMs },
49 })
50}