Skip to main content
Glama

exec_container

Execute commands inside containers to run applications, perform maintenance tasks, or troubleshoot running processes within your containerized environment.

Instructions

Execute a command inside a container.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
containerYes
commandYesCommand to execute

Implementation Reference

  • Main handler implementation for exec_container tool. Parses args, runs 'podman exec <container> <command>' using run_podman helper, returns stdout or error message.
    async def exec_container(self, args: Dict[str, Any]) -> Dict[str, Any]:
        container = args.get("container")
        command = args.get("command", [])
        cmd_args = ["exec", container] + command
        result = run_podman(cmd_args)
        return {"output": result["stdout"] if result["success"] else f"Error: {result['stderr']}"}
  • Input schema and registration in the tools list for the exec_container tool.
    Tool(
        name="exec_container",
        description="Execute a command inside a container",
        inputSchema={
            "type": "object",
            "properties": {
                "container": {
                    "type": "string",
                    "description": "Container name or ID"
                },
                "command": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "Command to execute"
                }
            },
            "required": ["container", "command"]
        }
    ),
  • main_b.py:459-472 (registration)
    Mapping of tool name 'exec_container' to its handler method in the dispatch dictionary used by handle_tools_call.
    tool_handlers = {
        "list_containers": self.list_containers,
        "container_info": self.container_info,
        "start_container": self.start_container,
        "stop_container": self.stop_container,
        "restart_container": self.restart_container,
        "container_logs": self.container_logs,
        "run_container": self.run_container,
        "remove_container": self.remove_container,
        "exec_container": self.exec_container,
        "list_images": self.list_images,
        "pull_image": self.pull_image,
        "container_stats": self.container_stats,
    }
  • main.py:239-246 (handler)
    Alternative handler implementation using FastMCP decorator @mcp.tool, which also defines input schema via Pydantic Field. Executes podman exec similarly.
    @mcp.tool(title="Exec in container", description="Execute a command inside a container.")
    def exec_container(
        container: str = Field(...),
        command: List[str] = Field(..., description="Command to execute"),
    ) -> str:
        args = ["exec", container] + command
        result = run_podman(args)
        return result["stdout"] if result["success"] else f"Error: {result['stderr']}"
  • Shared helper function used by exec_container (and other tools) to run podman subprocess commands and parse results.
    def run_podman(args: List[str]) -> Dict[str, Any]:
        """Run a podman command and capture output"""
        try:
            cmd = ["podman"] + args
            result = subprocess.run(
                cmd,
                capture_output=True,
                text=True,
                timeout=30
            )
            return {
                "success": result.returncode == 0,
                "stdout": result.stdout.strip(),
                "stderr": result.stderr.strip(),
                "returncode": result.returncode,
            }
        except subprocess.TimeoutExpired:
            return {"success": False, "stdout": "", "stderr": "Command timed out", "returncode": -1}
        except Exception as e:
            return {"success": False, "stdout": "", "stderr": str(e), "returncode": -1}

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/kunwarmahen/podman-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server