UNPKG

docker-mcp

Version:

A Model Context Protocol (MCP) server that enables AI agents to interact with Docker containers locally or remotely via SSH. Provides comprehensive Docker management capabilities including container operations, logs, monitoring, and cleanup.

75 lines 2.38 kB
import { Client } from 'ssh2'; import { Agent } from 'http'; /** * Custom SSH agent for Docker connections * This creates an SSH tunnel to the remote Docker socket */ export class DockerSSHAgent extends Agent { config; client = null; constructor(config) { super({ keepAlive: true }); this.config = config; } /** * Create an SSH tunnel to the Docker socket */ createConnection(opts, callback) { if (this.client) { this.client.end(); } this.client = new Client(); this.client.on('ready', () => { console.error(`SSH connection established to ${this.config.username}@${this.config.host}:${this.config.port}`); // Execute a command that connects to the Docker socket const command = `socat - UNIX-CONNECT:${this.config.socketPath}`; this.client?.exec(command, (err, stream) => { if (err) { console.error('SSH exec failed:', err); callback(err); return; } console.error(`SSH stream created to ${this.config.socketPath}`); callback(undefined, stream); }); }); this.client.on('error', (err) => { console.error('SSH connection error:', err); callback(err); }); const connectConfig = { host: this.config.host, port: this.config.port, username: this.config.username, readyTimeout: this.config.timeout || 10000 }; if (this.config.privateKey) { connectConfig.privateKey = this.config.privateKey; if (this.config.passphrase) { connectConfig.passphrase = this.config.passphrase; } } else { // Try to use SSH agent connectConfig.agent = process.env.SSH_AUTH_SOCK; } this.client.connect(connectConfig); } /** * Close the SSH connection */ destroy() { if (this.client) { this.client.end(); this.client = null; } super.destroy(); } } /** * Create a custom SSH agent for Docker connections */ export function createCustomSSHAgent(config) { return new DockerSSHAgent(config); } //# sourceMappingURL=ssh-agent.js.map