fs_write
Write data to files on remote servers via SSH to automate file management tasks.
Instructions
Writes data to a file on the remote system
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sessionId | Yes | SSH session ID | |
| path | Yes | File path to write | |
| data | Yes | Data to write to file | |
| mode | No | File permissions mode |
Implementation Reference
- src/fs-tools.ts:40-89 (handler)The handler function that performs the file write operation, using an atomic write approach (temp file then rename).
export async function writeFile( sessionId: string, path: string, data: string, mode?: number ): Promise<boolean> { logger.debug('Writing file', { sessionId, path, size: data.length, mode }); const session = sessionManager.getSession(sessionId); if (!session) { throw new Error(`Session ${sessionId} not found or expired`); } try { // Use atomic write: write to temp file, then rename const tempPath = `${path}.tmp.${Date.now()}`; try { // Write to temporary file await session.sftp.put(Buffer.from(data, 'utf8'), tempPath); // Set permissions if specified if (mode !== undefined) { await session.sftp.chmod(tempPath, mode); } // Atomic rename await session.sftp.rename(tempPath, path); logger.debug('File written successfully', { sessionId, path }); return true; } catch (writeError) { // Clean up temp file on failure try { await session.sftp.delete(tempPath); logger.debug('Cleaned up temp file after error', { tempPath }); } catch (cleanupError) { logger.warn('Failed to clean up temp file', { tempPath, cleanupError }); } throw writeError; } } catch (error) { logger.error('Failed to write file', { sessionId, path, error }); throw wrapError( error, ErrorCode.EFS, `Failed to write file ${path}. Check directory permissions and disk space.` ); } } - src/types.ts:251-256 (schema)The Zod schema used to validate inputs for the 'fs_write' tool.
export const FSWriteSchema = z.object({ sessionId: z.string().min(1), path: z.string().min(1), data: z.string(), mode: z.number().optional() }); - src/mcp.ts:433-438 (registration)The tool handler case that processes 'fs_write' requests and calls the implementation.
case 'fs_write': { const params = FSWriteSchema.parse(args); const result = await writeFile(params.sessionId, params.path, params.data, params.mode); logger.info('File written', { sessionId: params.sessionId, path: params.path }); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; }