Skip to content

Commit d1cab61

Browse files
committed
Added back support for lambda authorizer which was in the original implementation
1 parent 4d87248 commit d1cab61

File tree

4 files changed

+457
-45
lines changed

4 files changed

+457
-45
lines changed

README.md

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
# Streamable MCP Server on AWS Lambda with OAuth 2.1 Authorization
1+
# Streamable MCP Server on AWS Lambda with Multiple Authorization Options
22

3-
This project implements a Model Context Protocol (MCP) server as a containerized application on AWS Lambda, accessible via Amazon API Gateway. It showcases the [`Streamable-HTTP`](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http) transport along with OAuth 2.1 authorization through AWS Cognito, providing a fully standards-compliant implementation.
3+
This project implements a Model Context Protocol (MCP) server as a containerized application on AWS Lambda, accessible via Amazon API Gateway. It showcases the [`Streamable-HTTP`](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http) transport along with multiple authorization options:
4+
5+
1. **OAuth 2.1 Authorization** through AWS Cognito
6+
2. **Lambda Authorizer** for simpler token-based authorization
47

58
The MCP server in this repo:
69
- Uses session management via the `Mcp-Session-id` header
7-
- Implements OAuth 2.1 authorization at the transport layer
10+
- Supports both OAuth 2.1 and Lambda authorizer methods
811
- Provides tools to analyze Amazon Bedrock usage
912

1013
Both server and client are written in TypeScript, with the server deployed as a container on Lambda.
@@ -15,6 +18,7 @@ Both server and client are written in TypeScript, with the server deployed as a
1518

1619
- **Standards Compliance**: Implements both Streamable-HTTP transport and OAuth 2.1 authorization specs
1720
- **Serverless Deployment**: Runs on AWS Lambda and API Gateway for scalability
21+
- **Multiple Authorization Options**: Supports both OAuth 2.1 and Lambda authorizer methods
1822
- **Secure Authentication**: Uses AWS Cognito for OAuth 2.1 authentication
1923
- **Discovery Support**: Implements OAuth discovery flow per RFC9728
2024
- **Analytics Tool**: Provides Bedrock usage analysis tool
@@ -33,6 +37,62 @@ The architecture consists of:
3337
5. **CloudWatch Logs**: Server and Bedrock usage logging
3438
6. **Bedrock**: The foundation model service
3539

40+
## Authorization Options
41+
42+
This project supports two authorization methods:
43+
44+
### 1. OAuth 2.1 Authorization
45+
46+
Uses AWS Cognito as the OAuth provider with full OAuth 2.1 compliance:
47+
48+
- Standards-compliant authorization with Cognito
49+
- PKCE support for enhanced security
50+
- JWT verification via JWKS
51+
- Token refresh with rotation
52+
- RFC9728-compliant OAuth discovery
53+
54+
To deploy with OAuth authorization:
55+
56+
```bash
57+
python deploy.py \
58+
--function-name mcp-server \
59+
--role-arn <lambda-execution-role-arn> \
60+
--region us-east-1 \
61+
--memory 2048 \
62+
--timeout 300 \
63+
--api-gateway \
64+
--api-name mcp-server-api \
65+
--stage-name prod \
66+
--auth-method oauth \
67+
--cognito-user-pool-id <your-user-pool-id> \
68+
--cognito-domain <your-domain-prefix> \
69+
--cognito-client-ids <your-client-id>
70+
```
71+
72+
### 2. Lambda Authorizer
73+
74+
Uses a separate Lambda function to authorize requests:
75+
76+
- Simple token-based authorization
77+
- API Gateway integration
78+
- Accepts any properly formatted Bearer token
79+
80+
To deploy with Lambda authorizer:
81+
82+
```bash
83+
python deploy.py \
84+
--function-name mcp-server \
85+
--role-arn <lambda-execution-role-arn> \
86+
--region us-east-1 \
87+
--memory 2048 \
88+
--timeout 300 \
89+
--api-gateway \
90+
--api-name mcp-server-api \
91+
--stage-name prod \
92+
--auth-method lambda \
93+
--lambda-authorizer-name mcp-authorizer
94+
```
95+
3696
## Prerequisites
3797

3898
1. **Node.js & npm:** v18.x or later
@@ -125,11 +185,23 @@ The architecture consists of:
125185
5. **Connect client to deployed server:**
126186
```bash
127187
export MCP_SERVER_URL="https://<api-id>.execute-api.<region>.amazonaws.com/prod/mcp"
188+
189+
# For OAuth 2.1 authentication
190+
export AUTH_METHOD=oauth
191+
export OAUTH_CLIENT_ID="<your-client-id>"
192+
export OAUTH_REDIRECT_URI="http://localhost:8000/callback"
193+
194+
# Or for Lambda authorizer
195+
export AUTH_METHOD=lambda
196+
128197
npx tsx src/client.ts
129198

130-
# At the client prompt
199+
# At the client prompt (for OAuth)
131200
> auth login
132201
> connect
202+
203+
# Or for Lambda authorizer
204+
> connect
133205
```
134206

135207
## MCP Protocol Implementation
@@ -296,6 +368,7 @@ The server implements these tools:
296368
| `auth logout` | Clear the stored token |
297369
| `auth status` | Show current authentication status |
298370
| `auth refresh` | Force refresh the access token |
371+
| `set-auth-method <method>` | Set authorization method (oauth, lambda, or auto) |
299372
| `debug on off` | Enable or disable debug logging |
300373
| `help` | Show help information |
301374
| `quit` | Exit the program |
@@ -332,6 +405,7 @@ curl -XPOST "https://<api-id>.execute-api.<region>.amazonaws.com/prod/mcp" \
332405

333406
#### Client Variables
334407
- `MCP_SERVER_URL`: URL of the MCP server
408+
- `AUTH_METHOD`: Authorization method to use (`oauth`, `lambda`, or `auto`)
335409
- `OAUTH_CLIENT_ID`: Cognito app client ID
336410
- `OAUTH_REDIRECT_URI`: Redirect URI for OAuth flow
337411
- `MCP_CLIENT_DEBUG`: Set to 'true' to enable debug logging

0 commit comments

Comments
 (0)