Skip to main content
Glama

SSH MCP Server

by mfangtao
index.js4.91 kB
#!/usr/bin/env node import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError, } from '@modelcontextprotocol/sdk/types.js'; import { Client } from 'ssh2'; class SSHServer { constructor() { this.server = new Server({ name: 'ssh-server', version: '0.1.0', }, { capabilities: { tools: {}, }, }); this.setupToolHandlers(); this.server.onerror = (error) => console.error('[SSH Server Error]', error); } setupToolHandlers() { this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: 'execute_ssh_command', description: 'Execute command on remote server via SSH', inputSchema: { type: 'object', properties: { connection: { type: 'object', properties: { host: { type: 'string' }, port: { type: 'number', default: 22 }, username: { type: 'string' }, password: { type: 'string' }, privateKey: { type: 'string' }, }, required: ['host', 'username'], }, command: { type: 'string' }, }, required: ['connection', 'command'], }, }, ], })); this.server.setRequestHandler(CallToolRequestSchema, async (request) => { if (request.params.name !== 'execute_ssh_command') { throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}`); } if (!request.params.arguments) { throw new McpError(ErrorCode.InvalidParams, 'Missing arguments'); } const { connection, command } = request.params.arguments; if (!connection || !command) { throw new McpError(ErrorCode.InvalidParams, 'Missing connection or command'); } const result = await this.executeSSHCommand(connection, command); if (result.code !== 0) { return { content: [ { type: 'text', text: `Command failed with code ${result.code}\nSTDERR: ${result.stderr}`, }, ], isError: true, }; } return { content: [ { type: 'text', text: result.stdout, }, ], }; }); } executeSSHCommand(config, command) { return new Promise((resolve, reject) => { const conn = new Client(); let stdout = ''; let stderr = ''; let code = null; conn.on('ready', () => { conn.exec(command, (err, stream) => { if (err) { conn.end(); return reject(err); } stream .on('close', (exitCode) => { code = exitCode; conn.end(); resolve({ stdout, stderr, code }); }) .on('data', (data) => { stdout += data; }) .stderr.on('data', (data) => { stderr += data; }); }); }).on('error', (err) => { reject(err); }); const connectOptions = { host: config.host, port: config.port || 22, username: config.username, }; if (config.password) { connectOptions.password = config.password; } else if (config.privateKey) { connectOptions.privateKey = config.privateKey; } conn.connect(connectOptions); }); } async run() { const transport = new StdioServerTransport(); await this.server.connect(transport); console.error('SSH MCP server running on stdio'); } } const server = new SSHServer(); server.run().catch(console.error);

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mfangtao/mcp-ssh-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server