ssh_upload_file
Upload a local file to a remote server via SSH connection by specifying local and remote paths.
Instructions
Upload a file to the remote server
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| connectionId | Yes | ID of an active SSH connection | |
| localPath | Yes | Path to the local file | |
| remotePath | Yes | Path where the file should be saved on the remote server |
Implementation Reference
- src/index.ts:451-507 (handler)The primary handler function that implements the ssh_upload_file tool. It validates the connection and local file, initializes an SFTP session over the SSH connection, and uploads the file using fastPut.private async handleSSHUpload(params: any) { const { connectionId, localPath, remotePath } = params; // Check if the connection exists if (!this.connections.has(connectionId)) { return { content: [{ type: "text", text: `No active SSH connection with ID: ${connectionId}` }], isError: true }; } const { conn } = this.connections.get(connectionId)!; try { // Expand tilde if present in the local path const expandedLocalPath = localPath.replace(/^~/, os.homedir()); // Check if the local file exists if (!fs.existsSync(expandedLocalPath)) { return { content: [{ type: "text", text: `Local file does not exist: ${expandedLocalPath}` }], isError: true }; } // Get SFTP client const sftp: any = await new Promise((resolve, reject) => { conn.sftp((err: Error | undefined, sftp: any) => { if (err) { reject(new Error(`Failed to initialize SFTP: ${err.message}`)); } else { resolve(sftp); } }); }); // Upload the file await new Promise((resolve, reject) => { sftp.fastPut(expandedLocalPath, remotePath, (err: Error | undefined) => { if (err) { reject(new Error(`Failed to upload file: ${err.message}`)); } else { resolve(true); } }); }); return { content: [{ type: "text", text: `Successfully uploaded ${expandedLocalPath} to ${remotePath}` }] }; } catch (error: any) { return { content: [{ type: "text", text: `File upload failed: ${error.message}` }], isError: true }; } }
- src/index.ts:99-119 (schema)Input schema for ssh_upload_file tool defined in the server's capabilities declaration.ssh_upload_file: { description: "Upload a file to the remote server", inputSchema: { type: "object", properties: { connectionId: { type: "string", description: "ID of an active SSH connection" }, localPath: { type: "string", description: "Path to the local file" }, remotePath: { type: "string", description: "Path where the file should be saved on the remote server" } }, required: ["connectionId", "localPath", "remotePath"] } },
- src/index.ts:269-299 (registration)Registration of the tool call handler (CallToolRequestSchema), including the switch case that dispatches ssh_upload_file to its handler function.// Register tool call handler this.server.setRequestHandler(CallToolRequestSchema, async (request: any) => { const toolName = request.params.name; // Handle core SSH tools directly if (toolName.startsWith('ssh_')) { switch (toolName) { case 'ssh_connect': return this.handleSSHConnect(request.params.arguments); case 'ssh_exec': return this.handleSSHExec(request.params.arguments); case 'ssh_upload_file': return this.handleSSHUpload(request.params.arguments); case 'ssh_download_file': return this.handleSSHDownload(request.params.arguments); case 'ssh_list_files': return this.handleSSHListFiles(request.params.arguments); case 'ssh_disconnect': return this.handleSSHDisconnect(request.params.arguments); default: throw new Error(`Unknown SSH tool: ${toolName}`); } } // Handle Ubuntu tools directly if (toolName.startsWith('ubuntu_') && ubuntuToolHandlers[toolName]) { return ubuntuToolHandlers[toolName](request.params.arguments); } throw new Error(`Unknown tool: ${toolName}`); });
- src/index.ts:218-228 (schema)Duplicate input schema for ssh_upload_file in the ListToolsRequestSchema handler response.name: 'ssh_upload_file', description: 'Upload a file to the remote server', inputSchema: { type: 'object', properties: { connectionId: { type: 'string', description: 'ID of an active SSH connection' }, localPath: { type: 'string', description: 'Path to the local file' }, remotePath: { type: 'string', description: 'Path where the file should be saved on the remote server' } }, required: ['connectionId', 'localPath', 'remotePath'] }
- src/ubuntu-website-tools.ts:728-739 (schema)Input schema for ssh_upload_file included in the overridden ListTools response from Ubuntu tools module.name: 'ssh_upload_file', description: 'Upload a file to the remote server', inputSchema: { type: 'object', properties: { connectionId: { type: 'string', description: 'ID of an active SSH connection' }, localPath: { type: 'string', description: 'Path to the local file' }, remotePath: { type: 'string', description: 'Path where the file should be saved on the remote server' } }, required: ['connectionId', 'localPath', 'remotePath'] } },