Your test client is not a client
We had built a public OAuth server in front of an MCP (Model Context Protocol) endpoint, the kind of tool API that an outside AI client connects to on a user's behalf. Authorization code flow with PKCE, so a client that holds no secret can still prove it was the one that started the request. Dynamic client registration, so a client we had never heard of could sign itself up. A JWKS document, the set of public keys anyone can fetch to verify the tokens we issue.
It had unit tests and integration tests. I had driven the whole flow by hand with curl, and end to end with a real MCP client running on my laptop. The Friday on the calendar was labelled "deploy and verify".
By Monday evening we had merged nine pull requests across two repositories, fixing five distinct problems that were stacked one behind the other. Each fix was what made the next failure visible. Not one of the five was visible from anything I had tested with.
The first request never reached the application
The first real connection from an outside AI client did not fail in the OAuth flow. It failed before it got to any of our code.
Our MCP server sits behind a managed API gateway. The gateway had a resource policy that allowed our own networks. An AI client running on someone else's servers is not our network, so the request was rejected at the edge. Fix one.
With that open, the next request got further and then failed DNS. The hostname existed in our private zone, which is what everyone inside the company resolves, and did not exist in the public zone, which is what everyone outside resolves. Fix two.
With DNS in place, the gateway began running its token authorizer, the Lambda it calls to validate an incoming token, and that function could not fetch our JWKS document because it had no route to it. Fix three.
With the authorizer working, clients began doing discovery, and discovered that several of the metadata endpoints they look for did not exist. Fix four.
Only then did we reach the application, and the application had three bugs of its own. Fix five, six and seven, except they all looked like one bug.
A layered system reports only its outermost failure. Everything underneath stays invisible until you remove that one. There is no honest way to estimate the work, because you cannot see how deep the stack goes until you are at the bottom of it.
Three bugs wearing the same costume
The symptom, once we got into the application, was an infinite consent loop. The client sends the user to the authorization endpoint, the user approves, the server redirects, the client comes back, the server asks for consent again, forever.
The first cause was a guard that auto-approved consent only for our own first-party client ID. Every other client got an interaction prompt, the library's way of saying it needs something from the user, and our UI did not know how to render it, so it looped. I fixed it, watched a clean flow, and considered the problem closed.
It came back an hour later with a different cause. When a user switched accounts mid-flow, the session still pointed at the grant created under the previous account, the server's record of what that user had approved. The grant no longer matched the session, so the server asked for consent, created a new grant, and the session kept pointing at the old one.
Then it came back a third time. That one took the longest, and it was a single character.
One character
The OAuth library implements RFC 8707, resource indicators, which let a client say which protected API it wants a token for. A grant records which scopes the user approved for which resource, as a map keyed by resource URL. When the server checks whether an interaction has been satisfied, it compares the scopes recorded on the grant against the scopes the interaction says are still missing.
That comparison is strict string equality on the resource URL.
// what the grant recorded
grant.resources = {
'https://mcp.example.com': 'meetings:read'
};
// what the interaction still required
interaction.missingResourceScopes = {
'https://mcp.example.com/': ['meetings:read']
};
The grant was written with a hardcoded constant for our resource identifier, no trailing slash. The interaction record was built from the resource parameter that the client actually sent, and that particular client appends a trailing slash. The two never matched, the interaction was never satisfied, and the user was sent around again.
Our own first-party CLI client does not send the resource parameter at all. With no resource parameter there is nothing to compare, so the check passed every time, and had passed every time for months.
The fix is three lines. Read the resource from the request rather than from a constant, so the grant is keyed with the same string the interaction is keyed with.
Trailing slashes are the boring half of this. Hardcoding our own resource identifier was a bet that every client on the internet would spell that string the way we spelled it, and clients have no reason to. The slash is legal, the two URLs name the same resource to any human reading them, and the comparison is byte equality.
What a real client does that curl does not
One AI client ran this discovery sequence, reconstructed from firewall logs.
It tried four different metadata paths in order. Two of them were 403s from the gateway, because the routes did not exist. Two of them fell through to an Express catch-all, which 302'd to the dashboard, which then 401'd. Then it fell back to standard OIDC discovery, which worked. Then it posted a dynamic client registration request, which our server rejected because the redirect URI was an HTTPS callback on the vendor's own domain and our validation only accepted loopback URIs, per the RFC 8252 guidance for apps running on a user's own machine.
The message the user saw was that the server does not support Dynamic Client Registration. The server supported it and refused this particular registration. The client had no way to tell those two apart, so it reported the more general failure, and I spent an hour looking for a missing endpoint that was there the whole time.
None of that sequence is reproducible with curl. Curl sends exactly the request you type. It does not try four paths, fall back to a fifth, append a trailing slash you did not write, or post a registration request with a redirect URI you would never have chosen. Somebody else made those decisions months ago in a codebase I cannot read, and the only way to find out was to let their program talk to mine.
403 is not 404, and "Unauthorized" is an API
Two gateway details made the failures harder to read than they needed to be.
REST API Gateway v1 returns 403 for a route that does not exist, not 404. To a client doing optional discovery, a 404 means "this server does not offer that, try the next thing" and a 403 means "you are not allowed, stop". A missing endpoint therefore reads as a hard authorization failure, and a client that would have degraded gracefully gives up instead.
The second one still amuses me. A Lambda token authorizer that returns an explicit Deny policy produces a 403 with no WWW-Authenticate header, so the client gets no hint about how it is supposed to authenticate. To get a 401 with that header, you throw an error whose message is exactly the string Unauthorized. Not "unauthorized", not "Unauthorized: token expired". That exact string, compared case-sensitively, is the entire contract between your authorizer and the gateway.
// 403, no WWW-Authenticate, client has no idea what to do next
return denyPolicy(methodArn);
// 401 with WWW-Authenticate, client knows to start the OAuth flow
throw new Error('Unauthorized');
Why staging failed where production worked
After production was healthy, staging still timed out. Same code, same configuration, roughly ten seconds and then a 504.
Staging's public DNS record pointed at an external load balancer that shared a security group with the internal one. That group allowed a specific set of sources: the VPN, a QA vendor, our monitoring. There was no rule for the public internet. So the managed gateway's SYN was dropped with no RST, the connection never completed, and the caller waited out its timeout.
Production had a single internet-facing balancer with an open ingress rule, so it never showed the problem.
A security group that blocks traffic does not send you anything back. You get a timeout instead of a refusal, so the failure looks like a slow backend, so you go and look at the backend, and the backend is fine.
Every previous external integration we had built invoked Lambda directly over IAM. No load balancer, no security group, no public DNS. This was the first feature where an AWS-managed service had to reach our application over the public internet, and the entire network path was new even though the application code was not.
The harness we built, and what it could not reach
We did try to test this properly before shipping. Local testing needed two publicly reachable services at once, the MCP server for its protected resource metadata and the main app for discovery, registration, authorization, token exchange, JWKS and the login page. The free tier of the tunnelling service we use allows one tunnel.
So we wrote a small reverse proxy that fronted both, rewriting Host, X-Forwarded-Proto and Location headers so that redirects issued by the app came back pointing at the tunnel rather than at localhost. We added an environment flag that auto-approved the consent interaction, because our SSO cookies do not survive being served from a tunnelled domain.
It worked. A real external client completed a real flow against my laptop. And it caught none of the five problems, because everything it exercised was application code, while four of the five lived in gateway routing, DNS and resource policies, with the staging failure down in a security group.
The consent loop was the one bug the harness could in principle have found. It did not, because the client I pointed at it was the same first-party client we had been testing with all along, and that client never sends the parameter that broke.
What I would test first next time
The thing I keep coming back to is the VPN. Every engineer on the team tested from inside it. Inside the VPN, the hostname resolves through the private zone to the internal load balancer, which has no source restriction, no gateway resource policy in front of it and no public DNS dependency. Every local test I ran took a network path that no production request will ever take. The tests passed for a reason that had nothing to do with the code being correct.
So the first check on anything with an external caller is now the dumbest possible one. From a machine with no VPN and no corporate DNS, resolve the hostname and open a TCP connection. If that does not work, nothing above it matters, and you find out in thirty seconds instead of on a Friday afternoon.
After that, point at least two clients you did not write at the endpoint before you call it done. One of them will spell something differently than you did.