Skip to content

The gateway

The gateway is a proxy that agents send model API calls and MCP traffic through. It enforces the models and mcp sections of the agent’s AgentPolicy:

  • Model allowlist: only the listed models (with * globs) can be called.
  • Daily token budget: shared by every agent the policy selects. Once it is used up, calls get 429 until midnight UTC, so agents are paused, not killed.
  • Provider keys: with auth configured, the gateway adds the API key itself, so agents never hold it.
  • MCP tool rules: per server, denyTools, requireApproval and allowTools. Denied tools are also removed from tools/list, so the agent never sees them.
  • Human approval: a call to a requireApproval tool creates a ToolApproval, posts to Slack, and waits for someone to approve or deny it.
  • Kill switch: frozen policies are refused here too.

Why a central gateway, not a sidecar

A sidecar shares the agent pod’s network identity, so network policy can’t tell the two apart: the agent could skip the sidecar and call the provider directly. With a separate gateway, the agent’s AgentPolicy allows egress to the gateway only, and only the gateway can reach the provider.

When a policy sets models or mcp, Leash automatically allows egress to the gateway. To make the gateway mandatory, don’t also list the provider in egress.fqdns.

apiVersion: leash.simplyzee.dev/v1alpha1
kind: AgentPolicy
metadata:
name: claude-agent
namespace: agents
spec:
agentSelector:
matchLabels:
app: claude-agent
# No egress.fqdns for api.anthropic.com: model calls must go through the gateway.
models:
allow: ["claude-sonnet-*", "claude-haiku-*"]
dailyTokenBudget: 2000000
mcp:
approvalTimeout: 10m
servers:
- name: github
denyTools: ["delete_*"]
requireApproval: ["merge_pull_request", "create_release"]
allowTools: ["get_*", "list_*", "search_*", "create_issue", "create_pull_request"]
Terminal window
$ kubectl get agp -n agents
NAME PHASE BACKEND FROZEN DEGRADED TOKENS TODAY AGE
claude-agent Enforced cilium false False 48211 3h

Pointing agents at the gateway

Upstream Agent setting
Anthropic ANTHROPIC_BASE_URL=http://leash-gateway.leash-system.svc:8080/anthropic
OpenAI OPENAI_BASE_URL=http://leash-gateway.leash-system.svc:8080/openai/v1
MCP server github http://leash-gateway.leash-system.svc:8080/mcp/github (Streamable HTTP)

If the gateway holds the provider key, give the agent a placeholder key: SDKs require one, and the gateway replaces it.

Configuring providers and MCP servers

The registry is config/gateway/gateway.yaml, deployed as the leash-gateway-config ConfigMap:

providers:
anthropic:
url: https://api.anthropic.com
auth:
header: x-api-key
valueFromEnv: ANTHROPIC_API_KEY
openai:
url: https://api.openai.com
auth:
header: Authorization
prefix: "Bearer "
valueFromEnv: OPENAI_API_KEY
mcpServers:
github:
url: http://github-mcp.mcp.svc:8080/mcp

Credentials, and the optional Slack webhook, come from the leash-gateway-credentials Secret in leash-system:

Terminal window
kubectl -n leash-system create secret generic leash-gateway-credentials \
--from-literal=ANTHROPIC_API_KEY=sk-ant-... \
--from-literal=SLACK_WEBHOOK_URL=https://hooks.slack.com/services/...

The gateway itself needs egress to the providers, the MCP servers and Slack.

Approvals

When an agent calls a requireApproval tool, the gateway holds the call and creates a ToolApproval in the agent’s namespace:

Terminal window
$ kubectl get tap -n agents
NAME DECISION SERVER TOOL POD EXPIRES AGE
merge-pull-request-x7k2p github merge_pull_request claude-agent-0 4m 12s
$ kubectl -n agents patch tap merge-pull-request-x7k2p --type merge -p '{"spec":{"decision":"Approved"}}'

Set "decision":"Denied" (and optionally "reason":"...") to refuse it. The decision is final, and the request fields can’t be edited. If nobody decides before approvalTimeout (default 5m), the call is refused. kubectl leash approvals, approve and deny do the same with less typing.

Approving from Slack

With SLACK_WEBHOOK_URL set, every request is posted to a Slack channel. Add SLACK_SIGNING_SECRET and the post gets Approve and Deny buttons; once someone clicks one, the message is replaced by the outcome and who decided.

  1. Create a Slack app at https://api.slack.com/apps → Create New App → From a manifest, with your own host:

    display_information:
    name: Leash
    description: Approve or deny AI agents' tool calls
    features:
    bot_user:
    display_name: Leash
    oauth_config:
    scopes:
    bot: [incoming-webhook]
    settings:
    interactivity:
    is_enabled: true
    request_url: https://leash.example.com/slack/interactions
  2. Install it to your workspace and pick the approvals channel. Copy the webhook URL (Incoming Webhooks) and the signing secret (Basic Information).

  3. Give them to the gateway:

    Terminal window
    kubectl -n leash-system create secret generic leash-gateway-credentials \
    --from-literal=ANTHROPIC_API_KEY=sk-ant-... \
    --from-literal=SLACK_WEBHOOK_URL=https://hooks.slack.com/services/... \
    --from-literal=SLACK_SIGNING_SECRET=... \
    --dry-run=client -o yaml | kubectl apply -f -
  4. Let Slack reach the gateway. Button clicks arrive on a separate port (8082), never the one agents use. With the chart:

    gateway:
    credentials:
    existingSecret: leash-gateway-credentials
    slack:
    interactive: true
    approvers: [U012AB3CD, U045EF6GH] # Slack member IDs; empty = anyone in the channel
    ingress:
    enabled: true
    className: nginx
    host: leash.example.com
    tls:
    - hosts: [leash.example.com]
    secretName: leash-slack-tls

    The Ingress routes only /slack/interactions, to a leash-gateway-slack Service. With kustomize, add a Service for port 8082 and your own Ingress, and pass --slack-approvers to the gateway.

How it’s kept safe:

  • Every click is checked against Slack’s signature (HMAC with the signing secret) and must be under 5 minutes old, so nobody else can decide by calling the endpoint. Unsigned requests get 401.
  • approvers limits who can decide; anyone else gets a private “you’re not on the approvers list” reply.
  • The Approve button asks for confirmation. Decisions are final: a later click, or one after the approval expired, just shows what happened.
  • The decision is recorded on the ToolApproval (reason: approved by @zee (U012AB3CD) in Slack), and the gateway needs patch on toolapprovals for it.
  • A decision made with kubectl doesn’t update the Slack message (webhooks can’t edit their posts); clicking it afterwards shows the outcome.

Who can approve? Anyone who can update toolapprovals. Agents need no Kubernetes RBAC for the gateway to work, so don’t give their ServiceAccounts any on toolapprovals, or an agent could approve its own calls.

The agent’s MCP client must wait while the call is held. Raise its request timeout above approvalTimeout.

How callers are identified

The gateway matches the request’s source IP to a running pod and the one AgentPolicy that selects it. It refuses the request if:

  • no running pod has that IP, or it is a host-network pod;
  • no AgentPolicy selects the pod;
  • more than one AgentPolicy selects the pod. The gateway needs exactly one; split overlapping selectors.

Source IPs of pod-to-Service traffic are preserved by kube-proxy and Cilium. If your cluster masquerades in-cluster traffic, the gateway can’t identify callers.

Metrics

On :9090/metrics:

  • leash_gateway_requests_total{namespace,policy,kind,outcome}, where kind is model or mcp, and outcome is allowed, denied, budget_exhausted, approved, approval_denied or approval_error
  • leash_gateway_tokens_total{namespace,policy,provider,model,direction}

Running several replicas

The gateway runs 2 replicas by default (chart value gateway.replicas), with a PodDisruptionBudget, a rolling update that never drops below the current count, and a soft spread across nodes. Any replica can serve any agent:

  • Token budgets are shared. Each replica adds the tokens it metered to the policy’s status.usage every --usage-flush-interval (15s). Writes are guarded by resourceVersion and retried on conflict, so no count is lost. Between flushes a replica checks the budget against the latest stored total plus what it hasn’t written yet.
  • AgentRuns are shared the same way: each replica adds its counts to the pod’s run.
  • Approvals wait on the replica that holds the call; any replica, the plugin or Slack can be where the decision comes from.

Limits

  • Budgets are soft. A call in flight when the budget runs out still finishes, and with several replicas a budget can be overshot by roughly what the other replicas spend in one flush interval. Lower --usage-flush-interval for a tighter limit, at the cost of more API writes.
  • Cache tokens count in full. Anthropic cache reads and writes are counted as input tokens.
  • Globs use Go path.Match rules. * doesn’t match /, so a model name such as org/model needs org/*.
  • Batches. A batched JSON-RPC request with a tool call that needs approval is refused as a whole.