ssh_connect_with_credential
Connect to SSH servers using saved credentials for secure remote access and management. Establishes authenticated connections with stored credential IDs and unique connection identifiers.
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-1248 (handler)Implements the core logic for the 'ssh_connect_with_credential' tool: parses arguments using ConnectWithCredentialSchema, retrieves stored credential, creates NodeSSH connection with appropriate auth (password or private key), adds to connection pool and context, updates credential lastUsed, returns success message.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, required) and connectionId (string, required). 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 entry returned by ListToolsRequest handler. Defines name, description, and inputSchema matching the ConnectWithCredentialSchema. Dispatches to handleConnectWithCredential in the CallToolRequest switch statement (line 512).{ 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)Switch case in CallToolRequest handler that routes 'ssh_connect_with_credential' calls to the handler function.case 'ssh_connect_with_credential': return await this.handleConnectWithCredential(args);