ssh_write_file
Write content to a remote file via SFTP, creating or overwriting the file securely.
Instructions
Write content to a file on a remote host via SFTP. Creates or overwrites the file.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| host | Yes | SSH hostname or IP address | |
| port | No | SSH port (default: 22) | |
| username | No | SSH username (default: current user) | |
| privateKeyPath | No | Path to SSH private key | |
| password | No | SSH password. STRONGLY prefer key-based auth (privateKeyPath or ssh-agent). Passwords pass through MCP protocol frames as plaintext and may be logged by the transport or host process. | |
| path | Yes | Absolute path to the remote file | |
| content | Yes | File content to write |
Implementation Reference
- src/tools.ts:74-88 (registration)Registration of the 'ssh_write_file' tool on the MCP server. Uses server.tool() with Zod schema for path and content parameters.
server.tool( "ssh_write_file", "Write content to a file on a remote host via SFTP. Creates or overwrites the file.", { ...connectionParams, path: z.string().describe("Absolute path to the remote file"), content: z.string().describe("File content to write"), }, async ({ path, content, ...conn }) => { return connectionPool.withConnection(conn, async (client) => { await writeFile(client, path, content); return { content: [{ type: "text", text: `Wrote ${content.length} bytes to ${path}` }] }; }); }, ); - src/tools.ts:82-87 (handler)Handler function for ssh_write_file. Receives path, content, and connection params, then calls writeFile() helper and returns a success message with byte count.
async ({ path, content, ...conn }) => { return connectionPool.withConnection(conn, async (client) => { await writeFile(client, path, content); return { content: [{ type: "text", text: `Wrote ${content.length} bytes to ${path}` }] }; }); }, - src/tools.ts:77-81 (schema)Input schema for ssh_write_file. Extends connectionParams with 'path' (string) and 'content' (string) fields described with Zod.
{ ...connectionParams, path: z.string().describe("Absolute path to the remote file"), content: z.string().describe("File content to write"), }, - src/ssh.ts:443-455 (helper)The writeFile helper function that performs the actual SFTP write. Opens an SFTP session from the SSH client and calls sftp.writeFile() to write content to the remote path.
export async function writeFile(client: Client, remotePath: string, content: string): Promise<void> { const sftp = await getSftp(client); try { await new Promise<void>((resolve, reject) => { sftp.writeFile(remotePath, content, (err) => { if (err) return reject(err); resolve(); }); }); } finally { sftp.end(); } }