AI Explainers

Building a Secure Auth Code Flow Setup Using AgentCore Gateway with MCP Clients

Learn how to implement OAuth 2.0 Authorization Code flow for Model Context Protocol servers on Amazon Bedrock AgentCore Gateway, securing every AI agent…

Model Context Protocol guide illustration for agentic AI workflows
Featured image for Building a Secure Auth Code Flow Setup Using AgentCore Gateway with MCP Clients on Drop Agentic.

What This Guide Covers

Model Context Protocol (MCP) has become a practical standard for connecting AI assistants to external tools and data sources. When you host MCP servers on Amazon Bedrock AgentCore Gateway, you need a reliable inbound authorization mechanism so that only authenticated clients can invoke your tools. This guide walks through implementing the OAuth 2.0 Authorization Code flow as that mechanism — resulting in a setup where every AI agent request carries a verified identity before it reaches your MCP server.

The Authorization Code flow is the recommended OAuth pattern for scenarios where a user or service must explicitly grant access. Unlike simpler API key approaches, it issues short-lived access tokens and supports token refresh, making it better suited for production agentic workloads where sessions may be long-running or multi-step.

Architecture Overview

The core components in this setup are:

  • MCP client — the AI assistant or agent that initiates tool calls
  • AgentCore Gateway — the Amazon Bedrock managed layer that routes MCP requests and enforces authorization policies
  • Authorization server — an OAuth 2.0-compliant identity provider (such as Amazon Cognito) that issues tokens
  • MCP server — your backend tool implementation, protected behind the Gateway

At a high level, the MCP client first obtains an authorization code from the identity provider, exchanges it for an access token, and then includes that token in the Authorization header of each MCP request. AgentCore Gateway validates the token before forwarding the request to the MCP server. The MCP server itself never needs to handle raw credentials.

Step 1 — Configure Your OAuth 2.0 Authorization Server

Before touching AgentCore Gateway, set up your authorization server. Amazon Cognito is a straightforward choice on AWS, but any OAuth 2.0-compliant provider works. You will need to:

  • Create an application client with the Authorization Code grant type enabled
  • Define the allowed callback (redirect) URIs that your MCP client will use
  • Configure the required OAuth scopes — for example, a custom scope such as mcp/invoke that maps to MCP tool access
  • Note the authorization endpoint, token endpoint, and client credentials

Keep the client secret secure. For server-side MCP clients running in trusted environments, the confidential client model applies. For public clients, use PKCE (Proof Key for Code Exchange) to prevent authorization code interception.

Step 2 — Register the MCP Server on AgentCore Gateway

AgentCore Gateway acts as the front door for your MCP server. Registration involves specifying the MCP server endpoint and the authorization configuration that the Gateway should enforce. According to the AWS documentation for this setup, you configure the Gateway to expect a Bearer token in incoming requests and to validate that token against your authorization server’s JWKS (JSON Web Key Set) endpoint.

Key configuration parameters to supply during registration include:

  • The MCP server’s backend URL
  • The JWKS URI from your authorization server so the Gateway can verify token signatures
  • The expected token issuer and audience claims
  • The required scope that must be present in the token for access to be granted

Once registered, the Gateway will reject any request that lacks a valid, unexpired token matching the configured constraints — before the request ever reaches your MCP server.

Step 3 — Implement the Auth Code Flow in Your MCP Client

Your MCP client needs to complete the Authorization Code exchange before making tool calls. The standard flow proceeds as follows:

  • Redirect the user or service principal to the authorization endpoint with the required parameters: response_type=code, client_id, redirect_uri, scope, and a state value for CSRF protection
  • Receive the authorization code at the redirect URI after the user authenticates and consents
  • Exchange the code for tokens by posting to the token endpoint with grant_type=authorization_code, the code, and the redirect URI
  • Store the access token and refresh token securely — avoid storing tokens in browser local storage for sensitive workloads

For automated agent pipelines where no human is present to complete a browser redirect, consider using a service account with a pre-authorized consent or a machine-to-machine OAuth flow at the authorization server level, while still presenting the resulting token to AgentCore Gateway as a Bearer token. Teams should verify what grant types their chosen identity provider supports for headless scenarios.

Step 4 — Attach the Token to MCP Requests

With a valid access token in hand, your MCP client includes it in the HTTP Authorization header for every request to the AgentCore Gateway endpoint:

Authorization: Bearer <access_token>

AgentCore Gateway intercepts this header, validates the token signature using the JWKS endpoint, checks the expiry, issuer, audience, and required scope, and only then proxies the request to the MCP server. From the MCP server’s perspective, it receives a clean, pre-authorized request — it does not need to re-validate the token itself, though additional application-level checks remain your responsibility.

When the access token expires, the MCP client should use the refresh token to obtain a new one without requiring the user to re-authenticate. Implement token refresh logic before making calls if the token’s expiry is imminent.

Step 5 — Test the End-to-End Flow

Before promoting to production, verify each stage independently:

  • Confirm the authorization server issues tokens with the correct claims by decoding a sample token at a tool such as jwt.io
  • Send a request to the AgentCore Gateway endpoint without a token and confirm a 401 response
  • Send a request with an expired or malformed token and confirm rejection
  • Send a valid request and confirm the MCP server receives and processes it correctly
  • Test token refresh and confirm the client recovers without user intervention

What to Check Before Production

A few areas warrant careful review before this setup handles real workloads:

  • Token storage — ensure access and refresh tokens are stored in secure, encrypted storage appropriate for your deployment environment
  • Scope granularity — define narrow scopes rather than broad access grants; this limits the blast radius if a token is compromised
  • Token lifetime — short-lived access tokens (15–60 minutes) reduce exposure; configure this at the authorization server level
  • Logging and monitoring — enable Gateway access logs and set up alerts for unusual rejection rates, which may indicate token misuse or misconfiguration
  • PKCE for public clients — if any MCP client runs in an environment where the client secret cannot be kept confidential, PKCE is not optional

What Teams Should Verify First

This guide is based on AWS documentation published around mid-2025. AgentCore Gateway is a relatively recent service, and its configuration options, supported identity providers, and token validation behavior may evolve. Teams should consult the current Amazon Bedrock AgentCore documentation and AWS release notes before finalizing a production deployment. Licensing, data residency requirements, and applicable compliance frameworks should also be reviewed independently — particularly for workloads handling personal data or operating in regulated industries.

The OAuth 2.0 Authorization Code flow described here is a well-established standard, but its correct implementation depends on the specific identity provider and client library in use. Always test against your actual environment rather than assuming behavior from general OAuth documentation.

Sources