write_project_file
Write content to a local file within the VS Code workspace directory, ensuring restricted file operations for secure project development.
Instructions
Write to a local file in the VS Code workspace (restricted to workspace directory)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | ||
| path | Yes |
Implementation Reference
- local-mcp-server.ts:60-68 (handler)The handler function for the 'write_project_file' tool. It validates the file path for security, writes the provided content to the file using fs.writeFileSync, and returns a 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:56-69 (registration)Registers the 'write_project_file' tool with the MCP server, including 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:59-59 (schema)Zod schema defining the input parameters: path (string) and content (string).{ path: z.string(), content: z.string() },
- local-mcp-server.ts:15-21 (helper)Helper function used by the tool to validate and resolve the file path, 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; }