auth0-3lo-mcp-server

auth0-3lo-mcp-server

Demonstrates the AgentCore Gateway 3-Legged OAuth flow with Auth0, exposing whoami and echo tools for end-to-end authorization code testing.

Category
访问服务器

README

AgentCore Gateway 3LO: End-to-End Authorization Code Flow with Auth0

A complete working example of the Amazon Bedrock AgentCore Gateway 3-Legged OAuth (Authorization Code) flow using Auth0 as both the inbound JWT authorizer and the outbound credential provider, with a Lambda MCP server as the target.

What This Demonstrates

  1. Inbound JWT authentication (Auth0 token → Gateway)
  2. tools/list discovering tools from a remote MCP server
  3. tools/call triggering a -32042 OAuth elicitation
  4. User completing OAuth consent at Auth0
  5. Callback handler receiving the session_id
  6. CompleteResourceTokenAuth binding the token to the user
  7. Subsequent tools/call succeeding end-to-end

Architecture

MCP Client (curl) --> AgentCore Gateway --> Lambda MCP Server
       |                    |
       |                    +--> AgentCore Identity (token store)
       |                                |
       |                                +--> Auth0 (consent + code exchange)
       |
       +--> Callback Handler (Flask @ localhost:5000)
                    |
                    +--> Receives session_id after consent
                    +--> Calls CompleteResourceTokenAuth

Sequence Diagram

Prerequisites

  • AWS account with Amazon Bedrock AgentCore access
  • AWS CLI with bedrock-agentcore and bedrock-agentcore-control commands
  • Auth0 free developer account (sign up)
  • Python 3.12+
  • pip

Quick Start

# Clone this repo
git clone <repo-url>
cd agentcore-3lo-auth0-example

# Install dependencies
pip install -r requirements.txt

# Copy and fill in your credentials
cp .env.example .env
# Edit .env with your Auth0 and AWS values

# Start the Flask app
python app.py

Then follow the step-by-step guide below.

Step-by-Step Guide

Step 1: Auth0 Configuration

Create an API

  1. Auth0 Dashboard > APIs > Create API
  2. Name: agentcore-gateway-api
  3. Identifier: https://agentcore-gateway-api
  4. Signing Algorithm: RS256

Configure API Settings

  1. Permissions tab: Add access:tools (description: "Access MCP tools")
  2. Settings tab: Set "Allow Skipping User Consent" to OFF

API Permissions API Settings - Consent

Create an Application

  1. Auth0 Dashboard > Applications > Create Application
  2. Name: agentcore-3lo-repro, Type: Regular Web Application
  3. Settings:
    • Allowed Callback URLs: http://localhost:5000/callback, http://localhost:5000/token-callback
    • Allowed Logout URLs: http://localhost:5000/

You will add the AgentCore callback URL to this list in Step 4.

App Settings

Deploy a Post Login Action

  1. Auth0 Dashboard > Actions > Library > Build Custom
  2. Name: add-scp-claim, Trigger: Login / Post Login
exports.onExecutePostLogin = async (event, api) => {
  const scopes = event.transaction.requested_scopes || [];
  if (!scopes.includes('openid')) {
    scopes.push('openid');
  }
  api.accessToken.setCustomClaim('scp', scopes);
};
  1. Deploy, then add to the Login flow (Actions > Flows > Login)

Login Flow

Step 2: AWS IAM Role

Create an execution role for the Gateway with trust policy for bedrock-agentcore.amazonaws.com:

{
  "Version": "2012-10-17",
  "Statement": [
    {"Effect": "Allow", "Action": "bedrock-agentcore:*", "Resource": "*"},
    {"Effect": "Allow", "Action": "lambda:InvokeFunction", "Resource": "*"},
    {"Effect": "Allow", "Action": "secretsmanager:GetSecretValue",
     "Resource": "arn:aws:secretsmanager:<region>:<account>:secret:bedrock-agentcore-identity*"}
  ]
}

Step 3: Create AgentCore Gateway

Important: Use only allowedAudience. Do NOT use allowedClients with Auth0 — Auth0 places client_id in the azp claim, not client_id, causing insufficient_scope errors.

aws bedrock-agentcore-control create-gateway \
  --name "auth0-3lo-example" \
  --role-arn "arn:aws:iam::<account>:role/<role-name>" \
  --protocol-type MCP \
  --protocol-configuration '{"mcp":{"supportedVersions":["2025-11-25"]}}' \
  --authorizer-type CUSTOM_JWT \
  --authorizer-configuration '{
    "customJWTAuthorizer": {
      "discoveryUrl": "https://<tenant>.us.auth0.com/.well-known/openid-configuration",
      "allowedAudience": ["https://agentcore-gateway-api"]
    }
  }' \
  --region us-east-1

Wait for status: READY.

Step 4: Create OAuth2 Credential Provider

aws bedrock-agentcore-control create-oauth2-credential-provider \
  --name "auth0-3lo-example" \
  --credential-provider-vendor "CustomOauth2" \
  --oauth2-provider-config-input '{
    "customOauth2ProviderConfig": {
      "oauthDiscovery": {
        "authorizationServerMetadata": {
          "issuer": "https://<tenant>.us.auth0.com/",
          "authorizationEndpoint": "https://<tenant>.us.auth0.com/authorize",
          "tokenEndpoint": "https://<tenant>.us.auth0.com/oauth/token",
          "responseTypes": ["code"]
        }
      },
      "clientId": "<your-client-id>",
      "clientSecret": "<your-client-secret>",
      "clientAuthenticationMethod": "CLIENT_SECRET_POST"
    }
  }' \
  --region us-east-1

The response includes a callbackUrl. Add this URL to your Auth0 application's Allowed Callback URLs.

Step 5: Register Workload Identity Return URL

aws bedrock-agentcore-control update-workload-identity \
  --name "<gateway-id>" \
  --allowed-resource-oauth2-return-urls '["http://localhost:5000/agentcore-callback"]' \
  --region us-east-1

Step 6: Create Lambda MCP Server

Deploy the included Lambda function (see lambda/ directory):

cd lambda && zip -r ../lambda.zip . && cd ..

aws lambda create-function \
  --function-name auth0-3lo-mcp-server \
  --runtime python3.12 \
  --handler lambda_function.lambda_handler \
  --role arn:aws:iam::<account>:role/<lambda-role> \
  --zip-file fileb://lambda.zip \
  --region us-east-1

aws lambda create-function-url-config \
  --function-name auth0-3lo-mcp-server \
  --auth-type NONE \
  --region us-east-1

aws lambda add-permission \
  --function-name auth0-3lo-mcp-server \
  --statement-id FunctionURLAllowPublicAccess \
  --action lambda:InvokeFunctionUrl \
  --principal "*" \
  --function-url-auth-type NONE \
  --region us-east-1

Step 7: Create Gateway Target

aws bedrock-agentcore-control create-gateway-target \
  --gateway-identifier "<gateway-id>" \
  --name "auth0-3lo-lambda-mcp" \
  --target-configuration '{
    "mcp": {
      "mcpServer": {
        "endpoint": "<lambda-function-url>",
        "mcpToolSchema": {
          "inlinePayload": "{\"tools\":[{\"name\":\"whoami\",\"description\":\"Returns user info\",\"inputSchema\":{\"type\":\"object\",\"properties\":{},\"required\":[]}},{\"name\":\"echo\",\"description\":\"Echoes input\",\"inputSchema\":{\"type\":\"object\",\"properties\":{\"message\":{\"type\":\"string\"}},\"required\":[\"message\"]}}]}"
        }
      }
    }
  }' \
  --credential-provider-configurations '[{
    "credentialProviderType": "OAUTH",
    "credentialProvider": {
      "oauthCredentialProvider": {
        "providerArn": "arn:aws:bedrock-agentcore:us-east-1:<account>:token-vault/default/oauth2credentialprovider/auth0-3lo-example",
        "scopes": ["openid", "profile", "email"],
        "grantType": "AUTHORIZATION_CODE",
        "defaultReturnUrl": "http://localhost:5000/agentcore-callback",
        "customParameters": {
          "audience": "https://agentcore-gateway-api"
        }
      }
    }
  }]' \
  --region us-east-1

Step 8: Get an Access Token

  1. Start Flask: python app.py
  2. Open http://localhost:5000/token in your browser
  3. Log in to Auth0
  4. Copy the access token displayed

Step 9: Test the Flow

TOKEN="<your-access-token>"
GATEWAY="<gateway-id>"

# 1. List tools (confirms inbound auth)
curl -s -X POST "https://${GATEWAY}.gateway.bedrock-agentcore.us-east-1.amazonaws.com/mcp" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -H "MCP-Protocol-Version: 2025-11-25" \
  -d '{"jsonrpc":"2.0","id":"1","method":"tools/list","params":{}}'

# 2. Call a tool (triggers -32042 elicitation)
curl -s -X POST "https://${GATEWAY}.gateway.bedrock-agentcore.us-east-1.amazonaws.com/mcp" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -H "MCP-Protocol-Version: 2025-11-25" \
  -d '{"jsonrpc":"2.0","id":"1","method":"tools/call","params":{"name":"auth0-3lo-lambda-mcp___echo","arguments":{"message":"hello"}}}'

# 3. Open the URL from the -32042 response IMMEDIATELY (60s TTL)
#    Complete consent in browser
#    Browser lands on localhost:5000/agentcore-callback?session_id=...

# 4. Complete token binding
SESSION_ID="<session_id_from_callback>"
aws bedrock-agentcore complete-resource-token-auth \
  --user-identifier "{\"userToken\":\"${TOKEN}\"}" \
  --session-uri "${SESSION_ID}" \
  --region us-east-1

# 5. Retry the tool call (should succeed now)
curl -s -X POST "https://${GATEWAY}.gateway.bedrock-agentcore.us-east-1.amazonaws.com/mcp" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -H "MCP-Protocol-Version: 2025-11-25" \
  -d '{"jsonrpc":"2.0","id":"1","method":"tools/call","params":{"name":"auth0-3lo-lambda-mcp___echo","arguments":{"message":"hello from 3LO end-to-end"}}}'

Expected final response:

{
  "jsonrpc": "2.0",
  "id": "1",
  "result": {
    "content": [{"type": "text", "text": "Echo: hello from 3LO end-to-end"}]
  }
}

Callback Received

Key Learnings

Issue Root Cause Fix
insufficient_scope on all requests allowedClients in gateway config; Auth0 uses azp not client_id Use only allowedAudience
authorizationCode must not be null Auth0 skips consent for first-party apps without permissions Add API permission + disable consent skip
Authorization error when sending message Lambda Function URL missing resource policy aws lambda add-permission with lambda:InvokeFunctionUrl
Invalid request on consent URL request_uri TTL expired (~60 seconds) Open URL immediately after generation
Token never persists Nothing calls CompleteResourceTokenAuth Implement callback handler (this repo)

Auth0 Configuration Checklist

  • [ ] API created with custom identifier (audience)
  • [ ] At least one permission defined on the API
  • [ ] "Allow Skipping User Consent" is OFF
  • [ ] Post Login Action deployed (adds scp claim)
  • [ ] Application callback URLs include AgentCore callback URL
  • [ ] Application type: Regular Web Application
  • [ ] Token Endpoint Auth Method: client_secret_post

File Structure

.
├── README.md
├── requirements.txt
├── .env.example
├── .gitignore
├── app.py                          # Flask app (token endpoint + callback handler)
├── callback_handler.py             # Standalone callback handler example
├── lambda/
│   └── lambda_function.py          # Lambda MCP server (echo + whoami tools)
└── screenshots/
    ├── 3lo-sequence-diagram.png
    ├── 02-auth0-app-settings-callbacks.png
    ├── 04-auth0-api-settings-consent.png
    ├── 05-auth0-api-permissions.png
    ├── 08-agentcore-callback-received.png
    └── 09-auth0-login-flow-action.png

Cleanup

aws bedrock-agentcore-control delete-gateway-target \
  --gateway-identifier "<gateway-id>" --target-id "<target-id>" --region us-east-1

aws bedrock-agentcore-control delete-gateway \
  --gateway-identifier "<gateway-id>" --region us-east-1

aws bedrock-agentcore-control delete-oauth2-credential-provider \
  --name "auth0-3lo-example" --region us-east-1

aws lambda delete-function --function-name auth0-3lo-mcp-server --region us-east-1

Documentation

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选