Skip to content

Commit 398dc50

Browse files
authored
feat: add agentcore-cli User-Agent to all API calls (#960)
1 parent 27c47ca commit 398dc50

8 files changed

Lines changed: 27 additions & 36 deletions

File tree

src/cli/aws/agentcore-control.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { EvaluationLevel } from '../../schema/schemas/primitives/evaluator';
2+
import { PACKAGE_VERSION } from '../constants';
23
import { getCredentialProvider } from './account';
34
import {
45
BedrockAgentCoreControlClient,
@@ -23,6 +24,7 @@ export function createControlClient(region: string): BedrockAgentCoreControlClie
2324
return new BedrockAgentCoreControlClient({
2425
region,
2526
credentials: getCredentialProvider(),
27+
customUserAgent: [['agentcore-cli', PACKAGE_VERSION]],
2628
});
2729
}
2830

src/cli/aws/agentcore.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { parseJsonRpcResponse } from '../../lib/utils/json-rpc';
2+
import { PACKAGE_VERSION } from '../constants';
23
import { getCredentialProvider } from './account';
34
import { parseAguiSSEStream } from './agui-parser';
45
import { serviceEndpoint } from './partition';
@@ -23,11 +24,12 @@ function resolveDataPlaneEndpoint(region: string): string | undefined {
2324
return undefined;
2425
}
2526

26-
function createAgentCoreClient(region: string, headers?: Record<string, string>): BedrockAgentCoreClient {
27+
export function createAgentCoreClient(region: string, headers?: Record<string, string>): BedrockAgentCoreClient {
2728
const endpoint = resolveDataPlaneEndpoint(region);
2829
const client = new BedrockAgentCoreClient({
2930
region,
3031
credentials: getCredentialProvider(),
32+
customUserAgent: [['agentcore-cli', PACKAGE_VERSION]],
3133
...(endpoint && { endpoint }),
3234
});
3335

@@ -473,6 +475,7 @@ export async function evaluate(options: EvaluateOptions): Promise<EvaluateResult
473475
const client = new BedrockAgentCoreClient({
474476
region: options.region,
475477
credentials: getCredentialProvider(),
478+
customUserAgent: [['agentcore-cli', PACKAGE_VERSION]],
476479
});
477480

478481
const evaluationTarget = options.targetSpanIds
@@ -1066,6 +1069,7 @@ export async function stopRuntimeSession(options: StopRuntimeSessionOptions): Pr
10661069
const client = new BedrockAgentCoreClient({
10671070
region: options.region,
10681071
credentials: getCredentialProvider(),
1072+
customUserAgent: [['agentcore-cli', PACKAGE_VERSION]],
10691073
});
10701074

10711075
const command = new StopRuntimeSessionCommand({

src/cli/aws/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export {
5656
type InvokeHarnessOptions,
5757
} from './agentcore-harness';
5858
export {
59+
createAgentCoreClient,
5960
DEFAULT_RUNTIME_USER_ID,
6061
executeBashCommand,
6162
invokeA2ARuntime,

src/cli/aws/policy-generation.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { getCredentialProvider } from './account';
1+
import { createControlClient } from './agentcore-control';
22
import {
3-
BedrockAgentCoreControlClient,
43
GetPolicyGenerationCommand,
54
ListPolicyGenerationAssetsCommand,
65
StartPolicyGenerationCommand,
@@ -33,10 +32,7 @@ export interface GetPolicyGenerationResult {
3332
export async function startPolicyGeneration(
3433
options: StartPolicyGenerationOptions
3534
): Promise<StartPolicyGenerationResult> {
36-
const client = new BedrockAgentCoreControlClient({
37-
region: options.region,
38-
credentials: getCredentialProvider(),
39-
});
35+
const client = createControlClient(options.region);
4036

4137
const command = new StartPolicyGenerationCommand({
4238
policyEngineId: options.policyEngineId,
@@ -57,10 +53,7 @@ export async function startPolicyGeneration(
5753
}
5854

5955
export async function getPolicyGeneration(options: GetPolicyGenerationOptions): Promise<GetPolicyGenerationResult> {
60-
const client = new BedrockAgentCoreControlClient({
61-
region: options.region,
62-
credentials: getCredentialProvider(),
63-
});
56+
const client = createControlClient(options.region);
6457

6558
// Use the SDK waiter to poll until generation completes
6659
const waiterResult = await waitUntilPolicyGenerationCompleted(

src/cli/operations/deploy/gateway-status.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* Query gateway target sync statuses after deployment.
33
*/
4-
import { BedrockAgentCoreControlClient, ListGatewayTargetsCommand } from '@aws-sdk/client-bedrock-agentcore-control';
4+
import { createControlClient } from '../../aws/agentcore-control';
5+
import { ListGatewayTargetsCommand } from '@aws-sdk/client-bedrock-agentcore-control';
56

67
export interface TargetSyncStatus {
78
name: string;
@@ -29,7 +30,7 @@ export function formatTargetStatus(status: string): string {
2930
*/
3031
export async function getGatewayTargetStatuses(gatewayId: string, region: string): Promise<TargetSyncStatus[]> {
3132
try {
32-
const client = new BedrockAgentCoreControlClient({ region });
33+
const client = createControlClient(region);
3334
const response = await client.send(
3435
new ListGatewayTargetsCommand({ gatewayIdentifier: gatewayId, maxResults: 100 })
3536
);

src/cli/operations/deploy/pre-deploy-identity.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SecureCredentials, readEnvFile } from '../../../lib';
22
import type { AgentCoreProjectSpec, Credential } from '../../../schema';
3-
import { getCredentialProvider } from '../../aws';
3+
import { createControlClient, getCredentialProvider } from '../../aws';
44
import { isNoCredentialsError } from '../../errors';
55
import { getAwsLoginGuidance } from '../../external-requirements/checks';
66
import { computeDefaultCredentialEnvVarName } from '../../primitives/credential-utils';
@@ -13,7 +13,7 @@ import {
1313
updateApiKeyProvider,
1414
updateOAuth2Provider,
1515
} from '../identity';
16-
import { BedrockAgentCoreControlClient, GetTokenVaultCommand } from '@aws-sdk/client-bedrock-agentcore-control';
16+
import { type BedrockAgentCoreControlClient, GetTokenVaultCommand } from '@aws-sdk/client-bedrock-agentcore-control';
1717
import { CreateKeyCommand, KMSClient } from '@aws-sdk/client-kms';
1818

1919
// ─────────────────────────────────────────────────────────────────────────────
@@ -55,19 +55,17 @@ export interface SetupApiKeyProvidersOptions {
5555
export async function setupApiKeyProviders(options: SetupApiKeyProvidersOptions): Promise<PreDeployIdentityResult> {
5656
const { projectSpec, configBaseDir, region, runtimeCredentials, enableKmsEncryption } = options;
5757
const results: ApiKeyProviderSetupResult[] = [];
58-
const credentials = getCredentialProvider();
59-
6058
const envVars = await readEnvFile(configBaseDir);
6159
// Wrap env vars in SecureCredentials and merge with runtime credentials
6260
const envCredentials = SecureCredentials.fromEnvVars(envVars);
6361
const allCredentials = runtimeCredentials ? envCredentials.merge(runtimeCredentials) : envCredentials;
6462

65-
const client = new BedrockAgentCoreControlClient({ region, credentials });
63+
const client = createControlClient(region);
6664

6765
// Configure KMS encryption for token vault if enabled
6866
let kmsKeyArn: string | undefined;
6967
if (enableKmsEncryption) {
70-
const kmsResult = await setupTokenVaultKms(region, credentials, projectSpec);
68+
const kmsResult = await setupTokenVaultKms(region, projectSpec);
7169
if (!kmsResult.success) {
7270
return {
7371
results: [
@@ -100,11 +98,10 @@ export async function setupApiKeyProviders(options: SetupApiKeyProvidersOptions)
10098

10199
async function setupTokenVaultKms(
102100
region: string,
103-
credentials: ReturnType<typeof getCredentialProvider>,
104101
projectSpec: AgentCoreProjectSpec
105102
): Promise<{ success: boolean; keyArn?: string; error?: string }> {
106103
try {
107-
const controlClient = new BedrockAgentCoreControlClient({ region, credentials });
104+
const controlClient = createControlClient(region);
108105

109106
// Check if the token vault already has a customer-managed key
110107
try {
@@ -120,7 +117,7 @@ async function setupTokenVaultKms(
120117
}
121118

122119
// No CMK configured — create a new KMS key and set it on the vault
123-
const kmsClient = new KMSClient({ region, credentials });
120+
const kmsClient = new KMSClient({ region, credentials: getCredentialProvider() });
124121
const response = await kmsClient.send(
125122
new CreateKeyCommand({
126123
Description: `AgentCore Identity encryption key for ${projectSpec.name}`,
@@ -289,13 +286,12 @@ export interface PreDeployOAuth2Result {
289286
export async function setupOAuth2Providers(options: SetupOAuth2ProvidersOptions): Promise<PreDeployOAuth2Result> {
290287
const { projectSpec, configBaseDir, region, runtimeCredentials } = options;
291288
const results: OAuth2ProviderSetupResult[] = [];
292-
const credentials = getCredentialProvider();
293289

294290
const envVars = await readEnvFile(configBaseDir);
295291
const envCredentials = SecureCredentials.fromEnvVars(envVars);
296292
const allCredentials = runtimeCredentials ? envCredentials.merge(runtimeCredentials) : envCredentials;
297293

298-
const client = new BedrockAgentCoreControlClient({ region, credentials });
294+
const client = createControlClient(region);
299295

300296
for (const credential of projectSpec.credentials) {
301297
if (credential.authorizerType === 'OAuthCredentialProvider') {

src/cli/operations/memory/list-memory-records.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getCredentialProvider } from '../../aws';
2-
import { BedrockAgentCoreClient, ListMemoryRecordsCommand } from '@aws-sdk/client-bedrock-agentcore';
1+
import { createAgentCoreClient } from '../../aws';
2+
import { ListMemoryRecordsCommand } from '@aws-sdk/client-bedrock-agentcore';
33

44
export interface MemoryRecordEntry {
55
memoryRecordId: string;
@@ -33,10 +33,7 @@ export interface ListMemoryRecordsResult {
3333
export async function listMemoryRecords(options: ListMemoryRecordsOptions): Promise<ListMemoryRecordsResult> {
3434
const { region, memoryId, namespace, memoryStrategyId, maxResults = 50, nextToken } = options;
3535

36-
const client = new BedrockAgentCoreClient({
37-
region,
38-
credentials: getCredentialProvider(),
39-
});
36+
const client = createAgentCoreClient(region);
4037

4138
try {
4239
const response = await client.send(

src/cli/operations/memory/retrieve-memory-records.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { getCredentialProvider } from '../../aws';
1+
import { createAgentCoreClient } from '../../aws';
22
import type { MemoryRecordEntry } from './list-memory-records';
3-
import { BedrockAgentCoreClient, RetrieveMemoryRecordsCommand } from '@aws-sdk/client-bedrock-agentcore';
3+
import { RetrieveMemoryRecordsCommand } from '@aws-sdk/client-bedrock-agentcore';
44

55
export interface RetrieveMemoryRecordsOptions {
66
region: string;
@@ -28,10 +28,7 @@ export async function retrieveMemoryRecords(
2828
): Promise<RetrieveMemoryRecordsResult> {
2929
const { region, memoryId, namespace, searchQuery, memoryStrategyId, topK, maxResults, nextToken } = options;
3030

31-
const client = new BedrockAgentCoreClient({
32-
region,
33-
credentials: getCredentialProvider(),
34-
});
31+
const client = createAgentCoreClient(region);
3532

3633
try {
3734
const response = await client.send(

0 commit comments

Comments
 (0)