Secure MCP server with OAuth 2.1 and AWS Cognito for authentication and resource protection
The Model Context Protocol (MCP) server example integrates MCP with OAuth 2.1 to secure resources for AI applications such as Claude Desktop, Continue, Cursor, and others. This documentation provides a comprehensive guide on how to set up an MCP server that acts as a Resource Server (RS) and is secured through the OAuth 2.1 Authorization Code Flow with PKCE using AWS Cognito.
The core features of this MCP server implementation include:
The architecture overview involves a client making an unsecured request, which then leads through a series of OAuth 2.1 flows:
Client Request Without Token
401 Unauthorized with WWW-Authenticate Header
WWW-Authenticate header pointing to PRM metadata.PRM Metadata Discovery
Authorization Code Flow with PKCE
Access Token Retrieval and Retry Request
Token Validation and Access Granting
graph TD
A[Client] -->|Request| B[MCP Server]
B --> C[401 Unauthorized with WWW-Authenticate]
C --> D[Fetch PRM Metadata]
D --> E[AWS Cognito URL Discovery]
E --> F[OAuth 2.1 Authorization Code Flow (PKCE)]
F --> G[Receive Access Token]
G --> H[Rerun Request to MCP Server]
H --> I[MCP Server Validation and Access]
graph TD
A[Client] --> B[MCP Protocol]
B --> C[MCP Server]
C --> D[Data Source/Tool]
style A fill:#e1f5fe
style C fill:#f3e5f5
style D fill:#e8f5e8
http://localhost:3000/callback).openidprofileemail.well-known/oauth-protected-resource endpoint with links to Cognito’s Authorization Server.The clients include:
| MCP Client | Resources | Tools | Prompts |
|---|---|---|---|
| Claude Desktop | ✅ | ✅ | ✅ |
| Continue | ✅ | ✅ | ✅ |
| Cursor | ❌ | ✅ | ❌ |
The implementation ensures optimal performance and compatibility across different AI applications, while leveraging OAuth 2.1 for secure token management.
To configure the server:
{
"mcpServers": {
"[server-name]": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-[name]"],
"env": {
"API_KEY": "your-api-key"
}
}
}
}
Q: Can I use this MCP server with any AI application?
Q: How does PKCE enhance security?
Q: Can I integrate third-party authentication providers like Okta or Auth0?
Q: How do I handle dynamic client registration with MCP servers?
Q: Can this server be deployed without Node.js or C#.NET 8 environments?
Contributions from the community are highly encouraged. Follow our contribution guidelines to get started.
Explore further by visiting:
This project is licensed under the MIT License.
By following this comprehensive guide, developers can integrate and secure their AI applications with MCP through OAuth 2.1 and AWS Cognito. This implementation not only ensures robust security measures but also facilitates seamless interaction between various AI tools and data resources.
RuinedFooocus is a local AI image generator and chatbot image server for seamless creative control
Learn to set up MCP Airflow Database server for efficient database interactions and querying airflow data
Simplify MySQL queries with Java-based MysqlMcpServer for easy standard input-output communication
Explore CoRT MCP server for advanced self-arguing AI with multi-LLM inference and enhanced evaluation methods
Build stunning one-page websites track engagement create QR codes monetize content easily with Acalytica
Access NASA APIs for space data, images, asteroids, weather, and exoplanets via MCP integration