Repository navigation
Expand file tree
/
Copy pathoauth-cognito-example.ts
More file actions
175 lines (146 loc) · 5.9 KB
/
Copy pathoauth-cognito-example.ts
File metadata and controls
175 lines (146 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Example: OAuth 2.1 Authentication with AWS Cognito
*
* This example demonstrates how to set up an MCP server with OAuth authentication
* using AWS Cognito as the authorization server.
*
* Prerequisites:
* 1. AWS Cognito User Pool created
* 2. App Client configured with authorization_code flow
* 3. Callback URL registered: https://your-domain.com/oauth/callback
* 4. Environment variables set (see below)
*/
import { MCPServer, OAuthProvider } from "mcp-framework";
// Configuration from environment variables
const COGNITO_DOMAIN = process.env.COGNITO_DOMAIN; // e.g., "your-domain.auth.us-east-1.amazoncognito.com"
const COGNITO_CLIENT_ID = process.env.COGNITO_CLIENT_ID;
const COGNITO_CLIENT_SECRET = process.env.COGNITO_CLIENT_SECRET; // Optional for public clients
const RESOURCE_URI = process.env.RESOURCE_URI || "https://mcp.example.com";
const PORT = parseInt(process.env.PORT || "3001");
if (!COGNITO_DOMAIN || !COGNITO_CLIENT_ID) {
console.error("Missing required environment variables:");
console.error("- COGNITO_DOMAIN: Your Cognito domain (e.g., your-domain.auth.us-east-1.amazoncognito.com)");
console.error("- COGNITO_CLIENT_ID: Your Cognito app client ID");
process.exit(1);
}
// Create OAuth provider with Cognito configuration
const oauthProvider = new OAuthProvider({
// Cognito authorization server
authorizationServer: `https://${COGNITO_DOMAIN}`,
// OAuth client credentials
clientId: COGNITO_CLIENT_ID,
clientSecret: COGNITO_CLIENT_SECRET,
// Resource URI - the canonical URI of this MCP server
// This MUST match how clients access your server
resourceUri: RESOURCE_URI,
// Required scopes for Cognito
requiredScopes: ["openid", "profile"],
// Token cache TTL in seconds
tokenCacheTTL: 300, // 5 minutes
// Strict audience validation (recommended)
strictAudienceValidation: true,
// Custom metadata for Protected Resource Metadata endpoint
metadata: {
resource_name: "MCP Server with Cognito",
scopes_supported: ["openid", "profile", "email"]
}
});
// Create MCP server with OAuth authentication
const server = new MCPServer({
name: "oauth-cognito-example",
version: "1.0.0",
transport: {
type: "sse",
options: {
port: PORT,
endpoint: "/sse",
messageEndpoint: "/messages",
// CORS configuration for web clients
cors: {
allowOrigin: "*", // Configure this appropriately for production
allowHeaders: "Content-Type, Authorization",
allowMethods: "GET, POST, OPTIONS"
},
// Authentication configuration
auth: {
provider: oauthProvider,
endpoints: {
sse: false, // SSE connection endpoint is public
messages: true, // Message endpoint requires authentication
oauth: false, // OAuth callback endpoint is public
}
},
// OAuth callback handlers
oauth: {
// Called when user successfully authorizes
onCallback: async ({ accessToken, refreshToken, expiresIn, state }) => {
console.log("\n✅ OAuth Authorization Successful!");
console.log("State:", state);
console.log("Access Token (first 20 chars):", accessToken.substring(0, 20) + "...");
console.log("Expires in:", expiresIn, "seconds");
if (refreshToken) {
console.log("Refresh Token received:", refreshToken.substring(0, 20) + "...");
// TODO: Store refresh token securely for long-lived sessions
}
// You can store the token mapping here
// For example, associate the token with a user session
},
// Called when OAuth authorization fails
onError: async (error, state) => {
console.error("\n❌ OAuth Authorization Failed!");
console.error("State:", state);
console.error("Error:", error.message);
}
}
}
}
});
// Start the server
async function main() {
try {
console.log("\n🚀 Starting MCP Server with OAuth (Cognito)...");
console.log("\nConfiguration:");
console.log("- Authorization Server:", `https://${COGNITO_DOMAIN}`);
console.log("- Client ID:", COGNITO_CLIENT_ID);
console.log("- Resource URI:", RESOURCE_URI);
console.log("- Port:", PORT);
await server.start();
console.log("\n✨ Server is running!");
console.log("\nEndpoints:");
console.log(`- SSE Connection: http://localhost:${PORT}/sse`);
console.log(`- Messages: http://localhost:${PORT}/messages`);
console.log(`- OAuth Callback: http://localhost:${PORT}/oauth/callback`);
console.log(`- Protected Resource Metadata: http://localhost:${PORT}/.well-known/oauth-protected-resource`);
console.log("\n📋 To test authorization flow:");
console.log("1. Try to access the server without a token (you'll get a 401)");
console.log("2. Follow the WWW-Authenticate header to get authorization");
console.log("3. Or use the initiate authorization endpoint to get an auth URL");
// Example: Generate authorization URL for testing
const { authorizationUrl, state } = await oauthProvider.startAuthorizationFlow(
`http://localhost:${PORT}/oauth/callback`,
{
// Additional parameters for Cognito
// scope is automatically added from requiredScopes
}
);
console.log("\n🔐 Test Authorization URL:");
console.log(authorizationUrl);
console.log("\nVisit this URL in your browser to authorize.");
console.log("State:", state);
} catch (error) {
console.error("\n❌ Failed to start server:", error);
process.exit(1);
}
}
// Handle graceful shutdown
process.on('SIGINT', async () => {
console.log('\n\n⏹️ Shutting down server...');
await server.stop();
process.exit(0);
});
process.on('SIGTERM', async () => {
console.log('\n\n⏹️ Shutting down server...');
await server.stop();
process.exit(0);
});
main();