write_project_file
Write content to local files within the VS Code workspace directory to manage project files and code during AI-assisted development.
Instructions
Write to a local file in the VS Code workspace (restricted to workspace directory)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| content | Yes |
Implementation Reference
- local-mcp-server.ts:60-68 (handler)Handler function that validates the file path using validatePath helper and writes the provided content to the file using fs.writeFileSync, returning success or error message.async ({ path, content }) => { try { const safePath = validatePath(path); fs.writeFileSync(safePath, content, 'utf8'); return { content: [{ type: 'text', text: `File written: ${path}` }] }; } catch (err: any) { return { content: [{ type: 'text', text: `File write error: ${err.message}` }] }; } }
- local-mcp-server.ts:59-59 (schema)Input schema using Zod: requires 'path' (string) for the file location and 'content' (string) for the file contents.{ path: z.string(), content: z.string() },
- local-mcp-server.ts:56-69 (registration)Registers the 'write_project_file' tool on the MCP server with name, description, input schema, and handler function.server.tool( 'write_project_file', 'Write to a local file in the VS Code workspace (restricted to workspace directory)', { path: z.string(), content: z.string() }, async ({ path, content }) => { try { const safePath = validatePath(path); fs.writeFileSync(safePath, content, 'utf8'); return { content: [{ type: 'text', text: `File written: ${path}` }] }; } catch (err: any) { return { content: [{ type: 'text', text: `File write error: ${err.message}` }] }; } } );
- local-mcp-server.ts:15-21 (helper)Security helper that resolves and validates the file path to ensure it stays within the workspace root, preventing path traversal attacks.function validatePath(filePath: string): string { const resolvedPath = path.resolve(WORKSPACE_ROOT, filePath); if (!resolvedPath.startsWith(WORKSPACE_ROOT)) { throw new Error('Path traversal detected - access denied'); } return resolvedPath; }