MCPcopy Create free account
hub / github.com/MarsWang42/Awel / createWriteTool

Function createWriteTool

packages/cli/src/tools/write.ts:8–58  ·  view source on GitHub ↗
(ctx: ToolContext)

Source from the content-addressed store, hash-verified

6import type { ToolContext } from './index.js';
7
8export function createWriteTool(ctx: ToolContext) {
9 const { cwd, emitSSE, confirmFileWrites } = ctx;
10 return tool({
11 description: 'Write content to a file. Creates the file and any parent directories if they do not exist. Overwrites existing files.',
12 inputSchema: z.object({
13 file_path: z.string().describe('The path to the file to write (absolute or relative to project root)'),
14 content: z.string().describe('The content to write to the file'),
15 }),
16 execute: async ({ file_path, content }) => {
17 if (confirmFileWrites && !isAutoApproved('fileWrites')) {
18 const confirmId = crypto.randomUUID();
19 const preview = content.length > 2000 ? content.slice(0, 2000) + '\n... (truncated)' : content;
20 const confirmData = JSON.stringify({
21 type: 'confirm',
22 confirmId,
23 toolName: 'Write',
24 summary: file_path,
25 details: preview,
26 });
27 emitSSE('confirm', confirmData);
28
29 const approved = await requestConfirmation(confirmId);
30 if (!approved) {
31 const resolvedData = JSON.stringify({
32 type: 'confirm_resolved',
33 confirmId,
34 approved: false,
35 });
36 emitSSE('confirm_resolved', resolvedData);
37 return 'Error: Write was rejected by the user. Try a different approach or ask the user for guidance.';
38 }
39
40 const resolvedData = JSON.stringify({
41 type: 'confirm_resolved',
42 confirmId,
43 approved: true,
44 });
45 emitSSE('confirm_resolved', resolvedData);
46 }
47
48 const fullPath = file_path.startsWith('/') ? file_path : resolve(cwd, file_path);
49 try {
50 mkdirSync(dirname(fullPath), { recursive: true });
51 writeFileSync(fullPath, content, 'utf-8');
52 return `Successfully wrote to ${file_path}`;
53 } catch (err) {
54 return `Error writing file: ${err instanceof Error ? err.message : String(err)}`;
55 }
56 },
57 });
58}

Callers 1

awelToolsFunction · 0.85

Calls 3

isAutoApprovedFunction · 0.85
emitSSEFunction · 0.85
requestConfirmationFunction · 0.85

Tested by

no test coverage detected