k8s.pod_logs
Retrieve container logs from Kubernetes pods to diagnose application issues and monitor runtime behavior in development clusters.
Instructions
Obtiene los logs de un pod específico en Kubernetes. Útil para debugging y troubleshooting.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| namespace | Yes | namespace del pod | |
| podName | Yes | nombre exacto del pod | |
| tail | No | número de líneas desde el final (default: 100) | |
| previous | No | ver logs del contenedor anterior (crashed) | |
| container | No | nombre del contenedor específico (si tiene múltiples) | |
| since | No | logs desde hace X tiempo (e.g., '5m', '1h', '2d') | |
| timestamps | No | incluir timestamps en los logs |
Implementation Reference
- src/tools/getlogs.ts:27-72 (handler)Core implementation of the tool logic: executes `kubectl logs` with dynamic arguments based on options to fetch and process Kubernetes pod logs.export async function getPodLogs( namespace: string, podName: string, options: PodLogsOptions = {} ): Promise<PodLogsResult> { const { tail = DEFAULT_TAIL, previous, container, since, timestamps } = options; const result: PodLogsResult = { namespace, podName, container, logs: "", lineCount: 0, truncated: false, }; // Helper para construir args condicionales sin ifs por todos lados const pushIf = (cond: any, ...args: string[]) => cond && argsList.push(...args); const argsList: string[] = ["logs", podName, "-n", namespace, `--tail=${tail}`]; pushIf(previous, "--previous"); pushIf(container, "-c", container!); pushIf(since, `--since=${since}`); pushIf(timestamps, "--timestamps"); try { const { stdout, stderr } = await execFileAsync("kubectl", argsList, { maxBuffer: MAX_BUFFER, }); if (stderr?.trim()) { // kubectl a veces escribe warnings en stderr aunque funcione console.warn("kubectl logs warning:", stderr.trim()); } result.logs = stdout.trim(); result.lineCount = result.logs ? result.logs.split("\n").length : 0; // "truncated" es básicamente: pedí N y me devolvió N líneas (o más) result.truncated = result.lineCount >= tail; } catch (err: any) { result.error = `Error executing kubectl logs: ${err?.message ?? String(err)}`; } return result; }
- src/index.ts:71-79 (schema)Zod input schema defining the parameters for the k8s.pod_logs tool, including namespace, podName, and various log options.inputSchema: z.object({ namespace: z.string().describe("namespace del pod"), podName: z.string().describe("nombre exacto del pod"), tail: z.number().optional().describe("número de líneas desde el final (default: 100)"), previous: z.boolean().optional().describe("ver logs del contenedor anterior (crashed)"), container: z.string().optional().describe("nombre del contenedor específico (si tiene múltiples)"), since: z.string().optional().describe("logs desde hace X tiempo (e.g., '5m', '1h', '2d')"), timestamps: z.boolean().optional().describe("incluir timestamps en los logs"), }),
- src/index.ts:67-102 (registration)MCP server registration of the 'k8s.pod_logs' tool, including tool name, description, input schema, and thin handler wrapper that calls the core getPodLogs function.server.registerTool( "k8s.pod_logs", { description: "Obtiene los logs de un pod específico en Kubernetes. Útil para debugging y troubleshooting.", inputSchema: z.object({ namespace: z.string().describe("namespace del pod"), podName: z.string().describe("nombre exacto del pod"), tail: z.number().optional().describe("número de líneas desde el final (default: 100)"), previous: z.boolean().optional().describe("ver logs del contenedor anterior (crashed)"), container: z.string().optional().describe("nombre del contenedor específico (si tiene múltiples)"), since: z.string().optional().describe("logs desde hace X tiempo (e.g., '5m', '1h', '2d')"), timestamps: z.boolean().optional().describe("incluir timestamps en los logs"), }), }, async (args) => { const results = await getPodLogs( args.namespace, args.podName, { tail: args.tail, previous: args.previous, container: args.container, since: args.since, timestamps: args.timestamps, } ); return { content: [ { type: "text", text: JSON.stringify(results, null, 2), }, ], }; } )
- src/tools/getlogs.ts:6-12 (helper)TypeScript interface defining the options for pod logs retrieval, matching the tool's input schema.export interface PodLogsOptions { tail?: number; previous?: boolean; container?: string; since?: string; timestamps?: boolean; }
- src/tools/getlogs.ts:14-22 (helper)TypeScript interface defining the structure of the pod logs result returned by the handler.export interface PodLogsResult { namespace: string; podName: string; container?: string; logs: string; lineCount: number; truncated: boolean; error?: string; }