返回源码地图

packages/experimental/computer-use-cua-driver-mcp/src/index.ts

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

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

1/**
2 * Exclusive computer use through an installed Cua Driver MCP executable.
3 * The MCP client owns discovery, execution, image admission, and reconnection.
4 * @module
5 */
6
7import type { Context, Fiber } from '@deepseek-ai/cordis'
8import z from '@deepseek-ai/schemastery'
9import { ComputerUseProviderName } from '@deepseek-ai/dsh-computer-use/brand'
10import * as McpClient from '@deepseek-ai/dsh-mcp-client'
11import type {} from '@deepseek-ai/dsh-computer-use'
12
13/** Cordis plugin identity for the installed Cua Driver provider. */
14export const name = 'experimental-computer-use-cua-driver-mcp'
15
16/** The shared reservation and tool registry must exist before connection. */
17export const inject = ['computerUse', 'tools']
18
19/** Installed executable and MCP connection overrides. */
20export interface Config {
21 /** Executable path or PATH command; defaults to `cua-driver`. */
22 command: string
23 /** Arguments passed without a shell; defaults to `['mcp']`. */
24 args: string[]
25 /** Per-call timeout in milliseconds; omission uses the MCP client's default. */
26 toolCallTimeoutMs?: number
27 /** Reconnection overrides; defaults to the MCP client's policy. */
28 reconnect: McpClient.ReconnectConfig
29}
30
31/** Validate executable options; the MCP client resolves connection defaults. */
32export const Config: z<Partial<Config>, Config> = z.object({
33 command: z.string().pattern(/[^\s]/u).default('cua-driver'),
34 args: z.array(String).default(['mcp']),
35 toolCallTimeoutMs: z.number().min(1),
36 reconnect: z.object({
37 enabled: z.boolean(),
38 initialDelayMs: z.number().min(1),
39 maxDelayMs: z.number().min(1),
40 maxAttempts: z.number().min(1).step(1),
41 }),
42})
43
44/**
45 * Reserve computer use and activate the installed Cua Driver's MCP tools.
46 * Initial connection or discovery failure rejects activation and rolls back.
47 * Disposal retains the reservation until the MCP child has finished teardown.
48 * @param ctx - context providing computer use and the tool registry.
49 * @param config - validated executable options and optional connection overrides.
50 * @returns initial MCP tool-discovery completion.
51 */
52export async function apply(ctx: Context, config: Config): Promise<void> {
53 const connection = McpClient.Config({
54 command: config.command,
55 args: config.args,
56 ...config.toolCallTimeoutMs === undefined ? {} : { toolCallTimeoutMs: config.toolCallTimeoutMs },
57 reconnect: config.reconnect,
58 transport: 'stdio',
59 serverName: 'cua-driver-mcp',
60 failOnStartupError: true,
61 })
62 // One effect orders child shutdown before release; separate fiber effects
63 // unload concurrently and could otherwise admit another live driver.
64 let child!: Fiber
65 ctx.effect(function* () {
66 yield ctx.computerUse.register(ComputerUseProviderName('cua-driver-mcp'))
67 child = ctx.plugin(McpClient, connection)
68 yield child.dispose
69 }, 'computer-use-cua-driver-mcp.connection')
70 await child.await()
71}