diff --git a/mcp-docker-server/src/index.ts b/mcp-docker-server/src/index.ts index cfa9bfb..3374d9a 100644 --- a/mcp-docker-server/src/index.ts +++ b/mcp-docker-server/src/index.ts @@ -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