{
  "id": "tutorial-agent-gateway-delegation-chain",
  "title": "Follow a delegation chain across agents: Agent Gateway with the IndyKite Token Service",
  "description": "Send one prompt through three Agent Gateways - orchestrator, retriever, MCP server - and watch the delegation chain grow hop by hop, with delegation tokens minted by the IndyKite Token Service and users authenticated by an external identity provider, in the ibac-demo reference app.",
  "category": "Agent Gateway",
  "order": 23,
  "image": "/assets/images/categories/card-agent-gateway.svg",
  "summary": "This tutorial uses the IndyKite Token Service: the delegation tokens are minted by a service you run next to the gateways, not by your identity provider's token exchange. Follow the chapters in order to compare the two ways of getting a delegation token, follow one prompt across three gateways, prepare the external identity provider, provision the project, configure and start the stack, run the prompts and read the decisions, use the chain as a rule in your own data policies, and switch on the optional parts.",
  "body": "<p class=\"notion\">This tutorial is grounded in the <a href=\"https://github.com/indykite/developer-hub/tree/master/a2a/ibac-demo\" class=\"fancy-link\" target=\"_blank\">ibac-demo</a> reference application. A fictional bank, CanBank, gives its staff and customers a console and a set of AI agents: an orchestrator, a retriever, a weather agent and an analyst. Every agent sits behind its own IndyKite Agent Gateway (IAG), and the agents reach the IndyKite MCP server through one more gateway. One prompt therefore crosses <strong>three gateways in a row</strong>, and each of them decides on its own whether this person may come this way.</p>\n<p class=\"notion\">What makes that possible is the <strong>delegation chain</strong>: a token that says \"this person, through this agent, then this one, then this one\". In this tutorial the chain is written by the <strong>IndyKite Token Service</strong> (ITS), a small service you run next to the gateways. People and agents are still authenticated by an <strong>external identity provider</strong> (IdP) of your choice; the demo ships wired to Auth0, and any OAuth 2.0 provider that issues JWT access tokens fits.</p>\n\n<h3 class=\"\">How this tutorial differs from the other two gateway tutorials</h3>\n<p class=\"notion\">The three tutorials use the same gateway. They differ in <strong>who mints the delegation token</strong> and in what they focus on. Pick the one that matches your setup:</p>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"></td>\n<td class=\"simple-table-header-color\"><strong><a href=\"/tutorials/tutorial-agent-gateway-mcp\" class=\"fancy-link\">MCP server with external token exchange</a></strong></td>\n<td class=\"simple-table-header-color\"><strong>This tutorial</strong></td>\n</tr>\n<tr>\n<td class=\"\"><strong>Who mints the delegation token</strong></td>\n<td class=\"\">Your IdP, with its OAuth 2.0 token exchange (RFC 8693).</td>\n<td class=\"\">The IndyKite Token Service, which you run yourself.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>What your IdP must offer</strong></td>\n<td class=\"\">Token introspection, the client credentials grant and token exchange.</td>\n<td class=\"\">Token introspection and the client credentials grant. No token exchange.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>What the protected agent or server receives</strong></td>\n<td class=\"\">One token: the gateway replaces <code class=\"notion\">Authorization</code> with the delegation token.</td>\n<td class=\"\">Two tokens: the person's own token in <code class=\"notion\">Authorization</code> and the delegation token in <code class=\"notion\">X-IK-Token</code>.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>Token Introspect configurations in the project</strong></td>\n<td class=\"\">For one issuer: the IdP.</td>\n<td class=\"\">For two issuers: the IdP (the person's token) and the Token Service (the delegation token).</td>\n</tr>\n<tr>\n<td class=\"\"><strong>The intent of a prompt in the token</strong></td>\n<td class=\"\">Not available.</td>\n<td class=\"\">Available: with IBAC on, the Token Service writes it into <code class=\"notion\">act.intent</code>.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>Focus</strong></td>\n<td class=\"\">One gateway in MCP proxy mode in front of a third-party MCP server.</td>\n<td class=\"\">A chain of gateways, A2A then MCP, and the chain as a rule in policies.</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">The third tutorial, <a href=\"/tutorials/tutorial-intent-ibac-gateway\" class=\"fancy-link\">Intent-based access control with Agent Gateway</a>, also uses the Token Service. It concentrates on what a gateway does with the <strong>meaning</strong> of a prompt, with stand-in agents. This one concentrates on the <strong>route</strong> of a prompt, with real agents, and only switches intent checks on at the end.</p>\n\n<h3 class=\"\">What you will have at the end</h3>\n<ol type=\"1\" class=\"numbered-list\" start=\"1\">\n<li>A clear picture of the two ways a gateway gets a delegation token, and when to choose which.</li>\n<li>An understanding of one prompt crossing three gateways: the person's token, the three delegation tokens and the three admission decisions.</li>\n<li>An external IdP prepared with three audiences, a login client and one machine-to-machine client per gateway.</li>\n<li>A provisioned IndyKite project: people, agents, workflows, policies and the two Token Introspect configurations.</li>\n<li>A running stack: a console, four agents, five gateways and the Token Service.</li>\n<li>Prompts sent as different people, with every decision of every hop on screen.</li>\n<li>A knowledge query and a policy of your own that answer only when the request came through the full chain.</li>\n</ol>\n\n<h3 class=\"\">Prerequisites</h3>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\">An <strong>IndyKite organization</strong> and a Service Account token, to provision a project with the <a href=\"https://github.com/indykite/developer-hub/tree/master/instant-stack\" class=\"fancy-link\" target=\"_blank\">instant-stack</a> app and its <code class=\"notion\">ibac</code> dataset (chapter 4).</li>\n<li style=\"list-style-type:disc\">An <strong>external OAuth 2.0 IdP</strong> you can configure: users, a login client for the console, one machine-to-machine client per gateway, and JWT access tokens with an audience (chapter 3).</li>\n<li style=\"list-style-type:disc\">A <strong>Google Gemini API key</strong> for the agents' model, or an Ollama instance reachable from Docker.</li>\n<li style=\"list-style-type:disc\"><strong>Docker and Docker Compose v2</strong>, and <code class=\"notion\">make</code>. The gateways and the Token Service run the <code class=\"notion\">indykite/agent-gateway</code> and <code class=\"notion\">indykite/token-service</code> images with one platform release tag from <a href=\"https://hub.docker.com/u/indykite\" class=\"fancy-link\" target=\"_blank\">hub.docker.com/u/indykite</a>.</li>\n</ul>\n\n<h3 class=\"\">Who this tutorial is for</h3>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\">Developers building <strong>agents that call other agents</strong> and who need every hop to know on whose behalf it works.</li>\n<li style=\"list-style-type:disc\">Platform and security engineers whose <strong>identity provider has no token exchange</strong>, or who want the delegation tokens issued by a service they control.</li>\n<li style=\"list-style-type:disc\">AI agents consuming this doc as a runbook - every chapter uses explicit service names, ports, file paths and commands.</li>\n</ul>\n\n<h3 class=\"\">Demo topology at a glance</h3>\n<p class=\"notion\">Everything runs locally with Docker Compose; only the IndyKite platform, your IdP and the model are remote.</p>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Service</strong></td>\n<td class=\"simple-table-header-color\"><strong>Role</strong></td>\n<td class=\"simple-table-header-color\"><strong>Port</strong></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">console</code></td>\n<td class=\"\">Web UI: login at the IdP, the CanBank app, a prompt box and the audit terminal that shows every decision.</td>\n<td class=\"\"><code class=\"notion\">3300</code></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">orchestrator-iag</code> → <code class=\"notion\">orchestrator</code></td>\n<td class=\"\">The entry gateway and the agent that talks to the user and delegates to the other agents.</td>\n<td class=\"\"><code class=\"notion\">9881</code></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">retriever-iag</code> → <code class=\"notion\">retriever</code></td>\n<td class=\"\">Answers graph and authorization questions as a client of the IndyKite MCP server.</td>\n<td class=\"\"><code class=\"notion\">9882</code></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">weather-iag</code> → <code class=\"notion\">weather</code></td>\n<td class=\"\">Weather from a public API, or from the graph for the bank's headquarters.</td>\n<td class=\"\"><code class=\"notion\">9884</code></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">analyst-iag</code> → <code class=\"notion\">analyst</code></td>\n<td class=\"\">Like the retriever, with several MCP servers behind it (chapter 8).</td>\n<td class=\"\"><code class=\"notion\">9885</code></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">mcp-iag</code></td>\n<td class=\"\">A gateway in MCP mode in front of the IndyKite MCP server. Every agent's MCP call goes through it.</td>\n<td class=\"\"><code class=\"notion\">9886</code></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">token-service</code></td>\n<td class=\"\">The IndyKite Token Service: mints the delegation token of every hop.</td>\n<td class=\"\"><code class=\"notion\">9102</code></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">auth0-bridge</code></td>\n<td class=\"\">A small adapter between the gateways and the demo's IdP (chapter 3). Not needed with a provider that offers token introspection itself.</td>\n<td class=\"\">internal only</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">Four optional groups of services are switched on with Compose profiles and are presented in chapter 8: the Intent Agent (<code class=\"notion\">intent</code>), a Google Drive MCP server (<code class=\"notion\">drive</code>), a graph-filtered ERP (<code class=\"notion\">erp</code>) and a CRM agent (<code class=\"notion\">crm</code>).</p>\n\n<h3 class=\"\">Related reading</h3>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\"><a href=\"/guides/guide-agent-gateway\" class=\"fancy-link\">Agent Gateway guide</a>: every step, setting and status code of the gateway.</li>\n<li style=\"list-style-type:disc\"><a href=\"/guides/guide-token-service\" class=\"fancy-link\">Token Service guide</a>: the configuration file, the exchange and the token it signs.</li>\n<li style=\"list-style-type:disc\"><a href=\"/guides/guide-ibac\" class=\"fancy-link\">Intent-Based Access Control guide</a>: what chapter 8 switches on.</li>\n</ul>",
  "chapterCount": 8,
  "chapters": [
    {
      "chapter": 1,
      "title": "Two ways to get a delegation token",
      "description": "What a delegation token is, the two places a gateway can get one - your identity provider's token exchange or the IndyKite Token Service - and what changes for the agents and the project.",
      "body": "<h2 class=\"\">Chapter 1: Two ways to get a delegation token</h2>\n\n<p class=\"notion\">An agent that works for a person needs to prove two things at once: <strong>who the person is</strong> and <strong>which agents the request passed</strong>. A person's access token only proves the first. The Agent Gateway therefore gets a second token for every request it forwards, the <strong>delegation token</strong>:</p>\n\n<pre class=\"code\"><code class=\"language-JSON\">{\n  \"iss\": \"&lt;the issuer of delegation tokens&gt;\",\n  \"sub\": \"&lt;the person&gt;\",\n  \"aud\": \"ibac-console\",\n  \"act\": {\n    \"sub\": \"&lt;the retriever gateway&gt;\",\n    \"act\": {\n      \"sub\": \"&lt;the orchestrator gateway&gt;\"\n    }\n  }\n}</code></pre>\n\n<p class=\"notion\">Read it from the inside out: the person's request first passed the orchestrator's gateway, then the retriever's. <code class=\"notion\">sub</code> never changes; each gateway adds itself as the outermost <code class=\"notion\">act</code>. That nested list is the <strong>delegation chain</strong>. The IndyKite platform reads it from the token and hands it to your policies as <code class=\"notion\">$ik_token.act_chain</code>, oldest hop first.</p>\n\n<h3 class=\"\">The two issuers a gateway can use</h3>\n<p class=\"notion\">To make such a token, somebody has to take the person's token and the gateway's own token and sign a new one. The gateway can ask one of two parties:</p>\n\n<figure class=\"image\"><img src=\"/assets/images/iag-token-modes.svg\" alt=\"Two ways a gateway gets a delegation token: from the token exchange of the external identity provider, or from the IndyKite Token Service.\"/></figure>\n\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"></td>\n<td class=\"simple-table-header-color\"><strong>External token exchange</strong></td>\n<td class=\"simple-table-header-color\"><strong>IndyKite Token Service</strong></td>\n</tr>\n<tr>\n<td class=\"\"><strong>Who signs the delegation token</strong></td>\n<td class=\"\">Your IdP, at its OAuth 2.0 token exchange endpoint (RFC 8693).</td>\n<td class=\"\">The Token Service, a container you run next to the gateways, with a key you generate.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>What the IdP still does</strong></td>\n<td class=\"\">Everything: login, introspection, the gateways' client credentials tokens, and the exchange.</td>\n<td class=\"\">Login, introspection and the gateways' client credentials tokens. It never sees a delegation.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>What the gateway forwards</strong></td>\n<td class=\"\"><code class=\"notion\">Authorization</code>: the delegation token. The person's own token stops at the gateway.</td>\n<td class=\"\"><code class=\"notion\">Authorization</code>: the person's own token. <code class=\"notion\">X-IK-Token</code>: the delegation token.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>How the chain grows</strong></td>\n<td class=\"\">The next gateway receives the delegation token as the caller's token and exchanges it again at the IdP.</td>\n<td class=\"\">The next gateway receives both headers, checks the <code class=\"notion\">X-IK-Token</code> at the Token Service and asks it for a new one with itself added.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>What the project must trust</strong></td>\n<td class=\"\">One issuer: the IdP.</td>\n<td class=\"\">Two issuers: the IdP for the person's token, the Token Service for the delegation token.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>Gateway settings</strong></td>\n<td class=\"\">The <code class=\"notion\">identity_provider</code> section with its exchange endpoint.</td>\n<td class=\"\">The <code class=\"notion\">identity_provider</code> section <strong>and</strong> a <code class=\"notion\">token_service</code> section.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>Shown in</strong></td>\n<td class=\"\"><a href=\"/tutorials/tutorial-agent-gateway-mcp\" class=\"fancy-link\">Protect an MCP server with Agent Gateway and external token exchange</a></td>\n<td class=\"\">This tutorial</td>\n</tr>\n</tbody>\n</table>\n\n<h3 class=\"\">When to choose the Token Service</h3>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\"><strong>Your IdP has no token exchange</strong>, or you cannot change how it nests the <code class=\"notion\">act</code> claim. The Token Service asks nothing of the IdP beyond the tokens it already issues.</li>\n<li style=\"list-style-type:disc\"><strong>The agents need the person's own token</strong> as well as the chain, for example to call an API that only accepts tokens of your IdP.</li>\n<li style=\"list-style-type:disc\"><strong>You want the intent of the prompt inside the token.</strong> With intent-based access control on, the Token Service writes it into <code class=\"notion\">act.intent</code> (chapter 8). The IdP exchange has no place for it.</li>\n<li style=\"list-style-type:disc\"><strong>You want the delegation keys in your hands.</strong> The service signs with a key you generate; the platform validates the tokens with the public half and never calls the service.</li>\n</ul>\n<p class=\"notion\">Stay with the external exchange when your IdP already offers it and one issuer for every token is what you want. The decisions the gateway takes - who the caller is, whether they may come this way, which tools they may use - are the same in both cases.</p>\n\n<h3 class=\"\">One rule: the same choice on the whole route</h3>\n<p class=\"notion\">Every gateway of a route must work the same way. A gateway that uses the IdP exchange replaces <code class=\"notion\">Authorization</code> and does not pass an <code class=\"notion\">X-IK-Token</code> on, so the chain would stop there. In this demo all five gateways, A2A and MCP alike, share one settings block that points to the Token Service (chapter 5).</p>\n\n<h3 class=\"\">What the gateway does and does not do in this demo</h3>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\">It <strong>authenticates</strong> every caller: the person's token is introspected at the IdP, an incoming <code class=\"notion\">X-IK-Token</code> at the Token Service.</li>\n<li style=\"list-style-type:disc\">It <strong>resolves</strong> the person to a node of the graph with ContX IQ <code class=\"notion\">whoami</code>.</li>\n<li style=\"list-style-type:disc\">It asks AuthZEN <strong>one admission question</strong> per hop: may this person reach this agent through this chain (chapter 2).</li>\n<li style=\"list-style-type:disc\">In front of the MCP server it also <strong>limits the tools</strong> a caller sees and may call (chapter 6).</li>\n<li style=\"list-style-type:disc\">It does <strong>not check the intent of a prompt</strong> until you switch that on (chapter 8).</li>\n<li style=\"list-style-type:disc\">It does <strong>not decide what data a tool returns</strong>. That stays with the policies of the platform, which read the same chain (chapter 7).</li>\n</ul>",
      "links": {
        "html": "/tutorials/tutorial-agent-gateway-delegation-chain#chapter-1",
        "markdown": "/tutorials/tutorial-agent-gateway-delegation-chain/1.md"
      }
    },
    {
      "chapter": 2,
      "title": "One prompt, three hops",
      "description": "Follow the prompt 'who am I' from the console through the orchestrator, the retriever and the MCP gateway: the tokens on each hop, the delegation chain as it grows, and the one admission question every gateway asks.",
      "body": "<h2 class=\"\">Chapter 2: One prompt, three hops</h2>\n\n<p class=\"notion\">Millicent, a CanBank employee, logs in to the console and types <em>\"who am I\"</em>. The answer comes from the graph, through the IndyKite MCP server. To get there the prompt crosses three gateways, and each one adds itself to the chain:</p>\n\n<figure class=\"image\"><img src=\"/assets/images/iag-delegation-chain.svg\" alt=\"One prompt crosses three gateways. Each gateway keeps the person's token in Authorization and asks the Token Service for a delegation token with one more hop in X-IK-Token.\"/></figure>\n\n<h3 class=\"\">The tokens</h3>\n<p class=\"notion\">Four tokens are involved. Only the first comes from the identity provider:</p>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Token</strong></td>\n<td class=\"simple-table-header-color\"><strong>Signed by</strong></td>\n<td class=\"simple-table-header-color\"><strong>Says</strong></td>\n<td class=\"simple-table-header-color\"><strong>Travels in</strong></td>\n</tr>\n<tr>\n<td class=\"\"><strong>U</strong>, the person's token</td>\n<td class=\"\">The external IdP, at login</td>\n<td class=\"\">\"This is Millicent\", for the audience <code class=\"notion\">ibac-console</code>.</td>\n<td class=\"\"><code class=\"notion\">Authorization</code>, unchanged on every hop</td>\n</tr>\n<tr>\n<td class=\"\"><strong>D1</strong></td>\n<td class=\"\">The Token Service</td>\n<td class=\"\">\"Millicent, through the orchestrator.\" Chain: <code class=\"notion\">[orchestrator]</code></td>\n<td class=\"\"><code class=\"notion\">X-IK-Token</code>, from the first gateway to the orchestrator and on to the second gateway</td>\n</tr>\n<tr>\n<td class=\"\"><strong>D2</strong></td>\n<td class=\"\">The Token Service</td>\n<td class=\"\">\"Millicent, through the orchestrator, then the retriever.\" Chain: <code class=\"notion\">[orchestrator, retriever]</code></td>\n<td class=\"\"><code class=\"notion\">X-IK-Token</code>, from the second gateway to the retriever and on to the MCP gateway</td>\n</tr>\n<tr>\n<td class=\"\"><strong>D3</strong></td>\n<td class=\"\">The Token Service</td>\n<td class=\"\">\"Millicent, through the orchestrator, the retriever, then the MCP gateway.\" Chain: <code class=\"notion\">[orchestrator, retriever, mcp]</code></td>\n<td class=\"\"><code class=\"notion\">X-IK-Token</code>, from the MCP gateway to the IndyKite MCP server</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">Each gateway also holds a token of its own, obtained from the IdP with the client credentials grant and reused until shortly before it expires. That token is what proves to the Token Service <em>which</em> gateway is asking, and its subject is the name the gateway has in the chain.</p>\n\n<h3 class=\"\">What every gateway does</h3>\n<p class=\"notion\">The three gateways run the same steps. Only the length of the chain differs:</p>\n<ol type=\"1\" class=\"numbered-list\" start=\"1\">\n<li><strong>Check the person's token.</strong> The gateway introspects <strong>U</strong> at the IdP. No token, or an inactive one, ends in <code class=\"notion\">401</code>.</li>\n<li><strong>Check the incoming chain.</strong> When the request carries an <code class=\"notion\">X-IK-Token</code>, the gateway introspects it at the Token Service. The first gateway of a route receives none.</li>\n<li><strong>Ask the Token Service for the next token.</strong> The gateway sends two tokens: as the <em>subject</em>, the person's token on the first hop and the incoming delegation token on every later hop; as the <em>actor</em>, its own token. It gets a new delegation token back: the same person, the same chain, plus this gateway as the newest hop.</li>\n<li><strong>Resolve the person.</strong> ContX IQ <code class=\"notion\">whoami</code> turns <strong>U</strong> into a node of the graph: type <code class=\"notion\">Person</code>, id the subject of the token.</li>\n<li><strong>Ask the admission question.</strong> One AuthZEN evaluation, described below. <code class=\"notion\">false</code> ends in <code class=\"notion\">403</code>.</li>\n<li><strong>Write the audit record</strong> of the decision.</li>\n<li><strong>Forward.</strong> <code class=\"notion\">Authorization</code> still holds <strong>U</strong>; <code class=\"notion\">X-IK-Token</code> holds the new delegation token.</li>\n</ol>\n<p class=\"notion\">The agents do no token work. The orchestrator and the retriever simply copy both headers onto their own outgoing call, and the next gateway takes it from there.</p>\n\n<h3 class=\"\">The same prompt, hop by hop</h3>\n<pre class=\"code\"><code class=\"language-plaintext\">console          ── U ──────────▶ orchestrator-iag   chain asked: [orchestrator]                    → D1\norchestrator     ── U + D1 ─────▶ retriever-iag      chain asked: [orchestrator, retriever]         → D2\nretriever        ── U + D2 ─────▶ mcp-iag            chain asked: [orchestrator, retriever, mcp]    → D3\nmcp-iag          ── U + D3 ─────▶ IndyKite MCP       the knowledge query get-self runs → \"You are Millicent…\"</code></pre>\n\n<h3 class=\"\">The one admission question</h3>\n<p class=\"notion\">At every hop the gateway sends a single request to <code class=\"notion\">POST /access/v1/evaluation</code>, with <strong>U</strong> in <code class=\"notion\">Authorization</code> and the new delegation token in <code class=\"notion\">X-IK-Token</code>:</p>\n<pre class=\"code\"><code class=\"language-JSON\">{\n  \"subject\":  { \"type\": \"Person\", \"id\": \"&lt;subject of the person's token&gt;\" },\n  \"action\":   { \"name\": \"CAN_TRIGGER\" },\n  \"resource\": { \"type\": \"Agent\", \"id\": \"&lt;subject of this gateway's own token&gt;\" }\n}</code></pre>\n<p class=\"notion\">In words: <em>may this person reach this agent, coming the way they came?</em> The gateway compares nothing itself. One KBAC policy of the project, <code class=\"notion\">person-can-reach-agent</code>, answers for every gateway:</p>\n<pre class=\"code\"><code class=\"language-plaintext\">MATCH (subject:Person)-[:WORKS_IN|CAN_TRIGGER*..3]-&gt;(wf:Workflow)\nMATCH (wf)-[rels:INVOKES*]-&gt;(resource:Agent)\nWHERE ALL(r IN rels WHERE r.workflow_name = wf.external_id AND endNode(r):Agent)\n  AND [r IN rels | endNode(r).external_id] = $ik_token.act_chain</code></pre>\n<p class=\"notion\">Line by line:</p>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\"><strong>Line 1</strong> finds a workflow the person can trigger, either directly (<code class=\"notion\">Person -CAN_TRIGGER-&gt; Workflow</code>) or through their department (<code class=\"notion\">Person -WORKS_IN-&gt; Department -CAN_TRIGGER-&gt; Workflow</code>).</li>\n<li style=\"list-style-type:disc\"><strong>Lines 2 and 3</strong> walk that workflow's path of agents, relationship by relationship, up to the agent the gateway asks about.</li>\n<li style=\"list-style-type:disc\"><strong>Line 4</strong> requires that path to be <strong>exactly</strong> the chain of the delegation token, in the same order.</li>\n</ul>\n\n<h3 class=\"\">The workflows in the graph</h3>\n<p class=\"notion\">A workflow is a node, and its route is a path of <code class=\"notion\">INVOKES</code> relationships that each carry the workflow's name. The route of this chapter is <code class=\"notion\">wf1</code>:</p>\n<pre class=\"code\"><code class=\"language-plaintext\">(Department support) ─CAN_TRIGGER─▶ (Workflow wf1) ─INVOKES─▶ (Agent orchestrator) ─INVOKES─▶ (Agent retriever) ─INVOKES─▶ (Agent mcp)\n                                                    workflow_name: wf1          workflow_name: wf1          workflow_name: wf1</code></pre>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Workflow</strong></td>\n<td class=\"simple-table-header-color\"><strong>Path of agents</strong></td>\n<td class=\"simple-table-header-color\"><strong>Who can trigger it</strong></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">wf1</code></td>\n<td class=\"\">orchestrator → retriever → mcp</td>\n<td class=\"\">The <code class=\"notion\">support</code> and <code class=\"notion\">trading</code> departments, and a few people directly.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">wf2</code></td>\n<td class=\"\">orchestrator → weather → mcp</td>\n<td class=\"\">The <code class=\"notion\">support</code> department.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">wf3</code></td>\n<td class=\"\">analyst → mcp</td>\n<td class=\"\">Millicent only.</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">Because the policy compares the whole path at every hop, three things follow without any setting on the gateways:</p>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\"><strong>A route cannot start in the middle.</strong> A person's token sent straight to <code class=\"notion\">retriever-iag</code> gives the chain <code class=\"notion\">[retriever]</code>. No workflow has that path, so the answer is <code class=\"notion\">403</code>.</li>\n<li style=\"list-style-type:disc\"><strong>A route cannot borrow an agent.</strong> The chain <code class=\"notion\">[orchestrator, weather, mcp]</code> is fine for someone who can trigger <code class=\"notion\">wf2</code> and refused for someone who can only trigger <code class=\"notion\">wf1</code>, although both workflows end at the same MCP gateway.</li>\n<li style=\"list-style-type:disc\"><strong>Access is data.</strong> Adding or removing one <code class=\"notion\">CAN_TRIGGER</code> relationship changes who gets through. Nothing is restarted.</li>\n</ul>\n<p class=\"notion\">A gateway keeps each decision for a short time per person and chain, so the follow-up requests of a conversation do not each cost a platform call.</p>",
      "links": {
        "html": "/tutorials/tutorial-agent-gateway-delegation-chain#chapter-2",
        "markdown": "/tutorials/tutorial-agent-gateway-delegation-chain/2.md"
      }
    },
    {
      "chapter": 3,
      "title": "Prepare the external identity provider",
      "description": "What the gateways and the Token Service need from your identity provider: three audiences, a login client for the console, one machine-to-machine client per gateway, and the users of the demo.",
      "body": "<h2 class=\"\">Chapter 3: Prepare the external identity provider</h2>\n\n<p class=\"notion\">With the Token Service signing the delegations, the identity provider (IdP) has three plain jobs: <strong>log people in</strong>, <strong>give every gateway a token of its own</strong>, and <strong>say whether a token is valid</strong>. No token exchange is configured at the IdP. This chapter lists what to create; the names are the ones the dataset and the compose file expect, so keep them unless you change both.</p>\n\n<h3 class=\"\">1. Three audiences</h3>\n<p class=\"notion\">An audience says what a token is for. Create three, as APIs or resource identifiers of your IdP:</p>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Audience</strong></td>\n<td class=\"simple-table-header-color\"><strong>Carried by</strong></td>\n<td class=\"simple-table-header-color\"><strong>Checked by</strong></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">ibac-console</code></td>\n<td class=\"\">The person's token (the console asks for it at login) and every delegation token.</td>\n<td class=\"\">The gateways, the Token Service and the project's two Token Introspect configurations.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">ibac-agents</code></td>\n<td class=\"\">The gateways' own tokens.</td>\n<td class=\"\">The Token Service: it accepts a token as \"the gateway that asks\" only with this audience.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">intent_agent</code></td>\n<td class=\"\">The token the entry gateway presents to the Intent Agent.</td>\n<td class=\"\">The Intent Agent. Only used when you switch intent checks on (chapter 8); creating it now saves a second visit.</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">Using one audience, <code class=\"notion\">ibac-console</code>, for the person's token and for every delegation keeps the project simple: both tokens are validated for the same audience on every hop.</p>\n\n<h3 class=\"\">2. The clients</h3>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Client</strong></td>\n<td class=\"simple-table-header-color\"><strong>Kind</strong></td>\n<td class=\"simple-table-header-color\"><strong>Settings</strong></td>\n</tr>\n<tr>\n<td class=\"\">console</td>\n<td class=\"\">Web application: authorization code flow with PKCE</td>\n<td class=\"\">Callback URL <code class=\"notion\">http://localhost:3300/auth/callback</code>, logout URL <code class=\"notion\">http://localhost:3300/</code>. Its id and secret go to <code class=\"notion\">CHATBOT_IDP_CLIENT_ID</code> and <code class=\"notion\">CHATBOT_IDP_CLIENT_SECRET</code>.</td>\n</tr>\n<tr>\n<td class=\"\">orchestrator, retriever, weather, analyst, mcp</td>\n<td class=\"\">Machine-to-machine, <strong>one per gateway</strong>: client credentials grant</td>\n<td class=\"\">Allowed to get tokens for <code class=\"notion\">ibac-agents</code> and <code class=\"notion\">intent_agent</code>. Ids and secrets go to <code class=\"notion\">&lt;AGENT&gt;_IDP_CLIENT_ID</code> and <code class=\"notion\">&lt;AGENT&gt;_IDP_CLIENT_SECRET</code>.</td>\n</tr>\n<tr>\n<td class=\"\">drive, erp, crm</td>\n<td class=\"\">Machine-to-machine, one each</td>\n<td class=\"\">Same settings. Only needed for the optional profiles of chapter 8.</td>\n</tr>\n</tbody>\n</table>\n\n<h3 class=\"\">3. Note the subject of every gateway client</h3>\n<p class=\"notion\">This is the step that ties the IdP to the graph. A client credentials token has a subject, and that subject is the gateway's name everywhere:</p>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\">in the <strong>delegation chain</strong>: it is the <code class=\"notion\">act.sub</code> the Token Service writes for the hop;</li>\n<li style=\"list-style-type:disc\">in the <strong>graph</strong>: it is the <code class=\"notion\">external_id</code> of the gateway's <code class=\"notion\">Agent</code> node, which the admission policy compares with the chain;</li>\n<li style=\"list-style-type:disc\">in the <strong>audit records</strong> of the gateways.</li>\n</ul>\n<p class=\"notion\">Get one client credentials token per gateway client, read its <code class=\"notion\">sub</code> claim and write the value down: chapter 4 gives each one to the dataset. With the demo's provider the subject is the client id followed by <code class=\"notion\">@clients</code>. If the value in the graph and the value in the token differ by one character, every request ends in <code class=\"notion\">403</code>.</p>\n\n<h3 class=\"\">4. The people</h3>\n<p class=\"notion\">Every login of the demo, staff and customer alike, is a <code class=\"notion\">Person</code> node whose <code class=\"notion\">external_id</code> is the <strong>subject of that user's token</strong>. The cast of the dataset is listed in the demo script (<code class=\"notion\">usecases/ibac/DEMO_SCRIPT.md</code>). For your own tenant, create the users you want to log in as, read each user's subject, and use those values as the <code class=\"notion\">external_id</code> of the matching <code class=\"notion\">Person</code> nodes of the dataset before you provision it.</p>\n<p class=\"notion\">The person's token carries <code class=\"notion\">sub</code>, <code class=\"notion\">aud</code> and <code class=\"notion\">scope</code>; no other claim is needed. It must be a <strong>JWT</strong>: the Token Service and the platform verify it offline against the IdP's published keys. If your IdP issues an opaque token when no audience is requested, the console's request for <code class=\"notion\">ibac-console</code> is what makes it a JWT.</p>\n\n<h3 class=\"\">5. Introspection, and the demo's bridge</h3>\n<p class=\"notion\">A gateway validates the person's token by <strong>token introspection</strong> (RFC 7662) and gets its own token from the client credentials endpoint, both under one base URL (<code class=\"notion\">identity_provider.base_url</code>). An IdP that offers both needs nothing more: point the gateways at it.</p>\n<p class=\"notion\">The demo's provider publishes no introspection endpoint, so the stack includes a small adapter, <code class=\"notion\">auth0-bridge</code>. It answers introspection by verifying the JWT against the provider's keys, and passes client credentials requests through to the provider. The gateways name the <code class=\"notion\">ibac-agents</code> audience in that request themselves, with <code class=\"notion\">identity_provider.client_credential_audience</code>, because the provider refuses a grant without one. It holds no client list and no secret of its own. With another provider, remove the bridge and set the gateways' identity provider base URL to your IdP.</p>\n\n<h3 class=\"\">What you should have now</h3>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\">The IdP's issuer, exactly as it appears in the <code class=\"notion\">iss</code> claim of a token (a trailing slash counts).</li>\n<li style=\"list-style-type:disc\">The console client's id and secret.</li>\n<li style=\"list-style-type:disc\">For each gateway: client id, client secret, and the <code class=\"notion\">sub</code> of its token.</li>\n<li style=\"list-style-type:disc\">The subject of each user you will log in as.</li>\n</ul>",
      "links": {
        "html": "/tutorials/tutorial-agent-gateway-delegation-chain#chapter-3",
        "markdown": "/tutorials/tutorial-agent-gateway-delegation-chain/3.md"
      }
    },
    {
      "chapter": 4,
      "title": "Create the signing key and provision the project",
      "description": "Generate the Token Service's signing key, give its public half to the project, and provision the ibac dataset: people, agents, workflows, policies and the two Token Introspect configurations.",
      "body": "<h2 class=\"\">Chapter 4: Create the signing key and provision the project</h2>\n\n<p class=\"notion\">The IndyKite project has to trust two kinds of token: the person's token from your IdP and the delegation token from your Token Service. It trusts the second through the <strong>public half of a key you generate</strong>. So the key comes first, the provisioning second.</p>\n\n<h3 class=\"\">1. Generate the Token Service's signing key</h3>\n<p class=\"notion\">The Token Service signs with an RSA key in JWK format. Any tool that produces one works; this short script uses the PyJWT library (<code class=\"notion\">pip install \"pyjwt[crypto]\"</code>):</p>\n<pre class=\"code\"><code class=\"language-Python\">import json, uuid\nfrom cryptography.hazmat.primitives.asymmetric import rsa\nfrom jwt.algorithms import RSAAlgorithm\nkey = rsa.generate_private_key(public_exponent=65537, key_size=2048)\nnames = {\"alg\": \"RS256\", \"use\": \"sig\", \"kid\": uuid.uuid4().hex}\nprivate = {**json.loads(RSAAlgorithm.to_jwk(key)), **names}\nprivate.pop(\"key_ops\", None)\npublic = {k: private[k] for k in (\"kty\", \"n\", \"e\", \"alg\", \"use\", \"kid\")}\nprint(\"PRIVATE - for token-service.yaml only:\")\nprint(json.dumps(private))\nprint(\"PUBLIC - TOKEN_SERVICE_PUBLIC_JWK:\")\nprint(json.dumps(public))</code></pre>\n<p class=\"notion\">It prints two lines of JSON:</p>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Value</strong></td>\n<td class=\"simple-table-header-color\"><strong>Goes to</strong></td>\n<td class=\"simple-table-header-color\"><strong>Who reads it</strong></td>\n</tr>\n<tr>\n<td class=\"\">The <strong>private</strong> JWK</td>\n<td class=\"\"><code class=\"notion\">idp.signing_keys</code> in <code class=\"notion\">token-service/token-service.yaml</code> (chapter 5). Nowhere else.</td>\n<td class=\"\">The Token Service, to sign.</td>\n</tr>\n<tr>\n<td class=\"\">The <strong>public</strong> JWK (<code class=\"notion\">kty</code>, <code class=\"notion\">n</code>, <code class=\"notion\">e</code>, <code class=\"notion\">alg</code>, <code class=\"notion\">use</code>, <code class=\"notion\">kid</code>)</td>\n<td class=\"\"><code class=\"notion\">TOKEN_SERVICE_PUBLIC_JWK</code> in instant-stack's <code class=\"notion\">.env</code> (step 2).</td>\n<td class=\"\">The IndyKite platform, to verify. It never calls your Token Service.</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">Treat the private JWK like any private key: keep it out of commits, images, logs and screenshots. If you generate a new key later, the project needs the new public half too.</p>\n\n<h3 class=\"\">2. Provision the project</h3>\n<p class=\"notion\">The graph, the policies and the configurations are the <code class=\"notion\">ibac</code> dataset of the <a href=\"https://github.com/indykite/developer-hub/tree/master/instant-stack\" class=\"fancy-link\" target=\"_blank\">instant-stack</a> app. In instant-stack's own <code class=\"notion\">.env</code>, beside the Service Account token and organization it needs for any dataset, set:</p>\n<pre class=\"code\"><code class=\"language-Bash\">DATASET=ibac\n# The issuer of your identity provider, exactly as in the iss claim of its tokens.\nAUTH0_ISSUER=https://&lt;your-idp-domain&gt;/\n# The token subject of each gateway's client (chapter 3, step 3).\nAUTH0_SUB_ORCHESTRATOR=&lt;client subject&gt;\nAUTH0_SUB_RETRIEVER=&lt;client subject&gt;\nAUTH0_SUB_WEATHER=&lt;client subject&gt;\nAUTH0_SUB_ANALYST=&lt;client subject&gt;\nAUTH0_SUB_MCP=&lt;client subject&gt;\nAUTH0_SUB_DRIVE=&lt;client subject&gt;\nAUTH0_SUB_CRM=&lt;client subject&gt;\nAUTH0_SUB_ERP=&lt;client subject&gt;\n# The public JWK printed in step 1.\nTOKEN_SERVICE_PUBLIC_JWK=&lt;public JWK&gt;</code></pre>\n<p class=\"notion\">The variable names mention the demo's provider; the values are simply your IdP's issuer and subjects. The people of the dataset are identified by the subjects of the demo's own users. To log in with users of <strong>your</strong> IdP, replace those subjects with yours in the dataset's <code class=\"notion\">data/ibac/nodes.json</code> and <code class=\"notion\">data/ibac/relationships.json</code> before provisioning (chapter 3, step 4).</p>\n<p class=\"notion\">Then start instant-stack and run <strong>Provision everything</strong>. Keep three values of its output for chapter 5:</p>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\">the <strong>AppAgent credential</strong> (<code class=\"notion\">APP_TOKEN</code>);</li>\n<li style=\"list-style-type:disc\">the <strong>project id</strong> (<code class=\"notion\">gid:…</code>): it is part of the MCP server's path;</li>\n<li style=\"list-style-type:disc\">the ids of the two <strong>explain queries</strong>, which let the console draw why a decision was taken.</li>\n</ul>\n\n<h3 class=\"\">What the provisioning creates</h3>\n<p class=\"notion\">Each item is something a step of chapter 2 depends on:</p>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>In the project</strong></td>\n<td class=\"simple-table-header-color\"><strong>Needed by</strong></td>\n</tr>\n<tr>\n<td class=\"\">An AppAgent and its credential</td>\n<td class=\"\">The gateways, for <code class=\"notion\">whoami</code> and the admission question; the console, for its explanations.</td>\n</tr>\n<tr>\n<td class=\"\">Token Introspect configuration <code class=\"notion\">auth0-person-introspect</code>: your IdP's issuer, audience <code class=\"notion\">ibac-console</code>, <code class=\"notion\">ikg_node_type</code> <code class=\"notion\">Person</code></td>\n<td class=\"\">The person's token in <code class=\"notion\">Authorization</code>: <code class=\"notion\">whoami</code>, AuthZEN, ContX IQ and the MCP server.</td>\n</tr>\n<tr>\n<td class=\"\">Token Introspect configuration <code class=\"notion\">token-service-https-ibac-console</code>: the Token Service's issuer, audience <code class=\"notion\">ibac-console</code>, validated offline with the public JWK</td>\n<td class=\"\">The delegation token in <code class=\"notion\">X-IK-Token</code>: it is what lets a policy read <code class=\"notion\">$ik_token</code>.</td>\n</tr>\n<tr>\n<td class=\"\">The CanBank graph: 26 <code class=\"notion\">Person</code> nodes with families, departments, customers, accounts, invoices and documents</td>\n<td class=\"\">The answers to the prompts.</td>\n</tr>\n<tr>\n<td class=\"\">8 <code class=\"notion\">Agent</code> nodes, 11 <code class=\"notion\">Workflow</code> nodes, their <code class=\"notion\">INVOKES</code> paths and the <code class=\"notion\">CAN_TRIGGER</code> relationships, and the policy <code class=\"notion\">person-can-reach-agent</code></td>\n<td class=\"\">Admission at every gateway (chapter 2).</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">Tool</code>, <code class=\"notion\">McpResource</code> and <code class=\"notion\">Skill</code> nodes, attached to agents with <code class=\"notion\">HAS</code> relationships that name a workflow, and the <code class=\"notion\">person-can-use-*</code> policies</td>\n<td class=\"\">The capability checks: which tools a caller sees at the MCP gateway (chapter 6).</td>\n</tr>\n<tr>\n<td class=\"\">Knowledge queries with their ContX IQ policies, KBAC policies over the bank's data, external data resolvers and the MCP server configuration</td>\n<td class=\"\">The MCP server's tools, and the chain-guarded query of chapter 7.</td>\n</tr>\n</tbody>\n</table>\n\n<h3 class=\"\">The two Token Introspect configurations side by side</h3>\n<p class=\"notion\">These two are the heart of the setup with a Token Service. Both are created with Service Account credentials and both map to the same node type, because both tokens are about the same person:</p>\n<pre class=\"code\"><code class=\"language-JSON\">POST /configs/v1/token-introspects\nAuthorization: Bearer &lt;service-account-token&gt;\n{\n  \"project_id\": \"&lt;PROJECT_GID&gt;\",\n  \"name\": \"auth0-person-introspect\",\n  \"jwt_matcher\": {\n    \"issuer\": \"https://&lt;your-idp-domain&gt;/\",\n    \"audience\": \"ibac-console\"\n  },\n  \"offline_validation\": {},\n  \"ikg_node_type\": \"Person\",\n  \"perform_upsert\": true\n}</code></pre>\n<pre class=\"code\"><code class=\"language-JSON\">POST /configs/v1/token-introspects\nAuthorization: Bearer &lt;service-account-token&gt;\n{\n  \"project_id\": \"&lt;PROJECT_GID&gt;\",\n  \"name\": \"token-service-https-ibac-console\",\n  \"jwt_matcher\": {\n    \"issuer\": \"https://token-service.ibac-demo.local\",\n    \"audience\": \"ibac-console\"\n  },\n  \"offline_validation\": {\n    \"public_jwks\": [\n      \"&lt;TOKEN_SERVICE_PUBLIC_JWK&gt;\"\n    ]\n  },\n  \"ikg_node_type\": \"Person\",\n  \"perform_upsert\": true\n}</code></pre>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"></td>\n<td class=\"simple-table-header-color\"><strong>For the person's token</strong></td>\n<td class=\"simple-table-header-color\"><strong>For the delegation token</strong></td>\n</tr>\n<tr>\n<td class=\"\"><strong>Issuer</strong></td>\n<td class=\"\">Your IdP.</td>\n<td class=\"\">The Token Service's <code class=\"notion\">base_url</code>. It is an identifier and does not have to resolve, but it must start with <code class=\"notion\">https://</code>.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>Keys</strong></td>\n<td class=\"\">Fetched from the IdP's published keys.</td>\n<td class=\"\">Given inline: the public JWK of step 1.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>Arrives in</strong></td>\n<td class=\"\"><code class=\"notion\">Authorization</code></td>\n<td class=\"\"><code class=\"notion\">X-IK-Token</code></td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">With external token exchange, no configuration names a Token Service: the delegation token is signed by the IdP, so every configuration of the project has the IdP as its issuer. See the <a href=\"/guides/guide-token-introspect\" class=\"fancy-link\">Token Introspect guide</a> for every field.</p>\n\n<h3 class=\"\">What comes next</h3>\n<p class=\"notion\">Chapter 5 fills the two configuration files of the demo and starts the stack.</p>",
      "links": {
        "html": "/tutorials/tutorial-agent-gateway-delegation-chain#chapter-4",
        "markdown": "/tutorials/tutorial-agent-gateway-delegation-chain/4.md"
      }
    },
    {
      "chapter": 5,
      "title": "Configure and start the stack",
      "description": "Fill the Token Service's file and the demo's env file, read the settings that point every gateway to the Token Service, build the local images and start the stack.",
      "body": "<h2 class=\"\">Chapter 5: Configure and start the stack</h2>\n\n<p class=\"notion\">Two files hold everything you have to fill in: the <strong>Token Service's own file</strong> and the <strong>env file</strong> of the demo. A third, shared by all gateways, is worth reading because it is where \"use the Token Service\" is said. Work from the demo directory:</p>\n<pre class=\"code\"><code class=\"language-Bash\">cd a2a/ibac-demo\ncp .example.env .env\ncp token-service/token-service.example.yaml token-service/token-service.yaml</code></pre>\n<p class=\"notion\">Both copies are gitignored. They hold client secrets, the AppAgent credential and a private key: keep them out of commits, images and screenshots.</p>\n\n<h3 class=\"\">1. The Token Service's own file</h3>\n<p class=\"notion\">Open <code class=\"notion\">token-service/token-service.yaml</code>. It answers four questions: who am I, what do I sign, who may ask, and whose tokens do I accept.</p>\n<pre class=\"code\"><code class=\"language-YAML\">service:\n  name: token-service\n  port: 8102\n  # The iss claim of every token this service signs.\n  base_url: https://token-service.ibac-demo.local\nidp:\n  # The aud values a delegation token may be issued for.\n  audiences:\n    - ibac-console\n  # Delegation tokens are short-lived: nothing revokes them.\n  token_ttl: 5m\n  # Written as act.type; the platform finds the actor in the graph with it.\n  actor_type: Agent\n  # What a gateway presents when it asks for a token.\n  client_auth:\n    type: client_secret_basic\n    client_id: agent-gateway\n    client_secret: \"&lt;a secret you choose&gt;\"\n  # The PRIVATE JWK of chapter 4. The first key signs.\n  signing_keys:\n    - '&lt;private JWK&gt;'\ntoken_introspection:\n  configurations:\n    # The person's token.\n    auth0-subject:\n      token_types:\n        - subject_token\n      matcher:\n        type: jwt\n        issuer: https://&lt;your-idp-domain&gt;/\n        audience: ibac-console\n      validation:\n        offline:\n          jwks_uri: https://&lt;your-idp-domain&gt;/.well-known/jwks.json\n    # The gateways' own tokens.\n    auth0-actor:\n      token_types:\n        - actor_token\n      matcher:\n        type: jwt\n        issuer: https://&lt;your-idp-domain&gt;/\n        audience: ibac-agents\n      validation:\n        offline:\n          jwks_uri: https://&lt;your-idp-domain&gt;/.well-known/jwks.json</code></pre>\n<p class=\"notion\">What to change:</p>\n<ol type=\"1\" class=\"numbered-list\" start=\"1\">\n<li><code class=\"notion\">idp.signing_keys</code>: paste the <strong>private</strong> JWK of chapter 4.</li>\n<li><code class=\"notion\">idp.client_auth.client_secret</code>: choose a secret. The same value goes to <code class=\"notion\">TOKEN_SERVICE_CLIENT_SECRET</code> in <code class=\"notion\">.env</code>.</li>\n<li>The issuer and the keys URL of the two <code class=\"notion\">token_introspection</code> entries: your IdP, with the issuer written exactly as in its tokens.</li>\n</ol>\n<p class=\"notion\">Two things to notice. The service <strong>never calls your IdP's token endpoint</strong>: it verifies the person's token and the gateway's token offline, against the IdP's published keys, and then signs. And a token of any other issuer or audience is refused: there is no fallback entry. The template also contains an <code class=\"notion\">audit</code> section that sends every exchange to the console; leave it as it is. Every field is explained in the <a href=\"/guides/guide-token-service\" class=\"fancy-link\">Token Service guide</a>.</p>\n\n<h3 class=\"\">2. The env file</h3>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Where it comes from</strong></td>\n<td class=\"simple-table-header-color\"><strong>Variables</strong></td>\n</tr>\n<tr>\n<td class=\"\">Your identity provider (chapter 3)</td>\n<td class=\"\"><code class=\"notion\">AUTH0_DOMAIN</code> (the provider's domain), <code class=\"notion\">CHATBOT_IDP_CLIENT_ID</code> / <code class=\"notion\">_SECRET</code> (the console), and one <code class=\"notion\">_IDP_CLIENT_ID</code> / <code class=\"notion\">_SECRET</code> pair per gateway: <code class=\"notion\">ORCHESTRATOR</code>, <code class=\"notion\">RETRIEVER</code>, <code class=\"notion\">WEATHER</code>, <code class=\"notion\">ANALYST</code>, <code class=\"notion\">MCP</code>. The three audiences keep their defaults.</td>\n</tr>\n<tr>\n<td class=\"\">IndyKite (chapter 4)</td>\n<td class=\"\"><code class=\"notion\">INDYKITE_BASE_URL</code> (the API URL of your region), <code class=\"notion\">APP_AGENT_CREDENTIALS_TOKEN</code>, <code class=\"notion\">EXPLAIN_STAFF_QUERY_ID</code> and <code class=\"notion\">EXPLAIN_DIRECT_QUERY_ID</code>.</td>\n</tr>\n<tr>\n<td class=\"\">The MCP server of your project</td>\n<td class=\"\"><code class=\"notion\">MCP_SERVER_ORIGIN</code> (the MCP host of your region), <code class=\"notion\">MCP_SERVER_PATH</code> (<code class=\"notion\">/mcp/v1/gid:&lt;PROJECT_GID&gt;</code>, written as is, not URL-encoded) and <code class=\"notion\">MCP_SERVER_URL</code> (the two joined).</td>\n</tr>\n<tr>\n<td class=\"\">Step 1 above</td>\n<td class=\"\"><code class=\"notion\">TOKEN_SERVICE_CLIENT_ID</code> (<code class=\"notion\">agent-gateway</code>) and <code class=\"notion\">TOKEN_SERVICE_CLIENT_SECRET</code>.</td>\n</tr>\n<tr>\n<td class=\"\">The model</td>\n<td class=\"\"><code class=\"notion\">GEMINI_API_KEY</code> and <code class=\"notion\">GEMINI_ENABLED=true</code>, or <code class=\"notion\">OLLAMA_HOST</code>.</td>\n</tr>\n<tr>\n<td class=\"\">You</td>\n<td class=\"\"><code class=\"notion\">FLASK_SECRET_KEY</code>: a random value for the console's session.</td>\n</tr>\n<tr>\n<td class=\"\">Leave for chapter 8</td>\n<td class=\"\"><code class=\"notion\">COMPOSE_PROFILES</code> empty and <code class=\"notion\">IBAC_ENABLED=false</code>.</td>\n</tr>\n</tbody>\n</table>\n\n<h3 class=\"\">3. Name the images</h3>\n<p class=\"notion\">The gateway and the Token Service are published by <a href=\"https://hub.docker.com/u/indykite\" class=\"fancy-link\" target=\"_blank\">IndyKite on Docker Hub</a>. This tutorial runs one platform release tag for both, as the <a href=\"/guides/guide-token-service#release-pairing\" class=\"fancy-link\">Token Service guide</a> requires. In <code class=\"notion\">.env</code>:</p>\n<pre class=\"code\"><code class=\"language-Bash\">AGENT_GATEWAY_IMAGE=indykite/agent-gateway:&lt;version&gt;\nTOKEN_SERVICE_IMAGE=indykite/token-service:&lt;version&gt;</code></pre>\n<p class=\"notion\">Both images are built for <code class=\"notion\">linux/amd64</code>; on an arm64 host they run under emulation.</p>\n\n<h3 class=\"\">4. Read how a gateway is told to use the Token Service</h3>\n<p class=\"notion\">All gateways inherit one block, in <code class=\"notion\">iag-base-docker.yaml</code>. Nothing to fill in here; this is the part that differs from a setup with external token exchange. The lines that matter, with the defaults written out:</p>\n<pre class=\"code\"><code class=\"language-YAML\">environment:\n  # --- The Token Service: where delegation tokens come from. ---\n  JARVIS_TOKEN_SERVICE_BASE_URL: http://token-service:8102\n  JARVIS_TOKEN_SERVICE_EXCHANGE_ENDPOINT: \"oauth2/token\"\n  JARVIS_TOKEN_SERVICE_INTROSPECT_ENDPOINT: \"oauth2/introspect\"\n  JARVIS_TOKEN_SERVICE_CLIENT_AUTH_TYPE: client_secret_basic\n  JARVIS_TOKEN_SERVICE_CLIENT_AUTH_CLIENT_ID: ${TOKEN_SERVICE_CLIENT_ID}\n  JARVIS_TOKEN_SERVICE_CLIENT_AUTH_CLIENT_SECRET: ${TOKEN_SERVICE_CLIENT_SECRET}\n  JARVIS_TOKEN_SERVICE_AUDIENCE: ibac-console\n  # --- The identity provider: the person's token and the gateway's own token. ---\n  JARVIS_IDENTITY_PROVIDER_BASE_URL: http://auth0-bridge:8090\n  JARVIS_IDENTITY_PROVIDER_INTROSPECT_ENDPOINT: oauth/introspect\n  JARVIS_IDENTITY_PROVIDER_CLIENT_CREDENTIAL_ENDPOINT: oauth/token\n  JARVIS_IDENTITY_PROVIDER_ISSUER: https://${AUTH0_DOMAIN}/\n  # --- Which tokens may start a request here. ---\n  JARVIS_PROTECTED_AGENT_AUTHENTICATION_AUDIENCES: ibac-console\n  # --- IndyKite: who the person is, and the admission question. ---\n  JARVIS_CONTX_IQ_BASE_URL: ${INDYKITE_BASE_URL}/contx-iq/v1\n  JARVIS_CONTX_IQ_APP_AGENT_CREDENTIALS_TOKEN: ${APP_AGENT_CREDENTIALS_TOKEN}\n  JARVIS_AUTHZEN_BASE_URL: ${INDYKITE_BASE_URL}/access/v1\n  JARVIS_AUTHZEN_ACTION: CAN_TRIGGER\n  JARVIS_AUTHZEN_CAPABILITIES_ENABLED: \"true\"\n  JARVIS_AUTHZEN_CACHE_TTL: 30s</code></pre>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Settings</strong></td>\n<td class=\"simple-table-header-color\"><strong>What they say</strong></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">JARVIS_TOKEN_SERVICE_*</code></td>\n<td class=\"\">The presence of this section is the switch. With it, the gateway asks the Token Service for the delegation token, keeps the person's token in <code class=\"notion\">Authorization</code> and forwards the delegation in <code class=\"notion\">X-IK-Token</code>. Without it, the gateway uses the exchange endpoint of the identity provider.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">JARVIS_TOKEN_SERVICE_AUDIENCE</code></td>\n<td class=\"\">The audience the gateway asks the delegation for. It must be in the Token Service's <code class=\"notion\">idp.audiences</code> and in the project's Token Introspect configuration.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">JARVIS_IDENTITY_PROVIDER_*</code></td>\n<td class=\"\">Where the person's token is introspected and where the gateway gets its own token. In the demo that is the bridge; with an IdP that has an introspection endpoint, it is the IdP itself.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">JARVIS_PROTECTED_AGENT_AUTHENTICATION_AUDIENCES</code></td>\n<td class=\"\">A person's token must be issued for this audience to be accepted.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">JARVIS_AUTHZEN_*</code></td>\n<td class=\"\">The action of the admission question, the capability checks (on), and how long a decision is kept.</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">On top of that block, each gateway adds only what makes it itself. An A2A gateway and the MCP gateway, side by side:</p>\n<pre class=\"code\"><code class=\"language-YAML\">orchestrator-iag:\n  environment:\n    JARVIS_SERVICE_NAME: orchestrator-iag\n    JARVIS_SERVICE_BASE_URL: http://orchestrator-iag:9881\n    JARVIS_PROTECTED_AGENT_BASE_URL: http://orchestrator:7001\n    JARVIS_PROTECTED_AGENT_AUTHENTICATION_CLIENT_ID: ${ORCHESTRATOR_IDP_CLIENT_ID}\n    JARVIS_PROTECTED_AGENT_AUTHENTICATION_CLIENT_SECRET: ${ORCHESTRATOR_IDP_CLIENT_SECRET}\nmcp-iag:\n  environment:\n    JARVIS_SERVICE_NAME: mcp-iag\n    JARVIS_PROTECTED_AGENT_PROTOCOL: mcp\n    JARVIS_SERVICE_BASE_URL: http://mcp-iag:9886${MCP_SERVER_PATH}\n    JARVIS_PROTECTED_AGENT_BASE_URL: ${MCP_SERVER_ORIGIN}${MCP_SERVER_PATH}\n    JARVIS_PROTECTED_AGENT_AUTHENTICATION_CLIENT_ID: ${MCP_IDP_CLIENT_ID}\n    JARVIS_PROTECTED_AGENT_AUTHENTICATION_CLIENT_SECRET: ${MCP_IDP_CLIENT_SECRET}</code></pre>\n<p class=\"notion\">The client id and secret are the gateway's own client of chapter 3: they are what gives each gateway a different name in the chain. The MCP gateway has one more line, <code class=\"notion\">JARVIS_PROTECTED_AGENT_PROTOCOL: mcp</code>, and serves exactly one endpoint: the path of the MCP server it protects.</p>\n\n<h3 class=\"\">5. Build and start</h3>\n<pre class=\"code\"><code class=\"language-Bash\">make\ndocker compose up -d\ndocker compose ps</code></pre>\n<p class=\"notion\"><code class=\"notion\">make</code> builds the demo's own images: the console, the four agents and the bridge. The gateway and Token Service images are pulled.</p>\n\n<h3 class=\"\">6. Check before you prompt</h3>\n<p class=\"notion\">The Token Service publishes its public key. It must be the one the project holds:</p>\n<pre class=\"code\"><code class=\"language-Bash\">curl -s http://localhost:9102/.well-known/jwks.json</code></pre>\n<p class=\"notion\">Compare the <code class=\"notion\">kid</code> and <code class=\"notion\">n</code> of the answer with the <code class=\"notion\">TOKEN_SERVICE_PUBLIC_JWK</code> you provisioned in chapter 4. If they differ, every delegation token will be refused by the platform with <code class=\"notion\">401</code>: provision again with the current public key.</p>\n<p class=\"notion\">Then open <code class=\"notion\">http://localhost:3300</code>. Use <code class=\"notion\">localhost</code>, not <code class=\"notion\">127.0.0.1</code>: the address must match the callback URL registered at the IdP.</p>\n\n<h3 class=\"\">What comes next</h3>\n<p class=\"notion\">Chapter 6 logs in, sends prompts as different people and reads what every gateway decided.</p>",
      "links": {
        "html": "/tutorials/tutorial-agent-gateway-delegation-chain#chapter-5",
        "markdown": "/tutorials/tutorial-agent-gateway-delegation-chain/5.md"
      }
    },
    {
      "chapter": 6,
      "title": "Run the prompts and read the decisions",
      "description": "Log in as different people, send prompts, and read on the console's audit terminal what each gateway decided: the token of every hop, the chain, the admission answer and the tools the MCP gateway let through.",
      "body": "<h2 class=\"\">Chapter 6: Run the prompts and read the decisions</h2>\n\n<p class=\"notion\">Open <code class=\"notion\">http://localhost:3300</code> and log in. The console has two parts: the CanBank app with its prompt box, and the <strong>audit terminal</strong>, where every gateway and the Token Service report what they did. This chapter is about reading that terminal.</p>\n\n<h3 class=\"\">The cards on the terminal</h3>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Card</strong></td>\n<td class=\"simple-table-header-color\"><strong>Sent by</strong></td>\n<td class=\"simple-table-header-color\"><strong>What it tells you</strong></td>\n</tr>\n<tr>\n<td class=\"\"><strong>TOKEN</strong></td>\n<td class=\"\">The Token Service</td>\n<td class=\"\">A delegation token was issued: the person as subject, the agents in <code class=\"notion\">act</code>. One per hop.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>AUTHORIZED</strong> / <strong>NOT AUTHORIZED</strong>, tagged <code class=\"notion\">admission</code></td>\n<td class=\"\">A gateway</td>\n<td class=\"\">The answer to \"may this person reach this agent through this chain\", with the person, the agent and the reason.</td>\n</tr>\n<tr>\n<td class=\"\">Tagged <code class=\"notion\">discovery</code></td>\n<td class=\"\">The MCP gateway</td>\n<td class=\"\">A list was asked for (<code class=\"notion\">tools/list</code>): the set the caller is permitted, and how many entries of the server's list were kept.</td>\n</tr>\n<tr>\n<td class=\"\">Tagged <code class=\"notion\">invocation</code></td>\n<td class=\"\">The MCP gateway</td>\n<td class=\"\">A tool was called: allowed, or refused because it is outside the permitted set.</td>\n</tr>\n<tr>\n<td class=\"\">Tagged <code class=\"notion\">refused early</code></td>\n<td class=\"\">A gateway</td>\n<td class=\"\">The request was refused before any question was asked, for example for a missing or inactive token.</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">Under every card a <strong>chain</strong> line shows the delegation chain of that request. That line is what this tutorial is about: watch it grow.</p>\n\n<h3 class=\"\">1. One prompt, three hops, on screen</h3>\n<p class=\"notion\">Log in as a member of the support department (Vivienne in the demo's cast) and send:</p>\n<pre class=\"code\"><code class=\"language-plaintext\">who am I</code></pre>\n<p class=\"notion\">Read the terminal from top to bottom. It is chapter 2, live:</p>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Gateway</strong></td>\n<td class=\"simple-table-header-color\"><strong>Chain line</strong></td>\n<td class=\"simple-table-header-color\"><strong>What happened</strong></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">orchestrator-iag</code></td>\n<td class=\"\"><code class=\"notion\">orchestrator</code></td>\n<td class=\"\">TOKEN card, then a green admission card. The policy found <code class=\"notion\">wf1</code> behind her department.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">retriever-iag</code></td>\n<td class=\"\"><code class=\"notion\">orchestrator → retriever</code></td>\n<td class=\"\">A second TOKEN card with one more hop, and a second admission card.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">mcp-iag</code></td>\n<td class=\"\"><code class=\"notion\">orchestrator → retriever → mcp</code></td>\n<td class=\"\">A third TOKEN card, discovery and invocation cards for the tool the retriever chose, and the answer from the graph.</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">The same person, three questions, each for a different agent and with a chain one hop longer. With the explain queries configured, the <strong>why?</strong> button on a green card draws the path the policy found, for example <code class=\"notion\">Vivienne → support → CAN_TRIGGER → wf1</code>.</p>\n\n<h3 class=\"\">2. Two prompts, two routes</h3>\n<pre class=\"code\"><code class=\"language-plaintext\">What's the weather in London?\nWhat's the weather at CanBank HQ?</code></pre>\n<p class=\"notion\">The first is answered from a public weather API: its chain ends at <code class=\"notion\">weather</code>. The second reads a node of the graph, so the weather agent calls the MCP server and the chain ends one hop further, at <code class=\"notion\">mcp</code>. A different chain is a different question to the policy, which is why the graph holds a workflow path for each route.</p>\n\n<h3 class=\"\">3. Same MCP server, different tools</h3>\n<p class=\"notion\">Compare two <code class=\"notion\">discovery</code> cards of <code class=\"notion\">mcp-iag</code>: the one from step 1 and the one from the headquarters weather prompt.</p>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Route</strong></td>\n<td class=\"simple-table-header-color\"><strong>Workflow</strong></td>\n<td class=\"simple-table-header-color\"><strong>Permitted set at the MCP gateway</strong></td>\n</tr>\n<tr>\n<td class=\"\">orchestrator → retriever → mcp</td>\n<td class=\"\"><code class=\"notion\">wf1</code></td>\n<td class=\"\"><code class=\"notion\">ciq_execute</code>, the AuthZEN tools and the retriever's knowledge queries.</td>\n</tr>\n<tr>\n<td class=\"\">orchestrator → weather → mcp</td>\n<td class=\"\"><code class=\"notion\">wf2</code></td>\n<td class=\"\">Two tools: <code class=\"notion\">ciq_execute</code> and <code class=\"notion\">get-hq-weather</code>.</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">Nothing on the gateway names these tools. This is the <strong>capability check</strong>: for a request that lists or calls a tool, the gateway asks AuthZEN which <code class=\"notion\">Tool</code> nodes the person may <code class=\"notion\">CAN_USE</code>, again with the delegation token. The policy <code class=\"notion\">person-can-use-tool</code> walks the same chain as the admission policy and then one step more: the agent at the end of the chain must have the tool <strong>for that workflow</strong>.</p>\n<pre class=\"code\"><code class=\"language-plaintext\">(Agent mcp) ─HAS {workflow_name: wf1}─▶ (Tool ciq_execute)\n(Agent mcp) ─HAS {workflow_name: wf2}─▶ (Tool get-hq-weather)</code></pre>\n<p class=\"notion\">A <code class=\"notion\">tools/list</code> comes back filtered to the set. A <code class=\"notion\">tools/call</code> outside it is refused with <code class=\"notion\">403</code> and the JSON-RPC error <code class=\"notion\">-31403</code> before the server is called, and shows as a red invocation card. So the chain decides twice: whether the request gets in, and what it may use once inside.</p>\n\n<h3 class=\"\">4. A decision about data is not a gateway decision</h3>\n<p class=\"notion\">Log in as a member of the trading department (Millicent in the cast) and ask for a stock quote, then ask the same as someone from support only:</p>\n<pre class=\"code\"><code class=\"language-plaintext\">What is the price of META?\nAm I allowed to retrieve a stock quote? Check with authzen.</code></pre>\n<p class=\"notion\">For support the answer is <code class=\"notion\">false</code>, yet every gateway card is green. That is correct, and worth understanding:</p>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\">The <strong>gateways</strong> decided that this person may use this route and this tool. Both are true for support and for trading.</li>\n<li style=\"list-style-type:disc\">The <strong>platform</strong> decided about the data: the policy <code class=\"notion\">person-can-retrieve-quote</code> requires the person's department to hold <code class=\"notion\">CAN_RETRIEVE</code> on the quote, and only trading does.</li>\n</ul>\n<p class=\"notion\">A question asked through the console is always about the logged-in person: the platform ties an evaluation to the subject of the token it arrives with, so asking about somebody else returns <code class=\"notion\">false</code>.</p>\n\n<h3 class=\"\">5. What the gateways tell a client without the console</h3>\n<p class=\"notion\">A gateway with a Token Service announces that the agent behind it takes a delegation token. Ask the entry gateway for the agent card it serves:</p>\n<pre class=\"code\"><code class=\"language-Bash\">curl -s http://localhost:9881/.well-known/agent-card.json | jq .capabilities.extensions</code></pre>\n<p class=\"notion\">The list contains <code class=\"notion\">https://indykite.com/extensions/delegated-token/v1</code>. It is the contract behind the chain: an agent that sees it knows that the delegation arrives in <code class=\"notion\">X-IK-Token</code> and must be passed on. The MCP gateway says the same in its OAuth protected resource metadata, which also tells a client where to get a token:</p>\n<pre class=\"code\"><code class=\"language-Bash\">curl -s \"http://localhost:9886/.well-known/oauth-protected-resource/mcp/v1/gid:&lt;PROJECT_GID&gt;\" | jq .</code></pre>\n<p class=\"notion\">The answer needs no token: <code class=\"notion\">resource</code> is the gateway's own endpoint, <code class=\"notion\">authorization_servers</code> names your IdP, and <code class=\"notion\">mcp_extensions</code> lists <code class=\"notion\">com.indykite/delegated-token</code>.</p>\n\n<h3 class=\"\">What comes next</h3>\n<p class=\"notion\">So far the chain decided at the gateways. Chapter 7 uses the same chain inside a data policy, and tests it with requests you send yourself.</p>",
      "links": {
        "html": "/tutorials/tutorial-agent-gateway-delegation-chain#chapter-6",
        "markdown": "/tutorials/tutorial-agent-gateway-delegation-chain/6.md"
      }
    },
    {
      "chapter": 7,
      "title": "The chain as a rule in your data policies",
      "description": "Use the delegation chain inside a ContX IQ policy and a KBAC policy, then build the chain by hand at the Token Service and see the same query answer over the full chain and refuse a shorter one.",
      "body": "<h2 class=\"\">Chapter 7: The chain as a rule in your data policies</h2>\n\n<p class=\"notion\">The gateways use the chain to decide who gets in. The same chain is available to <strong>every policy of the project</strong>, because the delegation token travels all the way to the platform in <code class=\"notion\">X-IK-Token</code>. A policy can therefore say: <em>this data is only served when the request came through these agents, in this order</em>. This protects the data even from a caller that reaches the platform some other way.</p>\n\n<h3 class=\"\">What a policy can read</h3>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Reference</strong></td>\n<td class=\"simple-table-header-color\"><strong>Value</strong></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">$token.sub</code></td>\n<td class=\"\">The person: the subject of the token in <code class=\"notion\">Authorization</code>.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">$ik_token.act_chain</code></td>\n<td class=\"\">The agents of the delegation token in <code class=\"notion\">X-IK-Token</code>, as a list, oldest hop first: <code class=\"notion\">[orchestrator, retriever, mcp]</code>.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">$ik_token.act.sub</code>, <code class=\"notion\">$ik_token.act.act.sub</code>, …</td>\n<td class=\"\">One hop of the chain, counted from the newest: <code class=\"notion\">act.sub</code> is the last gateway, <code class=\"notion\">act.act.sub</code> the one before.</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">The values are the subjects of the gateways' clients, the same ones the <code class=\"notion\">Agent</code> nodes carry (chapter 3).</p>\n\n<h3 class=\"\">A knowledge query that needs the full chain</h3>\n<p class=\"notion\">The dataset contains the query <code class=\"notion\">get-internal-documents-chained</code>. It returns the same documents as its plain sibling <code class=\"notion\">get-internal-documents</code>, with one more condition. Its ContX IQ policy:</p>\n<pre class=\"code\"><code class=\"language-JSON\">{\n  \"meta\": {\n    \"policy_version\": \"1.0-ciq\"\n  },\n  \"subject\": {\n    \"type\": \"Person\"\n  },\n  \"condition\": {\n    \"cypher\": \"MATCH (subject:Person {external_id: $token.sub})-[:WORKS_IN|CAN_ACCESS|CONTAINS*..4]-&gt;(document:Document)-[:IS_TYPE]-&gt;(taxonomy:Taxonomy {external_id: $taxonomy_external_id}) WHERE $ik_token.act_chain = ['&lt;orchestrator subject&gt;', '&lt;retriever subject&gt;', '&lt;mcp subject&gt;']\",\n    \"token_filter\": {\n      \"attribute\": \"$ik_token.act.act.act.sub\",\n      \"operator\": \"=\",\n      \"value\": \"&lt;orchestrator subject&gt;\",\n      \"advice\": {\n        \"error\": \"invalid_delegation_chain\",\n        \"error_description\": \"Expected the console-&gt;orchestrator-&gt;retriever-&gt;mcp delegation chain\"\n      }\n    }\n  },\n  \"allowed_reads\": {\n    \"nodes\": [\n      \"subject.*\",\n      \"document.*\",\n      \"taxonomy.*\"\n    ]\n  }\n}</code></pre>\n<p class=\"notion\">Two parts work together:</p>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\">The <strong><code class=\"notion\">token_filter</code></strong> is checked first, before any data is read. It requires a chain of three hops whose oldest one is the orchestrator. When it fails, the caller gets <code class=\"notion\">401</code> with the <code class=\"notion\">advice</code> you wrote, so a client learns <em>why</em>.</li>\n<li style=\"list-style-type:disc\">The <strong><code class=\"notion\">WHERE</code></strong> of the Cypher then requires the whole list to be exactly orchestrator, retriever, mcp. A chain of three other agents passes no data.</li>\n</ul>\n<p class=\"notion\">The same idea as a KBAC policy, for a yes or no decision: <code class=\"notion\">person-can-disclose-document-chained</code> allows the action <code class=\"notion\">CAN_DISCLOSE</code> on a <code class=\"notion\">Document</code> only over the full chain.</p>\n<pre class=\"code\"><code class=\"language-JSON\">{\n  \"meta\": {\n    \"policy_version\": \"2.0-kbac\"\n  },\n  \"subject\": {\n    \"type\": \"Person\"\n  },\n  \"actions\": [\n    \"CAN_DISCLOSE\"\n  ],\n  \"resource\": {\n    \"type\": \"Document\"\n  },\n  \"condition\": {\n    \"cypher\": \"MATCH (subject:Person)-[:WORKS_IN|CAN_ACCESS|CONTAINS*..4]-&gt;(resource:Document) WHERE $ik_token.act_chain = ['&lt;orchestrator subject&gt;', '&lt;retriever subject&gt;', '&lt;mcp subject&gt;']\"\n  }\n}</code></pre>\n\n<h3 class=\"\">Try it through the agents</h3>\n<p class=\"notion\">Log in as someone from support and send:</p>\n<pre class=\"code\"><code class=\"language-plaintext\">Use get-internal-documents-chained to list the policy documents</code></pre>\n<p class=\"notion\">The request travels console → orchestrator → retriever → mcp, the chain is complete, and the documents come back.</p>\n\n<h3 class=\"\">Try it by hand: build the chain yourself</h3>\n<p class=\"notion\">To see the rule refuse, build chains of different lengths without the agents. This is exactly what the gateways do, one request per hop. You need a person's token (from a console login) and the client credentials of the three gateways.</p>\n<p class=\"notion\"><strong>1. Get each gateway's own token</strong> from your IdP, for the audience <code class=\"notion\">ibac-agents</code>:</p>\n<pre class=\"code\"><code class=\"language-Bash\">curl -s -X POST \"https://&lt;your-idp-domain&gt;/oauth/token\" \\\n  -d \"grant_type=client_credentials\" \\\n  -d \"client_id=$ORCHESTRATOR_IDP_CLIENT_ID\" \\\n  -d \"client_secret=$ORCHESTRATOR_IDP_CLIENT_SECRET\" \\\n  -d \"audience=ibac-agents\"</code></pre>\n<p class=\"notion\">Repeat for the retriever and the MCP gateway. The token path is your IdP's; the one shown is the demo provider's.</p>\n<p class=\"notion\"><strong>2. First hop</strong>: exchange the person's token and the orchestrator's token at the Token Service.</p>\n<pre class=\"code\"><code class=\"language-Bash\">curl -s -u \"agent-gateway:$TOKEN_SERVICE_CLIENT_SECRET\" -X POST http://localhost:9102/oauth2/token \\\n  -d \"grant_type=urn:ietf:params:oauth:grant-type:token-exchange\" \\\n  -d \"subject_token=$USER_TOKEN\" \\\n  -d \"subject_token_type=urn:ietf:params:oauth:token-type:access_token\" \\\n  -d \"actor_token=$ORCHESTRATOR_TOKEN\" \\\n  -d \"actor_token_type=urn:ietf:params:oauth:token-type:access_token\" \\\n  -d \"requested_token_type=urn:ietf:params:oauth:token-type:access_token\" \\\n  -d \"audience=ibac-console\"</code></pre>\n<p class=\"notion\">The <code class=\"notion\">access_token</code> of the answer is <strong>D1</strong>, with the chain <code class=\"notion\">[orchestrator]</code>.</p>\n<p class=\"notion\"><strong>3. Second and third hop</strong>: send the same request with the previous delegation token as <code class=\"notion\">subject_token</code> and the next gateway's token as <code class=\"notion\">actor_token</code>. D1 with the retriever's token gives <strong>D2</strong>; D2 with the MCP gateway's token gives <strong>D3</strong>.</p>\n<p class=\"notion\"><strong>4. Run the query</strong> at the platform with each token, and without one:</p>\n<pre class=\"code\"><code class=\"language-Bash\">curl -s -i -X POST \"$INDYKITE_BASE_URL/contx-iq/v1/execute\" \\\n  -H \"Authorization: Bearer $USER_TOKEN\" \\\n  -H \"X-IK-ClientKey: $APP_AGENT_CREDENTIALS_TOKEN\" \\\n  -H \"X-IK-Token: $D3\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"id\": \"get-internal-documents-chained\", \"input_params\": {\"taxonomy_external_id\": \"policy\"}}'</code></pre>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong><code class=\"notion\">X-IK-Token</code></strong></td>\n<td class=\"simple-table-header-color\"><strong>Chain</strong></td>\n<td class=\"simple-table-header-color\"><strong>Answer</strong></td>\n</tr>\n<tr>\n<td class=\"\"><strong>D3</strong></td>\n<td class=\"\"><code class=\"notion\">[orchestrator, retriever, mcp]</code></td>\n<td class=\"\"><code class=\"notion\">200</code> with the documents.</td>\n</tr>\n<tr>\n<td class=\"\"><strong>D1</strong></td>\n<td class=\"\"><code class=\"notion\">[orchestrator]</code></td>\n<td class=\"\"><code class=\"notion\">401</code>; the <code class=\"notion\">WWW-Authenticate</code> header carries <code class=\"notion\">invalid_delegation_chain</code>.</td>\n</tr>\n<tr>\n<td class=\"\">none</td>\n<td class=\"\">no chain</td>\n<td class=\"\"><code class=\"notion\">401</code>, with the same advice.</td>\n</tr>\n<tr>\n<td class=\"\">not a token</td>\n<td class=\"\">unreadable</td>\n<td class=\"\">Refused.</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">The person is the same in all four requests and is allowed to read these documents. Only the route differs. The demo's Bruno collection contains these requests ready to run, in the folder <code class=\"notion\">bruno/ibac-demo/chain-guard</code>.</p>\n\n<h3 class=\"\">When to write such a rule</h3>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\"><strong>Data that only one workflow should touch</strong>: a query that an agent may run for a person, but only inside the approved route.</li>\n<li style=\"list-style-type:disc\"><strong>A second line of defence</strong>: the gateway's admission protects the agents; the chain rule protects the data even when a request reaches the platform without passing a gateway.</li>\n</ul>\n<p class=\"notion\">Keep one thing in mind: the rule names the agents by the subjects of their clients. Recreate a client at the IdP and the policy must be updated, exactly like the <code class=\"notion\">Agent</code> node.</p>\n\n<h3 class=\"\">What comes next</h3>\n<p class=\"notion\">Chapter 8 switches on the optional parts and lists what to check when something does not work.</p>",
      "links": {
        "html": "/tutorials/tutorial-agent-gateway-delegation-chain#chapter-7",
        "markdown": "/tutorials/tutorial-agent-gateway-delegation-chain/7.md"
      }
    },
    {
      "chapter": 8,
      "title": "Going further and troubleshooting",
      "description": "Switch on intent checks so the intent of a prompt travels in the delegation token, meet the optional Drive, ERP and CRM parts of the demo, and find the cause of the usual 401, 403 and 502 answers.",
      "body": "<h2 class=\"\">Chapter 8: Going further and troubleshooting</h2>\n\n<p class=\"notion\">The base stack is complete. This chapter presents what the Compose profiles add, each in a few lines, and then lists what to check when a request does not get through.</p>\n\n<h3 class=\"\">1. The intent of the prompt, inside the token</h3>\n<p class=\"notion\">This is the option that <strong>only exists with a Token Service</strong>. With intent-based access control on, the entry gateway asks the IndyKite Intent Agent what the prompt means before it forwards it, and the Token Service writes the answer into the delegation token.</p>\n<pre class=\"code\"><code class=\"language-Bash\"># .env\nCOMPOSE_PROFILES=intent\nIBAC_ENABLED=true\nINTENT_AGENT_IMAGE=indykite/intent-agent:&lt;version&gt;</code></pre>\n<pre class=\"code\"><code class=\"language-Bash\">docker compose up -d</code></pre>\n<p class=\"notion\">What changes for one prompt at <code class=\"notion\">orchestrator-iag</code>:</p>\n<ol type=\"1\" class=\"numbered-list\" start=\"1\">\n<li>The gateway admits the request as before, on the first delegation token.</li>\n<li>It sends the prompt to the Intent Agent and gets a list of evaluations back: subject, action, resource, in the words of your project's policies.</li>\n<li>It asks the Token Service for a <strong>second</strong> token with those evaluations. The Token Service writes them into <code class=\"notion\">act.intent</code> of the newest hop.</li>\n<li>The orchestrator receives that second token in <code class=\"notion\">X-IK-Token</code>. The terminal shows an <strong>INTENT</strong> card and a second TOKEN card.</li>\n</ol>\n<pre class=\"code\"><code class=\"language-JSON\">{\n  \"sub\": \"&lt;the person&gt;\",\n  \"act\": {\n    \"sub\": \"&lt;the orchestrator gateway&gt;\",\n    \"intent\": [\n      {\n        \"subject\":  { \"type\": \"Person\", \"id\": \"&lt;the person&gt;\" },\n        \"action\":   { \"name\": \"CAN_VIEW\" },\n        \"resource\": { \"type\": \"Invoice\" }\n      }\n    ]\n  }\n}</code></pre>\n<p class=\"notion\">From then on, every later gateway and every policy can see not only <em>who</em> and <em>through which agents</em>, but also <em>for what</em>. Two consequences to expect:</p>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\">A prompt the Intent Agent cannot map onto a policy <strong>never reaches the agent</strong>. The gateway answers in its place. Greetings and web searches are refused this way, by design: the policies have no words for them.</li>\n<li style=\"list-style-type:disc\">The Token Service must be able to write the intent. If it cannot, every prompt with an intent ends in <code class=\"notion\">502</code>.</li>\n</ul>\n<p class=\"notion\">Run the prompts of chapter 6 with the switch off, and switch it on to study intent. The full story - the Intent Agent, the comparison of an intent with what an agent declares, <code class=\"notion\">x-coaz-mappings</code> - is the subject of the <a href=\"/tutorials/tutorial-intent-ibac-gateway\" class=\"fancy-link\">intent tutorial</a> and of the <a href=\"/guides/guide-ibac\" class=\"fancy-link\">Intent-Based Access Control guide</a>.</p>\n\n<h3 class=\"\">2. More routes: Drive, ERP, CRM</h3>\n<p class=\"notion\">Three profiles add a gateway each. They follow the recipe of this tutorial without exception: one more client at the IdP, one more <code class=\"notion\">Agent</code> node, workflows whose paths end at it. Each needs its own external account and a local image build; the demo's README has the setup.</p>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Profile</strong></td>\n<td class=\"simple-table-header-color\"><strong>Adds</strong></td>\n<td class=\"simple-table-header-color\"><strong>What it shows about the chain</strong></td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">drive</code></td>\n<td class=\"\">A Google Drive MCP server behind <code class=\"notion\">drive-mcp-iag</code> (<code class=\"notion\">9887</code>)</td>\n<td class=\"\">The gateway protects an MCP server that knows nothing about IndyKite. One workflow per route: direct, through the analyst, or through orchestrator and analyst.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">erp</code></td>\n<td class=\"\">An invoices database behind an MCP server and <code class=\"notion\">erp-mcp-iag</code> (<code class=\"notion\">9889</code>)</td>\n<td class=\"\">The server reads the person from the delegation token and asks AuthZEN which invoices they may see before it queries. Same prompt, different rows per person.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">crm</code></td>\n<td class=\"\">A CRM agent behind <code class=\"notion\">crm-iag</code> (<code class=\"notion\">9888</code>)</td>\n<td class=\"\">The agent reads the person and the chain from <code class=\"notion\">X-IK-Token</code> and files a case in a third-party service on that person's behalf.</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">To protect a third-party MCP server with <strong>external token exchange</strong> instead, follow the <a href=\"/tutorials/tutorial-agent-gateway-mcp\" class=\"fancy-link\">MCP tutorial</a>.</p>\n\n<h3 class=\"\">3. Troubleshooting</h3>\n<p class=\"notion\">Start with the terminal: the first red card names the gateway and the reason. Then find the symptom here.</p>\n<table class=\"simple-table\">\n<tbody>\n<tr>\n<td class=\"simple-table-header-color\"><strong>Symptom</strong></td>\n<td class=\"simple-table-header-color\"><strong>Cause and fix</strong></td>\n</tr>\n<tr>\n<td class=\"\">Login fails with a redirect error</td>\n<td class=\"\">The console client at the IdP must list <code class=\"notion\">http://localhost:3300/auth/callback</code> as callback and <code class=\"notion\">http://localhost:3300/</code> as logout URL, and you must browse to <code class=\"notion\">localhost</code>.</td>\n</tr>\n<tr>\n<td class=\"\">A freshly logged-in user is not active at the gateway</td>\n<td class=\"\">The person's token is not a JWT for <code class=\"notion\">ibac-console</code>: the audience does not exist at the IdP, or the console did not ask for it.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">401</code> from a gateway: the access token is not issued for this agent</td>\n<td class=\"\">The <code class=\"notion\">aud</code> of the person's token does not contain <code class=\"notion\">ibac-console</code>. Compare the audience at the IdP with <code class=\"notion\">AUTH0_CONSOLE_AUDIENCE</code>.</td>\n</tr>\n<tr>\n<td class=\"\">The gateway cannot get its own token (<code class=\"notion\">invalid_client</code>)</td>\n<td class=\"\">A wrong client id or secret for that gateway, or the client is not allowed the <code class=\"notion\">ibac-agents</code> audience.</td>\n</tr>\n<tr>\n<td class=\"\">The Token Service refuses the first exchange (<code class=\"notion\">invalid_grant</code>)</td>\n<td class=\"\">A token matched no entry of <code class=\"notion\">token_introspection</code>: the issuer in <code class=\"notion\">token-service.yaml</code> is not written exactly as in the tokens (mind the trailing slash), or the gateway's token has no <code class=\"notion\">ibac-agents</code> audience.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">401</code> with <code class=\"notion\">Invalid token in X-IK-Token header</code> from AuthZEN, ContX IQ or the MCP server</td>\n<td class=\"\">The project does not trust the Token Service: its Token Introspect configuration is missing, or holds another public key. Compare with <code class=\"notion\">http://localhost:9102/.well-known/jwks.json</code> and provision again (chapter 5, step 6).</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">401</code> on MCP calls for a user who logs in fine</td>\n<td class=\"\">The Token Introspect configuration for the IdP is missing or its issuer differs from the token's <code class=\"notion\">iss</code>, or the project's MCP server configuration is disabled.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">403</code>: the subject may not reach the agent through the delegation chain</td>\n<td class=\"\">The admission policy found no workflow with this path that the person can trigger. Check, in this order: the <code class=\"notion\">Agent</code> nodes' ids against the subjects of the gateways' tokens (chapter 3, step 3); the person's <code class=\"notion\">CAN_TRIGGER</code> path to the workflow; that the policy <code class=\"notion\">person-can-reach-agent</code> is active.</td>\n</tr>\n<tr>\n<td class=\"\">A card says the admission could not be evaluated</td>\n<td class=\"\">That is not a refusal: the call to the platform failed. Check <code class=\"notion\">INDYKITE_BASE_URL</code> and the AppAgent credential.</td>\n</tr>\n<tr>\n<td class=\"\">Admission is green, but the tool list is empty or a call is refused with <code class=\"notion\">-31403</code></td>\n<td class=\"\">The capability check found no permitted tool: the <code class=\"notion\">Tool</code> nodes, their <code class=\"notion\">HAS</code> relationships or the <code class=\"notion\">person-can-use-*</code> policies are missing, or a tool's name on the server differs from its node's id. Provision the dataset again. <code class=\"notion\">IAG_CAPABILITY_CHECKS=false</code> lets the lists through unfiltered.</td>\n</tr>\n<tr>\n<td class=\"\">The chain-guarded query answers <code class=\"notion\">invalid_delegation_chain</code> through the agents</td>\n<td class=\"\">The subjects written in the policy differ from the gateways' current subjects, or the request took another route than orchestrator → retriever → mcp.</td>\n</tr>\n<tr>\n<td class=\"\">Every prompt is a <code class=\"notion\">502</code> with <code class=\"notion\">IBAC_ENABLED=true</code></td>\n<td class=\"\">The gateway cannot reach the Intent Agent (is the <code class=\"notion\">intent</code> profile on?), or the Token Service did not write the intent into the token. A <code class=\"notion\">504</code> is the Intent Agent taking longer than <code class=\"notion\">INTENT_AGENT_TIMEOUT</code>.</td>\n</tr>\n<tr>\n<td class=\"\">The reply is \"Unable to extract intent from prompt.\"</td>\n<td class=\"\">The gateway answered in place of the agent: no policy has words for this prompt. Rephrase closer to an action of your policies, or switch intent checks off for such prompts.</td>\n</tr>\n<tr>\n<td class=\"\">A change in <code class=\"notion\">.env</code> has no effect</td>\n<td class=\"\">A restart does not read the file again. Recreate the service: <code class=\"notion\">docker compose up -d --force-recreate &lt;service&gt;</code>.</td>\n</tr>\n<tr>\n<td class=\"\"><code class=\"notion\">pull access denied</code> for a demo image</td>\n<td class=\"\">That image is built locally: run <code class=\"notion\">make</code>, or the profile's own target (<code class=\"notion\">make new-drive-mcp</code>, <code class=\"notion\">make new-erp-mcp</code>).</td>\n</tr>\n</tbody>\n</table>\n<p class=\"notion\">To follow the services that take the decisions:</p>\n<pre class=\"code\"><code class=\"language-Bash\">docker compose logs -f token-service orchestrator-iag retriever-iag mcp-iag</code></pre>\n\n<h3 class=\"\">4. Stop</h3>\n<pre class=\"code\"><code class=\"language-Bash\">docker compose down</code></pre>\n\n<h3 class=\"\">What you built</h3>\n<ul class=\"bulleted-list\">\n<li style=\"list-style-type:disc\">An identity provider that only logs people in and names the gateways; a Token Service of your own that writes the delegation chain.</li>\n<li style=\"list-style-type:disc\">Three gateways in a row that each ask one question, with a chain one hop longer.</li>\n<li style=\"list-style-type:disc\">Routes, tool sets and data rules that all read the same chain and are all data in the graph.</li>\n</ul>\n<p class=\"notion\">For every setting and status code, continue with the <a href=\"/guides/guide-agent-gateway\" class=\"fancy-link\">Agent Gateway guide</a> and the <a href=\"/guides/guide-token-service\" class=\"fancy-link\">Token Service guide</a>.</p>",
      "links": {
        "html": "/tutorials/tutorial-agent-gateway-delegation-chain#chapter-8",
        "markdown": "/tutorials/tutorial-agent-gateway-delegation-chain/8.md"
      }
    }
  ],
  "links": {
    "html": "/tutorials/tutorial-agent-gateway-delegation-chain",
    "markdown": "/tutorials/tutorial-agent-gateway-delegation-chain.md"
  }
}