diff --git a/src/cli/commands/remove/__tests__/remove-all.test.ts b/src/cli/commands/remove/__tests__/remove-all.test.ts index 52fd5cd2..1693ce6f 100644 --- a/src/cli/commands/remove/__tests__/remove-all.test.ts +++ b/src/cli/commands/remove/__tests__/remove-all.test.ts @@ -105,4 +105,31 @@ describe('remove all command', () => { expect(json.note).toContain('source code'); expect(json.note).toContain('agentcore deploy'); }); + + it('clears gateways from mcp.json after remove all', async () => { + // Write mcp.json with a gateway and target + const mcpPath = join(projectDir, 'agentcore', 'mcp.json'); + await writeFile( + mcpPath, + JSON.stringify({ + agentCoreGateways: [ + { + name: 'TestGateway', + authorizerType: 'NONE', + targets: [{ name: 'test-target', targetType: 'mcpServer', endpoint: 'https://example.com/mcp' }], + }, + ], + }) + ); + + // Run remove all + const result = await runCLI(['remove', 'all', '--force', '--json'], projectDir); + expect(result.exitCode).toBe(0); + const json = JSON.parse(result.stdout); + expect(json.success).toBe(true); + + // Verify mcp.json gateways are cleared + const mcpAfter = JSON.parse(await readFile(mcpPath, 'utf-8')); + expect(mcpAfter.agentCoreGateways.length, 'Gateways should be cleared after remove all').toBe(0); + }); }); diff --git a/src/cli/commands/remove/actions.ts b/src/cli/commands/remove/actions.ts index 74604ea2..0cf36895 100644 --- a/src/cli/commands/remove/actions.ts +++ b/src/cli/commands/remove/actions.ts @@ -112,6 +112,11 @@ export async function handleRemoveAll(_options: RemoveAllOptions): Promise 0) { + const targetCount = mcpSpec.agentCoreGateways.reduce( + (sum: number, gw: { targets?: unknown[] }) => sum + (gw.targets?.length ?? 0), + 0 + ); + items.push(`${gatewayCount} gateway${gatewayCount > 1 ? 's' : ''}`); + if (targetCount > 0) { + items.push(`${targetCount} gateway target${targetCount > 1 ? 's' : ''}`); + } + } + } catch { + // mcp.json exists but has issues - still allow reset + } + } + items.push('All schemas will be reset to empty state'); setItemsToRemove(items); @@ -142,6 +162,11 @@ export function useRemoveFlow({ force, dryRun }: RemoveFlowOptions): RemoveFlowS const defaultProjectSpec = createDefaultProjectSpec(projectName || 'Project'); await configIO.writeProjectSpec(defaultProjectSpec); + // Reset mcp.json gateways so a subsequent deploy can tear down gateway resources + if (configIO.configExists('mcp')) { + await configIO.writeMcpSpec({ agentCoreGateways: [] }); + } + // Preserve aws-targets.json and deployed-state.json so that // a subsequent `agentcore deploy` can tear down existing stacks. });