The token is valid. The destination is wrong.
An MCP authorization example: audience, caller binding, and a denial that actually stops execution.
A valid token issued for one service can still be the wrong credential for another. This example checks the target resource, the acting agent, and whether the action actually goes ahead.
Start with the destination
A token can be authentic and unexpired and still be wrong for the server receiving it. The MCP authorization specification requires servers to check that tokens were issued for their intended audience, and forbids token passthrough [1][2]. Scope says what access is permitted; audience says which resource it was meant for (RFC 8707, resource indicators [3]). The example accepts one exact audience identifier — a simplification of real deployments.
The contribution in this example
One decision path that:
- binds the proposed caller to an authenticated identity;
- restricts which agents a subject may use to start work;
- checks delegation against the specific resource and operation.
Four records: TrustedClaims, TrustedIdentity, ToolCall and Policy. The first two must come from trustworthy integration code, never from an agent's own arguments.
Why the caller check matters
The first version had a gap: an unknown actor could pass by supplying a one-element delegation chain with its own name, and the token subject went unused. The revised version requires the proposed actor to match the authenticated actor, checks the token subject, requires the chain's starting agent to be authorized for that subject, rejects repeated names, and checks each delegation link. These fixes cover the local decision model, not a real framework or its bypass paths.
A denial should change what executes
A DENY result means nothing if the caller can still invoke the tool. The package includes a dispatch wrapper that calls the transport only after an allow decision. Two tests use an in-memory ledger to confirm that an allowed call arrives once and a wrong-audience call leaves no trace. All 18 local tests pass. There are no network calls and no real tokens; direct transport access would bypass the wrapper.
What to test next
A controlled integration with two real protected-resource endpoints. Include successful requests, not only rejections, so a disconnected system cannot pass by doing nothing. Test changed destinations, fabricated caller context and bypass paths, and record the observed effect separately from the policy decision.
Code, README and 18 tests on GitHub