|
| 1 | +import * as cdk from "aws-cdk-lib"; |
| 2 | +import { Construct } from "constructs"; |
| 3 | +import { |
| 4 | + UserPool, |
| 5 | + OAuthScope, |
| 6 | + ResourceServerScope, |
| 7 | + UserPoolResourceServer, |
| 8 | + CfnUserPoolUser, |
| 9 | +} from "aws-cdk-lib/aws-cognito"; |
| 10 | +import { Secret } from "aws-cdk-lib/aws-secretsmanager"; |
| 11 | +import { AwsSolutionsChecks, NagSuppressions } from "cdk-nag"; |
| 12 | + |
| 13 | +export class McpAuthStack extends cdk.Stack { |
| 14 | + constructor(scope: Construct, id: string, props?: cdk.StackProps) { |
| 15 | + super(scope, id, props); |
| 16 | + |
| 17 | + // Create Cognito User Pool |
| 18 | + const userPool = new UserPool(this, "McpAuthUserPool", { |
| 19 | + userPoolName: `mcp-lambda-examples`, |
| 20 | + selfSignUpEnabled: false, |
| 21 | + signInAliases: { |
| 22 | + email: true, |
| 23 | + username: true, |
| 24 | + }, |
| 25 | + autoVerify: { |
| 26 | + email: true, |
| 27 | + }, |
| 28 | + passwordPolicy: { |
| 29 | + minLength: 12, |
| 30 | + requireLowercase: true, |
| 31 | + requireUppercase: true, |
| 32 | + requireDigits: true, |
| 33 | + requireSymbols: true, |
| 34 | + }, |
| 35 | + accountRecovery: cdk.aws_cognito.AccountRecovery.NONE, |
| 36 | + removalPolicy: cdk.RemovalPolicy.DESTROY, |
| 37 | + standardThreatProtectionMode: |
| 38 | + cdk.aws_cognito.StandardThreatProtectionMode.FULL_FUNCTION, |
| 39 | + featurePlan: cdk.aws_cognito.FeaturePlan.PLUS, |
| 40 | + }); |
| 41 | + |
| 42 | + // Create a user in the user pool |
| 43 | + new CfnUserPoolUser(this, "McpAuthUser", { |
| 44 | + userPoolId: userPool.userPoolId, |
| 45 | + username: "mcp-user", |
| 46 | + userAttributes: [ |
| 47 | + { |
| 48 | + name: "email", |
| 49 | + value: "mcp-user@example.com", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "email_verified", |
| 53 | + value: "true", |
| 54 | + }, |
| 55 | + ], |
| 56 | + messageAction: "SUPPRESS", |
| 57 | + }); |
| 58 | + |
| 59 | + const userCredentialsSecret = new Secret(this, "McpUserPassword", { |
| 60 | + secretName: `mcp-lambda-examples-user-creds`, |
| 61 | + description: "Credentials for MCP user", |
| 62 | + generateSecretString: { |
| 63 | + secretStringTemplate: JSON.stringify({ username: "mcp-user" }), |
| 64 | + generateStringKey: "password", |
| 65 | + excludeCharacters: '"@/\\', |
| 66 | + includeSpace: false, |
| 67 | + passwordLength: 16, |
| 68 | + requireEachIncludedType: true, |
| 69 | + }, |
| 70 | + removalPolicy: cdk.RemovalPolicy.DESTROY, |
| 71 | + }); |
| 72 | + |
| 73 | + NagSuppressions.addResourceSuppressions(userCredentialsSecret, [ |
| 74 | + { |
| 75 | + id: "AwsSolutions-SMG4", |
| 76 | + reason: |
| 77 | + "Credentials will not be automatically rotated for this example.", |
| 78 | + }, |
| 79 | + ]); |
| 80 | + |
| 81 | + // Create User Pool Domain |
| 82 | + userPool.addDomain("McpAuthUserPoolDomain", { |
| 83 | + cognitoDomain: { |
| 84 | + domainPrefix: `mcp-lambda-examples-${this.account}`, |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + // Scope for each MCP server that will use this user pool. |
| 89 | + // The scope name must match the URL path for the MCP server |
| 90 | + // in the API gateway. |
| 91 | + const mcpServers = ["mcpdoc", "cat-facts"]; |
| 92 | + const resourceServerScopes = mcpServers.map( |
| 93 | + (mcpServer) => |
| 94 | + new ResourceServerScope({ |
| 95 | + scopeName: mcpServer, |
| 96 | + scopeDescription: `Scope for ${mcpServer} MCP server`, |
| 97 | + }) |
| 98 | + ); |
| 99 | + const resourceServer = new UserPoolResourceServer(this, "ResourceServer", { |
| 100 | + identifier: "mcp-resource-server", |
| 101 | + userPool: userPool, |
| 102 | + scopes: resourceServerScopes, |
| 103 | + }); |
| 104 | + const oauthScopes = resourceServerScopes.map((scope) => |
| 105 | + OAuthScope.resourceServer(resourceServer, scope) |
| 106 | + ); |
| 107 | + |
| 108 | + // OAuth client for interactive chatbots: |
| 109 | + // The client will redirect users to the browser for sign-in |
| 110 | + const interactiveClient = userPool.addClient("InteractiveClient", { |
| 111 | + generateSecret: false, |
| 112 | + preventUserExistenceErrors: true, |
| 113 | + enableTokenRevocation: true, |
| 114 | + accessTokenValidity: cdk.Duration.minutes(60), |
| 115 | + refreshTokenValidity: cdk.Duration.days(60), |
| 116 | + oAuth: { |
| 117 | + flows: { |
| 118 | + authorizationCodeGrant: true, |
| 119 | + }, |
| 120 | + scopes: oauthScopes, |
| 121 | + callbackUrls: [ |
| 122 | + "http://localhost:9876/callback", // For local testing with oauth2c |
| 123 | + ], |
| 124 | + }, |
| 125 | + authFlows: { |
| 126 | + userPassword: true, |
| 127 | + }, |
| 128 | + }); |
| 129 | + |
| 130 | + // OAuth client for automated integration tests: |
| 131 | + // The client will provide a client secret for the access token |
| 132 | + const automatedClient = userPool.addClient("AutomatedClient", { |
| 133 | + generateSecret: true, |
| 134 | + preventUserExistenceErrors: true, |
| 135 | + enableTokenRevocation: true, |
| 136 | + accessTokenValidity: cdk.Duration.minutes(60), |
| 137 | + refreshTokenValidity: cdk.Duration.days(60), |
| 138 | + oAuth: { |
| 139 | + flows: { |
| 140 | + clientCredentials: true, |
| 141 | + }, |
| 142 | + scopes: oauthScopes, |
| 143 | + }, |
| 144 | + authFlows: {}, |
| 145 | + }); |
| 146 | + |
| 147 | + const automatedClientSecret = new Secret(this, "AutomatedClientSecret", { |
| 148 | + secretName: `mcp-lambda-examples-oauth-client-secret`, |
| 149 | + description: "Client secret for automated MCP client", |
| 150 | + secretStringValue: automatedClient.userPoolClientSecret, |
| 151 | + removalPolicy: cdk.RemovalPolicy.DESTROY, |
| 152 | + }); |
| 153 | + |
| 154 | + NagSuppressions.addResourceSuppressions(automatedClientSecret, [ |
| 155 | + { |
| 156 | + id: "AwsSolutions-SMG4", |
| 157 | + reason: |
| 158 | + "OAuth client secret will not be automatically rotated for this example", |
| 159 | + }, |
| 160 | + ]); |
| 161 | + |
| 162 | + // Outputs with export names for cross-stack references |
| 163 | + new cdk.CfnOutput(this, "UserPoolId", { |
| 164 | + value: userPool.userPoolId, |
| 165 | + description: "Cognito User Pool ID", |
| 166 | + exportName: "McpAuth-UserPoolId", |
| 167 | + }); |
| 168 | + |
| 169 | + new cdk.CfnOutput(this, "UserPoolDomain", { |
| 170 | + value: userPool.userPoolProviderUrl, |
| 171 | + description: "Cognito User Pool Domain URL", |
| 172 | + exportName: "McpAuth-UserPoolDomain", |
| 173 | + }); |
| 174 | + |
| 175 | + new cdk.CfnOutput(this, "AuthorizationUrl", { |
| 176 | + value: `${userPool.userPoolProviderUrl}/oauth2/authorize`, |
| 177 | + description: "OAuth Authorization URL", |
| 178 | + exportName: "McpAuth-AuthorizationUrl", |
| 179 | + }); |
| 180 | + |
| 181 | + new cdk.CfnOutput(this, "TokenUrl", { |
| 182 | + value: `${userPool.userPoolProviderUrl}/oauth2/token`, |
| 183 | + description: "OAuth Token URL", |
| 184 | + exportName: "McpAuth-TokenUrl", |
| 185 | + }); |
| 186 | + |
| 187 | + new cdk.CfnOutput(this, "InteractiveOAuthClientId", { |
| 188 | + value: interactiveClient.userPoolClientId, |
| 189 | + description: "Client ID for interactive OAuth flow", |
| 190 | + exportName: "McpAuth-InteractiveClientId", |
| 191 | + }); |
| 192 | + |
| 193 | + new cdk.CfnOutput(this, "AutomatedOAuthClientId", { |
| 194 | + value: automatedClient.userPoolClientId, |
| 195 | + description: "Client ID for automated OAuth flow", |
| 196 | + exportName: "McpAuth-AutomatedClientId", |
| 197 | + }); |
| 198 | + |
| 199 | + new cdk.CfnOutput(this, "OAuthClientSecretArn", { |
| 200 | + value: automatedClientSecret.secretArn, |
| 201 | + description: "ARN of the secret containing the OAuth client secret", |
| 202 | + exportName: "McpAuth-ClientSecretArn", |
| 203 | + }); |
| 204 | + |
| 205 | + new cdk.CfnOutput(this, "UserCredentialsSecretArn", { |
| 206 | + value: userCredentialsSecret.secretArn, |
| 207 | + description: |
| 208 | + "ARN of the secret containing the login credentials for mcp-user", |
| 209 | + exportName: "McpAuth-UserCredentialsArn", |
| 210 | + }); |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +const app = new cdk.App(); |
| 215 | +const stack = new McpAuthStack(app, "LambdaMcpServer-Auth", { |
| 216 | + env: { account: process.env["CDK_DEFAULT_ACCOUNT"], region: "us-east-2" }, |
| 217 | + stackName: "LambdaMcpServer-Auth", |
| 218 | +}); |
| 219 | + |
| 220 | +// Add CDK NAG suppressions for the entire stack |
| 221 | +NagSuppressions.addStackSuppressions(stack, [ |
| 222 | + { |
| 223 | + id: "AwsSolutions-IAM4", |
| 224 | + reason: |
| 225 | + "AWS managed policies are acceptable for CDK custom resource Lambda functions (created to retrieve OAuth client secret)", |
| 226 | + appliesTo: [ |
| 227 | + "Policy::arn:<AWS::Partition>:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole", |
| 228 | + ], |
| 229 | + }, |
| 230 | +]); |
| 231 | + |
| 232 | +cdk.Aspects.of(stack).add(new AwsSolutionsChecks({ verbose: true })); |
| 233 | +app.synth(); |
0 commit comments