New in version: 2.12.4
This guide shows you how to secure your FastMCP server using AWS Cognito user pools. Since AWS Cognito doesn’t support Dynamic Client Registration, this integration uses the OAuth Proxy pattern to bridge AWS Cognito’s traditional OAuth with MCP’s authentication requirements. It also includes robust JWT token validation, ensuring enterprise-grade authentication.
Configuration
Prerequisites
Before you begin, you will need:- An AWS Account with access to create AWS Cognito user pools
- Basic familiarity with AWS Cognito concepts (user pools, app clients)
- Your FastMCP server’s URL (can be localhost for development, e.g.,
http://localhost:8000
)
Step 1: Create an AWS Cognito User Pool and App Client
Set up AWS Cognito user pool with an app client to get the credentials needed for authentication:1
Navigate to AWS Cognito
Go to the AWS Cognito Console and ensure you’re in your desired AWS region.Select “User pools” from the side navigation (click on the hamburger icon at the top left in case you don’t see any), and click “Create user pool” to create a new user pool.
2
Define Your Application
AWS Cognito now provides a streamlined setup experience:
- Application type: Select “Traditional web application” (this is the correct choice for FastMCP server-side authentication)
- Name your application: Enter a descriptive name (e.g.,
FastMCP Server
)
- Server-side authentication with client secrets
- Authorization code grant flow
- Appropriate security settings for confidential clients
Choose “Traditional web application” rather than SPA, Mobile app, or Machine-to-machine options. This ensures proper OAuth 2.0 configuration for FastMCP.
3
Configure Options
AWS will guide you through configuration options:
- Sign-in identifiers: Choose how users will sign in (email, username, or phone)
- Required attributes: Select any additional user information you need
- Return URL: Add your callback URL (e.g.,
http://localhost:8000/auth/callback
for development)
The simplified interface handles most OAuth security settings automatically based on your application type selection.
4
Review and Create
Review your configuration and click “Create user pool”.After creation, you’ll see your user pool details. Save these important values:
- User pool ID (format:
eu-central-1_XXXXXXXXX
) - Client ID (found under → “Applications” → “App clients” in the side navigation → <Your application name, e.g.,
FastMCP Server
> → “App client information”) - Client Secret (found under → “Applications” → “App clients” in the side navigation → <Your application name, e.g.,
FastMCP Server
> → “App client information”)
The user pool ID and app client credentials are all you need for FastMCP configuration.
5
Configure OAuth Settings
Under “Login pages” in your app client’s settings, you can double check and adjust the OAuth configuration:
- Allowed callback URLs: Add your server URL +
/auth/callback
(e.g.,http://localhost:8000/auth/callback
) - Allowed sign-out URLs: Optional, for logout functionality
- OAuth 2.0 grant types: Ensure “Authorization code grant” is selected
- OpenID Connect scopes: Select scopes your application needs (e.g.,
openid
,email
,profile
)
For local development, you can use
http://localhost
URLs. For production, you must use HTTPS.6
Pick Up AWS Cognito Domain
Navigate to “Branding” → “Domain” in the side navigation to find or configure Your AWS Cognito domain:Option 1: Use Auto-Generated Domain
- If AWS has already created a domain automatically, note the domain prefix (the part before
.auth.region.amazoncognito.com
) - This prefix is what you’ll use in your FastMCP configuration
- If no domain exists or you want a better name, delete the existing domain and create a new one using the “Actions” menu
- Under “Domain” → “Cognito domain” in the “Create Cognito domain” dialog, enter a meaningful prefix (e.g.,
my-app
) that is available in the AWS region you are in - Just note the domain prefix you entered (e.g.,
my-fastmcp-app
) - this is what you’ll use in your FastMCP configuration
The FastMCP AWS Cognito provider automatically constructs the full domain from your prefix and region, simplifying configuration.
7
Save Your Credentials
After setup, you’ll have:
- User Pool ID: Format like
eu-central-1_XXXXXXXXX
- Client ID: Your application’s client identifier
- Client Secret: Generated client secret (keep secure)
- Domain Prefix: The prefix of Your AWS Cognito domain
- AWS Region: Where Your AWS Cognito user pool is located
Store these credentials securely. Never commit them to version control. Use environment variables or AWS Secrets Manager in production.
Step 2: FastMCP Configuration
Create your FastMCP server using theAWSCognitoProvider
, which handles AWS Cognito’s JWT tokens and user claims automatically:
server.py
Testing
Running the Server
Start your FastMCP server with HTTP transport to enable OAuth flows:Testing with a Client
Create a test client that authenticates with Your AWS Cognito-protected server:test_client.py
- Your browser will open to AWS Cognito’s hosted UI login page
- After you sign in (or sign up), you’ll be redirected back to your MCP server
- The client receives the JWT token and can make authenticated requests
The client caches tokens locally, so you won’t need to re-authenticate for subsequent runs unless the token expires or you explicitly clear the cache.
Environment Variables
For production deployments, use environment variables instead of hardcoding credentials.Provider Selection
Setting this environment variable allows the AWS Cognito provider to be used automatically without explicitly instantiating it in code.Set to
fastmcp.server.auth.providers.aws.AWSCognitoProvider
to use AWS Cognito authentication.AWS Cognito-Specific Configuration
These environment variables provide default values for the AWS Cognito provider, whether it’s instantiated manually or configured viaFASTMCP_SERVER_AUTH
.
Your AWS Cognito user pool ID (e.g.,
eu-central-1_XXXXXXXXX
)AWS region where your AWS Cognito user pool is located
Your AWS Cognito app client ID
Your AWS Cognito app client secret
Public URL of your FastMCP server for OAuth callbacks
One of the redirect paths configured in your AWS Cognito app client
Comma-, space-, or JSON-separated list of required OAuth scopes (e.g.,
openid email
or ["openid","email","profile"]
).env
file:
server.py
Features
JWT Token Validation
The AWS Cognito provider includes robust JWT token validation:- Signature Verification: Validates tokens against AWS Cognito’s public keys (JWKS)
- Expiration Checking: Automatically rejects expired tokens
- Issuer Validation: Ensures tokens come from your specific AWS Cognito user pool
- Scope Enforcement: Verifies required OAuth scopes are present
User Claims and Groups
Access rich user information from AWS Cognito JWT tokens:Enterprise Integration
Perfect for enterprise environments with:- Single Sign-On (SSO): Integrate with corporate identity providers
- Multi-Factor Authentication (MFA): Leverage AWS Cognito’s built-in MFA
- User Groups: Role-based access control through AWS Cognito groups
- Custom Attributes: Access custom user attributes defined in your AWS Cognito user pool
- Compliance: Meet enterprise security and compliance requirements