Skip to main content
Glama

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
NameRequiredDescriptionDefault
sessionIdYesSSH session ID
pathYesFile path to write
dataYesData to write to file
modeNoFile permissions mode

Implementation Reference

  • 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.`
        );
      }
    }
  • 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) }] };
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/oaslananka/mcp-ssh-tool'

If you have feedback or need assistance with the MCP directory API, please join our Discord server