MCPcopy Create free account
hub / github.com/cloudflare/computer / createExecTool

Function createExecTool

packages/computer/src/tools/exec.ts:32–105  ·  view source on GitHub ↗
(
  options: ExecToolOptions,
)

Source from the content-addressed store, hash-verified

30const DEFAULT_MAX_BYTES = 64 * 1024;
31
32export function createExecTool(
33 options: ExecToolOptions,
34): Tool<{ command: string; cwd?: string; backend?: string }> {
35 const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
36 const backendIds = Object.keys(options.backends);
37 if (backendIds.length === 0) {
38 throw new Error("createExecTool: pass at least one backend in `backends`");
39 }
40 if (!backendIds.includes(options.defaultBackend)) {
41 throw new Error(
42 `createExecTool: defaultBackend ${JSON.stringify(options.defaultBackend)} is not one of ${backendIds.map((id) => JSON.stringify(id)).join(", ")}`,
43 );
44 }
45
46 const backendGuidance = backendIds
47 .map((id) => `- ${JSON.stringify(id)}: ${options.backends[id].description}`)
48 .join("\n");
49 const description = [
50 "Run a shell command in the workspace. The workspace exposes multiple backends, each with different capabilities.",
51 "Pick the cheapest backend that can run the command; fall back to a heavier one only when the lighter backend's command set doesn't cover what you need.",
52 "",
53 "Backends:",
54 backendGuidance,
55 "",
56 `Default backend: ${JSON.stringify(options.defaultBackend)}. Try this first for any command you're not sure about; if it fails with a "command not found" or a similar capability error, retry on a backend whose description covers the missing tool.`,
57 "Use for builds, test runs, typechecks, formatters, and git plumbing. Prefer the dedicated read, write, and edit tools for file operations. Long output is truncated to keep tool replies small.",
58 ].join("\n");
59
60 const backendSchema = z
61 .enum(backendIds as [string, ...string[]])
62 .optional()
63 .describe(
64 [
65 "Which backend to run on. Omit to use the default",
66 `(${JSON.stringify(options.defaultBackend)}). Set explicitly when the`,
67 "default backend is not capable of running the command. If a command fails because the backend lacks that tool, retry on a backend whose description covers it.",
68 ].join(" "),
69 );
70
71 return tool({
72 description,
73 inputSchema: z.object({
74 command: z.string().describe("Shell command, e.g. 'npm test' or 'git diff HEAD'."),
75 cwd: z.string().optional().describe("Working directory. Defaults to the workspace root."),
76 backend: backendSchema,
77 }),
78 execute: async ({ command, cwd, backend }) => {
79 const selectedBackend = backend ?? options.defaultBackend;
80 try {
81 const handle = await options.workspace.runtime.exec(command, {
82 cwd,
83 encoding: "utf8",
84 backend: selectedBackend,
85 });
86 const result = await handle.result();
87 return {
88 command,
89 cwd: cwd ?? null,

Callers 1

createAIToolsFunction · 0.85

Calls 3

truncateFunction · 0.70
execMethod · 0.65
resultMethod · 0.65

Tested by

no test coverage detected