Skip to main content

Connect an MCP client end to end

In about ten minutes you will watch a standard MCP client sign a user in and call a tool through the Agentic Auth connector proxy, then trace that call back to the person who signed in. Everything runs in one Python process: no IdP, database or network needed.

Available from authzai 0.4.0

The example lives in the public repository at examples/mcp-oauth/.

What runs​

  • An embedded control plane with verified consent and client mapping on.
  • Its built-in test IdP, standing in for your organisation's IdP.
  • The connector proxy, configured as an OAuth protected resource.
  • A small MCP tool server with one tool, search.
  • An MCP client (client_flow.py) that imports nothing from Agentic Auth. It does only what the MCP authorization spec tells any client to do.

1. Run it​

From a checkout of the public repository:

pip install -e '.[server]'
python examples/mcp-oauth/run_local.py
1. call without a token -> 401
2. protected resource metadata -> authorization server https://idp.embedded.invalid
3. authorization server metadata -> issuer https://idp.embedded.invalid
4. user signed in -> access token for https://mcp-proxy.example
5. call with the token -> 200
tool result: 3 articles found
6. audit chain root -> https://idp.embedded.invalid#alice (embedded-test, client desktop-client)

2. What each line means​

1. The client is challenged. It called the proxy with no token. The proxy answered 401 with a WWW-Authenticate header whose resource_metadata parameter says where to learn more. This is an HTTP response, not a JSON-RPC error, because MCP clients look for it there.

2. The client finds the IdP. It fetched the proxy's Protected Resource Metadata. The authorization_servers field names the IdP, so the proxy never has to be told about the client, and the client never has to be told about Agentic Auth.

3. The client reads the IdP's metadata, which is where a real client finds the authorization endpoint for the login.

4. The user signs in. A real client runs the authorization code flow with PKCE here, asking for a token whose audience is the proxy. The embedded IdP has no login page, so the example hands the client the token that login would produce. Agentic Auth plays no part in the login: your IdP is the authorization server.

5. The call succeeds. Behind this one line, the proxy:

  1. validated the token: signature, issuer, audience exactly the proxy's URL, expiry and signing algorithm;
  2. started a session by sending the token to consent, with a credential that can call consent and nothing else;
  3. ran the usual per-call decision against the manifest of the agent this client maps to;
  4. called the tool with a narrowed hop token, not the client's token.

6. The chain leads back to a person. The session's root is the subject your IdP signed in, written issuer#subject. principal_source says how it was established: verified on a real deployment, embedded-test here because the embedded IdP is a test one. The client the user signed in with is recorded too.

3. Read the setup​

Open examples/mcp-oauth/run_local.py. Three parts are worth reading:

  • The mapping. Before anything runs, an admin maps the IdP client desktop-client to the agent kb-assistant:

    await cp.http.post(
    f"{BASE_URL}/v1/agents/{agent_id}/clients", headers=admin, json={"client_id": CLIENT_ID}
    )

    Remove that call and run again: step 5 prints refused: CLIENT_NOT_AUTHORIZED, and the refusal carries next: cold_start. The client is identified but not authorized.

  • The proxy's settings. oauth_resource_enabled, resource_url, idp_issuer and consent_token. No scope is set: the mapping chooses it.

  • The client. client_flow.py follows the challenge, reads two metadata documents, signs in and retries. That is the whole of it.

4. Try a bad token​

In sign_in, change audience=resource to audience="https://somewhere-else.example" and run again:

5. call with the token -> 401
refused: invalid_token

The proxy's WWW-Authenticate header says error_description="audience": it refused the token before any policy decision was made, because the token was issued for a different resource.

Next​