This directory contains comprehensive examples demonstrating various features of the MCP Framework.
A minimal example showing the basics of OAuth integration:
npm run build
node dist/examples/oauth-simple-example.jsWhat it demonstrates:
- Basic OAuth provider setup
- Simple configuration
- Callback handling
Complete example for AWS Cognito integration:
# Set environment variables
export COGNITO_DOMAIN="your-domain.auth.us-east-1.amazoncognito.com"
export COGNITO_CLIENT_ID="your-client-id"
export COGNITO_CLIENT_SECRET="your-client-secret"
export RESOURCE_URI="https://mcp.example.com"
export PORT=3001
# Run the example
npm run build
node dist/examples/oauth-cognito-example.jsWhat it demonstrates:
- AWS Cognito setup
- Authorization flow initiation
- Token management
- Production-ready configuration
Advanced example with custom token validation:
npm run build
node dist/examples/oauth-custom-validator.jsWhat it demonstrates:
- JWT validation with JWKS
- Token introspection
- Custom claims validation
- Advanced security features
A test client to verify OAuth implementation:
# Start your OAuth-enabled MCP server first, then:
export SERVER_URL="http://localhost:3001"
npm run build
node dist/examples/oauth-test-client.jsWhat it demonstrates:
- OAuth discovery flow
- Protected Resource Metadata
- Authorization Server Metadata
- Token validation testing
- Install dependencies:
npm install- Build the project:
npm run build- Create a User Pool:
aws cognito-idp create-user-pool \
--pool-name mcp-user-pool \
--auto-verified-attributes email- Create an App Client:
aws cognito-idp create-user-pool-client \
--user-pool-id <your-pool-id> \
--client-name mcp-app-client \
--generate-secret \
--allowed-o-auth-flows authorization_code \
--allowed-o-auth-scopes openid profile email \
--callback-urls http://localhost:3001/oauth/callback \
--supported-identity-providers COGNITO- Configure a Domain:
aws cognito-idp create-user-pool-domain \
--domain your-unique-domain \
--user-pool-id <your-pool-id>- Start the server:
node dist/examples/oauth-cognito-example.js-
Visit the authorization URL (printed in the console)
-
Log in with your Cognito credentials
-
Server receives the callback and exchanges code for token
-
Use the access token to make authenticated requests:
curl -X POST http://localhost:3001/messages?sessionId=<session-id> \
-H "Authorization: Bearer <access-token>" \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"tools/list","id":1}'┌─────────┐ ┌──────────────┐ ┌─────────────┐
│ Client │ │ MCP Server │ │ Cognito │
└────┬────┘ └──────┬───────┘ └──────┬──────┘
│ │ │
│ 1. Request (no token) │ │
├───────────────────────────────>│ │
│ │ │
│ 2. 401 + WWW-Authenticate │ │
│<───────────────────────────────┤ │
│ │ │
│ 3. GET /.well-known/oauth-protected-resource │
├───────────────────────────────>│ │
│ │ │
│ 4. Protected Resource Metadata│ │
│<───────────────────────────────┤ │
│ │ │
│ 5. Redirect to authorization URL │
├───────────────────────────────────────────────────────────────────>│
│ │ │
│ 6. User logs in and authorizes│ │
│<───────────────────────────────────────────────────────────────────┤
│ │ │
│ 7. Redirect to callback with code │
├───────────────────────────────>│ │
│ │ │
│ │ 8. Exchange code for token │
│ ├──────────────────────────────────>│
│ │ │
│ │ 9. Access token + Refresh token │
│ │<──────────────────────────────────┤
│ │ │
│ 10. Success page │ │
│<───────────────────────────────┤ │
│ │ │
│ 11. Request with Bearer token │ │
├───────────────────────────────>│ │
│ │ │
│ 12. Validate token (cached) │ │
│ │ │
│ 13. Response │ │
│<───────────────────────────────┤ │
│ │ │
- Ensure you're using SSE transport (OAuth only works with HTTP-based transports)
- Verify the OAuth provider is configured in the
authsection
- State parameters expire after 10 minutes
- Ensure the callback happens within this timeframe
- Clear cookies and try again
- Verify your
resourceUrimatches how clients access your server - Check Cognito app client settings
- Ensure the resource parameter is being sent in token requests
- Configure CORS appropriately for your domain
- For local testing, use
allowOrigin: "*" - For production, specify exact origins