write_to_project
Write content to files within registered project directories, enabling developers to save specifications and manage project files across Claude interfaces.
Instructions
Write content to a file in registered project directory
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| projectId | Yes | ID of registered project | |
| filePath | Yes | Relative path within project | |
| content | Yes | File content to write | |
| createDirs | No | Create parent directories if they don't exist | |
| overwrite | No | Overwrite if file exists |
Implementation Reference
- src/tools/write_to_project.ts:6-68 (handler)The main handler function that implements the write_to_project tool logic, including project validation, path security checks, file existence handling, directory creation, and file writing using Node.js fs/promises.export async function writeToProject(args: WriteToProjectArgs): Promise<{ success: boolean; fullPath: string; action: 'created' | 'updated'; bytes: number; }> { const projectManager = new ProjectManager(); const project = await projectManager.getProject(args.projectId); if (!project) { throw new MCPError(ErrorCode.PROJECT_NOT_FOUND, `Project '${args.projectId}' not found`); } // Prevent directory traversal attacks if (args.filePath.includes('..') || isAbsolute(args.filePath)) { throw new MCPError(ErrorCode.INVALID_PATH, `Invalid file path: ${args.filePath}`); } const fullPath = join(project.rootPath, args.filePath); // Check if file exists let exists = false; try { await stat(fullPath); exists = true; } catch (e) { exists = false; } if (exists && args.overwrite === false) { throw new MCPError( ErrorCode.FILE_ALREADY_EXISTS, `File '${args.filePath}' already exists`, "Set 'overwrite' to true to replace existing content." ); } // Create directories if requested if (args.createDirs !== false) { await mkdir(dirname(fullPath), { recursive: true }); } try { await writeFile(fullPath, args.content, 'utf-8'); const stats = await stat(fullPath); // Update last accessed // TODO: move this to project manager project.lastAccessed = new Date().toISOString(); return { success: true, fullPath, action: exists ? 'updated' : 'created', bytes: stats.size }; } catch (error: any) { throw new MCPError( ErrorCode.INTERNAL_ERROR, `Failed to write file: ${error.message}` ); } }
- src/models/types.ts:95-101 (schema)TypeScript interface defining the input arguments for the write_to_project tool.export interface WriteToProjectArgs { projectId: string; filePath: string; content: string; createDirs?: boolean; overwrite?: boolean; }
- src/index.ts:80-94 (registration)Registration of the write_to_project tool in the ListToolsRequestSchema handler, including name, description, and input schema.{ name: 'write_to_project', description: 'Write content to a file in registered project directory', inputSchema: { type: 'object', properties: { projectId: { type: 'string', description: 'ID of registered project' }, filePath: { type: 'string', description: 'Relative path within project' }, content: { type: 'string', description: 'File content to write' }, createDirs: { type: 'boolean', description: 'Create parent directories if they don\'t exist' }, overwrite: { type: 'boolean', description: 'Overwrite if file exists' } }, required: ['projectId', 'filePath', 'content'] } },
- src/index.ts:129-132 (registration)Tool execution dispatch in the CallToolRequestSchema handler's switch statement, calling the writeToProject function.case 'write_to_project': return { content: [{ type: 'text', text: JSON.stringify(await writeToProject(args as unknown as WriteToProjectArgs), null, 2) }] };
- src/index.ts:29-29 (registration)Import of the writeToProject handler function.import { writeToProject } from './tools/write_to_project.js';