UNPKG

it-tools-mcp

Version:

Full MCP 2025-06-18 compliant server with 121+ IT tools, logging, ping, progress tracking, cancellation, and sampling utilities

148 lines (147 loc) 5.75 kB
import { z } from "zod"; import yaml from "js-yaml"; export function registerDockerComposeToRun(server) { server.registerTool("convert_docker_compose_to_run", { description: "Convert Docker Compose files to docker run commands", inputSchema: { content: z.string().describe("Docker Compose file content to convert"), }, // VS Code compliance annotations annotations: { title: "Convert Docker Compose To Run", description: "Convert Docker Compose files to docker run commands", readOnlyHint: false } }, async ({ content }) => { if (!content?.trim()) { return { content: [ { type: "text", text: "Please provide a Docker Compose file content to convert", }, ], }; } try { const parsed = yaml.load(content); if (!parsed?.services) { return { content: [ { type: "text", text: "Invalid Docker Compose file: missing services section", }, ], }; } const commands = []; for (const [serviceName, service] of Object.entries(parsed.services)) { const serviceObj = service; let command = 'docker run'; // Add detached mode by default command += ' -d'; // Add name command += ` --name ${serviceName}`; // Add ports if (serviceObj.ports) { for (const port of serviceObj.ports) { if (typeof port === 'string') { command += ` -p ${port}`; } else if (typeof port === 'object' && port.published && port.target) { command += ` -p ${port.published}:${port.target}`; } } } // Add volumes if (serviceObj.volumes) { for (const volume of serviceObj.volumes) { if (typeof volume === 'string') { command += ` -v ${volume}`; } else if (typeof volume === 'object' && volume.source && volume.target) { command += ` -v ${volume.source}:${volume.target}`; if (volume.read_only) { command += ':ro'; } } } } // Add environment variables if (serviceObj.environment) { if (Array.isArray(serviceObj.environment)) { for (const env of serviceObj.environment) { command += ` -e "${env}"`; } } else if (typeof serviceObj.environment === 'object') { for (const [key, value] of Object.entries(serviceObj.environment)) { command += ` -e "${key}=${value}"`; } } } // Add restart policy if (serviceObj.restart) { command += ` --restart ${serviceObj.restart}`; } // Add networks if (serviceObj.networks) { if (Array.isArray(serviceObj.networks)) { for (const network of serviceObj.networks) { command += ` --network ${network}`; } } else if (typeof serviceObj.networks === 'object') { for (const networkName of Object.keys(serviceObj.networks)) { command += ` --network ${networkName}`; } } } // Add working directory if (serviceObj.working_dir) { command += ` -w ${serviceObj.working_dir}`; } // Add user if (serviceObj.user) { command += ` --user ${serviceObj.user}`; } // Add image if (serviceObj.image) { command += ` ${serviceObj.image}`; } else { command += ` <image-name>`; } // Add command/entrypoint if (serviceObj.command) { if (Array.isArray(serviceObj.command)) { command += ` ${serviceObj.command.join(' ')}`; } else { command += ` ${serviceObj.command}`; } } commands.push(command); } return { content: [ { type: "text", text: commands.join('\n\n'), }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error converting Docker Compose to run commands: ${error.message}`, }, ], }; } }); }