Agregar filtros avanzados a docker_ps
All checks were successful
build-and-deploy / build (push) Successful in 12s
build-and-deploy / deploy (push) Successful in 4s

- Por defecto ahora trae TODOS los contenedores (all=true)
- Nuevo filtro filter_name para buscar por nombre (regex)
- Nuevo filtro filter_status para filtrar por estado
- Nuevo filtro filter_label para filtrar por labels
- Nuevo parámetro limit para limitar resultados
- Output incluye total de contenedores y si se aplicó filtro
This commit is contained in:
2025-10-16 17:54:04 -06:00
parent a162292e70
commit 910ed7c397

View File

@@ -18,9 +18,13 @@ server.registerTool(
'docker_ps',
{
title: 'Listar Contenedores',
description: 'Lista todos los contenedores Docker (en ejecución o todos)',
description: 'Lista todos los contenedores Docker con filtros opcionales. Por defecto trae todos los contenedores (running y stopped).',
inputSchema: {
all: z.boolean().optional().describe('Mostrar todos los contenedores (incluidos detenidos)')
all: z.boolean().optional().describe('Mostrar todos los contenedores (default: true)'),
filter_name: z.string().optional().describe('Filtrar por nombre del contenedor (regex)'),
filter_status: z.enum(['created', 'restarting', 'running', 'removing', 'paused', 'exited', 'dead']).optional().describe('Filtrar por estado'),
filter_label: z.string().optional().describe('Filtrar por label (formato: key=value)'),
limit: z.number().optional().describe('Limitar número de resultados (default: sin límite)')
},
outputSchema: {
containers: z.array(z.object({
@@ -29,21 +33,48 @@ server.registerTool(
image: z.string(),
state: z.string(),
status: z.string()
}))
})),
total: z.number(),
filtered: z.boolean()
}
},
async ({ all = false }) => {
async ({ all = true, filter_name, filter_status, filter_label, limit }) => {
try {
const containers = await docker.listContainers({ all });
// Construir filtros de Docker
const filters: any = {};
if (filter_name) {
filters.name = [filter_name];
}
if (filter_status) {
filters.status = [filter_status];
}
if (filter_label) {
filters.label = [filter_label];
}
const containers = await docker.listContainers({
all,
filters: Object.keys(filters).length > 0 ? filters : undefined
});
// Aplicar límite si se especifica
const limitedContainers = limit ? containers.slice(0, limit) : containers;
const output = {
containers: containers.map(c => ({
containers: limitedContainers.map(c => ({
id: c.Id.substring(0, 12),
name: c.Names[0].replace('/', ''),
image: c.Image,
state: c.State,
status: c.Status
}))
})),
total: containers.length,
filtered: !!(filter_name || filter_status || filter_label || limit)
};
return {
content: [{ type: 'text', text: JSON.stringify(output, null, 2) }],
structuredContent: output