MCP service
The MCP service exposes kftray port-forward work through the Model Context Protocol (MCP). An MCP client can discover Kubernetes targets, manage saved configurations, and start or stop forwards through one HTTP service. The service runs as the kftray-mcp executable or as a local service inside the desktop app.
Service boundary
The service turns MCP requests into kftray operations. The standalone executable owns its listener and prepares shared kftray data before it serves requests. The desktop app starts the same service in a background task when its saved MCP setting is enabled.
The desktop-managed service listens only on 127.0.0.1. The standalone executable defaults to 127.0.0.1:3000 but can bind another host and port when an operator chooses that deployment. The desktop settings surface reports whether the service is running and its local endpoint. See crates/…/main.rs, crates/…/lib.rs, and crates/…/mcp.rs.
| Service mode | Main purpose | Lifecycle owner |
|---|---|---|
Standalone kftray-mcp | Provide MCP access as its own process | Process start and stop |
| Desktop-managed MCP service | Provide MCP access while kftray runs | Desktop app settings and startup |
Protocol layer
The protocol layer maps MCP messages to JSON-RPC 2.0 requests over streamable HTTP. A client starts a session with initialize, receives the server identity and tool capability, then uses tools/list and tools/call to work with kftray. Requests return a JSON-RPC result or error. Notifications are accepted without a response.
The HTTP surface uses these paths:
| Path | Role |
|---|---|
POST /mcp | Receive JSON-RPC requests and tool calls |
GET /mcp | Open the session event stream |
DELETE /mcp | End a client session |
GET /health | Report service and protocol health |
The server gives each new MCP client a session ID. The client must send that ID on later requests until it ends the session. An ended or unknown session cannot continue with the old ID. The server also keeps accepting connections after a temporary listener error. It logs the error, waits briefly, and tries again instead of ending the service. This keeps the endpoint available when the process briefly runs out of file descriptors. See crates/…/protocol.rs, crates/…/server.rs, and crates/…/server.rs.
Client session flow
An MCP client first creates a session, then completes protocol setup before it uses the tool catalog. This flow shows how the protocol layer protects tool calls with a known session.
sequenceDiagram
participant C as MCP client
participant S as MCP service
participant K as kftray operations
C->>S: POST /mcp initialize
S-->>C: JSON-RPC result + Mcp-Session-Id
C->>S: POST /mcp notifications/initialized
S-->>C: Accepted
C->>S: POST /mcp tools/list
S-->>C: Tool definitions and input schemas
C->>S: POST /mcp tools/call
S->>K: Run selected kftray operation
K-->>S: Operation result or error
S-->>C: JSON-RPC tool result
C->>S: DELETE /mcp
S-->>C: Session terminated
Server lifecycle
The standalone process initializes shared configuration data and migrations before it accepts connections. The desktop app instead reads saved settings during app startup, starts the service only when enabled, and keeps its task and port in shared runtime state.
The desktop settings commands apply changes in a safe order. A port change binds the new port before it replaces a running service, so a busy new port does not first take down a working endpoint. Disabling the setting stops the task and enabling it starts the saved port. A port value of 0 is rejected.
The service accepts HTTP connections until the owning task stops. A temporary failure to accept a connection does not stop this loop. The service waits one second and retries, so a short resource limit does not leave the desktop status enabled while clients receive connection failures. The desktop settings UI uses the enabled, configured port, and running values as separate facts. A saved enabled state can therefore still report a failed start as not running. See crates/…/server.rs, crates/…/mcp.rs, crates/…/settings.rs, and frontend/…/McpServerSettings.tsx.
Tool catalog
The tool catalog is the product boundary exposed to an MCP client. Each tool publishes a name, a plain description, and an input schema. The service uses the name to route the call to one operation family and returns structured JSON as text content or a marked tool error.
| Tool family | User goal | Main capabilities |
|---|---|---|
| Kubernetes discovery | Find valid targets before a forward | List contexts, namespaces, services, pods, and service or pod ports |
| Port-forward control | Control live network access | List active forwards, start a forward, stop one forward, or stop all forwards |
| Configuration management | Maintain reusable forward definitions | List, inspect, create, update, delete, export, and import configurations |
The catalog is fixed at service start and advertises only the tool capability. Tool calls use the shared kftray configuration store and port-forward service, so MCP actions affect the same saved configurations and forwarding state used by other kftray interfaces. See crates/…/mod.rs.
Kubernetes discovery tools
The discovery tools help a client move from a kubeconfig and context to a usable forward target. They read Kubernetes data through the selected context and return names or port details that a later configuration or start call can use. Port results are unique by port, protocol, and name so that repeated pod data does not create duplicate choices.
The client can list contexts from the default kubeconfig or a supplied path. It can then list namespaces, services, pods, and ports with a selected context and namespace. Pod discovery supports a label selector. Port discovery returns the remote port that kftray will use: a TCP target port, a named target resolved from a ready pod, or the service port for a non-headless UDP service. A headless UDP service uses its target port. If the service does not exist, the tool treats the name as a pod label selector and lists matching container ports once per distinct port, protocol, and name. Other Kubernetes errors return an error instead of using that fallback. A missing service with no matching ports also returns an error. See crates/…/kubernetes.rs, crates/…/kubernetes.rs, crates/…/kubernetes.rs, and crates/…/tests.rs
References: crates/…/kubernetes.rs• L21-74 crates/…/kubernetes.rs• L76-224 crates/…/kubernetes.rs• L227-527 crates/…/tests.rs• L977-1204
Port-forward tools
The port-forward tools turn a saved or newly described target into a live local connection. They use the same kftray forwarding behavior as the main application and report the configuration ID and port details needed for later control. UDP and proxy targets use a Kubernetes relay before the local connection is ready.
start_port_forward accepts an existing configuration ID or enough fields to create a new configuration first. A newly created configuration is removed when the start operation fails. A busy local port produces a retry hint with a free port: an existing configuration tells the client to update its local port before starting again, while a new request can retry with the suggested port. Stop operations use the configuration ID, and the all-stop operation ends every active forward. See crates/…/portforward.rs, crates/…/portforward.rs, crates/…/portforward.rs, and crates/…/tests.rs
References: crates/…/portforward.rs• L23-37 crates/…/portforward.rs• L44-121 crates/…/portforward.rs• L128-407 crates/…/tests.rs• L859-975
Configuration tools
The configuration tools provide durable control of the definitions that describe a forward. They separate saving a configuration from starting its live forward, which lets a client prepare or revise a target before it opens a connection.
Configuration calls support Kubernetes service, pod, and proxy targets plus SSH port-forward targets. The service validates the fields that belong to each workload type, rejects unsupported workload or protocol values, and rejects blank required fields. It applies updates only to fields provided by the caller and uses identity-aware import behavior to update matching entries or create new ones. Tags can be saved and used as filters, while an update changes tags only when the caller provides the tags field. Delete stops an active forward first and avoids removing a configuration that another kftray process still uses. See crates/…/config.rs, crates/…/config.rs, crates/…/config.rs, and crates/…/tests.rs
References: crates/…/config.rs• L33-109 crates/…/config.rs• L132-460 crates/…/config.rs• L463-842 crates/…/tests.rs• L456-723
Related service boundary
The MCP service gives an AI client control of kftray operations through a protocol boundary. The service integrations page places this service beside the other ways kftray connects to external systems. The proxy server is a related service integration that exposes a different network path for traffic, so changes to one should not be assumed to change the other.
References: crates/…/main.rs• L37-100 crates/…/server.rs• L115-175 crates/…/mcp.rs• L47-139
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the accept loop retry behavior aligned with the service status shown by the desktop app. A listener error must not leave the service task alive but unreachable.
- Keep the advertised tool list, tool-name router, and tool input schemas aligned. A mismatch can make a client offer a call that the service cannot route.
- Keep session ID creation, validation, and deletion consistent across
POST,GET, andDELETErequests. A change in one path can leave clients with unusable sessions. - Keep desktop settings defaults and standalone executable defaults clear. They are separate lifecycle paths even though they use the same MCP server.
- Preserve configuration validation when adding workload fields. The required fields and allowed values differ between Kubernetes, proxy, and SSH targets.
- Preserve cleanup when a new configuration is created as part of a failed start. Otherwise failed tool calls can leave unused saved entries.
- Keep port discovery aligned with forwarding rules for named ports, UDP services, headless services, distinct port results, and the 404-only pod-label fallback. Do not use the fallback for access errors.
- Keep retry guidance aligned with the failure path. Existing configurations need an update before retry, while new requests can retry with the suggested local port.
- Keep tag normalization and filter rules aligned with configuration create, update, and list behavior.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The process temporarily cannot accept a connection because it has too few file descriptors | The service can become unreachable if the accept loop exits; with the retry loop it logs the error, waits one second, and keeps listening | Retry accept errors instead of propagating them from the server loop |
| The selected listen port is already in use | The service cannot start or a requested desktop port change is rejected | Bind the new port before switching the running service |
| A client sends an unknown or ended session ID | The MCP request is rejected and the client must initialize a new session | Retain and send the Mcp-Session-Id returned by initialization |
| A tool call omits a required field, uses a blank workload field, or uses fields for the wrong workload type | The operation returns a tool error and no valid action occurs | Use the tool schema and discovery tools before control calls |
| A new port-forward fails after its configuration is inserted | The start call can fail; cleanup removes the temporary configuration | Retry with the reported free port or inspect the returned error |
| Kubernetes access fails for the chosen context or kubeconfig | Discovery or forwarding returns an operation error; a non-404 service error does not use the pod-label fallback | Select a valid context and kubeconfig before starting the forward |
Key files
| File | Why to read it |
|---|---|
| crates/…/main.rs | Shows standalone startup, CLI inputs, endpoints, and shared data initialization |
| crates/…/server.rs | Shows HTTP routing, resilient listener lifecycle, sessions, JSON-RPC dispatch, and tool calls |
| crates/…/protocol.rs | Defines the MCP and JSON-RPC contract sent to clients |
| crates/…/mod.rs | Shows the complete catalog and name-to-tool routing |
| crates/…/kubernetes.rs | Shows the discovery capability used to find Kubernetes targets |
| crates/…/portforward.rs | Shows live forward start and stop behavior |
| crates/…/config.rs | Shows saved configuration lifecycle and workload validation |
| crates/…/tests.rs | Shows the contract checks for sessions, listener recovery, tool schemas, validation, tags, forwarding, retry hints, and Kubernetes port discovery |
| crates/…/mcp.rs | Shows desktop-managed startup, replacement, and stop behavior |
| crates/…/settings.rs | Shows the desktop command contract for MCP status and settings |
| frontend/…/McpServerSettings.tsx | Shows how users view and control the desktop MCP service |