Service integrations
kftray exposes its forwarding capabilities through two standalone service boundaries. The MCP service gives external clients a structured control surface for Kubernetes discovery, saved configurations, and forwarding sessions. The proxy server carries application traffic between a client-facing listener and an upstream service or tunnel peer.
Service integration model
Service boundaries
The services separate control from traffic. The MCP service manages kftray state and forwarding actions. The proxy server relays network traffic after a path has been set up.
These services can run outside the desktop user interface. The MCP service can also be started by the desktop application when enabled. Proxy instances can run as relay processes in places such as Kubernetes workloads, which helps the forwarding system reach targets that are not directly reachable from the client.
| Service | External client | Main result |
|---|---|---|
| MCP service | MCP-aware client or agent | Resource data, configuration changes, and port-forward lifecycle actions |
| Proxy server | Forwarding client, exposed endpoint, or tunnel peer | Relayed TCP, UDP, HTTP, or upgraded stream traffic |
References: crates/…/main.rs• L1-24 crates/…/mcp.rs• L121-139 crates/…/main.rs• L126-157
Service relationships
The services meet through the forwarding domain rather than through a direct service-to-service call. An MCP client can create or start a forwarding configuration. The forwarding system then uses the selected target and protocol to route traffic, which can include a kftray proxy server.
The proxy server also supports endpoint exposure. A remote tunnel peer connects to the proxy server. Local HTTP or stream clients then use the proxy listener to reach the application on the other side of the tunnel.
The high-level relationship is shown below.
flowchart LR
C[External client] -->|MCP requests| M[MCP service]
M --> D[Forwarding configuration and lifecycle]
D --> F[Forwarding subsystem]
F -->|traffic path| P[Proxy server]
P --> U[Upstream service or tunnel peer]
References: crates/…/portforward.rs• L23-28 crates/…/portforward.rs• L231-352 crates/…/server.rs• L18-61
MCP service
Client protocol
The MCP service exposes kftray through Streamable HTTP at /mcp. Clients use JSON-RPC messages to initialize a session, list tools, and call a tool. A session identifier connects later requests to the initialized client. A POST without a session header creates a session, while later requests must use a known Mcp-Session-Id; an ended or unknown session is rejected. /health provides a simple service check.
The service also supports event-stream requests and explicit session termination. Notifications do not produce a normal result body, which preserves the protocol distinction between requests and notifications. Requests with a null JSON-RPC identifier are rejected because they do not identify a normal request.
See the MCP service page for the protocol and service lifecycle details.
References: crates/…/server.rs• L59-118 crates/…/server.rs• L155-265 crates/…/server.rs• L267-325 crates/…/server.rs• L405-493
MCP capabilities
The MCP tool set gives an external client a compact control surface for the main kftray workflows. The tools cover Kubernetes discovery, saved forwarding configurations, and active port-forward sessions.
| Capability group | Purpose |
|---|---|
| Kubernetes discovery | Find contexts, namespaces, services, pods, and ports that guide a forwarding request |
| Configuration management | Create, inspect, update, delete, import, and export forwarding configurations |
| Forwarding lifecycle | Start, list, and stop active port-forward sessions |
Port-forward tools support both saved configurations and new requests. A new request selects a workload type such as a service, pod, or proxy target. The tool validates the fields needed for that type before it creates the configuration and starts the path. The external schema accepts only the supported workload and protocol values, and it rejects blank values for fields that the selected workload needs.
Configuration tools also let clients use tags to group and filter saved paths. Tag keys are normalized for matching, and an update changes tags only when the request includes a tags value. This keeps agent-managed configurations compatible with the same saved configuration view used by other kftray clients.
The tools also report active sessions and give an agent a retry hint when a local port is busy. A failed new request does not leave its temporary configuration in the saved set. This keeps the external control surface aligned with the actual forwarding state.
Kubernetes discovery reports the port that a forwarding request should use. For a normal service, TCP entries resolve to their target port and UDP entries use the service port that the relay dials. A headless UDP service resolves to its target port. Named target ports use a matching ready pod. If the named service is absent, the tool uses the service name as a pod label selector and returns the distinct container ports it finds. Access errors remain errors instead of being treated as a missing service.
Tool calls use the same shared configuration and forwarding domain as the other kftray clients. This keeps an agent-created configuration subject to the same workload and protocol rules as a configuration created elsewhere.
References: crates/…/mod.rs• L20-89 crates/…/portforward.rs• L23-37 crates/…/portforward.rs• L65-121 crates/…/portforward.rs• L156-413 crates/…/kubernetes.rs• L373-503 crates/…/tests.rs• L456-723 crates/…/tests.rs• L859-1128 crates/…/tests.rs• L1149-1204 crates/…/config.rs• L1-5
Proxy server
Relay modes
The standalone proxy process loads a target address, listener ports, and relay mode from environment settings before it starts. Each server instance provides one kind of network path at a time.
| Relay mode | Traffic role |
|---|---|
| TCP | Carries bidirectional byte streams from connected clients to the target |
| UDP | Carries datagrams through a client connection and relays replies to that client |
| Reverse HTTP | Presents an HTTP endpoint and a WebSocket tunnel for a remote application connection |
TCP and UDP modes directly relay traffic to an upstream target. TCP target connections can use a resolved address and fall back to the configured host name when that address is stale. Reverse HTTP mode links an HTTP-facing listener to a tunnel peer. The peer can select raw TCP mode for stream traffic, or HTTP mode for requests and upgraded connections.
The process stops cleanly after a shutdown signal. See the proxy server page for executable settings and relay mode details.
References: crates/…/main.rs• L26-123 crates/…/main.rs• L126-157 crates/…/mod.rs• L1-9 crates/…/config.rs• L1-19 crates/…/server.rs• L28-55 crates/…/reverse.rs• L35-93 crates/…/tcp.rs• L38-100 crates/…/tcp.rs• L152-200 crates/…/websocket_server.rs• L617-638
Traffic paths
For direct modes, a client connects to the proxy listener and the proxy opens the matching upstream connection. The proxy moves data in both directions until the client, target, or service lifecycle closes the path. TCP target connections use keepalive settings so an idle dead peer can be detected.
Reverse HTTP mode uses two coordinated listeners. The HTTP side receives a request or upgraded connection. The WebSocket side carries request and response messages to a connected application peer. Stream identifiers keep concurrent upgraded or raw TCP connections separate. Flow credit limits unacknowledged data in each direction, and half-close support lets one side finish writing while it still receives a reply.
The reverse path is shown below.
sequenceDiagram
participant Client as HTTP or stream client
participant Relay as Proxy server
participant Peer as WebSocket tunnel peer
participant App as Local application
Client->>Relay: HTTP request or stream connection
Relay->>Peer: Tunnel message
Peer->>App: Request or stream data
App-->>Peer: Response or stream data
Peer-->>Relay: Tunnel response
Relay-->>Client: HTTP response or stream data
The tunnel keeps one active peer and checks it with WebSocket ping and pong messages. A replacement peer remains active if an older peer closes. When the active tunnel closes, pending requests receive a tunnel error and open streams close instead of waiting indefinitely.
References: crates/…/reverse_http.rs• L62-132 crates/…/reverse_http.rs• L136-254 crates/…/reverse_http.rs• L276-332 crates/…/tcp.rs• L38-124 crates/…/websocket_server.rs• L249-497 crates/…/websocket_server.rs• L500-614
Change checklist
When these files or sections are changed, remember to consider these:
- Keep MCP endpoint paths, session headers, protocol versions, and JSON-RPC tool names aligned. External MCP clients depend on these values together.
- Keep session creation and termination rules aligned with client retry behavior. A client must start a fresh session after the service removes an old one.
- Keep port-forward input rules aligned with workload behavior. Service, pod, and proxy targets need different fields.
- Keep start results and busy-port retry hints usable by an external agent. Saved and new configurations need different retry actions.
- Keep Kubernetes port discovery aligned with forwarding behavior. The reported port must remain the port that the selected service or pod path can reach for its protocol. Preserve the distinct TCP and UDP rules for normal and headless services.
- Keep configuration schema values, required workload fields, and tag filter rules aligned with the saved configuration contract.
- Keep proxy mode negotiation aligned between the HTTP listener and the tunnel peer. Raw TCP mode and HTTP mode use different traffic shapes on the listener.
- Keep tunnel heartbeat, replacement, pending-request cleanup, stream close, half-close, and flow-credit behavior compatible across the proxy server and its peer.
- Keep TCP target address fallback and keepalive behavior aligned with the target reachability assumptions of the deployment.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| MCP binds to port zero or a port already in use | The MCP service does not start or does not use the requested port | Use a valid, free port before enabling or launching the service |
| Client sends an MCP request with an unknown or ended session ID | The service returns a session error and does not run the operation | Initialize a fresh session and retain its Mcp-Session-Id |
| Client sends a request with a null JSON-RPC ID | The service returns an invalid-request error | Use a non-null ID for requests or omit the ID only for notifications |
| A new port-forward omits fields required by its workload type | The MCP tool rejects the request before it creates a usable path | Provide the service name, pod selector, or proxy address that matches the selected workload type |
| A saved port-forward uses a busy local port | The start fails and returns a port-specific retry action | Update the saved configuration before starting it again |
| A tool uses an unsupported protocol or workload type, or sends a blank required field | The configuration is rejected and is not saved | Use the values and fields defined by the tool schema |
| Kubernetes access denies the requested service | Port discovery returns an error instead of treating the service as a pod-label lookup | Grant the MCP process access to the service resource and namespace |
| A requested service is absent and no matching pods expose ports | Port discovery cannot provide a forwarding port | Use an existing service or a label selector that matches pods with container ports |
| Proxy configuration omits a required target, port, or mode | The proxy process exits before it can accept traffic | Provide the required relay settings for the selected mode |
| A proxy listener port is already in use | The proxy process returns a bind error instead of serving traffic | Allocate free listener ports before starting the relay |
| A TCP resolved address is stale and the configured host name cannot be reached | The client connection fails after the fallback attempt | Keep the target host name valid and reachable from the proxy |
| Reverse HTTP listener has no active tunnel peer | Client requests receive a service-unavailable or tunnel error response | Establish the WebSocket tunnel before sending application traffic |
| The tunnel peer misses the WebSocket pong deadline | The proxy closes the tunnel and pending work fails | Keep the peer alive and compatible with the heartbeat interval |
| Upgraded traffic lacks the required flow-control support | The reverse path rejects the upgrade with a gateway error | Use compatible proxy and peer versions for upgraded streams |
Key files
| File | Why to read it |
|---|---|
| crates/…/main.rs | Defines the standalone MCP service boundary, bind options, startup, and public endpoints. |
| crates/…/server.rs | Shows the MCP request, session, health, notification, and tool-call flow. |
| crates/…/mod.rs | Shows the capability groups exposed to external clients. |
| crates/…/portforward.rs | Shows how an external client lists, starts, retries, and stops forwarding sessions. |
| crates/…/kubernetes.rs | Shows how Kubernetes discovery turns cluster resources into forwarding choices and relay ports. |
| crates/…/tests.rs | Shows the external tool contract for validation, tags, forwarding choices, busy-port retries, and Kubernetes port discovery errors. |
| crates/…/config.rs | Connects MCP configuration tools to the shared configuration domain. |
| crates/…/mcp.rs | Shows how the desktop application starts, replaces, checks, and stops its MCP service. |
| crates/…/main.rs | Defines the standalone proxy process, environment contract, relay selection, and shutdown behavior. |
| crates/…/mod.rs | Shows the proxy service boundary and the relay components available to the standalone process. |
| crates/…/server.rs | Shows how one configured relay mode owns the proxy lifecycle. |
| crates/…/tcp.rs | Shows direct TCP relay behavior and target connection fallback. |
| crates/…/reverse_http.rs | Shows how HTTP clients use the reverse tunnel, raw TCP mode, and upgraded streams. |
| crates/…/websocket_server.rs | Shows the tunnel peer, stream routing, request correlation, heartbeat, and connection health behavior. |
| crates/…/tunnel_protocol.rs | Defines the cross-process messages and capabilities used by reverse traffic. |