CarolineDocs

Connect / MCP gateway

MCP gateway

A gateway in front of an MCP server asks Caroline before it forwards each tool call, and forwards it only on an exact yes. Caroline ships a reference gateway you can run as it is or read before you write your own.

Decide and enforce

Keys

KeyLooks likeSent as
Gateway keygk_<16 hex>.<secret>Authorization: Bearer gk_...
Agent keyak_<16 hex>.<secret>X-Agent-Token: ak_...

An author issues both in Keys. Each is shown once; Caroline keeps only a hash. A gateway may hold several keys, which is how you rotate. An agent key goes to the agent's runtime, never to the model. A gateway key may ask evaluations and nothing else.

The call

POST https://pdp.example.com/access/v1/evaluation
Authorization: Bearer gk_0123456789abcdef.<secret>
X-Agent-Token: ak_fedcba9876543210.<secret>
X-Request-ID: <your correlation id>
Content-Type: application/json

{
  "subject":  {"type": "agent", "id": "a_supply_agent"},
  "action":   {"name": "read"},
  "resource": {"type": "inventory", "id": "r_stock_azure_prod"},
  "context":  {"on_behalf_of": "u_plan_mia", "via": "mcp-reference-gateway"}
}
FieldComes fromCan the model choose it?
subject.idthe agent's runtime, header X-Agent-Idno
context.on_behalf_ofthe person whose session started the agent, header X-On-Behalf-Ofno
action.name, resource.typeyour mapping, by tool nameno
resource.idyour mapping: fixed, or one named argumentthe one id; Caroline resolves it and ignores any type the call claims

The answers

AnswerMeansThe gateway
200 {"decision": true}allowedforwards the call
200 {"decision": false}denied by policyrefuses; the reason goes to your operator's log
401 invalid_tokenthe gateway key is missing, wrong or revokedrefuses; page someone
403 insufficient_scopethe key may not call that routerefuses
403 invalid_agent_credentialthe agent key is wrong, revoked or someone else'srefuses; tell the agent's owner
503 engine_unavailablethe engine cannot answerrefuses; retry later
anything else, or a timeoutunknownrefuses

Fail closed

Never log

Keep these out of every log

The gateway key and any part of its secret (the id gk_... is fine); the agent key; the Authorization and X-Agent-Token headers; the tool call's arguments. Never show Caroline's reason to the model: tell it only "not permitted by policy".

The mapping

A JSON file turns a tool name into a question. An unmapped tool is refused without asking.

{
  "via": "mcp-reference-gateway",
  "tools": {
    "read_stock":      {"action": "read",    "resource_type": "inventory",     "resource_id_from": "stock_id"},
    "prepare_payment": {"action": "prepare", "resource_type": "payment_order", "resource_id": "r_pay_vendor"}
  }
}

Each tool takes exactly one of resource_id (a fixed id) and resource_id_from (the argument whose string value is the id, at most 256 characters).

Run the reference gateway

One Python file, standard library only, about 400 lines.

export CAROLINE_GATEWAY_KEY_FILE=~/.caroline/gateway.key   # mode 0600
python3 kit/mcp-gateway/gateway.py \
  --listen 127.0.0.1:8200 \
  --pdp https://pdp.example.com \
  --upstream http://127.0.0.1:9000/mcp \
  --mapping kit/mcp-gateway/mapping.example.json

Point your MCP client at http://127.0.0.1:8200/mcp and send X-Agent-Id, X-Agent-Token and X-On-Behalf-Of with each request. Those three are never passed on to the MCP server.

JSON-RPC errorWhen
-32001denied: "not permitted by policy"
-32002refused: a 401 or 403, or a call it cannot turn into a question
-32003unavailable: a 503, a timeout, no usable answer

Options: --pdp-timeout (2 s), --upstream-timeout (60 s), --name, --ready-file.

What it does not decide

Caroline DocsAuthZEN 1.0 · OPA