<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hasitha Jayanath Pathirana on Medium]]></title>
        <description><![CDATA[Stories by Hasitha Jayanath Pathirana on Medium]]></description>
        <link>https://medium.com/@sgmjaiya94?source=rss-6de6771ac7d7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*2KHoSQADt0kPGRuQ.jpg</url>
            <title>Stories by Hasitha Jayanath Pathirana on Medium</title>
            <link>https://medium.com/@sgmjaiya94?source=rss-6de6771ac7d7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 07:08:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sgmjaiya94/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Integration: Work IQ Through WSO2 AI Gateway]]></title>
            <link>https://medium.com/@sgmjaiya94/the-integration-work-iq-through-wso2-ai-gateway-d061b2d9fd27?source=rss-6de6771ac7d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/d061b2d9fd27</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[ai-gateway]]></category>
            <category><![CDATA[wso2]]></category>
            <category><![CDATA[entra-id]]></category>
            <category><![CDATA[mcp-server]]></category>
            <dc:creator><![CDATA[Hasitha Jayanath Pathirana]]></dc:creator>
            <pubDate>Tue, 22 Sep 2026 03:59:55 GMT</pubDate>
            <atom:updated>2026-09-22T03:59:55.181Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Work IQ Behind the WSO2 AI Gateway — Part 3 of 3</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OEAMluKb-bejwoI5UbtdKQ.png" /></figure><p><a href="https://medium.com/@sgmjaiya94/prove-it-with-curl-first-on-behalf-of-onto-microsoft-work-iq-0970a1fbdaab?sharedUserId=sgmjaiya94"><em>Part 1</em></a><em> proved the exchange with curl; </em><a href="https://medium.com/@sgmjaiya94/the-entra-side-three-app-registrations-and-the-strings-that-must-match-e5782b67103e?sharedUserId=sgmjaiya94"><em>Part 2</em></a><em> configured Entra. This part wires it into WSO2 AI Gateway — config, two policies, client — and verifies it from the outside.</em></p><p><strong>TL;DR</strong></p><ul><li>Two chained policies: inbound serves the PRM and validates token A; outbound performs OBO and swaps in token B. Auth first, OBO second, then <strong>Deploy</strong> — Save alone changes nothing.</li><li>The client config is three lines: gateway URL, clientId, callback port. No header helper, no secret.</li></ul><p>Product mechanics — Workspace versus Gateway, config.toml, building custom policies — are in Parts <a href="https://medium.com/@sgmjaiya94/building-it-on-wso2-ai-gateway-17e46927a1b5?sharedUserId=sgmjaiya94">7</a> and <a href="https://medium.com/@sgmjaiya94/writing-a-custom-policy-for-wso2-ai-gateway-2c4d2f52257a?sharedUserId=sgmjaiya94">8</a> of the first series. This post shows only what&#39;s specific to Work IQ.</p><h3>Gateway configuration</h3><p>In config.toml: an Entra key manager with the <strong>v2</strong> issuer, and the inbound header set to Authorization.</p><pre>[policy_configurations.jwtauth_v1]<br>headername = &quot;Authorization&quot;      # the client&#39;s OAuth token IS the gateway token now</pre><pre>[[policy_configurations.jwtauth_v1.keymanagers]]<br>name       = &quot;entra&quot;              # referenced by the auth policy&#39;s `issuers` parameter<br>issuer     = &quot;https://login.microsoftonline.com/&lt;tenant-id&gt;/v2.0&quot;<br>scopeClaim = &quot;scp&quot;                # Entra&#39;s scope claim is a space-separated string<br>[policy_configurations.jwtauth_v1.keymanagers.jwks.remote]<br>uri = &quot;https://login.microsoftonline.com/&lt;tenant-id&gt;/discovery/v2.0/keys&quot;</pre><pre>[policy_configurations.mcpauth_v1]<br>gatewayhost = &quot;gateway.example.com&quot;   # host only; the PRM resource is built from this<br># + the same `entra` key-manager block</pre><p>headername is <strong>gateway-wide</strong>. Our GitHub passthrough proxy on the same gateway kept its gateway token in a custom header and stopped working the moment we flipped this. Plan one gateway instance per header convention.</p><h3>The proxy</h3><p>Create an MCP proxy for https://workiq.svc.cloud.microsoft/mcp (Streamable HTTP), then read the published URL back. Ours was https://gateway.example.com:8443/default/workiq-mcp-server/mcp — the /default/…-server came from the product. That string is the identifier URI from Part 2. One character off and nothing works.</p><h3>The two policies</h3><p>Neither ships with the product; both are forks (<a href="https://medium.com/@sgmjaiya94/writing-a-custom-policy-for-wso2-ai-gateway-2c4d2f52257a?sharedUserId=sgmjaiya94">Part 8 covers how</a>): gateway-mcp-auth from the stock mcp-auth, gateway-obo-token from oauth2-generator.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kvWJ7c6P_sdRQwzumZa5SA.png" /><figcaption><em>[Diagram: token A in, token B out — and where each failure turns into a 401 or a 502]</em></figcaption></figure><p><strong>① </strong><strong>gateway-mcp-auth</strong> — the gateway&#39;s resource-server role:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PcdPAnJtRVWwKc8K-3oV3Q.png" /></figure><p>Two scope parameters because Entra puts the short form in scp while the client must request the URI form; the stock policy couples the two lists, and decoupling them was one of our fork&#39;s two changes. The other was audiences, which the stock MCP policy didn&#39;t forward to its JWT validator at all.</p><p><strong>Not covered here: tool-level authorization.</strong> The scopes check above is proxy-wide — it decides whether a token may reach this MCP server at all. Deciding which <em>tools</em> a given user may call — ask yes, fetch_blob no, or a role or group claim required for one specific tool — is a separate policy, <strong>MCP Authorization</strong> (mcp-authz), which sits in the same chain after ① and evaluates each tools/call against the claims in token A. I didn&#39;t configured here; but if you want it&#39;s the piece that turns &quot;authenticated employee&quot; into RBAC.</p><p><strong>② </strong><strong>gateway-obo-token</strong> — the exchange:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tdJgeq627jczqM7si_f-fQ.png" /></figure><p>It reads token A from the <em>original</em> request snapshot (so it doesn’t matter that ① stripped the header), caches B under sha256(A) with TTL min(exp_A, exp_B) − buffer, collapses concurrent requests for one A into one Entra call, and maps failures: invalid_grant / interaction_required → <strong>401</strong> with a PRM challenge (re-auth helps); bad secret, network, 5xx → <strong>502</strong> (re-auth won&#39;t, and a 401 would loop every client through sign-in).</p><p><strong>Order: ① above ②. Then Save, then Overview → Deploy to Gateway.</strong></p><h3>The client</h3><pre>claude mcp add-json workiq &#39;{<br>  &quot;type&quot;: &quot;http&quot;,<br>  &quot;url&quot;: &quot;https://gateway.example.com:8443/default/workiq-mcp-server/mcp&quot;,<br>  &quot;oauth&quot;: { &quot;clientId&quot;: &quot;&lt;client-app-id&gt;&quot;, &quot;callbackPort&quot;: 49643 }<br>}&#39;</pre><p>No headersHelper — there&#39;s no second credential. No secret — public client with PKCE. The callbackPort must match a <em>Mobile and desktop</em> redirect URI from Part 2. If this server name previously pointed elsewhere, claude mcp reset-project-choices clears the cached authorization-server choice.</p><p>Then /mcp → workiq → Authenticate → browser sign-in → tools listed. Ask what meetings you have today; the answer comes from <em>your</em> M365, because token B is yours.</p><h3>Verify from the outside first</h3><pre>GW=https://gateway.example.com:8443; CTX=/default/workiq-mcp-server<br>INIT=&#39;{&quot;jsonrpc&quot;:&quot;2.0&quot;,&quot;id&quot;:1,&quot;method&quot;:&quot;initialize&quot;,&quot;params&quot;:{&quot;protocolVersion&quot;:&quot;2025-06-18&quot;,&quot;capabilities&quot;:{},&quot;clientInfo&quot;:{&quot;name&quot;:&quot;verify&quot;,&quot;version&quot;:&quot;0.0.1&quot;}}}&#39;</pre><pre># PRM served without a token; resource == MCP URL; AS == Entra v2 issuer<br>curl -s &quot;$GW$CTX/.well-known/oauth-protected-resource&quot; | jq .</pre><pre># token-less POST → 401 with a resource_metadata pointer to that PRM<br>curl -si -X POST &quot;$GW$CTX/mcp&quot; -H &#39;Content-Type: application/json&#39; \<br>  -H &#39;Accept: application/json, text/event-stream&#39; -d &quot;$INIT&quot; | grep -i www-authenticate</pre><p>Then the same POST with Authorization: Bearer $A (a token A from Part 1&#39;s script) should return 200 — and a second call should show a cache hit and no Entra request in the gateway logs.</p><h3>What the client sees when things fail</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u3kzIdS5Z1hpDqSNDzyYDA.png" /></figure><h3>Where this leaves us</h3><p>An employee’s client holds a token for the gateway and nothing else. The gateway holds a validated identity it can route, meter and authorize on, and mints a Work IQ token per user on demand — nothing stored, nothing passed through. Work IQ sees the real employee. Three app registrations, one gateway, two policies, a dozen strings that have to agree.</p><p>If you’re putting a gateway in front of a Microsoft-published MCP server, this is the path that survived contact with Entra.</p><h3>References</h3><ul><li><a href="https://wso2.com/api-platform/ai-gateway/">WSO2 AI Gateway — product overview</a></li><li><a href="https://github.com/wso2/api-platform">WSO2 API Platform on GitHub — releases, including the </a><a href="https://github.com/wso2/api-platform">ap CLI</a></li><li><a href="https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-on-behalf-of-flow">Microsoft — OAuth 2.0 On-Behalf-Of flow</a></li><li><a href="https://code.claude.com/docs/en/mcp">Claude Code — MCP configuration</a></li><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization">MCP — Authorization specification</a></li></ul><p>If a parameter name or behaviour here doesn’t match the version you’re running, tell me and I’ll correct the post — this is the one in the series that will age fastest.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d061b2d9fd27" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Entra Side: Three App Registrations and the Strings That Must Match]]></title>
            <link>https://medium.com/@sgmjaiya94/the-entra-side-three-app-registrations-and-the-strings-that-must-match-e5782b67103e?source=rss-6de6771ac7d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5782b67103e</guid>
            <category><![CDATA[mcp-server]]></category>
            <category><![CDATA[ai-gateway]]></category>
            <category><![CDATA[entra-id]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[wso2]]></category>
            <dc:creator><![CDATA[Hasitha Jayanath Pathirana]]></dc:creator>
            <pubDate>Tue, 22 Sep 2026 03:58:41 GMT</pubDate>
            <atom:updated>2026-09-22T03:58:41.172Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Work IQ Behind the WSO2 AI Gateway — Part 2 of 3</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nSYWVP1uQ67DmPhLzymIZQ.png" /></figure><p><a href="https://medium.com/@sgmjaiya94/prove-it-with-curl-first-on-behalf-of-onto-microsoft-work-iq-0970a1fbdaab?sharedUserId=sgmjaiya94"><em>Part 1</em></a><em> proved with curl that a third-party app can On-Behalf-Of onto Work IQ. This part is the Entra configuration the real gateway needs — and the surprisingly short list of values that have to be identical in four places.</em></p><p><strong>TL;DR</strong></p><ul><li>Three registrations are involved: the MCP client (public), the gateway (an API <em>and</em> a confidential client), and Work IQ (Microsoft’s — you only need its service principal and consent).</li><li>The gateway’s published MCP URL — port and all — goes on the gateway app as an <strong>identifier URI</strong>. Entra accepted https://gateway.example.com:8443/…/mcp without complaint, which we had assumed it wouldn&#39;t.</li><li>Set the gateway app to mint <strong>v2 access tokens</strong>. Every silent validation failure we had traced back to the v1 default.</li></ul><h3>Why the client’s resource parameter drives everything</h3><p>A spec-following MCP client gets a 401 from the gateway, fetches its Protected Resource Metadata, checks that resource equals the URL it connected to, and then sends resource=&lt;that URL&gt; on every authorize and token request (RFC 8707).</p><p>Entra validates that parameter: resource must be one of the <strong>identifier URIs of the application that owns the requested scope</strong>, or you get AADSTS9010010. In <a href="https://medium.com/@sgmjaiya94/why-the-passthrough-trick-only-works-against-github-1d9d5e966f60?sharedUserId=sgmjaiya94">Part 4 of the first series</a> that was fatal — the scope belonged to Work IQ, whose identifier URIs are Microsoft&#39;s. Now the scope belongs to <em>our</em> gateway app, and its identifier URIs are ours to set.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UlXj8LRv6KP5KEqagfhdSQ.png" /><figcaption><em>[Diagram: who owns what, and which registration each Entra check consults]</em></figcaption></figure><h4>Registration 1 — the gateway app</h4><p>This is the one that does the work. It is both an API (the client gets tokens <em>for</em> it) and a confidential client (it redeems those tokens for Work IQ tokens).</p><p><strong>Expose an API → identifier URIs.</strong> Keep api://&lt;gateway-app-id&gt; <em>and</em> add the exact published MCP URL:</p><pre>https://gateway.example.com:8443/default/workiq-mcp-server/mcp</pre><p>Two notes on that string. It’s the URL the gateway <em>actually</em> publishes, not necessarily the context you typed — ours gained a /default/…-server prefix (Part 3). And Microsoft&#39;s documented identifier-URI formats don&#39;t mention a port, so we expected rejection and had a fallback planned (gateway on 443 under a verified domain). Entra saved it, and the authorize call with that resource returned a code. Test the assumption before you redesign around it.</p><p>Keep the api:// URI too. When we removed it, requests still using the api://…/mcp.access scope form failed with AADSTS500011.</p><p><strong>Expose an API → scope.</strong> mcp.access, admins and users. The client <em>requests</em> the full form — &lt;MCP URL&gt;/mcp.access — and Entra puts the short form mcp.access in the token&#39;s scp. Both spellings matter later.</p><p><strong>Manifest → </strong><strong>requestedAccessTokenVersion: 2</strong> (accessTokenAcceptedVersion in the older editor). The default null means v1, and v1 tokens differ in two ways that both break validation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z56dg454-0w5WFwvhWkYKg.png" /></figure><p>The gateway’s key manager uses the v2 issuer from Entra’s discovery document, so v1 tokens fail the issuer check — silently, as a plain 401. And v2’s stable aud means one audience to configure instead of one per URI. The flip affects <strong>newly minted</strong> tokens only; every client re-authenticates after it.</p><p><strong>Certificates &amp; secrets.</strong> One client secret for the gateway app — this is what the OBO policy authenticates with when it redeems token A for token B.</p><p><strong>API permissions.</strong> <em>APIs my organization uses</em> → Work IQ → Delegated → WorkIQAgent.Ask → admin consent. <strong>Only that one.</strong> OBO hands back every scope the app has consent for, so each extra permission here lands in every employee&#39;s upstream token.</p><p><strong>Expose an API → Authorized client applications.</strong> Add the public client (registration 2) for mcp.access. Otherwise users get a consent prompt on first connect — or, in a tenant that blocks user consent, AADSTS65001.</p><h4>Registration 2 — the public client</h4><p>What the MCP client presents itself as. Nothing in it a laptop couldn’t hold.</p><p><strong>Authentication → Add platform → Mobile and desktop applications.</strong> Redirect URI http://localhost:&lt;port&gt;/callback for each callback port your clients use (Claude Code takes a callbackPort in its config). <em>Allow public client flows</em> = Yes.</p><p>The platform matters more than the toggle: Entra classifies the client from the platform of the matched redirect URI. A URI under <strong>Web</strong> makes the client confidential, and code redemption fails with AADSTS7000218 asking for a secret it doesn&#39;t have.</p><p><strong>API permissions.</strong> <em>My APIs</em> → the gateway app → mcp.access, admin consent.</p><h4>Registration 3 — Work IQ</h4><p>Not yours. Its service principal just has to exist in the tenant (check <em>Enterprise applications</em>; if absent, az ad sp create --id &lt;workiq-app-id&gt;) so registration 1 can be granted delegated permission on it.</p><h3>What the two tokens look like</h3><p><strong>Token A</strong> — issued to the client, validated by the gateway:</p><pre>{<br>  &quot;aud&quot;: &quot;&lt;gateway-app-id&gt;&quot;,<br>  &quot;iss&quot;: &quot;https://login.microsoftonline.com/&lt;tenant-id&gt;/v2.0&quot;,<br>  &quot;azp&quot;: &quot;&lt;client-app-id&gt;&quot;,<br>  &quot;scp&quot;: &quot;mcp.access&quot;,<br>  &quot;oid&quot;: &quot;&lt;user-oid&gt;&quot;,<br>  &quot;ver&quot;: &quot;2.0&quot;<br>}</pre><p><strong>Token B</strong> — minted by OBO, seen only by Work IQ:</p><pre>{<br>  &quot;aud&quot;: &quot;&lt;workiq-app-id&gt;&quot;,<br>  &quot;iss&quot;: &quot;https://login.microsoftonline.com/&lt;tenant-id&gt;/v2.0&quot;,<br>  &quot;azp&quot;: &quot;&lt;gateway-app-id&gt;&quot;,<br>  &quot;idtyp&quot;: &quot;user&quot;,<br>  &quot;scp&quot;: &quot;WorkIQAgent.Ask&quot;,<br>  &quot;oid&quot;: &quot;&lt;user-oid&gt;&quot;,<br>  &quot;ver&quot;: &quot;2.0&quot;<br>}</pre><p>Same oid, same issuer, different audience, different azp. That is the entire security argument in two JSON blocks.</p><p>Everything above satisfies one specific check.</p><h3>The cross-reference</h3><p>Find the symptom and you have the row.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fdtChtyNKI-iRObUIesmmQ.png" /></figure><p>The first four rows are the same string. Change the gateway’s host, port or context and all four change together — and so does the client config.</p><h3>What’s next</h3><p><strong>Part 3</strong> wires this into the gateway: key-manager config, the two policies and their parameters, the client, the verification curls.</p><h3>References</h3><ul><li><a href="https://learn.microsoft.com/en-us/entra/identity-platform/identifier-uri-restrictions">Microsoft — Application ID URI restrictions and formats</a></li><li><a href="https://learn.microsoft.com/en-us/entra/identity-platform/access-token-claims-reference">Microsoft — access token claims and the </a><a href="https://learn.microsoft.com/en-us/entra/identity-platform/access-token-claims-reference">ver claim</a></li><li><a href="https://learn.microsoft.com/en-us/entra/identity-platform/reference-microsoft-graph-app-manifest">Microsoft — application manifest reference (</a><a href="https://learn.microsoft.com/en-us/entra/identity-platform/reference-microsoft-graph-app-manifest">requestedAccessTokenVersion)</a></li><li><a href="https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-on-behalf-of-flow">Microsoft — OAuth 2.0 On-Behalf-Of flow</a></li><li><a href="https://datatracker.ietf.org/doc/html/rfc8707">RFC 8707 — Resource Indicators for OAuth 2.0</a></li><li><a href="https://datatracker.ietf.org/doc/html/rfc9728">RFC 9728 — OAuth 2.0 Protected Resource Metadata</a></li><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization">MCP — Authorization specification</a></li></ul><p>If Entra rejects a port in your identifier URI where it accepted ours, or your MCP client refuses the discovery document where ours didn’t, tell me — those are the two behaviours here I’d least like readers to take on faith.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5782b67103e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prove It With curl First: On-Behalf-Of Onto Microsoft Work IQ]]></title>
            <link>https://medium.com/@sgmjaiya94/prove-it-with-curl-first-on-behalf-of-onto-microsoft-work-iq-0970a1fbdaab?source=rss-6de6771ac7d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/0970a1fbdaab</guid>
            <category><![CDATA[entra-id]]></category>
            <category><![CDATA[mcp-server]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[ai-gateway]]></category>
            <category><![CDATA[wso2]]></category>
            <dc:creator><![CDATA[Hasitha Jayanath Pathirana]]></dc:creator>
            <pubDate>Mon, 21 Sep 2026 00:59:46 GMT</pubDate>
            <atom:updated>2026-09-21T00:59:46.131Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Work IQ Behind the WSO2 AI Gateway — Part 1 of 3</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HhbQTvz8k8xQ-VCXt47s5g.png" /></figure><h3>About this series</h3><p>In an earlier series, <a href="https://medium.com/@sgmjaiya94/why-you-need-a-gateway-in-front-of-your-mcp-servers-9664433062df?sharedUserId=sgmjaiya94">MCP Behind the Gateway,</a> I wrote about putting a gateway in front of MCP servers: why you’d want one, why MCP’s OAuth rules stop it being a simple proxy, and the patterns that work. Those eight posts stay deliberately general.</p><p>This series is the hands-on follow-up. We took one real MCP server — Microsoft’s <strong>Work IQ</strong> — and connected an MCP client to it through <strong>WSO2 AI Gateway</strong>, with Microsoft Entra ID handling identity on both sides. Three posts, each one step of the job:</p><ol><li><strong>This post</strong> — proving with plain curl that the token exchange works before building anything.</li><li><strong>The Entra side</strong> — the app registrations and settings, step by step.</li><li><strong>The integration</strong> — the gateway configuration, the two policies, the client, and what went wrong on the way.</li></ol><p>You don’t need to have read the first series; where it helps, I link to the specific post.</p><p><strong>TL;DR</strong></p><ul><li>Before writing any gateway code, we needed one answer Microsoft’s docs don’t give: will Entra let a <em>third-party</em> confidential app exchange a user’s token for a Work IQ token via On-Behalf-Of? Four curl calls settle it in under an hour.</li><li>It does. Token B comes back with aud = Work IQ, the same oid as token A, and Work IQ answers initialize and tools/list with HTTP 200.</li><li>Two things the experiment taught us that shaped the design: B’s lifetime is capped by A’s (but not always equal), and OBO returns <em>every</em> scope the app has consent for.</li></ul><h3>The question</h3><p>Our first attempt at proxying Work IQ — passing the client’s own Microsoft token through the gateway — died at /authorize with AADSTS9010010 (<a href="https://medium.com/@sgmjaiya94/why-you-need-a-gateway-in-front-of-your-mcp-servers-9664433062df?sharedUserId=sgmjaiya94">why, in the first series</a>). The fix we settled on (<a href="https://medium.com/@sgmjaiya94/the-gateway-as-a-real-resource-server-with-on-behalf-of-4374d6a6d725?sharedUserId=sgmjaiya94">Part 5 there</a>): register the gateway as its own protected API, validate the client&#39;s token there, and exchange it for an upstream token with the same user inside.</p><p>That design has a precondition nobody could confirm for us. Work IQ is Microsoft’s own application. Would Entra allow <em>our</em> registration to perform On-Behalf-Of onto Work IQ scopes, or is that reserved for first-party clients? If no, the plan collapsed back to an authorization-server proxy that strips resource — still passthrough, still what the MCP spec says not to do.</p><p>Writing a gateway policy to find out costs days. curl costs an hour.</p><h3>Two facts before you start</h3><p><strong>Entra does not implement RFC 8693 token exchange</strong> (AADSTS70003). Its equivalent is On-Behalf-Of: grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer plus requested_token_use=on_behalf_of.</p><p><strong>The assertion must be a token <em>for your app</em>.</strong> A Work IQ-audience token — what a client gets talking to Work IQ directly — can’t be exchanged by anyone but Work IQ. So the experiment needs two tokens: A, minted for the gateway app, and B, minted from A.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*px_B4qUrG3MLtBfSTCrK6g.png" /><figcaption><em>[Diagram: two tokens, four calls — the same experiment the gateway will later automate]</em></figcaption></figure><h3>One-time setup: two app registrations</h3><p>You need two, not one — using the same registration as both client and API fails with AADSTS90009. Part 2 covers the production shape; this is the minimum for the test.</p><p><strong>Gateway app</strong> (confidential, single tenant):</p><ol><li><em>Expose an API</em> → Application ID URI api://&lt;gateway-app-id&gt; → add scope mcp.access.</li><li><em>Certificates &amp; secrets</em> → new client secret.</li><li><em>API permissions</em> → <em>APIs my organization uses</em> → search for Work IQ (its resource application ID was fxxxxx-xxxx-xxx-xxxx-xxxxxx7 in our tenant — confirm in yours) → <strong>Delegated</strong> → WorkIQAgent.Ask → <strong>Grant admin consent</strong>. If Work IQ doesn&#39;t appear, the service principal isn&#39;t in your tenant yet: az ad sp create --id &lt;workiq-app-id&gt;, then retry.</li></ol><p><strong>Public client app</strong> (stands in for the MCP client):</p><ol><li><em>Authentication</em> → <em>Add platform</em> → <strong>Mobile and desktop applications</strong> → redirect http://localhost:49643/callback; <em>Allow public client flows</em> = Yes.</li><li><em>API permissions</em> → <em>My APIs</em> → the gateway app → mcp.access → admin consent.</li><li>Back on the gateway app, <em>Expose an API</em> → <em>Authorized client applications</em> → add the public client’s ID for mcp.access. This suppresses the consent prompt.</li></ol><p>The platform in step 1 is not cosmetic. Entra classifies the client from the <em>platform of the matched redirect URI</em>. Under <em>Web</em>, code redemption demands a client_secret (AADSTS7000218) whatever the public-client toggle says.</p><h3>The four calls</h3><pre>TENANT=&lt;tenant-id&gt;; GW_APP=&lt;gateway-app-id&gt;; GW_SECRET=&#39;&lt;secret&gt;&#39;<br>PUB_APP=&lt;client-app-id&gt;; WORKIQ_APP=&lt;workiq-app-id&gt;<br>REDIRECT=&#39;http://localhost:49643/callback&#39;<br>TOKEN_EP=&quot;https://login.microsoftonline.com/$TENANT/oauth2/v2.0/token&quot;</pre><pre># PKCE<br>CODE_VERIFIER=$(openssl rand -base64 64 | tr -d &#39;=+/\n&#39; | cut -c1-96)<br>CODE_CHALLENGE=$(printf &#39;%s&#39; &quot;$CODE_VERIFIER&quot; | openssl dgst -sha256 -binary \<br>  | openssl base64 -A | tr &#39;+/&#39; &#39;-_&#39; | tr -d &#39;=&#39;)</pre><pre># print a JWT payload<br>jwtp() { python3 -c &#39;import sys,base64,json;p=sys.argv[1].split(&quot;.&quot;)[1];p+=&quot;=&quot;*(-len(p)%4);print(json.dumps(json.loads(base64.urlsafe_b64decode(p)),indent=2))&#39; &quot;$1&quot;; }</pre><p><strong>1. Authorize (browser), asking for the gateway scope.</strong> Open this URL, sign in, and copy code from the localhost:49643 address bar (&quot;connection refused&quot; is fine; nc -l 49643 catches it if you prefer).</p><pre>https://login.microsoftonline.com/$TENANT/oauth2/v2.0/authorize<br>  ?client_id=$PUB_APP&amp;response_type=code&amp;response_mode=query<br>  &amp;redirect_uri=http%3A%2F%2Flocalhost%3A49643%2Fcallback<br>  &amp;scope=api%3A%2F%2F$GW_APP%2Fmcp.access%20openid%20profile%20offline_access<br>  &amp;code_challenge=$CODE_CHALLENGE&amp;code_challenge_method=S256&amp;state=obo-test</pre><p><strong>2. Redeem the code → token A.</strong></p><pre>A=$(curl -s -X POST &quot;$TOKEN_EP&quot; -d client_id=&quot;$PUB_APP&quot; -d grant_type=authorization_code \<br>  -d code=&quot;$CODE&quot; -d redirect_uri=&quot;$REDIRECT&quot; -d code_verifier=&quot;$CODE_VERIFIER&quot; \<br>  -d scope=&quot;api://$GW_APP/mcp.access openid profile offline_access&quot; | jq -r .access_token)<br>jwtp &quot;$A&quot;    # expect: aud = api://&lt;gateway-app-id&gt; (or bare GUID), scp = mcp.access, oid present</pre><p><strong>3. On-Behalf-Of → token B.</strong> This is the call that answers the question.</p><pre>B=$(curl -s -X POST &quot;$TOKEN_EP&quot; \<br>  -d grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer \<br>  -d client_id=&quot;$GW_APP&quot; -d client_secret=&quot;$GW_SECRET&quot; \<br>  -d assertion=&quot;$A&quot; -d requested_token_use=on_behalf_of \<br>  -d scope=&quot;$WORKIQ_APP/WorkIQAgent.Ask offline_access&quot; | jq -r .access_token)<br>jwtp &quot;$B&quot;    # expect: aud = &lt;workiq-app-id&gt;, scp contains WorkIQAgent.Ask, SAME oid as A</pre><p><strong>4. Call Work IQ with B.</strong></p><pre>curl -si -X POST https://workiq.svc.cloud.microsoft/mcp \<br>  -H &quot;Authorization: Bearer $B&quot; -H &#39;Content-Type: application/json&#39; \<br>  -H &#39;Accept: application/json, text/event-stream&#39; \<br>  -d &#39;{&quot;jsonrpc&quot;:&quot;2.0&quot;,&quot;id&quot;:1,&quot;method&quot;:&quot;initialize&quot;,&quot;params&quot;:{&quot;protocolVersion&quot;:&quot;2025-06-18&quot;,&quot;capabilities&quot;:{},&quot;clientInfo&quot;:{&quot;name&quot;:&quot;curl-obo-test&quot;,&quot;version&quot;:&quot;0.1&quot;}}}&#39;</pre><p>Take the Mcp-Session-Id from the response, repeat with {&quot;method&quot;:&quot;tools/list&quot;}, and if you get 200 you have end-to-end proof.</p><h3>What we saw</h3><p>Step 3 succeeded on the first clean run: token B had aud = Work IQ, azp = the gateway app, and <strong>the same </strong><strong>oid as token A</strong>. Step 4: initialize 200, tools/list 200 with ask, fetch_blob and friends. Microsoft permits third-party OBO onto Work IQ.</p><p>Three observations went straight into the gateway design:</p><ul><li><strong>sub differs between A and B</strong> — it&#39;s pairwise per application. Correlate identity on oid, never sub.</li><li><strong>B’s lifetime is bounded by A’s, but not equal to it.</strong> Run one: exp_B ≈ exp_A. Run two: B expired eleven minutes <em>before</em> A. Cache B with min(exp_A, exp_B) − buffer; both bounds bite.</li><li><strong>B carried four scopes</strong>, not the one we asked for — every Work IQ scope the gateway app had consent for. OBO is not a filter. Grant WorkIQAgent.Ask and nothing else.</li></ul><h3>The error decoder</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*haY_ZCQhXjxsFeDGTaeRKQ.png" /></figure><h3>What this doesn’t solve</h3><ul><li><strong>It proves the grant, not the gateway.</strong> Nothing here validates token A, caches B, or maps failures to the right HTTP status. That’s Part 3.</li><li><strong>Token A’s audience here is </strong><strong>api://….</strong> A real MCP client will instead send resource=&lt;the gateway&#39;s MCP URL&gt;, because the spec requires it — and Entra validates that parameter. Whether it accepts <em>our</em> URL is the first question of Part 2.</li></ul><h3>What’s next</h3><p><strong>Part 2</strong> turns the two throwaway registrations into the real configuration: identifier URIs, token versions, redirect platforms, and the strings that must match character for character across Entra, the gateway and the client.</p><h3>References</h3><ul><li><a href="https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-on-behalf-of-flow">Microsoft identity platform — OAuth 2.0 On-Behalf-Of flow</a></li><li><a href="https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-auth-code-flow">Microsoft identity platform — authorization code flow (PKCE, public clients)</a></li><li><a href="https://learn.microsoft.com/en-us/entra/identity-platform/reference-error-codes">Microsoft — Entra authentication and authorization error codes</a></li><li><a href="https://learn.microsoft.com/en-us/troubleshoot/entra/entra-id/app-integration/confidential-client-application-authentication-error-aadsts7000218">Microsoft — AADSTS7000218 troubleshooting (Web vs. desktop redirect URIs)</a></li><li><a href="https://datatracker.ietf.org/doc/html/rfc8693">RFC 8693 — OAuth 2.0 Token Exchange</a> (what Entra <em>doesn’t</em> implement)</li><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization">MCP — Authorization specification</a></li></ul><p>If you’ve run this against a different Microsoft-published MCP server and got a different answer at step 3, I’d like to hear about it — that’s the one result I can only vouch for on Work IQ.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0970a1fbdaab" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing a Custom Policy for WSO2 AI Gateway]]></title>
            <link>https://medium.com/@sgmjaiya94/writing-a-custom-policy-for-wso2-ai-gateway-2c4d2f52257a?source=rss-6de6771ac7d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c4d2f52257a</guid>
            <category><![CDATA[entra-id]]></category>
            <category><![CDATA[wso2]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[mcp-server]]></category>
            <category><![CDATA[ai-gateway]]></category>
            <dc:creator><![CDATA[Hasitha Jayanath Pathirana]]></dc:creator>
            <pubDate>Fri, 18 Sep 2026 13:02:10 GMT</pubDate>
            <atom:updated>2026-09-18T13:02:10.454Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>MCP Behind the Gateway — Part 8</strong></p><p><em>Series index: </em><a href="https://medium.com/@sgmjaiya94/why-you-need-a-gateway-in-front-of-your-mcp-servers-9664433062df?sharedUserId=sgmjaiya94"><em>Part 1</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/the-one-line-in-the-mcp-spec-that-breaks-your-gateway-f0a78dbccaed?sharedUserId=sgmjaiya94"><em>Part 2</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/authenticated-passthrough-let-the-gateway-serve-the-metadata-2b0f4f5c9047?sharedUserId=sgmjaiya94"><em>Part 3</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/why-the-passthrough-trick-only-works-against-github-1d9d5e966f60?sharedUserId=sgmjaiya94"><em>Part 4</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/the-gateway-as-a-real-resource-server-with-on-behalf-of-4374d6a6d725?sharedUserId=sgmjaiya94"><em>Part 5 </em></a><em>· </em><a href="https://medium.com/@sgmjaiya94/four-ways-to-get-a-token-to-the-backend-mcp-server-447922c20145?sharedUserId=sgmjaiya94"><em>Part 6 </em></a><em>· </em><a href="https://medium.com/@sgmjaiya94/building-it-on-wso2-ai-gateway-17e46927a1b5?sharedUserId=sgmjaiya94"><em>Part 7</em></a><em> · Part 8 (this post).</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PjIHZUFx58iev6dJlDqHOQ.png" /></figure><p>This is the last article in the series, and it’s the one I’d have found most useful on day one.</p><p>Everything in Parts 3 and 5 needed behaviour the shipped policies don’t have — rewriting Protected Resource Metadata, rewriting an upstream challenge, doing an On-Behalf-Of exchange. So we wrote two custom policies. This article is how that works, what the pipeline looks like, and the one bug that took the longest to find.</p><h3>What a custom policy is</h3><p>A policy on WSO2 AI Gateway is <strong>Go code compiled into the gateway binaries</strong>. That’s the most important sentence here, because it sets everything else:</p><ul><li>You don’t upload a policy to a running gateway. You <strong>build a new gateway image</strong> that contains it.</li><li>The ap CLI does the build. It ships as its own release from the <a href="https://github.com/wso2/api-platform">wso2/api-platform</a> repository — <strong>not</strong> bundled with the gateway distribution. Check the flags against the version you have; ours took ap gateway image build --path &lt;dir&gt;.</li></ul><p><strong>Start by forking, not from scratch.</strong> Both of ours were forks: the inbound one from the shipped mcp-auth policy, the outbound one from oauth2-generator. The outbound fork inherited token-endpoint client code, caching with an expiry buffer, jittered retry backoff, cache purge on upstream 401, and header injection — roughly 1,100 lines we did not write. Our actual change was a few hundred lines: a different grant type, an assertion source, a per-token cache key, and a TTL bounded by the assertion&#39;s own expiry.</p><h3>The pipeline</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zZOFkSq8RPCSjKjS_UqhDg.png" /><figcaption><em>[Diagram: source to running policy, in seven steps]</em></figcaption></figure><p>BUILD_DIR=&lt;path to the gateway distribution&gt;</p><pre># 1. copy the policy in as a &quot;local policy&quot; (tests excluded)<br>rsync -a --delete --exclude &#39;*_test.go&#39; ./policies/my-policy/ &quot;$BUILD_DIR/my-policy-v0.2.0/&quot;</pre><pre># 2. build.yaml — keep the stock policies, add yours<br>policies:<br>  - name: my-policy<br>    filePath: my-policy-v0.2.0</pre><pre># 3. build, then point compose at the resulting image tags and roll the two services<br>cd &quot;$BUILD_DIR&quot; &amp;&amp; ap gateway image build --path .<br>docker compose up -d --force-recreate gateway-controller gateway-runtime</pre><p>Three verifications, and I’d do all three every time:</p><pre># the definition is inside the image — LOCAL policies land in /app/policies/,<br># NOT /app/default-policies/ (I looked in the wrong place for half an hour)<br>docker exec &lt;controller&gt; ls /app/policies/ | grep my-policy</pre><pre># the controller loaded it<br>docker logs &lt;controller&gt; --since 5m | grep &#39;Loaded policy definition&#39; | grep my-policy</pre><pre># the manifest reached the Workspace (pushed on controller connect, ~15s)<br>docker logs &lt;platform-api&gt; --since 5m | grep organization_policy_count</pre><p><strong>One rule that will save you a confusing hour: exactly one </strong><strong>v0.x.y directory of a given policy per image.</strong> Proxies reference the policy by <em>major</em> version (v0), and the controller has to resolve that to a unique installed version. When you upgrade, replace the directory and the filePath — never leave two behind.</p><h3>Sync and attach</h3><p>A custom policy does <strong>not</strong> appear in the Workspace attach list until it is synced from the gateway’s manifest into the organisation registry. This is manual and once per version: Gateways → your gateway → Policies → <strong>Sync</strong>. There’s a sync API if the UI can’t render that tab (it needs the public policy hub reachable, which an air-gapped environment won’t have).</p><p>Then attach it to a proxy, set parameters, Save, and <strong>Deploy to Gateway</strong> (see Part 7 — Save alone does nothing).</p><p>A few things that behave oddly here, so you don’t think you broke them:</p><ul><li>The Workspace stores the reference as <strong>major only</strong> (v0). Passing a full sever version gets you a validation failure that surfaces as a misleading 404 … not found — the real reason is in the controller logs.</li><li><strong>Leave the execution condition empty</strong> on MCP proxies. It’s silently dropped, so don’t build logic on it.</li><li>If you attach policies directly to the controller via CLI instead of through the Workspace, a later Workspace redeploy of that proxy will <strong>silently revert</strong> your attachment. We kept a small regression script and ran it after every Workspace action specifically to catch that.</li></ul><h3>Version bumps</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g1xN9RGht31EtZRGu_G83w.png" /></figure><h3>Phases beat order</h3><p>A policy hooks the request in a <strong>header</strong> phase and, if it declares one, a <strong>body</strong> phase — and every policy’s header phase runs before any policy’s body phase, whatever order they’re attached in. That follows from the transport: the policy engine is an Envoy external processor, and <a href="https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/filters/http/ext_proc/v3/ext_proc.proto">Envoy’s ext_proc protocol</a> delivers request headers and request body as separate, ordered messages, waiting for the processor’s answer to each. Attachment order decides the order <em>within</em> a phase; nothing can move a body hook ahead of a header hook.</p><p>Our inbound auth policy authenticated POST /mcp in the body phase, because it has to parse the JSON-RPC method to apply per-tool rules. The OBO policy&#39;s &quot;is the caller authenticated?&quot; check ran in the header phase. So the check always fired before authentication had happened, and a perfectly valid token was rejected every time. Reordering the two changed nothing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WH5Itnw_oW-CmMPzQm_kQw.png" /><figcaption><em>[Diagram: why attachment order cannot fix a phase mismatch]</em></figcaption></figure><p>If your policies depend on each other, put the dependent logic in the same phase as the thing it depends on. And on MCP routes: declare no response-body processing (the route streams text/event-stream, and buffering breaks it in a way that looks like a slow upstream), and keep any WWW-Authenticate rewrite header-only.</p><h3>What this doesn’t solve</h3><ul><li><strong>Other gateways.</strong> The concepts port — Kong’s equivalent work in their MCP cookbooks is DataKit plus Response Transformer, and agentgateway builds providers in Rust — but none of the mechanics here do.</li><li><strong>The design questions.</strong> A custom policy makes a design possible; it doesn’t make a bad design correct. Parts 2, 4 and 6 are the ones to argue about first.</li></ul><h3>Wrapping up the series</h3><p>Where we landed, in one paragraph:</p><p>A gateway in front of MCP is worth building (Part 1), but MCP’s authorization rules mean it can’t be a transparent proxy (Part 2): the token issued to the gateway never goes upstream, and the token that goes upstream was never meant for the gateway. Passthrough with metadata rewriting is quick and stateless, and it works against lenient authorization servers (Part 3) — which is a narrower guarantee than it looks (Part 4). Where your identity provider covers both the user and the MCP server, make the gateway a real resource server and exchange tokens (Part 5). Where it doesn’t, you need a per-user credential vault, and you should know which of the four patterns you’re picking and why (Part 6). Parts 7 and 8 are how to build that on WSO2 AI Gateway.</p><p>Thanks for reading all eight. If you’re building something similar and hit a wall — I’ve probably hit the same one, so do get in touch.</p><h3>References</h3><ul><li><a href="https://wso2.com/api-platform/ai-gateway/">WSO2 AI Gateway — product overview</a></li><li><a href="https://github.com/wso2/api-platform">WSO2 API Platform on GitHub — releases, including the </a><a href="https://github.com/wso2/api-platform">ap CLI</a></li><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization">MCP — Authorization specification</a></li><li><a href="https://developer.konghq.com/cookbooks/secure-external-mcp-gateway/">Kong — Secure External MCP Gateway cookbook</a></li><li><a href="https://developer.konghq.com/cookbooks/secure-internal-mcp-gateway/">Kong — Secure Internal MCP Gateway cookbook</a></li><li><a href="https://agentgateway.dev/docs/standalone/latest/documentation/mcp/mcp-authn/">agentgateway — MCP authentication</a></li><li><a href="https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-on-behalf-of-flow">Microsoft identity platform — On-Behalf-Of flow</a></li><li><a href="https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/filters/http/ext_proc/v3/ext_proc.proto">Envoy — External Processing filter</a></li></ul><p>If you write a policy for this gateway and find the phase model behaves differently from what I’ve described, please tell me — I’d rather correct the post than have someone lose the week we lost. And if the series was useful, pass it on to whoever on your team is about to put a gateway in front of MCP.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c4d2f52257a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building It on WSO2 AI Gateway]]></title>
            <link>https://medium.com/@sgmjaiya94/building-it-on-wso2-ai-gateway-17e46927a1b5?source=rss-6de6771ac7d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/17e46927a1b5</guid>
            <category><![CDATA[ai-gateway]]></category>
            <category><![CDATA[wso2]]></category>
            <category><![CDATA[entra-id]]></category>
            <category><![CDATA[mcp-server]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Hasitha Jayanath Pathirana]]></dc:creator>
            <pubDate>Fri, 18 Sep 2026 12:59:57 GMT</pubDate>
            <atom:updated>2026-09-21T01:45:51.287Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>MCP Behind the Gateway — Part 7</strong></p><p><em>Series index: </em><a href="https://medium.com/@sgmjaiya94/why-you-need-a-gateway-in-front-of-your-mcp-servers-9664433062df?sharedUserId=sgmjaiya94"><em>Part 1</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/the-one-line-in-the-mcp-spec-that-breaks-your-gateway-f0a78dbccaed?sharedUserId=sgmjaiya94"><em>Part 2</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/authenticated-passthrough-let-the-gateway-serve-the-metadata-2b0f4f5c9047?sharedUserId=sgmjaiya94"><em>Part 3 </em></a><em>· </em><a href="https://medium.com/@sgmjaiya94/why-the-passthrough-trick-only-works-against-github-1d9d5e966f60?sharedUserId=sgmjaiya94"><em>Part 4</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/the-gateway-as-a-real-resource-server-with-on-behalf-of-4374d6a6d725?sharedUserId=sgmjaiya94"><em>Part 5</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/four-ways-to-get-a-token-to-the-backend-mcp-server-447922c20145?sharedUserId=sgmjaiya94"><em>Part 6</em></a><em> · Part 7 (this post) · </em><a href="https://medium.com/@sgmjaiya94/writing-a-custom-policy-for-wso2-ai-gateway-2c4d2f52257a?sharedUserId=sgmjaiya94"><em>Part 8</em></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KLyvJyRJMAUL6_u0zK2UJw.png" /></figure><p>Six articles of protocol. Time for some configuration.</p><p>This article walks through putting an MCP proxy on <strong>WSO2 AI Gateway 1.2</strong> with <strong>AI Workspace 1.0</strong>, and the handful of things I wish somebody had told me before I started. The design decisions behind all of it are in Parts 2–6; here I’m only covering mechanics.</p><h3>The two halves of the product</h3><p>Two pieces you need to keep straight:</p><ul><li><strong>AI Workspace</strong> is design time and the control plane. You create the MCP proxy, attach policies, set their parameters, and deploy. One Workspace can manage several gateways.</li><li><strong>AI Gateway</strong> is runtime — a controller plus a runtime, both Docker images, running as a standalone data plane. Some settings live <em>only</em> here, in config.toml, and are <strong>gateway-wide</strong> rather than per-proxy. That distinction bites, and I&#39;ll come back to it.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OWRN0ZmtSf-RHMs_rabUlw.png" /><figcaption><em>[Diagram: what you configure where, and how it reaches the runtime]</em></figcaption></figure><p>Step 1: Publish the MCP proxy</p><p>In the Workspace, create an entry to proxy an existing MCP server:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7t2pmZvbYvwHh-QR7waj_w.png" /></figure><p>Then <strong>write down the published URL</strong>, because it is load-bearing in a way no other field is:</p><pre>https://gateway.example.com:8443/default/vendor-mcp-server/mcp</pre><p>Note that the deployed context may not be the bare name you typed — ours came out as /default/&lt;name&gt;-server. This exact string has to match, character for character:</p><ol><li>the PRM resource the gateway serves,</li><li>the url in every client config,</li><li>the identifier URI you register at the IdP (if you’re doing the Part 5 design),</li><li>the scope string derived from it.</li></ol><p>If you change the host, the port or the context later, all four change together. I lost an evening to a /vendor-mcp written in my notes versus a /default/vendor-mcp-server actually deployed — everything else was correct, and the client just kept refusing.</p><h3>Step 2: Gateway configuration</h3><p>In configs/config.toml on the gateway distribution:</p><pre>[policy_configurations.jwtauth_v1]<br># The inbound header carrying the token the GATEWAY validates.<br># ⚠ gateway-WIDE, shared by jwt-auth and mcp-auth. One header name per gateway.<br>headername = &quot;Authorization&quot;</pre><pre>[[policy_configurations.jwtauth_v1.keymanagers]]<br>name       = &quot;my-idp&quot;<br>issuer     = &quot;https://idp.example.com/oauth2/token&quot;   # must equal the token&#39;s iss exactly<br>scopeClaim = &quot;scp&quot;                                    # some IdPs use a space-separated string</pre><pre>[policy_configurations.jwtauth_v1.keymanagers.jwks.remote]<br>uri = &quot;https://idp.example.com/oauth2/jwks&quot;</pre><pre>[policy_configurations.mcpauth_v1]<br># Host ONLY — no scheme, no port (the port is taken from the request).<br># Used to build the PRM resource URL and the rewritten challenge pointer.<br>gatewayhost = &quot;gateway.example.com&quot;</pre><pre># mcp-auth reads its key managers from its own section — mirror the block here.<br>[[policy_configurations.mcpauth_v1.keymanagers]]<br>name       = &quot;my-idp&quot;<br>issuer     = &quot;https://idp.example.com/oauth2/token&quot;<br>scopeClaim = &quot;scp&quot;<br>[policy_configurations.mcpauth_v1.keymanagers.jwks.remote]<br>uri = &quot;https://idp.example.com/oauth2/jwks&quot;</pre><p>The key manager name matters beyond being a label: the issuers parameter on the MCP auth policy references it, and it is what ends up in the PRM&#39;s authorization_servers.</p><p><strong>The </strong><strong>headername line deserves a warning label.</strong> It is gateway-wide. The passthrough design from Part 3 needs a <em>non</em>-Authorization header, because Authorization belongs to the upstream token; the OBO design from Part 5 needs it to <em>be</em> Authorization, because the client&#39;s OAuth token goes there. You cannot have both on one gateway instance. We discovered this by flipping it for a new proxy and quietly breaking the old one. Plan your instances around it, or your demo will have a hole in it.</p><h3>Step 3: Policies</h3><p>Attach policies on the proxy’s <strong>Policies</strong> tab. The ones that matter for MCP:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CQuQ74RG4Zjk1XGFILSCgw.png" /></figure><p>Two operational rules that are not obvious from the UI:</p><ul><li><strong>The list order is the execution order</strong> — top runs first. Put inbound auth above anything that depends on the authenticated identity. Getting this wrong doesn’t just fail requests; if the credential policy fails closed above the auth policy, it will also block the unauthenticated PRM endpoint, and client discovery dies completely.</li><li><strong>Save is not Deploy.</strong> After Save, go to Overview → <strong>Deploy to Gateway</strong>. A reordered chain that was only saved changes nothing at runtime, and you will spend twenty minutes debugging the last deployment.</li></ul><h3>Step 4: Verify from the outside</h3><p>Three curls tell you whether the wiring is right. Run them before you touch a client.</p><pre>GW=https://gateway.example.com:8443; CTX=/default/vendor-mcp-server<br>INIT=&#39;{&quot;jsonrpc&quot;:&quot;2.0&quot;,&quot;id&quot;:1,&quot;method&quot;:&quot;initialize&quot;,&quot;params&quot;:{&quot;protocolVersion&quot;:&quot;2025-06-18&quot;,&quot;capabilities&quot;:{},&quot;clientInfo&quot;:{&quot;name&quot;:&quot;verify&quot;,&quot;version&quot;:&quot;0.0.1&quot;}}}&#39;</pre><pre># 1. PRM is served WITHOUT a token, and resource matches the published URL exactly<br>curl -s &quot;$GW$CTX/.well-known/oauth-protected-resource&quot; | jq &#39;{resource, authorization_servers}&#39;</pre><pre># 2. A token-less POST returns 401 with a pointer to that PRM<br>curl -s -i -X POST &quot;$GW$CTX/mcp&quot; -H &#39;Content-Type: application/json&#39; \<br>  -H &#39;Accept: application/json, text/event-stream&#39; -d &quot;$INIT&quot; | grep -i www-authenticate</pre><pre># 3. Happy path with a valid token<br>curl -s -o /dev/null -w &#39;%{http_code}\n&#39; -X POST &quot;$GW$CTX/mcp&quot; \<br>  -H &#39;Content-Type: application/json&#39; -H &#39;Accept: application/json, text/event-stream&#39; \<br>  -H &quot;Authorization: Bearer $TOKEN&quot; -d &quot;$INIT&quot;</pre><p>Note the dual Accept header on /mcp POSTs — some MCP servers reject a single value with a 400, and older examples floating around still show one.</p><h3>The gotchas that cost us an afternoon each</h3><ul><li><strong>The PRM lives at a non-RFC location.</strong> The gateway serves it at /{context}/.well-known/oauth-protected-resource, not at the RFC 9728 path-insertion form on the origin. Clients that follow the resource_metadata pointer in the 401 — Claude Code, MCP Inspector — are fine. A client that probes the RFC well-known path without reading the challenge first will get a 404. Know which kind you have.</li><li><strong>Client-side discovery caching.</strong> If a client previously talked to a different gateway or authorization server for the same server name, it may have cached the discovered AS. Reset the client’s stored choices before concluding your config is wrong.</li><li><strong>A client suddenly opening </strong><strong>https://&lt;gateway&gt;/authorize.</strong> That is not a client misconfiguration — it means the PRM fetch <em>failed</em>, so the client fell back to treating the MCP origin as its own authorization server. Go and check why the well-known endpoint isn&#39;t being served (nine times out of ten: a policy above it failing closed).</li></ul><h3>What this doesn’t solve</h3><p>The built-in policies get you a long way, but not all the way. Neither the PRM rewriting from Part 3 nor the On-Behalf-Of exchange from Part 5 exists as a shipped policy — we had to write both.</p><h3>What’s next</h3><p><strong>Part 8</strong>, the last in the series: writing a custom policy for the gateway. How the build and sync pipeline actually works, how versioning behaves, and the request-phase trap that produced the single most confusing bug of the entire PoC.</p><h3>References</h3><ul><li><a href="https://wso2.com/api-platform/ai-gateway/">WSO2 AI Gateway — product overview</a></li><li><a href="https://github.com/wso2/api-platform">WSO2 API Platform on GitHub — releases, including the </a><a href="https://github.com/wso2/api-platform">ap CLI</a></li><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization">MCP — Authorization specification</a></li><li><a href="https://datatracker.ietf.org/doc/html/rfc9728">RFC 9728 — OAuth 2.0 Protected Resource Metadata</a></li><li><a href="https://code.claude.com/docs/en/mcp">Claude Code — MCP configuration</a></li><li><a href="https://developer.konghq.com/cookbooks/secure-external-mcp-gateway/">Kong — Secure External MCP Gateway cookbook</a> (for comparison of the same pattern on another gateway)</li></ul><p>If a step here doesn’t match what your version of the product does, tell me and I’ll fix the post — this one will age faster than the others. And if it was useful, pass it on to whoever on your team is about to put a gateway in front of MCP.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17e46927a1b5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Four Ways to Get a Token to the Backend MCP Server]]></title>
            <link>https://medium.com/@sgmjaiya94/four-ways-to-get-a-token-to-the-backend-mcp-server-447922c20145?source=rss-6de6771ac7d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/447922c20145</guid>
            <category><![CDATA[wso2]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[mcp-server]]></category>
            <category><![CDATA[mcps]]></category>
            <category><![CDATA[ai-gateway]]></category>
            <dc:creator><![CDATA[Hasitha Jayanath Pathirana]]></dc:creator>
            <pubDate>Thu, 17 Sep 2026 10:37:04 GMT</pubDate>
            <atom:updated>2026-09-18T13:03:05.672Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>MCP Behind the Gateway — Part 6</strong></p><p><em>Series index: </em><a href="https://medium.com/@sgmjaiya94/why-you-need-a-gateway-in-front-of-your-mcp-servers-9664433062df?sharedUserId=sgmjaiya94"><em>Part 1</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/the-one-line-in-the-mcp-spec-that-breaks-your-gateway-f0a78dbccaed?sharedUserId=sgmjaiya94"><em>Part 2</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/authenticated-passthrough-let-the-gateway-serve-the-metadata-2b0f4f5c9047?sharedUserId=sgmjaiya94"><em>Part 3</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/why-the-passthrough-trick-only-works-against-github-1d9d5e966f60?sharedUserId=sgmjaiya94"><em>Part 4</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/the-gateway-as-a-real-resource-server-with-on-behalf-of-4374d6a6d725?sharedUserId=sgmjaiya94"><em>Part 5</em></a><em> · Part 6 (this post) · </em><a href="https://medium.com/@sgmjaiya94/building-it-on-wso2-ai-gateway-17e46927a1b5?sharedUserId=sgmjaiya94"><em>Part 7</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/writing-a-custom-policy-for-wso2-ai-gateway-2c4d2f52257a?sharedUserId=sgmjaiya94"><em>Part 8</em></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QdcYp9Z9Tmdf-nh2Vff9Lw.png" /></figure><p>By now we have two working patterns and a clear sense of where each one breaks. <a href="https://medium.com/@sgmjaiya94/authenticated-passthrough-let-the-gateway-serve-the-metadata-2b0f4f5c9047?sharedUserId=sgmjaiya94">Part 3</a> gave us authenticated passthrough, <a href="https://medium.com/@sgmjaiya94/why-the-passthrough-trick-only-works-against-github-1d9d5e966f60?sharedUserId=sgmjaiya94">Part 4</a> showed it depends on a lenient authorization server, and <a href="https://medium.com/@sgmjaiya94/the-gateway-as-a-real-resource-server-with-on-behalf-of-4374d6a6d725?sharedUserId=sgmjaiya94">Part 5</a> gave us the clean On-Behalf-Of design that only works inside one trust domain.</p><p>A real organisation has neither problem in isolation. It has a mixed estate: a couple of internal MCP servers, GitHub, an issue tracker, a vendor tool with no user model at all.</p><p>This article is the reference sheet: <strong>four outbound patterns, and how to choose.</strong> If you only bookmark one article in this series, make it this one.</p><h3>The question you’re actually answering</h3><p>From Part 2: the gateway is a resource server on the inbound leg, and on the outbound leg it is responsible for making sure the credential that goes upstream was issued for the upstream. Inbound is the easy half — you authenticate your own employees against your own identity provider, and it looks the same for every MCP server you front.</p><p>The outbound leg is where all the variety lives. The question is simply:</p><blockquote><strong><em>What credential goes in </em></strong><strong><em>Authorization when the gateway calls the upstream, and where did that credential come from?</em></strong></blockquote><p>There are four defensible answers. They are not interchangeable, and the choice is made for you by two properties of the backend.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LOs_RjGnJErZ6oa3tnUoBg.png" /><figcaption><em>[Diagram: the decision tree — two questions decide it]</em></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1TJVkDj4ykcrhOgzFhzUSw.png" /></figure><h3>1. Token exchange / On-Behalf-Of</h3><p>Covered in Part 5. The best option whenever it’s available: no stored credentials, no consent dance, real user identity end to end, and nothing in it that contradicts the spec.</p><p><strong>Use it when</strong> your identity provider protects both your employees and the MCP server — or, put the other way round, when the upstream already trusts your issuer.</p><h3>2. Per-user 3LO plus a credential vault</h3><p>This is the answer for third-party SaaS, and it’s worth being precise about <em>why</em> nothing simpler works.</p><p>Token exchange needs an authorization server capable of minting a token the resource server accepts. GitHub accepts only GitHub-issued tokens. Your IdP cannot mint one. This isn’t a configuration gap — <strong>exchange is an intra-trust-domain mechanism by construction</strong>, and no amount of parameter shaping crosses that boundary.</p><p>So somebody has to complete a real OAuth flow with GitHub, once, per user. The gateway’s job is to make that happen once and then never bother the user again:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m8fqqP0yjUWz5O4KEW9GSA.png" /><figcaption><em>[Diagram: the gateway as OAuth client, with a per-user token store]</em></figcaption></figure><p>The gateway owns <strong>one</strong> OAuth application per provider, with a fixed callback. On a cache miss it returns “authorization required” plus a consent URL; the user completes the provider’s flow; access and refresh tokens are stored keyed by (user, backend). Every later call is a vault lookup and a header swap.</p><p>This is not an exotic design — it’s what AWS ships as <a href="https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-oauth.html">AgentCore Inbound Auth / Outbound Auth</a> with 3LO, and what Azure API Management ships as <a href="https://learn.microsoft.com/en-us/azure/api-management/credentials-overview">Credential Manager</a>. It is also, notably, the pattern that <strong>works for OAuth-only clients</strong>, which passthrough never will.</p><p><strong>The cost is real:</strong> you are now storing refresh tokens for every employee at every provider. That is a high-value target and an operational commitment — encryption, rotation, revocation, offboarding, and a clear answer to “what happens when someone leaves”.</p><p><strong>And one hard rule.</strong> If your gateway becomes a full OAuth proxy — a static client id at the third-party authorization server, plus dynamic client registration facing your MCP clients — you have assembled the precondition set for the confused-deputy attack the <a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/security-considerations">MCP security considerations</a> document. The response there is unambiguous:</p><blockquote><em>MCP proxy servers using static client IDs </em><strong><em>MUST</em></strong><em> obtain user consent for each dynamically registered client before forwarding to third-party authorization servers.</em></blockquote><p>Per-client consent at the gateway is not optional in that shape.</p><h3>3. Service credential</h3><p>Some MCP servers have no user model — an internal data service, a vendor tool with a single API key. The gateway injects a PAT, a client-credentials token, or presents an mTLS identity: one machine identity from the gateway to the MCP server. If any user context travels at all, it travels as claims or headers the gateway propagates and the backend chooses to trust — not as a credential the backend can verify.</p><p>Perfectly legitimate, with one consequence to be honest about in your design doc: the backend’s audit log will show <em>the gateway</em> for every call, forever. All per-user accountability lives on your side of the hop, so your logs are now the only record that ties an action to a person. Keep them accordingly.</p><h3>4. Authenticated passthrough</h3><p>Part 3. Stateless and quick, but it needs a client that can send a custom header, and an upstream authorization server that ignores the resource parameter. Treat it as a tactical option — good for a pilot, good for a developer-tools audience, not something to standardise a company on.</p><h3>Where the standards are going</h3><p>The reason the vault exists at all is that there’s no standard way to say <em>“this user, at my IdP, is the same user as at your IdP”</em> across a trust boundary.</p><p>That’s actively being fixed. The IETF draft <a href="https://datatracker.ietf.org/doc/html/draft-ietf-oauth-identity-assertion-authz-grant">Identity Assertion JWT Authorization Grant</a> (ID-JAG) and Okta’s <a href="https://developer.okta.com/docs/concepts/xaa/">Cross App Access</a> (XAA, which implements ID-JAG) let an identity provider issue an assertion that a <em>different</em> provider will exchange for its own access token. No per-user consent dance, no refresh tokens in your vault.</p><p>When the SaaS vendors you care about support it, pattern 2 collapses into pattern 1 and this whole article gets much shorter. They mostly don’t yet — so build the vault, but keep the exchange point behind an interface you can swap.</p><h3>What’s next</h3><p>Six articles of protocol and design; time for some YAML. <strong>Part 7</strong> builds this on WSO2 AI Gateway — publishing an MCP proxy, wiring up key managers, which policies matter, and the handful of operational gotchas that will otherwise eat an afternoon.</p><h3>References</h3><ul><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization">MCP — Authorization specification</a></li><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/security-considerations">MCP — Authorization security considerations (confused deputy, token passthrough)</a></li><li><a href="https://datatracker.ietf.org/doc/html/rfc8693">RFC 8693 — OAuth 2.0 Token Exchange</a></li><li><a href="https://datatracker.ietf.org/doc/html/rfc8707">RFC 8707 — Resource Indicators for OAuth 2.0</a></li><li><a href="https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-oauth.html">Amazon Bedrock AgentCore — Inbound Auth and Outbound Auth</a></li><li><a href="https://learn.microsoft.com/en-us/azure/api-management/credentials-overview">Azure API Management — Credential Manager overview</a></li><li><a href="https://developer.konghq.com/cookbooks/secure-external-mcp-gateway/">Kong — Secure External MCP Gateway cookbook</a></li><li><a href="https://developer.okta.com/docs/concepts/xaa/">Okta — Cross App Access (XAA)</a></li><li><a href="https://datatracker.ietf.org/doc/html/draft-ietf-oauth-identity-assertion-authz-grant">IETF — Identity Assertion JWT Authorization Grant (ID-JAG)</a></li></ul><p>If you’ve got a backend that doesn’t fit any of the four rows, tell me — I’d rather find out the table is incomplete than have people force-fit it. And if it was useful, pass it on to whoever on your team is about to put a gateway in front of MCP.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=447922c20145" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Gateway as a Real Resource Server, with On-Behalf-Of]]></title>
            <link>https://medium.com/@sgmjaiya94/the-gateway-as-a-real-resource-server-with-on-behalf-of-4374d6a6d725?source=rss-6de6771ac7d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/4374d6a6d725</guid>
            <category><![CDATA[wso2]]></category>
            <category><![CDATA[mcps]]></category>
            <category><![CDATA[ai-gateway]]></category>
            <category><![CDATA[obo]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Hasitha Jayanath Pathirana]]></dc:creator>
            <pubDate>Thu, 17 Sep 2026 10:33:27 GMT</pubDate>
            <atom:updated>2026-09-18T13:02:58.799Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>MCP Behind the Gateway — Part 5</strong></p><p><em>Series index: </em><a href="https://medium.com/@sgmjaiya94/why-you-need-a-gateway-in-front-of-your-mcp-servers-9664433062df?sharedUserId=sgmjaiya94"><em>Part 1</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/the-one-line-in-the-mcp-spec-that-breaks-your-gateway-f0a78dbccaed?sharedUserId=sgmjaiya94"><em>Part 2</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/authenticated-passthrough-let-the-gateway-serve-the-metadata-2b0f4f5c9047?sharedUserId=sgmjaiya94"><em>Part 3</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/why-the-passthrough-trick-only-works-against-github-1d9d5e966f60?sharedUserId=sgmjaiya94"><em>Part 4</em></a><em> · Part 5 (this post) · </em><a href="https://medium.com/@sgmjaiya94/four-ways-to-get-a-token-to-the-backend-mcp-server-447922c20145?sharedUserId=sgmjaiya94"><em>Part 6 </em></a><em>· </em><a href="https://medium.com/@sgmjaiya94/building-it-on-wso2-ai-gateway-17e46927a1b5?sharedUserId=sgmjaiya94"><em>Part 7</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/writing-a-custom-policy-for-wso2-ai-gateway-2c4d2f52257a?sharedUserId=sgmjaiya94"><em>Part 8</em></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ULpHcW9_cIGgTACxlTJQcg.png" /></figure><p><a href="https://claude.ai/cowork/cse_01Cz7DpAYhnX19gQwe9Fkc1Q#">Part 4</a> ended on an uncomfortable note: the passthrough pattern worked against GitHub only because GitHub’s authorization server ignores the resource parameter. Point it at an Entra-protected MCP server and it dies at /authorize.</p><p>This article is the design we actually shipped, and it is the one I’d recommend whenever it’s available to you. It has no rewriting, no lying about resource, and no passthrough — <strong>every hop carries a token minted for exactly that hop&#39;s audience</strong>.</p><p>The trick is a change of perspective, not a change of plumbing.</p><h3>Stop proxying the identity — own it</h3><p>In every design so far, the gateway tried to get the <em>client</em> a token for the <em>upstream</em>. That’s the part that keeps breaking.</p><p>Instead: <strong>make the gateway its own protected API.</strong></p><p>Register the gateway in your identity provider as an application that exposes a scope. Now there is a legitimate thing for the client to authenticate against, and the token it gets is genuinely and honestly a token for the gateway. No metadata rewriting required — the PRM tells the truth.</p><p>Then, on the outbound leg, the gateway exchanges that token for a <em>different</em> token, one minted for the upstream MCP server, carrying the same user. In OAuth that mechanism is <a href="https://datatracker.ietf.org/doc/html/rfc8693">RFC 8693 token exchange</a>; in Microsoft’s dialect it’s the <a href="https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-on-behalf-of-flow">On-Behalf-Of flow</a>, which uses the jwt-bearer grant with requested_token_use=on_behalf_of. (Worth knowing: Entra exposes no RFC 8693 token-exchange grant at all — send one and you get an unsupported-grant-type error, AADSTS70003 in our testing. Use Microsoft&#39;s own OBO shape.)</p><p>The one-line precondition, which also explains why this didn’t work for GitHub: <strong>the upstream has to already trust your issuer.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*53hNBOtDrxt61QHUZX84hQ.png" /><figcaption><em>[Diagram: who holds which token, and what audience it carries]</em></figcaption></figure><p>The flow</p><p>Two tokens. Call them <strong>A</strong> and <strong>B</strong>.</p><ul><li><strong>Token A</strong> — issued to the client, audience = <strong>the gateway</strong>, subject = the employee.</li><li><strong>Token B</strong> — obtained by the gateway via OBO, audience = <strong>the upstream MCP server</strong>, same employee.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AOsAhJTPqgpiyJhEoVjdww.png" /><figcaption><em>[Diagram: discovery, token A, the exchange, token B]</em></figcaption></figure><p>The OBO call itself is unremarkable:</p><pre>POST /{tenant}/oauth2/v2.0/token<br>Content-Type: application/x-www-form-urlencoded</pre><pre>grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer<br>&amp;client_id=&lt;the gateway app&gt;<br>&amp;client_secret=&lt;the gateway app secret&gt;<br>&amp;assertion=&lt;token A&gt;<br>&amp;requested_token_use=on_behalf_of<br>&amp;scope=&lt;upstream scope&gt;</pre><p>The identity provider will only do this if assertion.aud is the gateway&#39;s own application — Microsoft&#39;s documentation puts it plainly: an application &quot;can&#39;t redeem a token for a different app&quot;. Which is precisely why token A has to be <em>for the gateway</em>. That single requirement is what makes the whole design honest.</p><p>The client config gets simpler too. There’s no header helper any more, because the token the client already puts in Authorization <strong>is</strong> the gateway token:</p><pre>{<br>  &quot;vendor-mcp&quot;: {<br>    &quot;type&quot;: &quot;http&quot;,<br>    &quot;url&quot;: &quot;https://gateway.example.com/vendor-mcp/mcp&quot;,<br>    &quot;oauth&quot;: { &quot;clientId&quot;: &quot;&lt;public client app id&gt;&quot;, &quot;callbackPort&quot;: 8080 }<br>  }<br>}</pre><p>And because token A is a real token from your own identity provider, tool-level authorization can finally key on its claims. In our demo one tool required a specific directory role; a user who had it called the tool, a user who didn’t got a clean permission error from the gateway — and the upstream never saw either request.</p><h3>The details that actually cost us days</h3><p>The design is a page. The debugging was a week. Four of these are about the design and will apply on any gateway; three are Entra-specific and you can skip them if your IdP is something else.</p><h4>Design</h4><p><strong>1. Four strings must be identical, character for character.</strong> The published MCP URL, the PRM resource, the identifier URI you register on the gateway app, and the URL in the client config. Change the host, port or context path and all four change together.</p><p><strong>2. Cache token B, and get the TTL right.</strong> <em>(current item 5, unchanged)</em></p><p><strong>3. Map errors to the right status code.</strong> <em>(current item 6, unchanged)</em></p><p><strong>4. Remove token A before forwarding.</strong> Obvious. Still worth a test that asserts it.</p><p>Entra-specific</p><p><strong>5. A port in the identifier URI is fine.</strong> We assumed Entra would reject https://host:8443/context/mcp as an Application ID URI. It didn&#39;t. Test the assumption before you redesign around it.</p><p><strong>6. Check which token version your IdP is minting.</strong> <em>(current item 3, unchanged)</em></p><p><strong>7. Register the client’s redirect URI as a desktop/mobile platform, not web.</strong> <em>(current item 4, unchanged)</em></p><p>One more thing bit us here that has nothing to do with OBO and everything to do with how gateway policies are ordered — it’s in Part 8, where it belongs.</p><h3>What you get</h3><ul><li><strong>A real subject on every request</strong>, from a token you actually validated. Routing, quotas, cost attribution, tool-level authorization and analytics all key off it.</li><li><strong>The upstream sees the real employee</strong> and applies their own permissions.</li><li><strong>The client never sees an upstream token</strong>, and the upstream never sees yours.</li><li><strong>No credential vault, no refresh handling</strong> — the identity provider does that work.</li><li>Nothing in the design contradicts the MCP specification, which means it survives security review instead of needing a defence.</li></ul><h3>What this doesn’t solve</h3><p><strong>It only works inside one trust domain.</strong> Token exchange requires an authorization server that can mint a token the resource server will accept. Your IdP cannot mint a GitHub token, a Google token, or an Atlassian token — no matter how the request is shaped. If the upstream MCP server is third-party SaaS, OBO is simply not available and you need a different pattern.</p><p>Also worth noting: the gateway becomes a <strong>confidential client</strong> (so you own a secret or certificate and its rotation), and each upstream scope needs delegated permission with admin consent — keep those permissions minimal, because the exchanged token carries every scope the gateway app has consent for. Treat OBO as a single-tenant mechanism unless your identity provider’s documentation tells you otherwise.</p><h3>What’s next</h3><p>We now have two working patterns with a clean split between them: OBO when the upstream shares your trust domain, passthrough when it doesn’t and the client can carry a header.</p><p>That’s still not a full answer for a real estate of MCP servers. <strong>Part 6</strong> lays out all four outbound-credential patterns side by side — including the per-user token vault that third-party SaaS actually requires — and how to pick between them.</p><h3>References</h3><ul><li><a href="https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-on-behalf-of-flow">Microsoft identity platform — OAuth 2.0 On-Behalf-Of flow</a></li><li><a href="https://datatracker.ietf.org/doc/html/rfc8693">RFC 8693 — OAuth 2.0 Token Exchange</a></li><li><a href="https://datatracker.ietf.org/doc/html/rfc9728">RFC 9728 — OAuth 2.0 Protected Resource Metadata</a></li><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization">MCP — Authorization specification</a></li><li><a href="https://agentgateway.dev/docs/kubernetes/latest/documentation/mcp/auth/entra/">agentgateway — Set up Microsoft Entra ID</a></li><li><a href="https://developer.konghq.com/cookbooks/secure-internal-mcp-gateway/">Kong — Secure Internal MCP Gateway cookbook (gateway as resource server)</a></li><li><a href="https://learn.microsoft.com/en-us/troubleshoot/entra/entra-id/app-integration/confidential-client-application-authentication-error-aadsts7000218">Microsoft — AADSTS7000218 troubleshooting (Web vs. desktop redirect URIs)</a></li><li><a href="https://learn.microsoft.com/en-us/entra/identity-platform/reference-error-codes">Microsoft — Entra authentication and authorization error codes</a></li></ul><p>If you’ve done this against a different identity provider and the token-exchange shape was different, I’d like to compare notes. And if it was useful, pass it on to whoever on your team is about to put a gateway in front of MCP.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4374d6a6d725" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why the Passthrough Trick Only Works Against GitHub]]></title>
            <link>https://medium.com/@sgmjaiya94/why-the-passthrough-trick-only-works-against-github-1d9d5e966f60?source=rss-6de6771ac7d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d9d5e966f60</guid>
            <category><![CDATA[ai-gateway]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[mcps]]></category>
            <category><![CDATA[mcp-server]]></category>
            <category><![CDATA[wso2]]></category>
            <dc:creator><![CDATA[Hasitha Jayanath Pathirana]]></dc:creator>
            <pubDate>Wed, 16 Sep 2026 10:24:58 GMT</pubDate>
            <atom:updated>2026-09-18T13:02:51.933Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>MCP Behind the Gateway — Part 4</strong></p><p><em>Series index: </em><a href="https://medium.com/@sgmjaiya94/why-you-need-a-gateway-in-front-of-your-mcp-servers-9664433062df?sharedUserId=sgmjaiya94"><em>Part 1</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/the-one-line-in-the-mcp-spec-that-breaks-your-gateway-f0a78dbccaed?sharedUserId=sgmjaiya94"><em>Part 2</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/authenticated-passthrough-let-the-gateway-serve-the-metadata-2b0f4f5c9047?sharedUserId=sgmjaiya94"><em>Part 3</em></a><em> · Part 4 (this post) · </em><a href="https://medium.com/@sgmjaiya94/the-gateway-as-a-real-resource-server-with-on-behalf-of-4374d6a6d725?sharedUserId=sgmjaiya94"><em>Part 5</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/four-ways-to-get-a-token-to-the-backend-mcp-server-447922c20145?sharedUserId=sgmjaiya94"><em>Part 6 </em></a><em>· </em><a href="https://medium.com/@sgmjaiya94/building-it-on-wso2-ai-gateway-17e46927a1b5?sharedUserId=sgmjaiya94"><em>Part 7</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/writing-a-custom-policy-for-wso2-ai-gateway-2c4d2f52257a?sharedUserId=sgmjaiya94"><em>Part 8</em></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LXFl8LRLY5PDLMjSvKHopg.png" /></figure><p>At the end of <a href="https://medium.com/@sgmjaiya94/authenticated-passthrough-let-the-gateway-serve-the-metadata-2b0f4f5c9047?sharedUserId=sgmjaiya94">Part 3</a> we had authenticated passthrough working end to end. The gateway served the rewritten Protected Resource Metadata, the client OAuthed directly with GitHub, the GitHub token was forwarded untouched, and the gateway knew exactly which employee was calling. Green across the board.</p><p>So we pointed the same policy at the next MCP server on the list — a Microsoft-hosted one, protected by <strong>Microsoft Entra ID</strong>. Same gateway, same policy, same client, new upstream.</p><p>It failed before the browser even finished loading:</p><pre>AADSTS9010010: The resource parameter provided in the request<br>doesn&#39;t match with the requested scopes</pre><p>(On some tenants and endpoints we saw AADSTS901002: The &#39;resource&#39; request parameter is not supported instead — same root cause.)</p><p>Nothing we had built was involved in that error. It came straight from Entra, at the /authorize call, before a single byte reached the gateway.</p><p>This article is about why — and it is, I think, the most useful thing we learned in the whole PoC.</p><h3>The parameter in the middle of it</h3><p>Recall from Part 2 that the MCP spec requires the client to send the <a href="https://datatracker.ietf.org/doc/html/rfc8707">RFC 8707</a> resource parameter on both the authorization and token requests, set to the canonical URI of the MCP server it intends to use the token with.</p><p>With the passthrough design, that value is <strong>the gateway’s URL</strong> — that’s the whole point, it’s what we rewrote into the metadata so the client’s RFC 9728 check would pass.</p><p>So the client dutifully sends:</p><pre>GET /oauth2/v2.0/authorize<br>  ?client_id=...<br>  &amp;scope=&lt;upstream scopes&gt;<br>  &amp;resource=https://gateway.example.com/vendor-mcp/mcp     ← our gateway<br>  &amp;code_challenge=...&amp;code_challenge_method=S256</pre><p>Now: what does an authorization server do with a resource parameter naming a host it has never heard of?</p><h3>Two behaviours, neither of them a bug</h3><p>RFC 8707 §2 says that a server which does not consider the requested resource acceptable “should” reject the request with invalid_target — lower-case <em>should</em>, not a normative MUST, and in any case a sentence that only applies to a server that implements RFC 8707 in the first place. That leaves a fork with two perfectly defensible sides:</p><ul><li><strong>GitHub ignores it.</strong> GitHub’s authorization server has no RFC 8707 support at all, so an unknown query parameter is simply dropped on the floor. The flow continues, a token is issued, everything works.</li><li><strong>Entra validates it.</strong> Entra compares resource against the <strong>Application ID URI</strong> of the API that owns the requested scopes. Those scopes belong to a Microsoft application; its identifier URI is Microsoft&#39;s. A URL pointing at your gateway can never match it. So Entra rejects — correctly.</li></ul><p>Neither server is misbehaving. One implements a spec you were relying on; the other never implemented it, which is exactly why your request survived.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I0J2iwDIh-XjiRefEzS2tw.png" /><figcaption><em>[Diagram: the identical authorize request, and why one succeeds and one does not]</em></figcaption></figure><p>Here’s the uncomfortable conclusion, and it’s worth sitting with for a second:</p><blockquote><em>Our passthrough design did not work because it was sound. It worked because the first authorization server we happened to test against was lenient.</em></blockquote><p>Generalised: <strong>any design that rewrites </strong><strong>resource to point at something other than the resource the token is really for is betting that nobody checks.</strong> Some authorization servers check. That is not a bug in them.</p><h3>This isn’t just our implementation</h3><p>I want to be fair here, because it would be easy to read this as “we built it wrong”.</p><p>Kong’s <a href="https://developer.konghq.com/cookbooks/secure-external-mcp-gateway/">Secure External MCP Gateway</a> cookbook has the same limitation. Their recipe uses DataKit to return the 401 with a Kong-hosted PRM, and Response Transformer to rewrite only resource; authorization_servers still points at GitHub, the client OAuths with GitHub directly, and Kong forwards the GitHub token unchanged, identifying the consumer by a separate apikey header. It is, step for step, what we built. Point it at an Entra-backed MCP server and it fails identically.</p><p>Notice, too, that Kong’s <em>second</em> recipe for MCP — <a href="https://developer.konghq.com/cookbooks/secure-internal-mcp-gateway/">Secure Internal MCP Gateway</a> — doesn’t rewrite anything at all. There, the client authenticates directly against the organisation’s own IdP and Kong validates that token as a proper resource server, mapping claims to consumer groups for tool-level access control. Two recipes, because one pattern was never going to cover both cases.</p><h3>How Entra-aware gateways deal with it</h3><p>Everyone who has hit this has landed on the same workaround: <strong>stop letting the client talk to the authorization server directly.</strong> Put the gateway in front of the OAuth endpoints and quietly drop the resource parameter on the way through.</p><p>Solo.io’s <a href="https://agentgateway.dev/docs/kubernetes/latest/documentation/mcp/auth/entra/">agentgateway</a> ships a native Entra provider that does exactly this — it serves the RFC 8414 authorization server metadata itself (derived from Entra’s OIDC discovery document), strips the resource parameter Entra rejects, uses a pre-registered client id in place of dynamic client registration, and accepts both audience formats Entra can mint (api://&lt;client-id&gt; and the bare GUID). Community MCP-to-Entra proxies do the same, usually adding one more patch: injecting code_challenge_methods_supported into the metadata, because Entra&#39;s v2 discovery document omits it and the MCP spec says a client <strong>MUST</strong> refuse to proceed when it&#39;s absent. (In our testing Claude Code proceeded anyway. Don&#39;t design around one client&#39;s leniency.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JEL5Evy6lDCBZpjoOFPmKg.png" /><figcaption><em>[Diagram: what an “Entra-compatible” MCP proxy actually does]</em></figcaption></figure><p>Nobody makes Entra accept a gateway URL. They intercept the call and remove the question.</p><h3>Why I’d call that a band-aid</h3><p>It works, and if you need something on Monday it’s a legitimate choice. But be clear about what you’ve bought:</p><ul><li>You are <strong>still doing passthrough</strong>. The token still has the upstream’s audience, the gateway still never validates it, and the spec still says MUST NOT.</li><li>Your gateway is now a <strong>full OAuth proxy</strong>. The MCP spec’s confused-deputy section is specifically about proxies with a static client id at a third-party authorization server that also accept dynamic client registration — if that’s your shape, per-client consent before forwarding becomes a MUST, not a nice-to-have.</li><li>You have taken on <strong>permanent compatibility debt</strong> to a vendor’s OAuth quirks. Every one of those patches is a thing that breaks when Microsoft changes something.</li></ul><h3>The question that unlocks the real answer</h3><p>Here is what we should have asked earlier: <strong>why are we trying to get the client a token for the upstream at all?</strong></p><p>The reason it feels necessary for GitHub is that GitHub and your employees live in different trust domains — your identity provider cannot mint a token GitHub will accept, so somebody has to go and get a GitHub one.</p><p>But an Entra-backed MCP server is different. Your users are <em>already in that tenant</em>. The identity provider that authenticates your employee is the same one that protects the upstream MCP server. When both ends share a trust domain, there is a mechanism designed for exactly this, and it has been in OAuth for years: <strong>token exchange</strong>, or in Microsoft’s dialect, <strong>On-Behalf-Of</strong>.</p><p>That is Part 5, and it’s the design we ended up shipping.</p><h3>What this article does not solve</h3><ul><li>It doesn’t help with third-party SaaS MCP servers where OBO is genuinely impossible. That’s Part 6.</li><li>It doesn’t tell you which pattern to standardise on across a mixed estate — also Part 6.</li></ul><h3>References</h3><ul><li><a href="https://datatracker.ietf.org/doc/html/rfc8707">RFC 8707 — Resource Indicators for OAuth 2.0</a></li><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization">MCP — Authorization specification</a></li><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/security-considerations">MCP — Authorization security considerations (confused deputy, PKCE metadata requirement)</a></li><li><a href="https://developer.konghq.com/cookbooks/secure-external-mcp-gateway/">Kong — Secure External MCP Gateway cookbook</a></li><li><a href="https://developer.konghq.com/cookbooks/secure-internal-mcp-gateway/">Kong — Secure Internal MCP Gateway cookbook</a></li><li><a href="https://agentgateway.dev/docs/kubernetes/latest/documentation/mcp/auth/entra/">agentgateway — Set up Microsoft Entra ID</a></li><li><a href="https://www.solo.io/blog/enterprise-mcp-sso-with-microsoft-entra-and-agentgateway">Solo.io — Enterprise MCP SSO with Microsoft Entra and agentgateway</a></li><li><a href="https://learn.microsoft.com/en-us/entra/identity-platform/reference-error-codes">Microsoft — Entra authentication and authorization error codes</a> (AADSTS9010010 isn’t listed there; the live lookup at https://login.microsoftonline.com/error?code=9010010 is the quickest source)</li></ul><p>If your authorization server behaves differently from either of these two, I’d like to know — the whole point of this post is that the behaviour varies. And if it was useful, pass it on to whoever on your team is about to put a gateway in front of MCP.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d9d5e966f60" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Authenticated Passthrough: Let the Gateway Serve the Metadata]]></title>
            <link>https://medium.com/@sgmjaiya94/authenticated-passthrough-let-the-gateway-serve-the-metadata-2b0f4f5c9047?source=rss-6de6771ac7d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b0f4f5c9047</guid>
            <category><![CDATA[mcp-server]]></category>
            <category><![CDATA[mcps]]></category>
            <category><![CDATA[gateway]]></category>
            <category><![CDATA[wso2]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Hasitha Jayanath Pathirana]]></dc:creator>
            <pubDate>Wed, 16 Sep 2026 10:20:46 GMT</pubDate>
            <atom:updated>2026-09-18T13:02:44.099Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>MCP Behind the Gateway — Part 3</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FtjPmm4Y00ncgHsZyLu7aQ.png" /></figure><p><em>Series index: </em><a href="https://medium.com/@sgmjaiya94/why-you-need-a-gateway-in-front-of-your-mcp-servers-9664433062df?sharedUserId=sgmjaiya94"><em>Part 1</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/the-one-line-in-the-mcp-spec-that-breaks-your-gateway-f0a78dbccaed?sharedUserId=sgmjaiya94"><em>Part 2</em></a><em> · Part 3 (this post) · </em><a href="https://medium.com/@sgmjaiya94/why-the-passthrough-trick-only-works-against-github-1d9d5e966f60?sharedUserId=sgmjaiya94"><em>Part 4 </em></a><em>· </em><a href="https://medium.com/@sgmjaiya94/the-gateway-as-a-real-resource-server-with-on-behalf-of-4374d6a6d725?sharedUserId=sgmjaiya94"><em>Part 5</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/four-ways-to-get-a-token-to-the-backend-mcp-server-447922c20145?sharedUserId=sgmjaiya94"><em>Part 6 </em></a><em>· </em><a href="https://medium.com/@sgmjaiya94/building-it-on-wso2-ai-gateway-17e46927a1b5?sharedUserId=sgmjaiya94"><em>Part 7</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/writing-a-custom-policy-for-wso2-ai-gateway-2c4d2f52257a?sharedUserId=sgmjaiya94"><em>Part 8</em></a><em>.</em></p><p>In <a href="https://medium.com/@sgmjaiya94/the-one-line-in-the-mcp-spec-that-breaks-your-gateway-f0a78dbccaed?sharedUserId=sgmjaiya94">Part 2</a> we found the wall: the MCP client checks that the resource in the Protected Resource Metadata equals the URL it connected to, and a relayed 401 always fails that check.</p><p>There are two families of fix. This article is the first one, and it is the one most people reach for because it needs <strong>no credential storage at the gateway at all</strong>. Kong documents it in their <a href="https://developer.konghq.com/cookbooks/secure-external-mcp-gateway/">Secure External MCP Gateway</a> cookbook; we implemented the same shape on WSO2 AI Gateway and got it working end to end against the GitHub MCP server.</p><p>I’ll call it <strong>authenticated passthrough</strong>.</p><h3>The idea in one paragraph</h3><p>Let the client keep doing OAuth with the upstream’s own authorization server, and keep using the upstream’s own token. The gateway simply <strong>stops lying by omission</strong>: instead of relaying the upstream’s metadata, it serves that metadata itself with the resource field rewritten to the gateway&#39;s URL. The RFC 9728 check then passes, because the document the client fetched now describes the endpoint the client actually connected to.</p><p>And because the Authorization header is fully occupied by the upstream&#39;s token, the gateway&#39;s <em>own</em> credential travels in a <strong>separate header</strong>.</p><p>Two credentials, one request — which is exactly the two-leg model from Part 2, just with the client carrying the outbound credential instead of the gateway obtaining it. The gateway’s token never goes upstream; the upstream’s token was never meant for the gateway.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ut9GmUtQYEApkRcuwmDlw.png" /><figcaption><em>[Diagram: one request, two headers, two different audiences]</em></figcaption></figure><p>The four moves</p><p>There are only four things the gateway has to do. Every one of them is a header or body rewrite — no outbound calls from policy, no state, no vault.</p><p><strong>1. Authenticate the caller on a dedicated header.</strong><br> The gateway validates its own JWT (issuer, signature, audience, expiry) from something like AuthorizationGW. This token is minted by <em>your</em> identity provider for <em>the employee</em> — it carries their subject, groups and roles — and it is what gives you identity, and therefore routing, quotas, tool-level authorization, cost attribution and analytics. The upstream never sees it.</p><p>The header name is load-bearing, not cosmetic. In Claude Code, if you configure an Authorization header — statically or via a helper — the client treats that as <em>the</em> credential for the server and never falls back to OAuth. So the gateway credential has to live somewhere else, or the client will never start the upstream OAuth flow at all.</p><p>Getting that user token onto the request is a solved problem, and it does not need anything MCP-specific. You build a small login tool — a few hundred lines — that runs the browser-based OAuth flow against your IdP once and stores the resulting tokens in the OS keychain. Claude Code’s headersHelper — a small script the client runs fresh on <strong>every connection</strong>, and re-runs once if a call comes back 401 or 403 — then reads the current access token from the keychain, refreshing it when it has expired, and emits it as AuthorizationGW. The access token stays short-lived, nothing long-lived is ever written into a config file, and revoking a person at the IdP takes effect on their next connection. It is the same pattern the cloud CLIs use for their own tooling, applied to the gateway; we used a small CLI built for Asgardeo in the PoC (link in the references), and the same shape works against any OAuth 2.0 provider.</p><p><strong>2. Challenge with a pointer to your own metadata.</strong> When there’s no upstream token, answer 401 yourself:</p><pre>HTTP/1.1 401 Unauthorized<br>WWW-Authenticate: Bearer resource_metadata=&quot;https://gateway.example.com/github-mcp/.well-known/oauth-protected-resource&quot;</pre><p><strong>3. Serve the metadata with </strong><strong>resource rewritten.</strong> Fetch the upstream&#39;s PRM, cache it, change one field, serve the rest byte-for-byte. For GitHub that comes out as:</p><pre>{<br>  &quot;resource&quot;: &quot;https://gateway.example.com/github-mcp/mcp&quot;,<br>  &quot;authorization_servers&quot;: [&quot;https://github.com/login/oauth&quot;],<br>  &quot;scopes_supported&quot;: [&quot;repo&quot;, &quot;read:org&quot;, &quot;...&quot;],<br>  &quot;bearer_methods_supported&quot;: [&quot;header&quot;],<br>  &quot;resource_name&quot;: &quot;GitHub MCP Server&quot;<br>}</pre><p>authorization_servers still points at the upstream. The client will OAuth directly with them, exactly as it would have without a gateway. (Two GitHub-specific details that cost us time: the authorization server is https://github.com/login/oauth, not https://github.com, and GitHub has no dynamic client registration — so every client needs a pre-registered GitHub App client id, and in our testing a client secret too.)</p><p><strong>4. Strip your own header, forward theirs.</strong> Before proxying upstream, remove AuthorizationGW — otherwise you are shipping your internal JWTs to a third party on every call. Easy to forget, embarrassing to discover.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dVSzg5d_3I889pPZBQybig.png" /><figcaption><em>[Diagram: discovery through the gateway, OAuth direct with the upstream]</em></figcaption></figure><p>The fifth move everyone forgets</p><p>Rewrite resource_metadata on <strong>upstream 401s</strong> as well, not just on your own challenge.</p><p>When the user’s upstream token expires or is revoked mid-session, the upstream answers 401 with <em>its</em> resource_metadata pointer. Relay that unchanged and you reproduce the exact Part 2 error, weeks later, on a Friday afternoon, to a user who has no idea what changed. Rewrite the pointer and the client re-authorizes cleanly.</p><p>Two implementation notes that cost us time:</p><ul><li>Do this <strong>headers-only</strong>. The MCP route streams (text/event-stream), so a policy that buffers the response body will break streaming. Declare no body processing on that route.</li><li>Give your <em>own</em> auth failures a <strong>distinct challenge</strong> — a realm of your own and no resource_metadata pointer at all. Otherwise a gateway-credential problem sends the user off to authenticate with GitHub, which will succeed and change nothing, twice, before anyone thinks to look at the gateway.</li></ul><h3>Why this is attractive</h3><ul><li><strong>Completely stateless.</strong> No token vault, no refresh handling, no gateway-owned OAuth app, no cross-request state. Every step is a header check or a rewrite.</li><li><strong>The upstream’s permission model stays intact.</strong> The user’s own token means the user’s own repository permissions — nothing is escalated or pooled.</li><li><strong>The gateway finally has a real caller identity</strong> — the employee, from a token your own IdP issued — which was the entire point of Part 1.</li><li><strong>It is quick.</strong> We had this running end to end against the GitHub MCP server, through Claude Code, with a forked auth policy.</li></ul><h3>What this doesn’t solve — read this part twice</h3><p>I want to be straight about the limits, because this pattern is easy to over-sell.</p><p><strong>1. It is a custom-header client only.</strong> Desktop-style connectors are OAuth-only and have no way to send an extra header. If your goal is “everyone in the company uses this”, a sizeable chunk of your users cannot. You will need the Part 5 design for them.</p><p><strong>2. The gateway still is not the resource server for the upstream token.</strong> It advertises resource = the gateway, then forwards a token whose real audience is the upstream and which it never validated — and with GitHub it <em>couldn&#39;t</em> validate it even if it wanted to, because GitHub&#39;s tokens are opaque strings with no readable audience or expiry. That is, nominally, the very token-passthrough anti-pattern from Part 2. It is <em>defensible</em> — identity and governance come from the gateway token, and the gateway brokers nothing — but a security reviewer will raise it, so have the answer ready and do not call it spec-compliant.</p><p><strong>3. Do not make the gateway token long-lived.</strong> A headersHelper runs on every connection and re-runs on a 401, so a short-lived token costs you nothing. Long-lived bearer tokens on developer laptops are the thing the gateway exists to eliminate.</p><p><strong>4. It depends on how the upstream’s authorization server behaves.</strong> This is the big one and it deserves its own article.</p><h3>What’s next</h3><p>We shipped this, it worked, and we moved on to the second MCP server on the list — a Microsoft-backed one. Same gateway, same policy, same client.</p><p>It failed at the /authorize call, with an error that had nothing to do with anything we&#39;d built.</p><p><strong>Part 4</strong> is about why, and why the answer means this whole pattern is far less general than it appears — including in Kong’s cookbook.</p><h3>References</h3><ul><li><a href="https://developer.konghq.com/cookbooks/secure-external-mcp-gateway/">Kong — Secure External MCP Gateway cookbook</a></li><li><a href="https://datatracker.ietf.org/doc/html/rfc9728">RFC 9728 — OAuth 2.0 Protected Resource Metadata</a></li><li><a href="https://datatracker.ietf.org/doc/html/rfc8414">RFC 8414 — OAuth 2.0 Authorization Server Metadata</a></li><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization">MCP — Authorization specification</a></li><li><a href="https://modelcontextprotocol.io/docs/2026-07-28/tutorials/security/security_best_practices">MCP — Security best practices</a></li><li><a href="https://code.claude.com/docs/en/mcp">Claude Code — MCP configuration (headersHelper, OAuth client settings)</a></li><li><a href="https://github.com/SavinduDimal/asgardeo-login-cli">Asgardeo CLI login tool </a>— example of keychain-backed token retrieval for headersHelper</li></ul><p>If you build this and it behaves differently against your upstream, I’d like to hear about it — Part 4 is about exactly that kind of surprise. And if it was useful, pass it on to whoever on your team is about to put a gateway in front of MCP.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b0f4f5c9047" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The One Line in the MCP Spec That Breaks Your Gateway]]></title>
            <link>https://medium.com/@sgmjaiya94/the-one-line-in-the-mcp-spec-that-breaks-your-gateway-f0a78dbccaed?source=rss-6de6771ac7d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0a78dbccaed</guid>
            <category><![CDATA[wso2]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[mcps]]></category>
            <category><![CDATA[mcp-server]]></category>
            <category><![CDATA[gateway]]></category>
            <dc:creator><![CDATA[Hasitha Jayanath Pathirana]]></dc:creator>
            <pubDate>Tue, 15 Sep 2026 02:18:18 GMT</pubDate>
            <atom:updated>2026-09-18T13:02:34.254Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>MCP Behind the Gateway — Part 2</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6GUOZDxSftk1UtnDkiQTYQ.png" /></figure><p><em>Series index: </em><a href="https://medium.com/@sgmjaiya94/why-you-need-a-gateway-in-front-of-your-mcp-servers-9664433062df?sharedUserId=sgmjaiya94"><em>Part 1</em></a><em> · Part 2 (this post) · </em><a href="https://medium.com/@sgmjaiya94/authenticated-passthrough-let-the-gateway-serve-the-metadata-2b0f4f5c9047?sharedUserId=sgmjaiya94"><em>Part 3</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/why-the-passthrough-trick-only-works-against-github-1d9d5e966f60?sharedUserId=sgmjaiya94"><em>Part 4</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/the-gateway-as-a-real-resource-server-with-on-behalf-of-4374d6a6d725?sharedUserId=sgmjaiya94"><em>Part 5 </em></a><em>· </em><a href="https://medium.com/@sgmjaiya94/four-ways-to-get-a-token-to-the-backend-mcp-server-447922c20145?sharedUserId=sgmjaiya94"><em>Part 6</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/building-it-on-wso2-ai-gateway-17e46927a1b5?sharedUserId=sgmjaiya94"><em>Part 7</em></a><em> · </em><a href="https://medium.com/@sgmjaiya94/writing-a-custom-policy-for-wso2-ai-gateway-2c4d2f52257a?sharedUserId=sgmjaiya94"><em>Part 8</em></a><em>.</em></p><p>In <a href="https://medium.com/@sgmjaiya94/why-you-need-a-gateway-in-front-of-your-mcp-servers-9664433062df?sharedUserId=sgmjaiya94">Part 1</a> we put a gateway in front of our MCP servers and everything was fine, right up until we proxied a server that actually requires authentication. Then the client refused to connect:</p><pre>Protected resource (https://api.githubcopilot.com/mcp) does not match<br>expected (https://gateway.example.com/github-mcp/mcp)</pre><p>In this article I’ll take that message apart. It is the single most important thing to understand before you design an MCP gateway, because almost every design decision in the rest of this series is downstream of it.</p><p>Short version: <strong>the client is right, and your proxy is the thing that’s wrong.</strong></p><h3>How an MCP client finds its authorization server</h3><p>MCP does not ask you to configure an authorization server in the client. The client discovers it, and the discovery is specified precisely:</p><ol><li>The client POSTs to the MCP endpoint with no token.</li><li>The server answers <strong>401</strong> with a WWW-Authenticate header carrying a resource_metadata pointer. (If the header is missing, the client falls back to the RFC 9728 default locations we saw in Part 1 — under /.well-known/ on the server&#39;s origin.)</li><li>The client fetches that pointer — an <a href="https://datatracker.ietf.org/doc/html/rfc9728">RFC 9728</a> <strong>Protected Resource Metadata (PRM)</strong> document.</li><li>The PRM says two things: resource (the canonical URI of <em>this</em> MCP server) and authorization_servers (who may issue tokens for it).</li><li>The client then does normal OAuth against that authorization server, sending the <a href="https://datatracker.ietf.org/doc/html/rfc8707">RFC 8707</a> resource parameter so the token comes back <strong>bound to that specific resource</strong>.</li></ol><p>Step 4 is where a proxy dies. The MCP spec says the client <strong>MUST</strong> use PRM for authorization server discovery, and RFC 9728 §3.3 is blunt about what the client does with a document whose resource doesn&#39;t line up:</p><blockquote><em>The </em><em>resource value returned </em><strong><em>MUST</em></strong><em> be identical to the protected resource&#39;s resource identifier value into which the well-known URI path suffix was inserted to create the URL used to retrieve the metadata. If these values are not identical, the data contained in the response </em><strong><em>MUST NOT</em></strong><em> be used.</em></blockquote><p>The same rule applies when the document’s URL came from the 401 pointer rather than the well-known location: the client compares the PRM&#39;s resource against the MCP endpoint it actually connected to, and if they differ, the document is discarded. So the client does the obvious sanity check: <em>is the </em><em>resource in this document the same URL I just connected to?</em></p><p>When you relay the upstream’s 401, the answer is no. The client connected to your gateway and was handed metadata describing the vendor’s server. It stops.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LhYNErk6XCy2FVLvDbUo1g.png" /><figcaption><em>[Diagram: relay the upstream 401 and discovery fails before OAuth even begins]</em></figcaption></figure><p>This is not client pedantry</p><p>It is tempting to file this under “strict client, find the bypass flag”. In the clients we tested there isn’t one, and asking for one is not a popular request — but more importantly, you shouldn’t want one.</p><p>The check exists because OAuth tokens in MCP are <strong>audience-bound on purpose</strong>. The authorization specification is unusually direct about it:</p><blockquote><em>MCP servers </em><strong><em>MUST</em></strong><em> validate that access tokens were issued specifically for them as the intended audience, according to RFC 8707 Section 2.</em></blockquote><blockquote><em>MCP clients </em><strong><em>MUST NOT</em></strong><em> send tokens to the MCP server other than ones issued by the MCP server’s authorization server.</em></blockquote><p>And the sentence that is aimed squarely at gateways, from the <a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/security-considerations">authorization security considerations</a>:</p><blockquote><em>If the MCP server makes requests to upstream APIs, it may act as an OAuth client to them. The access token used at the upstream API is a separate token, issued by the upstream authorization server. The MCP server </em><strong><em>MUST NOT</em></strong><em> pass through the token it received from the MCP client.</em></blockquote><p>The spec’s <a href="https://modelcontextprotocol.io/docs/2026-07-28/tutorials/security/security_best_practices">security best practices</a> call forwarding a client’s token onwards <strong>token passthrough</strong> and list it as an anti-pattern in its own right — one that, in the worst case, degenerates into the <strong>confused deputy</strong> problem, which the same document treats as a separate attack with its own mitigations. The documented risks of passthrough are worth repeating, because they’re all operational, not theoretical: rate limiting and monitoring keyed on audience get bypassed, the downstream audit trail shows the wrong identity, and a stolen token turns your gateway into a data-exfiltration proxy.</p><p>That check the client just failed? It is the mechanism that enforces all of that.</p><h3>The mental model: your gateway is two things at once</h3><p>Here is the model that makes everything afterwards make sense, and the one I wish I’d written on the whiteboard on day one.</p><p>A gateway proxying an MCP server is:</p><ul><li>a <strong>Resource Server</strong> to the MCP client — it must validate the <em>inbound</em> token, the one issued for the gateway, and</li><li><strong>responsible for the outbound leg</strong> to the backend MCP server — it must make sure the credential that goes upstream was issued for the <em>upstream</em>. Sometimes the gateway obtains that credential itself, acting as an OAuth client; sometimes, as we’ll see in Part 3, the client carries it. Either way it is a different credential.</li></ul><p>Those are <strong>two different credentials, always</strong>. The token issued to the gateway never goes upstream; the token that goes upstream was never meant for the gateway.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H3PiC9A2VR14sxfdIKnmVw.png" /><figcaption><em>[Diagram: inbound leg and outbound leg — the gateway wears both hats]</em></figcaption></figure><p>Once you accept this, the design question stops being “how do I make the client stop complaining” and becomes the much better question: <strong>which credential goes on the outbound leg, and where does it come from?</strong> That question has four reasonable answers, and Part 6 is entirely about choosing between them.</p><p>You will also notice that this is not a WSO2 idea, or a Kong idea. Kong’s <a href="https://developer.konghq.com/plugins/ai-mcp-oauth2/">ai-mcp-oauth2</a> plugin strips the inbound token before forwarding <strong>by default</strong> — &quot;preventing token theft and confused deputy attacks&quot;, in their words. AWS gives the two legs actual product names — <a href="https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-oauth.html">Inbound Auth and Outbound Auth</a>. agentgateway&#39;s MCP auth docs describe the same split. Everyone has converged on this because the spec doesn&#39;t leave another option.</p><h3>“Fine — I’ll just turn off auth at the gateway”</h3><p>This is the first thing everyone tries, including us. It fails twice.</p><p><strong>First, it doesn’t even fix the error.</strong> The failure is in <em>discovery</em>, not in authorization. With no gateway authentication, the upstream 401 is still relayed unchanged, the client still fetches the vendor’s PRM, and the resource still doesn&#39;t match. Same error, same place.</p><p><strong>Second — and this is the fatal one — an unauthenticated gateway has no caller identity.</strong> No identity means no tool-level authorization for <em>this</em> user, and nothing meaningful in your analytics. You have built an open relay to a third-party API and put it on your network. That is strictly worse than the laptops you were trying to fix, because now the relay is shared.</p><p>There is exactly one legitimate no-inbound-auth variant: a network-isolated gateway where identity arrives some other way — mTLS, a service mesh, or a trusted header injected upstream of the gateway. If that’s not your deployment, gateway authentication is not optional.</p><h3>What’s next</h3><p>In <strong>Part 3</strong> we take the first fix: make the gateway serve the Protected Resource Metadata itself, with resource rewritten to the gateway&#39;s own URL, and let the client keep doing OAuth against the upstream&#39;s authorization server. It&#39;s the pattern Kong documents in their external MCP gateway cookbook, it needs no credential storage at all, and we had it working end to end.</p><p>Then, in Part 4, I’ll show you the day it stopped working.</p><h3>References</h3><ul><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization">Model Context Protocol — Authorization specification</a></li><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/authorization-server-discovery">MCP — Authorization server discovery</a></li><li><a href="https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/security-considerations">MCP — Authorization security considerations</a></li><li><a href="https://modelcontextprotocol.io/docs/2026-07-28/tutorials/security/security_best_practices">MCP — Security best practices (token passthrough, confused deputy)</a></li><li><a href="https://datatracker.ietf.org/doc/html/rfc9728">RFC 9728 — OAuth 2.0 Protected Resource Metadata</a> — §3.3 for the resource validation rule</li><li><a href="https://datatracker.ietf.org/doc/html/rfc8707">RFC 8707 — Resource Indicators for OAuth 2.0</a></li><li><a href="https://datatracker.ietf.org/doc/html/rfc8414">RFC 8414 — OAuth 2.0 Authorization Server Metadata</a></li><li><a href="https://developer.konghq.com/plugins/ai-mcp-oauth2/">Kong — </a><a href="https://developer.konghq.com/plugins/ai-mcp-oauth2/">ai-mcp-oauth2 plugin</a></li><li><a href="https://developer.konghq.com/cookbooks/secure-external-mcp-gateway/">Kong — Secure External MCP Gateway cookbook</a></li><li><a href="https://agentgateway.dev/docs/standalone/latest/documentation/mcp/mcp-authn/">agentgateway — MCP authentication</a></li><li><a href="https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-oauth.html">Amazon Bedrock AgentCore — Inbound Auth and Outbound Auth</a></li></ul><p>If you’ve hit this error and your gateway vendor told you it’s a client bug, send them this post. And if it was useful, pass it on to whoever on your team is about to put a gateway in front of MCP.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0a78dbccaed" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>