Skip to content

Commit d4aa281

Browse files
authored
fix: clear mcp.json gateways during remove-all to prevent orphaned AWS resources (#484)
remove-all only reset agentcore.json, leaving mcp.json untouched. This caused gateways to survive removal, and subsequent deploys would keep gateway resources alive in AWS instead of triggering teardown. Changes: - handleRemoveAll (CLI path): reset mcp.json gateways after writeProjectSpec - useRemoveFlow (TUI path): reset mcp.json gateways + enumerate gateways/targets in the confirmation list - Add test verifying mcp.json gateways are cleared during remove-all Fixes GW-08
1 parent c1144e0 commit d4aa281

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/cli/commands/remove/__tests__/remove-all.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,31 @@ describe('remove all command', () => {
105105
expect(json.note).toContain('source code');
106106
expect(json.note).toContain('agentcore deploy');
107107
});
108+
109+
it('clears gateways from mcp.json after remove all', async () => {
110+
// Write mcp.json with a gateway and target
111+
const mcpPath = join(projectDir, 'agentcore', 'mcp.json');
112+
await writeFile(
113+
mcpPath,
114+
JSON.stringify({
115+
agentCoreGateways: [
116+
{
117+
name: 'TestGateway',
118+
authorizerType: 'NONE',
119+
targets: [{ name: 'test-target', targetType: 'mcpServer', endpoint: 'https://example.com/mcp' }],
120+
},
121+
],
122+
})
123+
);
124+
125+
// Run remove all
126+
const result = await runCLI(['remove', 'all', '--force', '--json'], projectDir);
127+
expect(result.exitCode).toBe(0);
128+
const json = JSON.parse(result.stdout);
129+
expect(json.success).toBe(true);
130+
131+
// Verify mcp.json gateways are cleared
132+
const mcpAfter = JSON.parse(await readFile(mcpPath, 'utf-8'));
133+
expect(mcpAfter.agentCoreGateways.length, 'Gateways should be cleared after remove all').toBe(0);
134+
});
108135
});

src/cli/commands/remove/actions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ export async function handleRemoveAll(_options: RemoveAllOptions): Promise<Remov
112112
credentials: [],
113113
});
114114

115+
// Reset mcp.json gateways so a subsequent deploy can tear down gateway resources
116+
if (configIO.configExists('mcp')) {
117+
await configIO.writeMcpSpec({ agentCoreGateways: [] });
118+
}
119+
115120
// Preserve aws-targets.json and deployed-state.json so that
116121
// a subsequent `agentcore deploy` can tear down existing stacks.
117122

src/cli/tui/screens/remove/useRemoveFlow.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,26 @@ export function useRemoveFlow({ force, dryRun }: RemoveFlowOptions): RemoveFlowS
8181
items.push('AgentCore project (corrupted or incomplete)');
8282
}
8383

84+
// Check for gateways in mcp.json
85+
if (configIO.configExists('mcp')) {
86+
try {
87+
const mcpSpec = await configIO.readMcpSpec();
88+
const gatewayCount = mcpSpec.agentCoreGateways?.length ?? 0;
89+
if (gatewayCount > 0) {
90+
const targetCount = mcpSpec.agentCoreGateways.reduce(
91+
(sum: number, gw: { targets?: unknown[] }) => sum + (gw.targets?.length ?? 0),
92+
0
93+
);
94+
items.push(`${gatewayCount} gateway${gatewayCount > 1 ? 's' : ''}`);
95+
if (targetCount > 0) {
96+
items.push(`${targetCount} gateway target${targetCount > 1 ? 's' : ''}`);
97+
}
98+
}
99+
} catch {
100+
// mcp.json exists but has issues - still allow reset
101+
}
102+
}
103+
84104
items.push('All schemas will be reset to empty state');
85105
setItemsToRemove(items);
86106

@@ -142,6 +162,11 @@ export function useRemoveFlow({ force, dryRun }: RemoveFlowOptions): RemoveFlowS
142162
const defaultProjectSpec = createDefaultProjectSpec(projectName || 'Project');
143163
await configIO.writeProjectSpec(defaultProjectSpec);
144164

165+
// Reset mcp.json gateways so a subsequent deploy can tear down gateway resources
166+
if (configIO.configExists('mcp')) {
167+
await configIO.writeMcpSpec({ agentCoreGateways: [] });
168+
}
169+
145170
// Preserve aws-targets.json and deployed-state.json so that
146171
// a subsequent `agentcore deploy` can tear down existing stacks.
147172
});

0 commit comments

Comments
 (0)