docker_monitoring
Monitor Docker containers to view logs, inspect resources, execute commands, and troubleshoot issues with real-time metrics and events.
Instructions
Monitor containers, get logs, inspect resources, and troubleshoot issues
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Monitoring action to perform | |
| container | No | Container name or ID | |
| follow | No | Follow log output | |
| tail | No | Number of lines to show from end of logs | |
| command | No | Command to execute in container (for exec) | |
| since | No | Show logs since timestamp | |
| until | No | Show logs until timestamp |
Implementation Reference
- src/index.ts:1747-1812 (handler)Handler function for the 'docker_monitoring' tool. Constructs Docker commands for monitoring actions (logs, inspect, exec, top, port, stats, events, diff) based on input parameters and executes them using executeDockerCommand.async ({ action, container, follow, tail, command, since, until }) => { try { let dockerCommand: string; switch (action) { case "logs": if (!container) throw new Error("Container name is required for logs"); dockerCommand = `docker logs`; if (follow) dockerCommand += " -f"; if (tail) dockerCommand += ` --tail ${tail}`; if (since) dockerCommand += ` --since ${since}`; if (until) dockerCommand += ` --until ${until}`; dockerCommand += ` ${container}`; break; case "inspect": if (!container) throw new Error("Container name is required for inspect"); dockerCommand = `docker inspect ${container}`; break; case "exec": if (!container) throw new Error("Container name is required for exec"); const execCommand = command || "bash"; dockerCommand = `docker exec -it ${container} ${execCommand}`; break; case "top": if (!container) throw new Error("Container name is required for top"); dockerCommand = `docker top ${container}`; break; case "port": if (!container) throw new Error("Container name is required for port"); dockerCommand = `docker port ${container}`; break; case "stats": dockerCommand = container ? `docker stats --no-stream ${container}` : "docker stats --no-stream"; break; case "events": dockerCommand = "docker events --since 1h"; if (container) dockerCommand += ` --filter container=${container}`; break; case "diff": if (!container) throw new Error("Container name is required for diff"); dockerCommand = `docker diff ${container}`; break; } const result = await executeDockerCommand(dockerCommand); return { content: [ { type: "text", text: `Monitoring ${action} completed:\n\n${result.stdout}${result.stderr ? `\nWarnings:\n${result.stderr}` : ""}` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error with monitoring operation: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } }
- src/index.ts:1737-1745 (schema)Input schema for the 'docker_monitoring' tool defining parameters for various monitoring actions.inputSchema: { action: z.enum(["logs", "inspect", "exec", "top", "port", "stats", "events", "diff"]).describe("Monitoring action to perform"), container: z.string().optional().describe("Container name or ID"), follow: z.boolean().optional().describe("Follow log output"), tail: z.number().optional().describe("Number of lines to show from end of logs"), command: z.string().optional().describe("Command to execute in container (for exec)"), since: z.string().optional().describe("Show logs since timestamp"), until: z.string().optional().describe("Show logs until timestamp") }
- src/index.ts:1732-1736 (registration)Registration of the 'docker_monitoring' MCP tool with title, description, and input schema.server.registerTool( "docker_monitoring", { title: "Docker Monitoring and Troubleshooting", description: "Monitor containers, get logs, inspect resources, and troubleshoot issues",
- src/index.ts:383-390 (helper)Helper function executeDockerCommand used by the docker_monitoring handler to run Docker commands.async function executeDockerCommand(command: string): Promise<{ stdout: string; stderr: string }> { try { const result = await execAsync(command); return result; } catch (error: any) { throw new Error(`Docker command failed: ${error.message}`); } }