ssh_connect_with_credential
Connect to SSH servers using saved credentials for remote access and management. This tool establishes secure connections with stored authentication details to execute commands and transfer files.
Instructions
Connect to SSH server using saved credentials
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| credentialId | Yes | Stored credential ID to use | |
| connectionId | Yes | Unique identifier for this connection |
Implementation Reference
- src/index.ts:1178-1247 (handler)Handler function that executes the ssh_connect_with_credential tool. Retrieves stored credential by ID, establishes SSH connection using node-ssh library, manages connection pool and contexts, updates credential usage timestamp, handles errors.private async handleConnectWithCredential(args: unknown) { const params = ConnectWithCredentialSchema.parse(args); if (connectionPool.has(params.connectionId)) { throw new McpError( ErrorCode.InvalidParams, `Connection ID '${params.connectionId}' already exists` ); } const credential = credentialStore.get(params.credentialId); if (!credential) { throw new McpError( ErrorCode.InvalidParams, `Credential ID '${params.credentialId}' not found` ); } const ssh = new NodeSSH(); try { const connectConfig: any = { host: credential.host, port: credential.port, username: credential.username, }; if (credential.privateKeyPath) { const privateKey = await fs.readFile(credential.privateKeyPath, 'utf8'); connectConfig.privateKey = privateKey; if (credential.passphrase) { connectConfig.passphrase = credential.passphrase; } } else if (credential.password) { connectConfig.password = credential.password; } else { throw new McpError( ErrorCode.InvalidParams, 'Credential has neither password nor private key' ); } await ssh.connect(connectConfig); connectionPool.set(params.connectionId, ssh); // Initialize connection context connectionContexts.set(params.connectionId, { ssh, currentWorkingDirectory: undefined, defaultWorkingDirectory: undefined }); // Update last used timestamp credential.lastUsed = new Date().toISOString(); credentialStore.set(params.credentialId, credential); return { content: [ { type: 'text', text: `Successfully connected to ${credential.host}:${credential.port} as ${credential.username} using saved credential '${params.credentialId}' (Connection ID: ${params.connectionId})`, }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `SSH connection failed: ${error instanceof Error ? error.message : String(error)}` ); }
- src/index.ts:143-146 (schema)Zod schema defining input parameters for the ssh_connect_with_credential tool: credentialId (string) and connectionId (string). Used for validation in the handler.const ConnectWithCredentialSchema = z.object({ credentialId: z.string().describe('Stored credential ID to use'), connectionId: z.string().describe('Unique identifier for this connection') });
- src/index.ts:408-419 (registration)Tool registration in the ListTools response, defining name, description, and inputSchema for ssh_connect_with_credential.{ name: 'ssh_connect_with_credential', description: 'Connect to SSH server using saved credentials', inputSchema: { type: 'object', properties: { credentialId: { type: 'string', description: 'Stored credential ID to use' }, connectionId: { type: 'string', description: 'Unique identifier for this connection' } }, required: ['credentialId', 'connectionId'] }, },
- src/index.ts:511-512 (registration)Dispatch registration in the CallToolRequest handler switch statement, mapping the tool name to its handler function.case 'ssh_connect_with_credential': return await this.handleConnectWithCredential(args);