# Build an OAuth MCP client

Use a Streamable HTTP MCP SDK with OAuth support when building your own client. The resource and token audience are `https://i2v.ai/api/mcp`.

## Discover and register

1. An unauthenticated MCP request returns HTTP `401`. Read the `resource_metadata` URL in its `WWW-Authenticate` header. The endpoint is `https://i2v.ai/.well-known/oauth-protected-resource/api/mcp`.
2. Follow its authorization server to `https://i2v.ai/.well-known/oauth-authorization-server` to discover authorization, token, registration and revocation endpoints.
3. Register a public client using the advertised `registration_endpoint`. Supply your callback in `redirect_uris` and use `token_endpoint_auth_method: none`. Register the exact HTTPS callback or a loopback callback for an installed app.

## Authorize and initialize

1. Generate a fresh PKCE verifier for this authorization and send its S256 challenge. Generate and store `state` for the callback. Request `response_type=code`, the registered `client_id` and `redirect_uri`, the MCP `resource`, and the required scopes.
2. Request `mcp:read` for discovery and account queries. To submit or upload, request `mcp:write` as well and let the user approve it on the consent screen. A client should check the granted scopes rather than infer them from its request.
3. Validate `state` at the callback and exchange the code for tokens with the original verifier and redirect URI. Send the resulting access token as a Bearer credential to the MCP endpoint. Initialize the SDK connection before discovering or calling tools.

## Refresh and revoke

The authorization code expires after 60 seconds. Access tokens last one hour, and refresh tokens last 30 days and rotate on use. Persist the newly returned refresh token atomically so another worker cannot retry with a consumed token. Submit revocation to the discovered endpoint or have the user revoke the client in [Connections](/settings/connections). Keep tokens out of logs and support tickets. See the [MCP authorization specification](https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization) for client behavior.