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
).openid
profile
email
.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.
Next-generation MCP server enhances documentation analysis with AI-powered neural processing and multi-language support
Learn to connect to MCP servers over HTTP with Python SDK using SSE for efficient protocol communication
Python MCP client for testing servers avoid message limits and customize with API key
Discover easy deployment and management of MCP servers with Glutamate platform for Windows Linux Mac
Explore community contributions to MCP including clients, servers, and projects for seamless integration
Learn how to use MCProto Ruby gem to create and chain MCP servers for custom solutions