#!/usr/bin/env node import { AgentCoreStack } from '../lib/cdk-stack'; import { ConfigIO, type AwsDeploymentTarget } from '@aws/agentcore-cdk'; import { App, type Environment } from 'aws-cdk-lib'; import * as path from 'path'; import * as fs from 'fs'; function toEnvironment(target: AwsDeploymentTarget): Environment { return { account: target.account, region: target.region, }; } function sanitize(name: string): string { return name.replace(/_/g, '-'); } function toStackName(projectName: string, targetName: string): string { return `AgentCore-${sanitize(projectName)}-${sanitize(targetName)}`; } async function main() { // Config root is parent of cdk/ directory. The CLI sets process.cwd() to agentcore/cdk/. const configRoot = path.resolve(process.cwd(), '..'); const configIO = new ConfigIO({ baseDir: configRoot }); const spec = await configIO.readProjectSpec(); const targets = await configIO.readAWSDeploymentTargets(); // Extract MCP configuration from project spec. // Gateway fields are stored in agentcore.json but may not yet be on the // AgentCoreProjectSpec type from @aws/agentcore-cdk, so we read them // dynamically and cast the resulting object. // eslint-disable-next-line @typescript-eslint/no-explicit-any const specAny = spec as any; const mcpSpec = specAny.agentCoreGateways?.length ? { agentCoreGateways: specAny.agentCoreGateways, mcpRuntimeTools: specAny.mcpRuntimeTools, unassignedTargets: specAny.unassignedTargets, } : undefined; // Read deployed state for credential ARNs (populated by pre-deploy identity setup) let deployedState: Record | undefined; try { deployedState = JSON.parse(fs.readFileSync(path.join(configRoot, '.cli', 'deployed-state.json'), 'utf8')); } catch { // Deployed state may not exist on first deploy } if (targets.length === 0) { throw new Error('No deployment targets configured. Please define targets in agentcore/aws-targets.json'); } const app = new App(); for (const target of targets) { const env = toEnvironment(target); const stackName = toStackName(spec.name, target.name); // Extract credentials from deployed state for this target const targetState = (deployedState as Record)?.targets as | Record> | undefined; const targetResources = targetState?.[target.name]?.resources as Record | undefined; const credentials = targetResources?.credentials as | Record | undefined; new AgentCoreStack(app, stackName, { spec, mcpSpec, credentials, env, description: `AgentCore stack for ${spec.name} deployed to ${target.name} (${target.region})`, tags: { 'agentcore:project-name': spec.name, 'agentcore:target-name': target.name, }, }); } app.synth(); } main().catch((error: unknown) => { console.error('AgentCore CDK synthesis failed:', error instanceof Error ? error.message : error); process.exitCode = 1; });