@aws/nx-plugin 1.0.0-rc.11 → 1.0.0-rc.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/LICENSE-THIRD-PARTY +81 -3
  2. package/generators.json +31 -1
  3. package/package.json +2 -2
  4. package/sdk/agentcore-gateway.d.ts +6 -0
  5. package/sdk/agentcore-gateway.js +11 -0
  6. package/sdk/agentcore-gateway.js.map +1 -0
  7. package/sdk/license.d.ts +4 -4
  8. package/sdk/license.js +5 -4
  9. package/sdk/license.js.map +1 -1
  10. package/sdk/py.d.ts +6 -6
  11. package/sdk/py.js +7 -7
  12. package/sdk/py.js.map +1 -1
  13. package/sdk/ts.d.ts +18 -14
  14. package/sdk/ts.js +38 -32
  15. package/sdk/ts.js.map +1 -1
  16. package/src/agentcore-gateway/files/cedar/policies/README.md +52 -0
  17. package/src/agentcore-gateway/files/cedar/policies/permit-all.cedar +11 -0
  18. package/src/agentcore-gateway/files/project/serve-local.ts.template +133 -0
  19. package/src/agentcore-gateway/generator.d.ts +25 -0
  20. package/src/agentcore-gateway/generator.js +129 -0
  21. package/src/agentcore-gateway/generator.js.map +1 -0
  22. package/src/agentcore-gateway/mcp-connection/generator.d.ts +19 -0
  23. package/src/agentcore-gateway/mcp-connection/generator.js +79 -0
  24. package/src/agentcore-gateway/mcp-connection/generator.js.map +1 -0
  25. package/src/agentcore-gateway/mcp-connection/schema.d.ts +12 -0
  26. package/src/agentcore-gateway/mcp-connection/schema.json +26 -0
  27. package/src/agentcore-gateway/schema.d.ts +16 -0
  28. package/src/agentcore-gateway/schema.json +65 -0
  29. package/src/connection/generator.d.ts +1 -1
  30. package/src/connection/generator.js +64 -46
  31. package/src/connection/generator.js.map +1 -1
  32. package/src/infra/app/__snapshots__/generator.spec.ts.snap +9 -9
  33. package/src/mcp-server/schema-registry.d.ts +9 -3
  34. package/src/mcp-server/schema-registry.js +22 -6
  35. package/src/mcp-server/schema-registry.js.map +1 -1
  36. package/src/preset/__snapshots__/generator.spec.ts.snap +3 -1
  37. package/src/py/agent/__snapshots__/generator.spec.ts.snap +629 -7
  38. package/src/py/agent/a2a-connection/files/agent-connection/app/__targetAgentSnakeCase___client.py.template +9 -3
  39. package/src/py/agent/a2a-connection/generator.js +1 -1
  40. package/src/py/agent/a2a-connection/generator.js.map +1 -1
  41. package/src/py/agent/files/http/init.py.template +5 -23
  42. package/src/py/agent/gateway-connection/files/agent-connection/app/__gatewaySnakeCase___client.py.template +35 -0
  43. package/src/py/agent/gateway-connection/generator.d.ts +10 -0
  44. package/src/py/agent/gateway-connection/generator.js +106 -0
  45. package/src/py/agent/gateway-connection/generator.js.map +1 -0
  46. package/src/py/agent/gateway-connection/schema.d.ts +12 -0
  47. package/src/py/agent/gateway-connection/schema.json +26 -0
  48. package/src/py/agent/generator.js +27 -22
  49. package/src/py/agent/generator.js.map +1 -1
  50. package/src/py/agent/mcp-connection/__snapshots__/generator.spec.ts.snap +8 -4
  51. package/src/py/agent/mcp-connection/files/agent-connection/app/__mcpServerSnakeCase___client.py.template +9 -3
  52. package/src/py/agent/mcp-connection/generator.js +2 -72
  53. package/src/py/agent/mcp-connection/generator.js.map +1 -1
  54. package/src/py/fast-api/__snapshots__/generator.spec.ts.snap +5 -22
  55. package/src/py/fast-api/files/app/__name__/init.py.template +5 -22
  56. package/src/py/mcp-server/__snapshots__/generator.spec.ts.snap +58 -3
  57. package/src/ts/agent/__snapshots__/generator.spec.ts.snap +619 -2
  58. package/src/ts/agent/a2a-connection/files/agent-connection/app/__targetAgentKebabCase__-client.ts.template +10 -6
  59. package/src/ts/agent/gateway-connection/files/agent-connection/app/__gatewayKebabCase__-client.ts.template +30 -0
  60. package/src/ts/agent/gateway-connection/generator.d.ts +10 -0
  61. package/src/ts/agent/gateway-connection/generator.js +106 -0
  62. package/src/ts/agent/gateway-connection/generator.js.map +1 -0
  63. package/src/ts/agent/gateway-connection/schema.d.ts +12 -0
  64. package/src/ts/agent/gateway-connection/schema.json +26 -0
  65. package/src/ts/agent/generator.js +23 -19
  66. package/src/ts/agent/generator.js.map +1 -1
  67. package/src/ts/agent/mcp-connection/__snapshots__/generator.spec.ts.snap +9 -4
  68. package/src/ts/agent/mcp-connection/files/agent-connection/app/__mcpServerKebabCase__-client.ts.template +9 -6
  69. package/src/ts/agent/mcp-connection/generator.js +1 -13
  70. package/src/ts/agent/mcp-connection/generator.js.map +1 -1
  71. package/src/ts/mcp-server/__snapshots__/generator.spec.ts.snap +58 -3
  72. package/src/ts/nx-generator/generator.js +29 -0
  73. package/src/ts/nx-generator/generator.js.map +1 -1
  74. package/src/ts/react-website/app/__snapshots__/generator.spec.ts.snap +86 -39
  75. package/src/utils/agent-chat/agent-chat.d.ts +31 -0
  76. package/src/utils/agent-chat/agent-chat.js +30 -0
  77. package/src/utils/agent-chat/agent-chat.js.map +1 -0
  78. package/src/utils/agent-chat/files/a2a/chat.ts.template +33 -0
  79. package/src/utils/agent-chat/files/ag-ui/chat.ts.template +17 -0
  80. package/src/utils/agent-chat/files/common/agentcore.ts.template +81 -0
  81. package/src/utils/agent-chat/files/http-py/chat.ts.template +43 -0
  82. package/src/utils/agent-chat/files/http-ts/chat.ts.template +54 -0
  83. package/src/utils/agent-connection/agent-connection.d.ts +17 -0
  84. package/src/utils/agent-connection/agent-connection.js +94 -0
  85. package/src/utils/agent-connection/agent-connection.js.map +1 -1
  86. package/src/utils/agent-connection/files/core-gateway/agentcore-gateway-mcp-client.ts.template +78 -0
  87. package/src/utils/agent-connection/files/core-runtime-config/runtime-config.ts.template +22 -32
  88. package/src/utils/agent-connection/files/py-core-gateway/agentcore_gateway_mcp_client.py.template +78 -0
  89. package/src/utils/agent-connection/files/py-core-runtime-config/runtime_config.py.template +5 -18
  90. package/src/utils/agent-core-constructs/agent-core-constructs.d.ts +8 -0
  91. package/src/utils/agent-core-constructs/agent-core-constructs.js +54 -1
  92. package/src/utils/agent-core-constructs/agent-core-constructs.js.map +1 -1
  93. package/src/utils/agent-core-constructs/files/cdk/app/agent-core/__nameKebabCase__/__nameKebabCase__.ts.template +4 -0
  94. package/src/utils/agent-core-constructs/files/cdk/app/agentcore-gateway/__nameKebabCase__/__nameKebabCase__.ts.template +37 -0
  95. package/src/utils/agent-core-constructs/files/cdk/core/agentcore-gateway/agentcore-gateway.ts.template +293 -0
  96. package/src/utils/agent-core-constructs/files/cdk/core/agentcore-gateway/readiness-probe/index.js.template +55 -0
  97. package/src/utils/agent-core-constructs/files/terraform/app/agentcore-gateway/__nameKebabCase__/__nameKebabCase__.tf.template +242 -0
  98. package/src/utils/agent-core-constructs/files/terraform/app/agentcore-gateway/__nameKebabCase__/render-cedar.cjs.template +13 -0
  99. package/src/utils/agent-core-constructs/files/terraform/core/agent-core/runtime.tf.template +55 -2
  100. package/src/utils/agent-core-constructs/files/terraform/core/agentcore-gateway/gateway.tf.template +14 -0
  101. package/src/utils/versions.d.ts +46 -44
  102. package/src/utils/versions.js +45 -43
  103. package/src/utils/versions.js.map +1 -1
  104. package/src/py/agent/scripts/http/chat.ts.template +0 -38
  105. package/src/ts/agent/scripts/http/chat.ts.template +0 -35
@@ -1,5 +1,569 @@
1
1
  // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
2
 
3
+ exports[`ts#agent generator > chat scripts for a2a protocol > should match snapshot for chat scripts with cognito auth > agentcore.ts (a2a, cognito) 1`] = `
4
+ "// Resolves the deployed TestProjectAgent agent from runtime config and authenticates requests to it.
5
+ import { randomUUID } from 'node:crypto';
6
+ import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
7
+
8
+ const SESSION_HEADER = 'X-Amzn-Bedrock-AgentCore-Runtime-Session-Id';
9
+
10
+ // AgentCore session ids must be at least 33 characters.
11
+ export const SESSION_ID = randomUUID().replaceAll('-', '').padEnd(33, '0');
12
+
13
+ export interface RemoteAgent {
14
+ /** ARN of the deployed Bedrock AgentCore runtime. */
15
+ arn: string;
16
+ /** AWS region parsed from the runtime ARN. */
17
+ region: string;
18
+ }
19
+
20
+ // Returns the deployed agent when \`RUNTIME_CONFIG_APP_ID\` is set, otherwise \`undefined\` to chat locally.
21
+ export const resolveRemoteAgent = async (): Promise<
22
+ RemoteAgent | undefined
23
+ > => {
24
+ const application = process.env.RUNTIME_CONFIG_APP_ID;
25
+ if (!application) {
26
+ return undefined;
27
+ }
28
+ const config = (await getAppConfig('agentcore', {
29
+ application,
30
+ environment: 'default',
31
+ transform: 'json',
32
+ })) as { agentRuntimes?: Record<string, string> };
33
+ const arn = config?.agentRuntimes?.['TestProjectAgent'];
34
+ if (!arn) {
35
+ throw new Error(
36
+ \`No deployed agent named 'TestProjectAgent' found in runtime configuration (application \${application}).\`,
37
+ );
38
+ }
39
+ return { arn, region: arn.split(':')[3] };
40
+ };
41
+
42
+ /** The Cognito access token used to authenticate with the deployed agent. */
43
+ export const getAccessToken = (): string => {
44
+ const accessToken = process.env.AGENT_ACCESS_TOKEN;
45
+ if (!accessToken) {
46
+ throw new Error(
47
+ 'AGENT_ACCESS_TOKEN is not set. Provide a Cognito access token to chat with the deployed agent.',
48
+ );
49
+ }
50
+ return accessToken;
51
+ };
52
+
53
+ // A \`fetch\` that authenticates requests to the deployed agent and forwards the session id.
54
+ export const createAgentCoreFetch = (): typeof fetch => {
55
+ const accessToken = getAccessToken();
56
+ return (input, init) => {
57
+ const headers = new Headers(init?.headers);
58
+ headers.set(SESSION_HEADER, SESSION_ID);
59
+ headers.set('Authorization', \`Bearer \${accessToken}\`);
60
+ return fetch(input, { ...init, headers });
61
+ };
62
+ };
63
+ "
64
+ `;
65
+
66
+ exports[`ts#agent generator > chat scripts for a2a protocol > should match snapshot for chat scripts with cognito auth > chat.ts (a2a, cognito) 1`] = `
67
+ "// Chat CLI for TestProjectAgent (A2A protocol). Connects to the local
68
+ // \`serve-local\` server, or the deployed agent when \`RUNTIME_CONFIG_APP_ID\` is set.
69
+ import {
70
+ ClientFactory,
71
+ ClientFactoryOptions,
72
+ DefaultAgentCardResolver,
73
+ JsonRpcTransportFactory,
74
+ } from '@a2a-js/sdk/client';
75
+ import { A2AChatAdapter, chatLoop } from 'agent-chat-cli';
76
+ import { createAgentCoreFetch, resolveRemoteAgent } from './agentcore.js';
77
+
78
+ const remote = await resolveRemoteAgent();
79
+
80
+ const url = remote
81
+ ? \`https://bedrock-agentcore.\${remote.region}.amazonaws.com/runtimes/\${encodeURIComponent(remote.arn)}/invocations/\`
82
+ : process.env.URL!;
83
+
84
+ // Remote requests are authenticated via a custom fetch; locally the default client talks plain HTTP.
85
+ const clientFactory = remote
86
+ ? new ClientFactory({
87
+ ...ClientFactoryOptions.default,
88
+ transports: [
89
+ new JsonRpcTransportFactory({
90
+ fetchImpl: createAgentCoreFetch(),
91
+ }),
92
+ ],
93
+ cardResolver: new DefaultAgentCardResolver({
94
+ fetchImpl: createAgentCoreFetch(),
95
+ }),
96
+ })
97
+ : undefined;
98
+
99
+ await chatLoop(new A2AChatAdapter({ clientFactory }), url);
100
+ "
101
+ `;
102
+
103
+ exports[`ts#agent generator > chat scripts for a2a protocol > should match snapshot for chat scripts with iam auth > agentcore.ts (a2a, iam) 1`] = `
104
+ "// Resolves the deployed TestProjectAgent agent from runtime config and authenticates requests to it.
105
+ import { randomUUID } from 'node:crypto';
106
+ import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
107
+ import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
108
+ import { AwsClient } from 'aws4fetch';
109
+
110
+ const SESSION_HEADER = 'X-Amzn-Bedrock-AgentCore-Runtime-Session-Id';
111
+
112
+ // AgentCore session ids must be at least 33 characters.
113
+ export const SESSION_ID = randomUUID().replaceAll('-', '').padEnd(33, '0');
114
+
115
+ export interface RemoteAgent {
116
+ /** ARN of the deployed Bedrock AgentCore runtime. */
117
+ arn: string;
118
+ /** AWS region parsed from the runtime ARN. */
119
+ region: string;
120
+ }
121
+
122
+ // Returns the deployed agent when \`RUNTIME_CONFIG_APP_ID\` is set, otherwise \`undefined\` to chat locally.
123
+ export const resolveRemoteAgent = async (): Promise<
124
+ RemoteAgent | undefined
125
+ > => {
126
+ const application = process.env.RUNTIME_CONFIG_APP_ID;
127
+ if (!application) {
128
+ return undefined;
129
+ }
130
+ const config = (await getAppConfig('agentcore', {
131
+ application,
132
+ environment: 'default',
133
+ transform: 'json',
134
+ })) as { agentRuntimes?: Record<string, string> };
135
+ const arn = config?.agentRuntimes?.['TestProjectAgent'];
136
+ if (!arn) {
137
+ throw new Error(
138
+ \`No deployed agent named 'TestProjectAgent' found in runtime configuration (application \${application}).\`,
139
+ );
140
+ }
141
+ return { arn, region: arn.split(':')[3] };
142
+ };
143
+
144
+ // A \`fetch\` that authenticates requests to the deployed agent and forwards the session id.
145
+ export const createAgentCoreFetch = (region: string): typeof fetch => {
146
+ const credentialProvider = fromNodeProviderChain();
147
+ return async (input, init) => {
148
+ const headers = new Headers(init?.headers);
149
+ headers.set(SESSION_HEADER, SESSION_ID);
150
+ const client = new AwsClient({
151
+ ...(await credentialProvider()),
152
+ service: 'bedrock-agentcore',
153
+ region,
154
+ });
155
+ return client.fetch(input, { ...init, headers });
156
+ };
157
+ };
158
+ "
159
+ `;
160
+
161
+ exports[`ts#agent generator > chat scripts for a2a protocol > should match snapshot for chat scripts with iam auth > chat.ts (a2a, iam) 1`] = `
162
+ "// Chat CLI for TestProjectAgent (A2A protocol). Connects to the local
163
+ // \`serve-local\` server, or the deployed agent when \`RUNTIME_CONFIG_APP_ID\` is set.
164
+ import {
165
+ ClientFactory,
166
+ ClientFactoryOptions,
167
+ DefaultAgentCardResolver,
168
+ JsonRpcTransportFactory,
169
+ } from '@a2a-js/sdk/client';
170
+ import { A2AChatAdapter, chatLoop } from 'agent-chat-cli';
171
+ import { createAgentCoreFetch, resolveRemoteAgent } from './agentcore.js';
172
+
173
+ const remote = await resolveRemoteAgent();
174
+
175
+ const url = remote
176
+ ? \`https://bedrock-agentcore.\${remote.region}.amazonaws.com/runtimes/\${encodeURIComponent(remote.arn)}/invocations/\`
177
+ : process.env.URL!;
178
+
179
+ // Remote requests are authenticated via a custom fetch; locally the default client talks plain HTTP.
180
+ const clientFactory = remote
181
+ ? new ClientFactory({
182
+ ...ClientFactoryOptions.default,
183
+ transports: [
184
+ new JsonRpcTransportFactory({
185
+ fetchImpl: createAgentCoreFetch(remote.region),
186
+ }),
187
+ ],
188
+ cardResolver: new DefaultAgentCardResolver({
189
+ fetchImpl: createAgentCoreFetch(remote.region),
190
+ }),
191
+ })
192
+ : undefined;
193
+
194
+ await chatLoop(new A2AChatAdapter({ clientFactory }), url);
195
+ "
196
+ `;
197
+
198
+ exports[`ts#agent generator > chat scripts for ag-ui protocol > should match snapshot for chat scripts with cognito auth > agentcore.ts (ag-ui, cognito) 1`] = `
199
+ "// Resolves the deployed TestProjectAgent agent from runtime config and authenticates requests to it.
200
+ import { randomUUID } from 'node:crypto';
201
+ import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
202
+
203
+ const SESSION_HEADER = 'X-Amzn-Bedrock-AgentCore-Runtime-Session-Id';
204
+
205
+ // AgentCore session ids must be at least 33 characters.
206
+ export const SESSION_ID = randomUUID().replaceAll('-', '').padEnd(33, '0');
207
+
208
+ export interface RemoteAgent {
209
+ /** ARN of the deployed Bedrock AgentCore runtime. */
210
+ arn: string;
211
+ /** AWS region parsed from the runtime ARN. */
212
+ region: string;
213
+ }
214
+
215
+ // Returns the deployed agent when \`RUNTIME_CONFIG_APP_ID\` is set, otherwise \`undefined\` to chat locally.
216
+ export const resolveRemoteAgent = async (): Promise<
217
+ RemoteAgent | undefined
218
+ > => {
219
+ const application = process.env.RUNTIME_CONFIG_APP_ID;
220
+ if (!application) {
221
+ return undefined;
222
+ }
223
+ const config = (await getAppConfig('agentcore', {
224
+ application,
225
+ environment: 'default',
226
+ transform: 'json',
227
+ })) as { agentRuntimes?: Record<string, string> };
228
+ const arn = config?.agentRuntimes?.['TestProjectAgent'];
229
+ if (!arn) {
230
+ throw new Error(
231
+ \`No deployed agent named 'TestProjectAgent' found in runtime configuration (application \${application}).\`,
232
+ );
233
+ }
234
+ return { arn, region: arn.split(':')[3] };
235
+ };
236
+
237
+ /** The Cognito access token used to authenticate with the deployed agent. */
238
+ export const getAccessToken = (): string => {
239
+ const accessToken = process.env.AGENT_ACCESS_TOKEN;
240
+ if (!accessToken) {
241
+ throw new Error(
242
+ 'AGENT_ACCESS_TOKEN is not set. Provide a Cognito access token to chat with the deployed agent.',
243
+ );
244
+ }
245
+ return accessToken;
246
+ };
247
+
248
+ // A \`fetch\` that authenticates requests to the deployed agent and forwards the session id.
249
+ export const createAgentCoreFetch = (): typeof fetch => {
250
+ const accessToken = getAccessToken();
251
+ return (input, init) => {
252
+ const headers = new Headers(init?.headers);
253
+ headers.set(SESSION_HEADER, SESSION_ID);
254
+ headers.set('Authorization', \`Bearer \${accessToken}\`);
255
+ return fetch(input, { ...init, headers });
256
+ };
257
+ };
258
+ "
259
+ `;
260
+
261
+ exports[`ts#agent generator > chat scripts for ag-ui protocol > should match snapshot for chat scripts with cognito auth > chat.ts (ag-ui, cognito) 1`] = `
262
+ "// Chat CLI for TestProjectAgent (AG-UI protocol). Connects to the local
263
+ // \`serve-local\` server, or the deployed agent when \`RUNTIME_CONFIG_APP_ID\` is set.
264
+ import { AGUIChatAdapter, chatLoop } from 'agent-chat-cli';
265
+ import { createAgentCoreFetch, resolveRemoteAgent } from './agentcore.js';
266
+
267
+ const remote = await resolveRemoteAgent();
268
+
269
+ const url = remote
270
+ ? \`https://bedrock-agentcore.\${remote.region}.amazonaws.com/runtimes/\${encodeURIComponent(remote.arn)}/invocations?qualifier=DEFAULT\`
271
+ : process.env.URL!;
272
+
273
+ // Remote requests are authenticated via a custom fetch; locally the default agent talks plain HTTP.
274
+ const fetchImpl = remote ? createAgentCoreFetch() : undefined;
275
+
276
+ await chatLoop(new AGUIChatAdapter({ fetch: fetchImpl }), url);
277
+ "
278
+ `;
279
+
280
+ exports[`ts#agent generator > chat scripts for ag-ui protocol > should match snapshot for chat scripts with iam auth > agentcore.ts (ag-ui, iam) 1`] = `
281
+ "// Resolves the deployed TestProjectAgent agent from runtime config and authenticates requests to it.
282
+ import { randomUUID } from 'node:crypto';
283
+ import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
284
+ import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
285
+ import { AwsClient } from 'aws4fetch';
286
+
287
+ const SESSION_HEADER = 'X-Amzn-Bedrock-AgentCore-Runtime-Session-Id';
288
+
289
+ // AgentCore session ids must be at least 33 characters.
290
+ export const SESSION_ID = randomUUID().replaceAll('-', '').padEnd(33, '0');
291
+
292
+ export interface RemoteAgent {
293
+ /** ARN of the deployed Bedrock AgentCore runtime. */
294
+ arn: string;
295
+ /** AWS region parsed from the runtime ARN. */
296
+ region: string;
297
+ }
298
+
299
+ // Returns the deployed agent when \`RUNTIME_CONFIG_APP_ID\` is set, otherwise \`undefined\` to chat locally.
300
+ export const resolveRemoteAgent = async (): Promise<
301
+ RemoteAgent | undefined
302
+ > => {
303
+ const application = process.env.RUNTIME_CONFIG_APP_ID;
304
+ if (!application) {
305
+ return undefined;
306
+ }
307
+ const config = (await getAppConfig('agentcore', {
308
+ application,
309
+ environment: 'default',
310
+ transform: 'json',
311
+ })) as { agentRuntimes?: Record<string, string> };
312
+ const arn = config?.agentRuntimes?.['TestProjectAgent'];
313
+ if (!arn) {
314
+ throw new Error(
315
+ \`No deployed agent named 'TestProjectAgent' found in runtime configuration (application \${application}).\`,
316
+ );
317
+ }
318
+ return { arn, region: arn.split(':')[3] };
319
+ };
320
+
321
+ // A \`fetch\` that authenticates requests to the deployed agent and forwards the session id.
322
+ export const createAgentCoreFetch = (region: string): typeof fetch => {
323
+ const credentialProvider = fromNodeProviderChain();
324
+ return async (input, init) => {
325
+ const headers = new Headers(init?.headers);
326
+ headers.set(SESSION_HEADER, SESSION_ID);
327
+ const client = new AwsClient({
328
+ ...(await credentialProvider()),
329
+ service: 'bedrock-agentcore',
330
+ region,
331
+ });
332
+ return client.fetch(input, { ...init, headers });
333
+ };
334
+ };
335
+ "
336
+ `;
337
+
338
+ exports[`ts#agent generator > chat scripts for ag-ui protocol > should match snapshot for chat scripts with iam auth > chat.ts (ag-ui, iam) 1`] = `
339
+ "// Chat CLI for TestProjectAgent (AG-UI protocol). Connects to the local
340
+ // \`serve-local\` server, or the deployed agent when \`RUNTIME_CONFIG_APP_ID\` is set.
341
+ import { AGUIChatAdapter, chatLoop } from 'agent-chat-cli';
342
+ import { createAgentCoreFetch, resolveRemoteAgent } from './agentcore.js';
343
+
344
+ const remote = await resolveRemoteAgent();
345
+
346
+ const url = remote
347
+ ? \`https://bedrock-agentcore.\${remote.region}.amazonaws.com/runtimes/\${encodeURIComponent(remote.arn)}/invocations?qualifier=DEFAULT\`
348
+ : process.env.URL!;
349
+
350
+ // Remote requests are authenticated via a custom fetch; locally the default agent talks plain HTTP.
351
+ const fetchImpl = remote ? createAgentCoreFetch(remote.region) : undefined;
352
+
353
+ await chatLoop(new AGUIChatAdapter({ fetch: fetchImpl }), url);
354
+ "
355
+ `;
356
+
357
+ exports[`ts#agent generator > chat scripts for http protocol > should match snapshot for chat scripts with cognito auth > agentcore.ts (http, cognito) 1`] = `
358
+ "// Resolves the deployed TestProjectAgent agent from runtime config and authenticates requests to it.
359
+ import { randomUUID } from 'node:crypto';
360
+ import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
361
+
362
+ const SESSION_HEADER = 'X-Amzn-Bedrock-AgentCore-Runtime-Session-Id';
363
+
364
+ // AgentCore session ids must be at least 33 characters.
365
+ export const SESSION_ID = randomUUID().replaceAll('-', '').padEnd(33, '0');
366
+
367
+ export interface RemoteAgent {
368
+ /** ARN of the deployed Bedrock AgentCore runtime. */
369
+ arn: string;
370
+ /** AWS region parsed from the runtime ARN. */
371
+ region: string;
372
+ }
373
+
374
+ // Returns the deployed agent when \`RUNTIME_CONFIG_APP_ID\` is set, otherwise \`undefined\` to chat locally.
375
+ export const resolveRemoteAgent = async (): Promise<
376
+ RemoteAgent | undefined
377
+ > => {
378
+ const application = process.env.RUNTIME_CONFIG_APP_ID;
379
+ if (!application) {
380
+ return undefined;
381
+ }
382
+ const config = (await getAppConfig('agentcore', {
383
+ application,
384
+ environment: 'default',
385
+ transform: 'json',
386
+ })) as { agentRuntimes?: Record<string, string> };
387
+ const arn = config?.agentRuntimes?.['TestProjectAgent'];
388
+ if (!arn) {
389
+ throw new Error(
390
+ \`No deployed agent named 'TestProjectAgent' found in runtime configuration (application \${application}).\`,
391
+ );
392
+ }
393
+ return { arn, region: arn.split(':')[3] };
394
+ };
395
+
396
+ /** The Cognito access token used to authenticate with the deployed agent. */
397
+ export const getAccessToken = (): string => {
398
+ const accessToken = process.env.AGENT_ACCESS_TOKEN;
399
+ if (!accessToken) {
400
+ throw new Error(
401
+ 'AGENT_ACCESS_TOKEN is not set. Provide a Cognito access token to chat with the deployed agent.',
402
+ );
403
+ }
404
+ return accessToken;
405
+ };
406
+
407
+ // A \`fetch\` that authenticates requests to the deployed agent and forwards the session id.
408
+ export const createAgentCoreFetch = (): typeof fetch => {
409
+ const accessToken = getAccessToken();
410
+ return (input, init) => {
411
+ const headers = new Headers(init?.headers);
412
+ headers.set(SESSION_HEADER, SESSION_ID);
413
+ headers.set('Authorization', \`Bearer \${accessToken}\`);
414
+ return fetch(input, { ...init, headers });
415
+ };
416
+ };
417
+ "
418
+ `;
419
+
420
+ exports[`ts#agent generator > chat scripts for http protocol > should match snapshot for chat scripts with cognito auth > chat.ts (http, cognito) 1`] = `
421
+ "// Chat CLI for TestProjectAgent (HTTP / tRPC WebSocket). Connects to the local
422
+ // \`serve-local\` server, or the deployed agent when \`RUNTIME_CONFIG_APP_ID\` is set.
423
+ import { chatLoop, type ChatAdapter } from 'agent-chat-cli';
424
+ import { TestProjectAgentClient } from '../../src/agent/client.js';
425
+ import { resolveRemoteAgent, SESSION_ID, getAccessToken } from './agentcore.js';
426
+
427
+ const remote = await resolveRemoteAgent();
428
+
429
+ class TrpcWebSocketAdapter implements ChatAdapter {
430
+ private client!: ReturnType<typeof TestProjectAgentClient.local>;
431
+
432
+ async connect(url: string) {
433
+ this.client = remote
434
+ ? TestProjectAgentClient.withJwtAuth({
435
+ agentRuntimeArn: remote.arn,
436
+ sessionId: SESSION_ID,
437
+ accessTokenProvider: async () => getAccessToken(),
438
+ })
439
+ : TestProjectAgentClient.local({ url, sessionId: SESSION_ID });
440
+ return { agentName: 'TestProjectAgent' };
441
+ }
442
+
443
+ async *sendMessage(text: string): AsyncIterable<string> {
444
+ const stream = new ReadableStream<string>({
445
+ start: (controller) => {
446
+ this.client.invoke.subscribe(
447
+ { message: text },
448
+ {
449
+ onData: (chunk: string) => controller.enqueue(chunk),
450
+ onComplete: () => controller.close(),
451
+ onError: (err: unknown) => controller.error(err),
452
+ },
453
+ );
454
+ },
455
+ });
456
+
457
+ yield* stream as unknown as AsyncIterable<string>;
458
+ }
459
+ }
460
+
461
+ await chatLoop(new TrpcWebSocketAdapter(), process.env.URL ?? '');
462
+ "
463
+ `;
464
+
465
+ exports[`ts#agent generator > chat scripts for http protocol > should match snapshot for chat scripts with iam auth > agentcore.ts (http, iam) 1`] = `
466
+ "// Resolves the deployed TestProjectAgent agent from runtime config and authenticates requests to it.
467
+ import { randomUUID } from 'node:crypto';
468
+ import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
469
+ import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
470
+ import { AwsClient } from 'aws4fetch';
471
+
472
+ const SESSION_HEADER = 'X-Amzn-Bedrock-AgentCore-Runtime-Session-Id';
473
+
474
+ // AgentCore session ids must be at least 33 characters.
475
+ export const SESSION_ID = randomUUID().replaceAll('-', '').padEnd(33, '0');
476
+
477
+ export interface RemoteAgent {
478
+ /** ARN of the deployed Bedrock AgentCore runtime. */
479
+ arn: string;
480
+ /** AWS region parsed from the runtime ARN. */
481
+ region: string;
482
+ }
483
+
484
+ // Returns the deployed agent when \`RUNTIME_CONFIG_APP_ID\` is set, otherwise \`undefined\` to chat locally.
485
+ export const resolveRemoteAgent = async (): Promise<
486
+ RemoteAgent | undefined
487
+ > => {
488
+ const application = process.env.RUNTIME_CONFIG_APP_ID;
489
+ if (!application) {
490
+ return undefined;
491
+ }
492
+ const config = (await getAppConfig('agentcore', {
493
+ application,
494
+ environment: 'default',
495
+ transform: 'json',
496
+ })) as { agentRuntimes?: Record<string, string> };
497
+ const arn = config?.agentRuntimes?.['TestProjectAgent'];
498
+ if (!arn) {
499
+ throw new Error(
500
+ \`No deployed agent named 'TestProjectAgent' found in runtime configuration (application \${application}).\`,
501
+ );
502
+ }
503
+ return { arn, region: arn.split(':')[3] };
504
+ };
505
+
506
+ // A \`fetch\` that authenticates requests to the deployed agent and forwards the session id.
507
+ export const createAgentCoreFetch = (region: string): typeof fetch => {
508
+ const credentialProvider = fromNodeProviderChain();
509
+ return async (input, init) => {
510
+ const headers = new Headers(init?.headers);
511
+ headers.set(SESSION_HEADER, SESSION_ID);
512
+ const client = new AwsClient({
513
+ ...(await credentialProvider()),
514
+ service: 'bedrock-agentcore',
515
+ region,
516
+ });
517
+ return client.fetch(input, { ...init, headers });
518
+ };
519
+ };
520
+ "
521
+ `;
522
+
523
+ exports[`ts#agent generator > chat scripts for http protocol > should match snapshot for chat scripts with iam auth > chat.ts (http, iam) 1`] = `
524
+ "// Chat CLI for TestProjectAgent (HTTP / tRPC WebSocket). Connects to the local
525
+ // \`serve-local\` server, or the deployed agent when \`RUNTIME_CONFIG_APP_ID\` is set.
526
+ import { chatLoop, type ChatAdapter } from 'agent-chat-cli';
527
+ import { TestProjectAgentClient } from '../../src/agent/client.js';
528
+ import { resolveRemoteAgent, SESSION_ID } from './agentcore.js';
529
+
530
+ const remote = await resolveRemoteAgent();
531
+
532
+ class TrpcWebSocketAdapter implements ChatAdapter {
533
+ private client!: ReturnType<typeof TestProjectAgentClient.local>;
534
+
535
+ async connect(url: string) {
536
+ this.client = remote
537
+ ? TestProjectAgentClient.withIamAuth({
538
+ agentRuntimeArn: remote.arn,
539
+ sessionId: SESSION_ID,
540
+ })
541
+ : TestProjectAgentClient.local({ url, sessionId: SESSION_ID });
542
+ return { agentName: 'TestProjectAgent' };
543
+ }
544
+
545
+ async *sendMessage(text: string): AsyncIterable<string> {
546
+ const stream = new ReadableStream<string>({
547
+ start: (controller) => {
548
+ this.client.invoke.subscribe(
549
+ { message: text },
550
+ {
551
+ onData: (chunk: string) => controller.enqueue(chunk),
552
+ onComplete: () => controller.close(),
553
+ onError: (err: unknown) => controller.error(err),
554
+ },
555
+ );
556
+ },
557
+ });
558
+
559
+ yield* stream as unknown as AsyncIterable<string>;
560
+ }
561
+ }
562
+
563
+ await chatLoop(new TrpcWebSocketAdapter(), process.env.URL ?? '');
564
+ "
565
+ `;
566
+
3
567
  exports[`ts#agent generator > should match snapshot for BedrockAgentCoreRuntime generated constructs files > agent-Dockerfile 1`] = `
4
568
  "FROM public.ecr.aws/docker/library/node:lts-jod
5
569
 
@@ -594,6 +1158,59 @@ resource "aws_bedrockagentcore_agent_runtime" "agent_runtime" {
594
1158
  ]
595
1159
  }
596
1160
 
1161
+ # MCP consumers (e.g. gateway targets, which fetch tools at creation) can race
1162
+ # the runtime's first cold start, so probe it until it serves MCP. The ARN
1163
+ # output is routed through this resource so consumers wait for readiness.
1164
+ resource "null_resource" "runtime_ready" {
1165
+ count = var.server_protocol == "MCP" ? 1 : 0
1166
+
1167
+ triggers = {
1168
+ runtime_arn = aws_bedrockagentcore_agent_runtime.agent_runtime.agent_runtime_arn
1169
+ }
1170
+
1171
+ provisioner "local-exec" {
1172
+ command = <<-EOT
1173
+ uv run --with boto3 python -c "
1174
+ import boto3
1175
+ import json
1176
+ import time
1177
+ import uuid
1178
+
1179
+ runtime_arn = '\${self.triggers.runtime_arn}'
1180
+ client = boto3.client('bedrock-agentcore')
1181
+ initialize_request = json.dumps({
1182
+ 'jsonrpc': '2.0',
1183
+ 'id': 1,
1184
+ 'method': 'initialize',
1185
+ 'params': {
1186
+ 'protocolVersion': '2025-03-26',
1187
+ 'capabilities': {},
1188
+ 'clientInfo': {'name': 'readiness-probe', 'version': '1.0'},
1189
+ },
1190
+ })
1191
+
1192
+ deadline = time.time() + 3 * 60
1193
+ while True:
1194
+ try:
1195
+ client.invoke_agent_runtime(
1196
+ agentRuntimeArn=runtime_arn,
1197
+ runtimeSessionId=f'readiness-probe-{uuid.uuid4()}',
1198
+ contentType='application/json',
1199
+ accept='application/json, text/event-stream',
1200
+ payload=initialize_request,
1201
+ )
1202
+ break
1203
+ except Exception:
1204
+ if time.time() >= deadline:
1205
+ raise
1206
+ time.sleep(5)
1207
+
1208
+ print(f'Runtime {runtime_arn} is serving MCP')
1209
+ "
1210
+ EOT
1211
+ }
1212
+ }
1213
+
597
1214
  # Outputs
598
1215
  output "agent_core_runtime_role_arn" {
599
1216
  description = "ARN of the Agent Core Runtime IAM role"
@@ -611,8 +1228,8 @@ output "agent_runtime_name" {
611
1228
  }
612
1229
 
613
1230
  output "agent_core_runtime_arn" {
614
- description = "ARN of the Bedrock Agent Core runtime"
615
- value = aws_bedrockagentcore_agent_runtime.agent_runtime.agent_runtime_arn
1231
+ description = "ARN of the Bedrock Agent Core runtime. For MCP runtimes the value flows through the readiness probe, so consumers wait until the runtime serves MCP."
1232
+ value = var.server_protocol == "MCP" ? null_resource.runtime_ready[0].triggers.runtime_arn : aws_bedrockagentcore_agent_runtime.agent_runtime.agent_runtime_arn
616
1233
  }
617
1234
 
618
1235
  output "agent_runtime_id" {
@@ -1,6 +1,6 @@
1
1
  import { A2AAgent } from '@strands-agents/sdk/a2a';
2
2
  import { AgentCoreA2aClient } from '../core/agentcore-a2a-client.js';
3
- import { getConnectedAgentRuntimeArn } from '../core/runtime-config.js';
3
+ import { getAgentCoreRuntimeConfig } from '../core/runtime-config.js';
4
4
 
5
5
  /** Client for the <%- targetAgentClassName %> A2A agent. */
6
6
  export class <%- targetAgentClassName %>Client {
@@ -10,10 +10,14 @@ export class <%- targetAgentClassName %>Client {
10
10
  url: 'http://localhost:<%- targetAgentPort %>/',
11
11
  });
12
12
  }
13
- return AgentCoreA2aClient.withIamAuth({
14
- agentRuntimeArn: await getConnectedAgentRuntimeArn(
15
- '<%- targetAgentClassName %>',
16
- ),
17
- });
13
+ const config = await getAgentCoreRuntimeConfig();
14
+ const agentRuntimeArn =
15
+ config.agentRuntimes?.['<%- targetAgentClassName %>'];
16
+ if (!agentRuntimeArn) {
17
+ throw new Error(
18
+ `No connected agent runtime named '<%- targetAgentClassName %>' found in runtime configuration.`,
19
+ );
20
+ }
21
+ return AgentCoreA2aClient.withIamAuth({ agentRuntimeArn });
18
22
  }
19
23
  }
@@ -0,0 +1,30 @@
1
+ import { McpClient } from '@strands-agents/sdk';
2
+ import { AgentCoreGatewayMcpClient } from '../core/agentcore-gateway-mcp-client.js';
3
+ import { getAgentCoreRuntimeConfig } from '../core/runtime-config.js';
4
+
5
+ /**
6
+ * Client for the <%- gatewayClassName %> AgentCore Gateway.
7
+ *
8
+ * Deployed: a SigV4-signed MCP client pointing at the gateway URL.
9
+ * Local (`SERVE_LOCAL=true`): a plain-HTTP client pointing at the local
10
+ * gateway started by the gateway project's serve-local target. The local
11
+ * gateway aggregates every attached MCP server but does not evaluate
12
+ * Cedar policies.
13
+ */
14
+ export class <%- gatewayClassName %>Client {
15
+ static async create(): Promise<McpClient> {
16
+ if (process.env.SERVE_LOCAL === 'true') {
17
+ return AgentCoreGatewayMcpClient.withoutAuth({
18
+ gatewayUrl: 'http://localhost:<%- gatewayPort %>/mcp',
19
+ });
20
+ }
21
+ const config = await getAgentCoreRuntimeConfig();
22
+ const gatewayUrl = config.gateways?.['<%- gatewayClassName %>'];
23
+ if (!gatewayUrl) {
24
+ throw new Error(
25
+ `No connected gateway named '<%- gatewayClassName %>' found in runtime configuration.`,
26
+ );
27
+ }
28
+ return AgentCoreGatewayMcpClient.withIamAuth({ gatewayUrl });
29
+ }
30
+ }