UNPKG

@chinchillaenterprises/mcp-amplify

Version:

AWS Amplify MCP server with intelligent deployment automation, specialized logging suite, and recursive resource discovery

140 lines 4.85 kB
import { existsSync, readFileSync } from 'fs'; import { join } from 'path'; export class AmplifyResourceDiscovery { config = null; identifiers; constructor() { this.config = this.loadAmplifyOutputs(); this.identifiers = this.extractIdentifiers(); } loadAmplifyOutputs() { try { const outputsPath = join(process.cwd(), 'amplify_outputs.json'); if (existsSync(outputsPath)) { return JSON.parse(readFileSync(outputsPath, 'utf8')); } } catch (error) { console.error('Failed to load amplify_outputs.json:', error); } return null; } extractIdentifiers() { const identifiers = { apiId: null, appName: null, username: null, userPoolId: null, region: 'us-east-1', stackName: null, sandboxId: null, s3BucketName: null }; if (!this.config) { return identifiers; } // Extract AppSync API ID from GraphQL URL const appsyncUrl = this.config.data?.url || ''; const apiIdMatch = appsyncUrl.match(/https:\/\/([^.]+)\.appsync-api/); identifiers.apiId = apiIdMatch ? apiIdMatch[1] : null; // Extract app name and username from S3 bucket name const bucketName = this.config.storage?.bucket_name || ''; identifiers.s3BucketName = bucketName || null; const bucketMatch = bucketName.match(/amplify-([^-]+)-([^-]+)-/); if (bucketMatch) { identifiers.appName = bucketMatch[1]; identifiers.username = bucketMatch[2]; } // Extract other identifiers identifiers.userPoolId = this.config.auth?.user_pool_id || null; identifiers.region = this.config.auth?.aws_region || this.config.storage?.aws_region || process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'us-east-1'; // Extract stack name and sandbox ID identifiers.stackName = this.config.metadata?.stackName || null; if (identifiers.stackName) { const sandboxMatch = identifiers.stackName.match(/sandbox-([a-z0-9]+)/); identifiers.sandboxId = sandboxMatch ? sandboxMatch[1] : null; } return identifiers; } getIdentifiers() { return this.identifiers; } hasValidConfig() { return this.config !== null && this.identifiers.apiId !== null; } /** * Get the physical DynamoDB table name from logical model name * Pattern: {ModelName}-{AppSyncApiId}-{environment} */ getDynamoTableName(modelName, environment = 'NONE') { if (!this.identifiers.apiId) { throw new Error('No AppSync API ID found in amplify_outputs.json'); } return `${modelName}-${this.identifiers.apiId}-${environment}`; } /** * Get Lambda function name pattern for discovery * Pattern: amplify-{appName}-{username}-{functionName}-{hash} */ getLambdaFunctionPattern(functionName) { if (!this.identifiers.appName || !this.identifiers.username) { throw new Error('No app name or username found in amplify_outputs.json'); } return `amplify-${this.identifiers.appName}-${this.identifiers.username}-*${functionName}*`; } /** * Get CloudFormation stack name pattern * Pattern: amplify-{appName}-{username}-sandbox-{hash} */ getStackNamePattern() { if (!this.identifiers.appName || !this.identifiers.username) { throw new Error('No app name or username found in amplify_outputs.json'); } return `amplify-${this.identifiers.appName}-${this.identifiers.username}-sandbox-*`; } /** * Get CloudWatch log group name for a Lambda function * Pattern: /aws/lambda/{functionName} */ getLogGroupName(functionName) { return `/aws/lambda/${functionName}`; } /** * Check if a resource belongs to the current sandbox */ isCurrentSandboxResource(resourceName) { if (!this.identifiers.sandboxId) { return true; // If no sandbox ID, accept all } return resourceName.includes(this.identifiers.sandboxId); } /** * Get S3 bucket name from config */ getS3BucketName() { return this.config?.storage?.bucket_name || null; } /** * Get Cognito User Pool ID */ getUserPoolId() { return this.identifiers.userPoolId; } /** * Get AppSync API URL */ getAppSyncApiUrl() { return this.config?.data?.url || null; } /** * Get region */ getRegion() { return this.identifiers.region; } } //# sourceMappingURL=AmplifyResourceDiscovery.js.map