Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Give an AI agent or MCP server an API key

Checked with iron-vault 0.2.0-alpha (features: dev-mode), built from main at cc59b80.

Let an agent or MCP server call Brave Search, Venice and an FX API through Iron Vault, holding a base URL, a placeholder key and a caller token, never a provider key:

ProviderSlotThe agent’s base URL
Brave Searcha header: X-Subscription-Tokenhttp://127.0.0.1:7878/brave
Venicea header with a format: Authorization: Bearer {}http://127.0.0.1:7878/venice
An FX APIa query parameter: app_idhttp://127.0.0.1:7878/fx

The FX route uses OpenExchangeRates, which takes its key in an app_id query parameter; set paths to the endpoints your own FX provider uses. The Brave and Venice output below comes from runs with a dummy key; the FX approval summary and request were not captured from a run.

Prerequisites

  • The service running and Ready, with the loopback listener from getting started. This recipe adds to that recipe’s dev-config.toml.
  • A key for each provider you want. Each step stands alone, so skip a provider you don’t use.

Steps

1. Store the keys

add prompts for each key with echo off. Each --origin names the only host its key may be sent to:

iron-vault add brave  --origin api.search.brave.com
iron-vault add venice --origin api.venice.ai
iron-vault add fx_key --origin openexchangerates.org
iron-vault list
added brave (static) as record 8a6c0dd9-…
added venice (static) as record 469b5ab8-…
added fx_key (static) as record c8653d1f-…
brave  static  api.search.brave.com:443  healthy
fx_key  static  openexchangerates.org:443  healthy
httpbin_key  static  httpbin.org:443  healthy
venice  static  api.venice.ai:443  healthy

A pipe from a password manager, such as pass show fx | iron-vault add fx_key --origin openexchangerates.org, also works.

2. Issue the agent a caller token

iron-vault caller-token > ~/.iron-vault-tokens/agent
token_hash = "blake2b:…"
This token is shown once and the service keeps no copy of it.
…

3. Add the caller and routes

Add these tables to dev-config.toml, with your own token_hash:

[callers.agent]
listener = "loopback"
token_hash = "blake2b:<the agent's hash>"
routes = ["brave", "venice", "fx"]

[routes.brave]
prefix = "/brave"
upstream = "https://api.search.brave.com"
credential = "brave"
slot = { header = "X-Subscription-Token" }
methods = ["GET"]
paths = ["/res/v1/web/search"]
rate_limit = { requests = 60, per_seconds = 60, burst = 10 }

[routes.venice]
prefix = "/venice"
upstream = "https://api.venice.ai/api/v1"
credential = "venice"
slot = { header = "Authorization", format = "Bearer {}" }
methods = ["POST"]
paths = ["/chat/completions"]
rate_limit = { requests = 120, per_seconds = 60, burst = 20 }

[routes.fx]
prefix = "/fx"
upstream = "https://openexchangerates.org/api"
credential = "fx_key"
slot = { query = "app_id" }
methods = ["GET"]
paths = ["/latest.json", "/historical/*", "/currencies.json", "/usage.json"]
rate_limit = { requests = 10, per_seconds = 60, burst = 2 }

paths match the path after the route prefix, without the upstream’s base path: the agent’s /venice/chat/completions reaches Venice as /api/v1/chat/completions, and paths names /chat/completions. See configuration for slots and path patterns.

Approve it:

iron-vault config approve dev-config.toml
Approval summary
vault: 4b3faa85-…
version to commit: 3
config digest: blake2b:ce35d419…
committed version: 2
callers:
  + agent: listener loopback, auth token, rate limits none
route grants:
  + agent: granted brave, fx, venice
routes:
  + brave: prefix /brave, upstream https://api.search.brave.com/, credential brave, slot header X-Subscription-Token format "{}", ca file none, methods GET, paths /res/v1/web/search, rate limit 60 per 60s burst 10, confirm none
  + fx: prefix /fx, upstream https://openexchangerates.org/api, credential fx_key, slot query app_id, ca file none, methods GET, paths /currencies.json, /historical/*, /latest.json, /usage.json, rate limit 10 per 60s burst 2, confirm none
  + venice: prefix /venice, upstream https://api.venice.ai/api/v1, credential venice, slot header Authorization format "Bearer {}", ca file none, methods POST, paths /chat/completions, rate limit 120 per 60s burst 20, confirm none
Approve this config as version 3? Type yes to approve: yes
Approval passphrase for "approval passphrase":
approved version 3 (blake2b:ce35d419…)
callers: 1 added; route grants: 1 added; routes: 3 added; listeners: none; limits: none; logging: none; recovery: none

4. Try each route

AG="Proxy-Authorization: Bearer $(cat ~/.iron-vault-tokens/agent)"

Brave Search. The agent sends no X-Subscription-Token; Iron Vault adds it.

curl -s -H "$AG" -H 'Accept: application/json' 'http://127.0.0.1:7878/brave/res/v1/web/search?q=iron+vault'

With a dummy key, Brave answers:

{"error":{"code":"SUBSCRIPTION_TOKEN_INVALID","detail":"The provided subscription token is invalid.","meta":{"component":"authentication"},"status":422},"type":"ErrorResponse"}

Venice.

curl -s -H "$AG" -H 'Content-Type: application/json' http://127.0.0.1:7878/venice/chat/completions \
  -d '{"model": "google-gemma-3-27b-it", "messages": [{"role": "user", "content": "hi"}]}'

With a dummy key, Venice answers 401 Unauthorized with:

{"error":"Authentication failed"}

FX. Iron Vault appends app_id=<key> to the query. Set no base: the free plan serves USD rates only.

curl -s -H "$AG" 'http://127.0.0.1:7878/fx/latest.json?symbols=EUR'

A provider’s own refusal shows the request reached it with a key in the slot. The log (trimmed):

{"event":"request.completed","caller":"agent","route":"brave","status":422,"outcome":"success"}
{"event":"request.completed","caller":"agent","route":"venice","status":401,"outcome":"success"}

outcome is success whenever Iron Vault forwarded the request, whatever the provider answered.

5. Configure the agent

Give the agent three things:

  • Base URL: the route’s prefix on the listener, from the table at the top. The agent appends its usual API path. For Venice, an OpenAI-compatible client sets its base URL to http://127.0.0.1:7878/venice.
  • API key: any placeholder, such as placeholder. Iron Vault replaces it with the real key.
  • Caller token: sent as Proxy-Authorization: Bearer <token> on every request. If the agent can only add a header with a fixed name, see point a real application at Iron Vault.

These settings were not tried with a particular agent. Before relying on one, check that it uses the base URL for every request.

In a protected install, run the agent as its own OS account and make it a peer caller on the Unix socket, so there is no token file to steal:

[callers.fx-mcp]
listener = "local"
auth = "peer"
user = "fx-mcp"
routes = ["fx"]

What can go wrong

  • iron-vault: the service answered error 7 (config-invalid): the proposed config is invalid: route fx sends credential fx_key to an origin its record does not permit. The route’s upstream is not one of the credential’s origins. Fix the upstream, or replace the credential with the right --origin. No version is used up.
  • 403 Forbidden from Iron Vault. The method or path is not in the route. The log’s reason says which: method_not_allowed or path_not_allowed. Remember that paths leave out the upstream’s base path.
  • 401 or 422 from the provider. The request reached the provider with the stored key, and the provider refused it. Check the key, or replace it.
  • 400 Bad Request with foreign_marker in the log. The agent put a marker such as {iv:brave} in the slot of a route whose credential is not brave.
  • 429 Too Many Requests right after an approval or unlock. Each route’s token bucket starts empty whenever the service becomes Ready; wait a few seconds.

Reference

  • Configuration: slots, format, path patterns and rate limits.
  • CLI: add, list and caller-token.
  • Audit log: request.completed and its reason values.
  • Errors.