docker_logs
Fetch container logs to monitor application output and debug issues by retrieving log entries with optional filtering by time, line count, and timestamp display.
Instructions
Fetch logs from a container
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| container | Yes | Container name or ID | |
| tail | No | Number of lines to show from the end of logs | |
| follow | No | Follow log output (not recommended for MCP) | |
| since | No | Show logs since timestamp (e.g., "2023-01-01T00:00:00") | |
| timestamps | No | Show timestamps | |
| cwd | No | Working directory |
Implementation Reference
- src/tools/docker.ts:216-226 (handler)Main handler function that constructs and executes the 'docker logs' command using flags from input args and the executeDockerCommand helper.export async function dockerLogs(args: z.infer<typeof dockerLogsSchema>): Promise<ToolResponse> { const tailFlag = args.tail ? `--tail ${args.tail}` : ''; const followFlag = args.follow ? '-f' : ''; const sinceFlag = args.since ? `--since ${args.since}` : ''; const timestampsFlag = args.timestamps ? '-t' : ''; return executeDockerCommand( `docker logs ${tailFlag} ${followFlag} ${sinceFlag} ${timestampsFlag} ${args.container}`.trim(), args.cwd ); }
- src/tools/docker.ts:96-103 (schema)Zod schema defining the input parameters and validation for the docker_logs tool.export const dockerLogsSchema = z.object({ container: z.string().describe('Container name or ID'), tail: z.number().optional().describe('Number of lines to show from the end of logs'), follow: z.boolean().optional().default(false).describe('Follow log output (not recommended for MCP)'), since: z.string().optional().describe('Show logs since timestamp (e.g., "2023-01-01T00:00:00")'), timestamps: z.boolean().optional().default(false).describe('Show timestamps'), cwd: z.string().optional().describe('Working directory') });
- src/tools/docker.ts:378-393 (registration)Tool registration object in the dockerTools array, which is exported and included in the server's tool list response.{ name: 'docker_logs', description: 'Fetch logs from a container', inputSchema: { type: 'object', properties: { container: { type: 'string', description: 'Container name or ID' }, tail: { type: 'number', description: 'Number of lines to show from the end of logs' }, follow: { type: 'boolean', default: false, description: 'Follow log output (not recommended for MCP)' }, since: { type: 'string', description: 'Show logs since timestamp (e.g., "2023-01-01T00:00:00")' }, timestamps: { type: 'boolean', default: false, description: 'Show timestamps' }, cwd: { type: 'string', description: 'Working directory' } }, required: ['container'] } },
- src/index.ts:447-449 (registration)Dispatch logic in the main MCP server request handler that routes 'docker_logs' calls to the handler function after schema validation.if (name === 'docker_logs') { const validated = dockerLogsSchema.parse(args); return await dockerLogs(validated);
- src/tools/docker.ts:21-62 (helper)Helper function that executes Docker shell commands via child_process.exec and formats the ToolResponse.async function executeDockerCommand(command: string, cwd?: string): Promise<ToolResponse> { try { const { stdout, stderr } = await execAsync(command, { cwd: cwd || process.cwd(), shell: '/bin/bash', maxBuffer: 10 * 1024 * 1024, // 10MB buffer for logs timeout: 60000 // 60 second timeout for builds }); return { content: [ { type: "text" as const, text: JSON.stringify({ success: true, command: command, stdout: stdout.trim(), stderr: stderr.trim(), cwd: cwd || process.cwd() }, null, 2) } ] }; } catch (error: any) { return { content: [ { type: "text" as const, text: JSON.stringify({ success: false, command: command, stdout: error.stdout?.trim() || '', stderr: error.stderr?.trim() || error.message, exitCode: error.code || 1, cwd: cwd || process.cwd() }, null, 2) } ], isError: true }; } }