Skip to content

Commit 9d8e58f

Browse files
committed
merged with main
2 parents 2b9b2b5 + 3e2a8cd commit 9d8e58f

File tree

8 files changed

+69
-31
lines changed

8 files changed

+69
-31
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ celerybeat.pid
132132

133133
# Environments
134134
.env
135+
.env.user
136+
.env.agent
135137
.env.docker
136138
.venv
137139
env/

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ flowchart TB
148148
* An Amazon EC2 machine (`ml.t3.2xlarge`) with a standard Ubuntu AMI for running this solution.
149149
* An SSL cert for securing the communication to the Gateway. _This Gateway uses a self-signed cert by default and is also available over HTTP_.
150150
* One of the example MCP servers packaged in this repo uses the [`Polygon`](https://polygon.io/stocks) API for stock ticker data. Get an API key from [here](https://polygon.io/dashboard/signup?redirect=%2Fdashboard%2Fkeys). The server will still start without the API key but you will get a 401 Unauthorized error when using the tools provided by this server.
151+
* Setup authentication using Amazon Cognito as per instructions [here](docs/auth.md).
151152

152153
## Installation
153154

@@ -361,11 +362,9 @@ See the full API spec [here](docs/registry_api.md).
361362
## Roadmap
362363

363364
1. Store the server information in persistent storage.
364-
1. Add OAUTH 2.1 support to Gateway and Registry.
365365
1. Use GitHub API to retrieve information (license, programming language etc.) about MCP servers.
366366
1. Add option to deploy MCP servers.
367367

368368
## License
369369

370-
- Free for non-commercial use under AGPL-3.0
371-
- Commercial use requires a paid license
370+
This project is licensed under the Apache-2.0 License.

agents/.env.template

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ COGNITO_CLIENT_SECRET=your_cognito_client_secret_here
1111
COGNITO_USER_POOL_ID=your_cognito_user_pool_id_here
1212

1313
# AWS Region for Cognito
14-
AWS_REGION=us-east-1
14+
AWS_REGION=us-east-1
15+
16+
# Cognito Domain (without https:// prefix, just the domain name)
17+
# Example: mcp-gateway or your-custom-domain
18+
# COGNITO_DOMAIN=
19+
20+
# Secret key for session cookie signing (must match registry SECRET_KEY), string of hex characters
21+
# To generate: python -c 'import secrets; print(secrets.token_hex(32))'
22+
SECRET_KEY=your-secret-key-here

agents/agent_w_auth.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,13 @@ def load_env_config(use_session_cookie: bool) -> Dict[str, Optional[str]]:
163163
if os.path.exists(env_file):
164164
logger.info(f"Found .env file in parent directory: {env_file}")
165165
load_dotenv(env_file, override=True)
166-
file_found = True
167-
file_path = env_file
168166
logger.info(f"Loading environment variables from {env_file}")
169167
else:
170168
# Try to load from current working directory
171169
env_file = os.path.join(os.getcwd(), env_file_name)
172170
if os.path.exists(env_file):
173171
logger.info(f"Found .env file in current working directory: {env_file}")
174172
load_dotenv(env_file, override=True)
175-
file_found = True
176-
file_path = env_file
177173
logger.info(f"Loading environment variables from {env_file}")
178174
else:
179175
# Fallback to default .env loading

auth_server/.env.template

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Cognito Authentication Configuration
2+
# Copy this file to .env and fill in your actual values
3+
4+
# Auth Server URL
5+
AUTH_SERVER_URL=http://localhost:8888
6+
7+
# Cognito App Client ID
8+
COGNITO_CLIENT_ID=your_cognito_client_id_here
9+
10+
# Cognito App Client Secret
11+
COGNITO_CLIENT_SECRET=your_cognito_client_secret_here
12+
13+
# Cognito User Pool ID
14+
COGNITO_USER_POOL_ID=your_cognito_user_pool_id_here
15+
16+
# Cognito Domain (without https:// prefix, just the domain name)
17+
# Example: mcp-gateway or your-custom-domain
18+
COGNITO_DOMAIN=your_cognito_domain_here
19+
20+
# Secret key for session cookie signing (must match registry SECRET_KEY)
21+
# Generate a secure random string for production use
22+
# To generate: python -c 'import secrets; print(secrets.token_hex(32))'
23+
SECRET_KEY=your-secret-key-here
24+
25+
USER_POOL_ID=your_cognito_user_pool_id_here
26+
27+
# AWS Region for Cognito
28+
AWS_REGION=us-east-1

auth_server/scopes.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,3 @@ mcp-servers-restricted/execute:
289289
- user_profile_analyzer
290290
- synthetic_data_generator
291291

292-

auth_server/server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ async def validate_request(request: Request):
644644
if cookie_value:
645645
try:
646646
validation_result = validate_session_cookie(cookie_value)
647+
logger.info(f"Session cookie validation result: {validation_result}")
647648
logger.info(f"Session cookie validation successful for user: {validation_result['username']}")
648649
except ValueError as e:
649650
logger.warning(f"Session cookie validation failed: {e}")

docs/auth.md

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,16 @@ The above implementation provides an OAuth compliant way to MCP security without
168168

169169
This section discusses the reference implementation using Amazon Cognito as the IdP, supporting both Machine-to-Machine (M2M) and Session Cookie authentication methods.
170170

171+
>This section will soon be updated with detailed steps for Cognito configuration.
172+
171173
### Key Components
172174

173175
#### 1. Auth Server (`auth_server/server.py`)
174-
The enhanced auth server provides dual authentication support:
176+
The auth server provides dual authentication support:
175177
- **Primary Check**: Session cookie validation using `itsdangerous.URLSafeTimedSerializer`
176178
- **Fallback**: JWT token validation with Cognito
177-
- **Group Mapping**: Maps Cognito groups to MCP scopes
178-
- `mcp-admin` → Full unrestricted access
179-
- `mcp-user` → Restricted read access
180-
- `mcp-server-*` → Server-specific execute access
179+
- **Group Mapping**: Maps Cognito groups to MCP scopes via `scopes.yml` configuration
180+
- Both M2M and session cookie auth use the same scope definitions
181181

182182
#### 2. CLI Authentication Tool (`auth_server/cli_auth.py`)
183183
A standalone tool for user-based authentication:
@@ -204,12 +204,17 @@ Cognito supports machine-to-machine authentication, enabling Agents to have thei
204204
- MCP Server(s) function as resource servers
205205

206206
#### Authentication Flow:
207+
Run the Agent with the following command:
208+
```{.bash}
209+
python agents/agent_w_auth.py
210+
```
211+
1. Copy `agents/.env.template` to `agents/.env.agent` and set the environment variables (`COGNITO_CLIENT_ID`, `COGNITO_CLIENT_SECRET`, `COGNITO_USER_POOL_ID`) as appropriate for your setup.
207212
1. Agent startup:
208-
- Configured with client ID, client secret, and a set of scopes
213+
- Configured with client ID, client secret, and a set of scopes. _Each agent is an App Client in a Cognito user pool_.
209214
- Requests scopes (e.g., MCP Registry with tool finder and basic MCP servers)
210-
2. Cognito issues a JWT token
211-
3. Agent includes the JWT token in MCP headers
212-
4. Auth server on Nginx side:
215+
1. Cognito issues a JWT token
216+
1. Agent includes the JWT token in MCP headers
217+
1. Auth server on Nginx side:
213218
- Retrieves JWT token
214219
- Calls Cognito to validate token and get allowed scopes
215220
- Returns 200 or 403 based on:
@@ -219,10 +224,10 @@ Cognito supports machine-to-machine authentication, enabling Agents to have thei
219224

220225
#### Advantages
221226
1. Leverages existing Cognito user identities and groups
222-
2. No need to manage separate M2M credentials for user-initiated actions
223-
3. Maintains user context throughout the session
224-
4. Compatible with existing web-based authentication flow
225-
5. Auth server handles both authentication methods transparently
227+
1. No need to manage separate M2M credentials for user-initiated actions
228+
1. Maintains user context throughout the session
229+
1. Compatible with existing web-based authentication flow
230+
1. Auth server handles both authentication methods transparently
226231

227232
### 2. Session Cookie Authentication
228233

@@ -236,8 +241,7 @@ The CLI tool handles the OAuth flow with Cognito and saves the session cookie lo
236241

237242
```bash
238243
# Run the CLI authentication tool
239-
cd auth_server
240-
python cli_auth.py
244+
python agents/cli_auth.py
241245

242246
# This will:
243247
# 1. Open your browser to Cognito hosted UI
@@ -247,13 +251,15 @@ python cli_auth.py
247251
```
248252

249253
Required environment variables:
250-
- `COGNITO_DOMAIN`: Your Cognito domain (e.g., 'mcp-gateway')
254+
- `COGNITO_USER_POOL_ID`: Your Cognito user pool id
251255
- `COGNITO_CLIENT_ID`: OAuth client ID configured for PKCE flow
252256
- `SECRET_KEY`: Must match the registry's SECRET_KEY for cookie compatibility
253257

254258
##### b. Agent with Session Cookie Support
255259

256-
The enhanced agent (`agents/agent_w_auth.py`) now supports session cookie authentication:
260+
Copy `agents/.env.template` to `agents/.env.user` and set the environment variables (`COGNITO_CLIENT_ID`, `COGNITO_CLIENT_SECRET`, `COGNITO_USER_POOL_ID`, `SECRET_KEY`) as appropriate for your setup.
261+
262+
The agent (`agents/agent_w_auth.py`) supports session cookie authentication:
257263

258264
```bash
259265
# Use agent with session cookie
@@ -269,15 +275,14 @@ Key features:
269275
- Automatically reads cookie and includes in request headers
270276
- Falls back to M2M if session cookie flag not provided
271277

272-
##### c. Auth Server Enhancements
278+
##### c. Auth Server
273279

274280
The auth server validates session cookies alongside JWT tokens:
275281
- Checks for `mcp_gateway_session` cookie in request headers
276282
- Validates cookie signature using `itsdangerous.URLSafeTimedSerializer`
277-
- Maps Cognito groups to MCP scopes:
278-
- `mcp-admin` → unrestricted read/execute access
279-
- `mcp-user` → restricted read access
280-
- `mcp-server-{name}` → server-specific execute access
283+
- Maps Cognito groups to MCP scopes using `scopes.yml` configuration:
284+
- Configuration-driven mapping ensures consistency with M2M authentication
285+
- Single source of truth for all permission definitions
281286
- Falls back to JWT validation if no valid cookie found
282287

283288
#### Advantages:

0 commit comments

Comments
 (0)