# googlecloudplatform/kubectl-ai architecture

> Architecture diagram of the GitHub repository googlecloudplatform/kubectl-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/googlecloudplatform/kubectl-ai
- Repository: https://github.com/googlecloudplatform/kubectl-ai

## Overview

#### Purpose and Entry Points
`kubectl-ai` is a Go CLI/kubectl plugin that turns natural-language requests into Kubernetes operations through an LLM agent. Primary startup paths are the normal CLI in `cmd/main.go` and MCP-server mode in `cmd/mcp.go`. It accepts interactive terminal input, piped/one-shot prompts, or an HTML UI.

#### Application Composition
The agent is assembled around conversation state, an LLM client, tools, UI, and optional persistence. `pkg/agent/manager.go` coordinates request execution; `pkg/agent/conversation.go` maintains multi-turn context; `pkg/api/models.go` supplies shared API models. The agent iterates up to configured limits, letting model tool calls drive execution and returning streamed/final output to the UI.

#### LLM Provider Boundary
The nested `gollm` Go module abstracts model-specific APIs behind common interfaces and a provider factory: `gollm/interfaces.go`, `gollm/factory.go`, `gollm/schema.go`. Implementations support Gemini, OpenAI-compatible/Azure OpenAI, Anthropic, Grok, Bedrock, Ollama, and llama.cpp, separating provider authentication/protocols from agent orchestration. Journal/persistence facilities support recording and replayable LLM HTTP/context data in `gollm/http_journal.go` and `gollm/persist.go`.

#### Tool Execution and Kubernetes Safety Boundary
Tools exposed to the model are defined and assembled in `pkg/tools/interfaces.go`, `pkg/tools/tools.go`, and `pkg/tools/kubectl_tool.go`. Built-ins include kubectl and bash; YAML-defined custom tools and MCP-backed tools extend the same tool surface. Kubernetes commands can be filtered and routed through permission/sandbox controls, with execution backends in `pkg/sandbox/executor.go`, `pkg/sandbox/kubernetes.go`, and `pkg/sandbox/local.go`. This is the critical boundary between generated model instructions and cluster/local side effects.

#### MCP Integration Hub
In client mode, configured external MCP servers are discovered and translated into callable tools via `pkg/mcp/manager.go`, `pkg/mcp/stdio_client.go`, and `pkg/mcp/http_client.go`; the agent consumes them through `pkg/agent/mcp_client.go` and `pkg/tools/mcp_tool.go`. In server mode, `cmd/mcp.go` exposes native tools, optionally aggregates those external tools, and supports stdio or streamable HTTP transport. This makes the binary both an MCP tool consumer and provider.

#### State, Interaction, and Observability
Session storage is abstracted by `pkg/sessions/store.go` and managed through `pkg/sessions/manager.go`, with memory and filesystem implementations in `pkg/sessions/memory.go` and `pkg/sessions/filesystem.go`. UI adapters in `pkg/ui/terminal.go`, `pkg/ui/tui.go`, and `pkg/ui/html/htmlui.go` present the same agent flow through terminal, TUI, or web runtime. Execution/context logging is handled by `pkg/journal/recorder.go` and `pkg/journal/loader.go`.

#### Deployment and External Dependencies
The runtime needs a configured kubeconfig/kubectl and credentials for the selected LLM provider. Container packaging is `images/kubectl-ai/Dockerfile`; Kubernetes deployment manifests are `k8s/kubectl-ai.yaml` and `k8s/kubectl-ai-gke.yaml`. Local or cluster-hosted llama.cpp serving is supplied separately under `modelserving/k8s/llm-server.yaml` and `modelserving/images/llamacpp-server/Dockerfile`. Releases are built through GoReleaser (`.goreleaser.yaml`) and distributed as a standalone binary/Krew plugin.

Main entry points: [`pkg/agent/manager.go`](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/agent/manager.go) (Agent manager), [`pkg/mcp/manager.go`](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/mcp/manager.go) (MCP client manager), [`pkg/tools/tools.go`](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/tools/tools.go) (Tool registry), [`gollm/factory.go`](https://github.com/googlecloudplatform/kubectl-ai/blob/main/gollm/factory.go) (LLM factory & interfaces)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry &amp; Interaction"]
  node_cli["CLI entrypoint<br/>Go command<br/>[main.go]"]
  node_mcp_server["MCP server mode<br/>Go command<br/>[mcp.go]"]
  node_ui["Terminal, TUI &amp; HTML UI<br/>UI adapters<br/>[terminal.go]"]
end

subgraph group_agent["Agent Core"]
  node_agent_manager{{"Agent manager<br/>orchestrator<br/>[manager.go]"}}
  node_conversation["Conversation state<br/>context<br/>[conversation.go]"]
  node_api_models["Shared API models<br/>[models.go]"]
end

subgraph group_llm["LLM Boundary"]
  node_llm_factory{{"LLM factory &amp; interfaces<br/>provider abstraction<br/>[factory.go]"}}
  node_provider_apis(("LLM providers<br/>external APIs<br/>[interfaces.go]"))
  node_llm_persistence["LLM HTTP persistence<br/>journal persistence<br/>[http_journal.go]"]
end

subgraph group_tools["Tools &amp; Safety"]
  node_tool_registry{{"Tool registry<br/>tool surface<br/>[tools.go]"}}
  node_kubectl_tool["kubectl &amp; bash tools<br/>native tools<br/>[kubectl_tool.go]"]
  node_sandbox{{"Permissioned execution<br/>safety boundary<br/>[executor.go]"}}
  node_kubernetes(("Kubernetes cluster<br/>external runtime<br/>[kubernetes.go]"))
  node_local_shell(("Local execution<br/>external runtime<br/>[local.go]"))
end

subgraph group_mcp["MCP Hub"]
  node_mcp_manager{{"MCP client manager<br/>tool discovery<br/>[manager.go]"}}
  node_external_mcp(("External MCP servers<br/>external tools<br/>[stdio_client.go]"))
end

subgraph group_state["State &amp; Observability"]
  node_sessions[("Session manager &amp; store<br/>persistence<br/>[manager.go]")]
  node_journal["Execution journal<br/>observability<br/>[recorder.go]"]
end

node_packaging["Container &amp; Kubernetes packaging<br/>deployment"]

node_cli -->|"selects interaction"| node_ui
node_ui -->|"submits prompts"| node_agent_manager
node_cli -->|"one-shot or piped prompt"| node_agent_manager
node_agent_manager -->|"reads and updates"| node_conversation
node_agent_manager -->|"uses"| node_api_models
node_agent_manager -->|"invokes model"| node_llm_factory
node_llm_factory -->|"provider protocol"| node_provider_apis
node_llm_factory -.->|"records/replays"| node_llm_persistence
node_agent_manager -->|"executes tool calls"| node_tool_registry
node_tool_registry -->|"native tools"| node_kubectl_tool
node_kubectl_tool -->|"command request"| node_sandbox
node_sandbox -->|"approved cluster commands"| node_kubernetes
node_sandbox -->|"approved local commands"| node_local_shell
node_mcp_manager -->|"stdio or HTTP"| node_external_mcp
node_mcp_manager -->|"registers discovered tools"| node_tool_registry
node_agent_manager -->|"MCP tool client"| node_mcp_manager
node_mcp_server -->|"exposes native tools"| node_tool_registry
node_mcp_server -.->|"optionally aggregates"| node_mcp_manager
node_agent_manager -->|"loads and saves session"| node_sessions
node_agent_manager -.->|"records execution"| node_journal
node_packaging -.->|"packages runtime"| node_cli

click node_cli "https://github.com/googlecloudplatform/kubectl-ai/blob/main/cmd/main.go"
click node_mcp_server "https://github.com/googlecloudplatform/kubectl-ai/blob/main/cmd/mcp.go"
click node_ui "https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/ui/terminal.go"
click node_agent_manager "https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/agent/manager.go"
click node_conversation "https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/agent/conversation.go"
click node_api_models "https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/api/models.go"
click node_llm_factory "https://github.com/googlecloudplatform/kubectl-ai/blob/main/gollm/factory.go"
click node_provider_apis "https://github.com/googlecloudplatform/kubectl-ai/blob/main/gollm/interfaces.go"
click node_llm_persistence "https://github.com/googlecloudplatform/kubectl-ai/blob/main/gollm/http_journal.go"
click node_tool_registry "https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/tools/tools.go"
click node_kubectl_tool "https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/tools/kubectl_tool.go"
click node_sandbox "https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/sandbox/executor.go"
click node_kubernetes "https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/sandbox/kubernetes.go"
click node_local_shell "https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/sandbox/local.go"
click node_mcp_manager "https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/mcp/manager.go"
click node_external_mcp "https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/mcp/stdio_client.go"
click node_sessions "https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/sessions/manager.go"
click node_journal "https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/journal/recorder.go"
click node_packaging "https://github.com/googlecloudplatform/kubectl-ai/tree/main/images/kubectl-ai/Dockerfile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_mcp_server,node_ui toneBlue
class node_agent_manager,node_conversation,node_api_models toneAmber
class node_llm_factory,node_provider_apis,node_llm_persistence toneMint
class node_tool_registry,node_kubectl_tool,node_sandbox,node_kubernetes,node_local_shell toneRose
class node_mcp_manager,node_external_mcp toneIndigo
class node_sessions,node_journal toneTeal
class node_packaging toneNeutral
```

## Components

### Entry & Interaction

- [CLI entrypoint](https://github.com/googlecloudplatform/kubectl-ai/blob/main/cmd/main.go): Go command · Normal kubectl plugin and CLI startup. · `cmd/main.go`
- [MCP server mode](https://github.com/googlecloudplatform/kubectl-ai/blob/main/cmd/mcp.go): Go command · Exposes native and aggregated tools over MCP. · `cmd/mcp.go`
- [Terminal, TUI & HTML UI](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/ui/terminal.go): UI adapters · Presents interactive, piped, and web agent flows. · `pkg/ui/terminal.go`

### Agent Core

- [Agent manager](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/agent/manager.go): orchestrator · Runs bounded LLM/tool iterations and streams results. · `pkg/agent/manager.go`
- [Conversation state](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/agent/conversation.go): context · Maintains multi-turn request context. · `pkg/agent/conversation.go`
- [Shared API models](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/api/models.go): models · `pkg/api/models.go`

### LLM Boundary

- [LLM factory & interfaces](https://github.com/googlecloudplatform/kubectl-ai/blob/main/gollm/factory.go): provider abstraction · Selects a common client for configured providers. · `gollm/factory.go`
- [LLM providers](https://github.com/googlecloudplatform/kubectl-ai/blob/main/gollm/interfaces.go): external APIs · Gemini, OpenAI-compatible, Anthropic, Grok, Bedrock, Ollama, or llama.cpp. · `gollm/interfaces.go`
- [LLM HTTP persistence](https://github.com/googlecloudplatform/kubectl-ai/blob/main/gollm/http_journal.go): journal persistence · Records and replays LLM HTTP and context data. · `gollm/http_journal.go`

### Tools & Safety

- [Tool registry](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/tools/tools.go): tool surface · Assembles built-in, custom YAML, and MCP tools. · `pkg/tools/tools.go`
- [kubectl & bash tools](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/tools/kubectl_tool.go): native tools · Turns model tool calls into Kubernetes or local commands. · `pkg/tools/kubectl_tool.go`
- [Permissioned execution](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/sandbox/executor.go): safety boundary · Filters and routes side-effecting commands to execution backends. · `pkg/sandbox/executor.go`
- [Kubernetes cluster](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/sandbox/kubernetes.go): external runtime · Reached through kubeconfig and kubectl. · `pkg/sandbox/kubernetes.go`
- [Local execution](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/sandbox/local.go): external runtime · `pkg/sandbox/local.go`

### MCP Hub

- [MCP client manager](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/mcp/manager.go): tool discovery · Discovers external MCP tools over stdio or HTTP. · `pkg/mcp/manager.go`
- [External MCP servers](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/mcp/stdio_client.go): external tools · `pkg/mcp/stdio_client.go`

### State & Observability

- [Session manager & store](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/sessions/manager.go): persistence · Persists sessions in memory or filesystem storage. · `pkg/sessions/manager.go`
- [Execution journal](https://github.com/googlecloudplatform/kubectl-ai/blob/main/pkg/journal/recorder.go): observability · Records and loads execution and context logs. · `pkg/journal/recorder.go`

### Other components

- [Container & Kubernetes packaging](https://github.com/googlecloudplatform/kubectl-ai/tree/main/images/kubectl-ai/Dockerfile): deployment · Docker image and Kubernetes deployment manifests. · `images/kubectl-ai/Dockerfile`

## Connections

- CLI entrypoint → Terminal, TUI & HTML UI: selects interaction
- Terminal, TUI & HTML UI → Agent manager: submits prompts
- CLI entrypoint → Agent manager: one-shot or piped prompt
- Agent manager → Conversation state: reads and updates
- Agent manager → Shared API models: uses
- Agent manager → LLM factory & interfaces: invokes model
- LLM factory & interfaces → LLM providers: provider protocol
- LLM factory & interfaces → LLM HTTP persistence: records/replays
- Agent manager → Tool registry: executes tool calls
- Tool registry → kubectl & bash tools: native tools
- kubectl & bash tools → Permissioned execution: command request
- Permissioned execution → Kubernetes cluster: approved cluster commands
- Permissioned execution → Local execution: approved local commands
- MCP client manager → External MCP servers: stdio or HTTP
- MCP client manager → Tool registry: registers discovered tools
- Agent manager → MCP client manager: MCP tool client
- MCP server mode → Tool registry: exposes native tools
- MCP server mode → MCP client manager: optionally aggregates
- Agent manager → Session manager & store: loads and saves session
- Agent manager → Execution journal: records execution
- Container & Kubernetes packaging → CLI entrypoint: packages runtime
