Skip to content

Commit 8b6d3c7

Browse files
committed
Replace mastra to Strands Agents for AgentCore Runtime example
1 parent 2aa17c9 commit 8b6d3c7

17 files changed

Lines changed: 323 additions & 4412 deletions

File tree

amplify/amplify-cdk/app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"@mui/material": "^9.0.0",
2222
"autoprefixer": "^10.5.0",
2323
"next": "^16.2.4",
24-
"postcss": "^8.5.13",
24+
"postcss": "^8",
2525
"react": "^19.2.5",
2626
"react-dom": "^19.2.5",
2727
"tailwindcss": "^4.2.4",

bedrock/agentcore-runtime-agent-example/app/Dockerfile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ COPY package.json ./
1212
RUN npx -y pnpm@latest i
1313
COPY . ./
1414

15-
RUN npx -y bun@latest run mastra build
15+
RUN npx -y bun@latest run tsc
1616

1717

1818
FROM --platform=$BUILDPLATFORM node:$NODE_VERSION-slim AS dev
@@ -28,10 +28,17 @@ ENV PORT=8080
2828

2929
ENTRYPOINT ["npx", "-y", "pnpm", "dev"]
3030

31+
FROM --platform=$BUILDPLATFORM node:$NODE_VERSION-slim AS base
3132

32-
FROM --platform=$BUILDPLATFORM node:$NODE_VERSION-slim
33+
WORKDIR /var/task
34+
COPY package.json ./
35+
36+
RUN npx -y pnpm@latest i --production
37+
RUN rm -rf package.json
38+
39+
FROM --platform=$BUILDPLATFORM base
3340

34-
COPY --from=build /build /var/task
41+
COPY --from=build /build/dist /var/task
3542

3643
# Change ownership to non-root user
3744
RUN chown -R node:node /var/task
@@ -43,4 +50,4 @@ USER node
4350

4451
ENV PORT=8080
4552

46-
ENTRYPOINT [ "node", "--import=./.mastra/output/instrumentation.mjs", ".mastra/output/index.mjs" ]
53+
ENTRYPOINT [ "node", "./index.js" ]

bedrock/agentcore-runtime-agent-example/app/package.json

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,38 @@
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
88
"clean": "tsc --build --clean",
9-
"dev": "mastra dev",
10-
"build": "mastra build",
11-
"start": "mastra start",
9+
"dev": "tsx src/index.ts",
10+
"build": "tsc",
11+
"start": "node dist/index.js",
1212
"lint": "eslint .",
1313
"lint-fix": "eslint . --fix"
1414
},
1515
"keywords": [],
1616
"author": "",
17-
"license": "ISC",
1817
"type": "module",
1918
"engines": {
2019
"node": ">=24.0.0"
2120
},
2221
"dependencies": {
2322
"@ai-sdk/amazon-bedrock": "^4.0.96",
2423
"@aws-sdk/credential-providers": "^3.1041.0",
25-
"@aws/aws-distro-opentelemetry-node-autoinstrumentation": "^0.10.0",
26-
"@mastra/core": "^1.28.0",
27-
"@mastra/duckdb": "^1.2.0",
28-
"@mastra/evals": "^1.2.1",
29-
"@mastra/libsql": "^1.9.0",
30-
"@mastra/loggers": "^1.1.1",
31-
"@mastra/mcp": "^1.5.2",
32-
"@mastra/memory": "^1.17.1",
33-
"@mastra/observability": "^1.10.0",
34-
"@mastra/otel-exporter": "^1.0.17",
24+
"@hono/node-server": "^1.19.14",
25+
"@modelcontextprotocol/sdk": "^1.29.0",
26+
"@strands-agents/sdk": "^0.7.0",
3527
"hono": "^4.12.16",
3628
"zod": "^4.4.2"
3729
},
3830
"devDependencies": {
3931
"@eslint/compat": "^2.0.5",
4032
"@eslint/js": "^10.0.1",
41-
"@mastra/deployer": "^1.28.0",
4233
"@stylistic/eslint-plugin": "^5.10.0",
4334
"@types/node": "^24.12.2",
4435
"eslint": "^10.3.0",
4536
"eslint-import-resolver-typescript": "^4.4.4",
4637
"eslint-plugin-import-x": "^4.16.2",
4738
"eslint-plugin-promise": "^7.3.0",
4839
"jiti": "^2.6.1",
49-
"mastra": "^1.6.3",
40+
"tsx": "^4.21.0",
5041
"typescript": "^6.0.3",
5142
"typescript-eslint": "^8.59.1"
5243
}

bedrock/agentcore-runtime-agent-example/app/src/mastra/agents/aws-agent.ts renamed to bedrock/agentcore-runtime-agent-example/app/src/agents/aws-agent.ts

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import { tools } from '../tools/aws-tool.js';
2-
import { scorers } from '../scorers/aws-scorer.js';
3-
import { Agent } from '@mastra/core/agent';
4-
import { Memory } from '@mastra/memory';
5-
import { createAmazonBedrock } from '@ai-sdk/amazon-bedrock';
6-
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
2+
import { Agent, BedrockModel } from '@strands-agents/sdk';
73

8-
const bedrock = createAmazonBedrock({
4+
const bedrockModel = new BedrockModel({
5+
modelId: 'global.amazon.nova-2-lite-v1:0',
96
region: 'us-east-1',
10-
credentialProvider: fromNodeProviderChain(),
7+
temperature: 0.3,
118
});
12-
139
export const awsAgent = new Agent({
1410
id: 'aws-agent',
1511
name: 'AWS Agent',
16-
instructions: `
12+
systemPrompt: `
1713
You are an assistant that helps architects design systems using Amazon Web Services (AWS). Your primary function is to answer user questions based on AWS knowledge and propose system architectures. When responding, follow these guidelines:
1814
1915
- If information is not available in aws-knowledge-mcp-server, clearly state that you don't know
@@ -25,30 +21,6 @@ export const awsAgent = new Agent({
2521
- For non-AWS information, use Context7 to resolve queries with up-to-date information
2622
- If Context7 cannot resolve the query, clearly state that you don't know
2723
`,
28-
model: bedrock('global.amazon.nova-2-lite-v1:0'),
24+
model: bedrockModel,
2925
tools,
30-
scorers: {
31-
toolCallAppropriateness: {
32-
scorer: scorers.toolCallAppropriatenessScorer,
33-
sampling: {
34-
type: 'ratio',
35-
rate: 1,
36-
},
37-
},
38-
completeness: {
39-
scorer: scorers.completenessScorer,
40-
sampling: {
41-
type: 'ratio',
42-
rate: 1,
43-
},
44-
},
45-
translation: {
46-
scorer: scorers.translationScorer,
47-
sampling: {
48-
type: 'ratio',
49-
rate: 1,
50-
},
51-
},
52-
},
53-
memory: new Memory(),
5426
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
import { awsAgent } from './agents/aws-agent.js';
3+
import { mcp } from './tools/aws-tool.js';
4+
import { Hono } from 'hono';
5+
import { streamSSE } from 'hono/streaming';
6+
7+
export const app = new Hono();
8+
9+
app.get('/ping', (c) =>
10+
c.json({
11+
status: 'Healthy',
12+
time_of_last_update: Math.floor(Date.now() / 1000),
13+
}),
14+
);
15+
16+
app.post('/invocations', async (c) => {
17+
const body = await c.req.json();
18+
// Input validation
19+
if (!body || typeof body !== 'object') {
20+
return c.json({ error: 'Invalid request body' }, 400);
21+
}
22+
23+
const { prompt } = body;
24+
25+
if (!prompt || typeof prompt !== 'string') {
26+
return c.json({ error: 'prompt is required and must be a string' }, 400);
27+
}
28+
29+
if (prompt.length > 10000) { // Reasonable limit
30+
return c.json({ error: 'prompt is too long' }, 400);
31+
}
32+
33+
// ストリーミングレスポンス
34+
try {
35+
const streamResult = await awsAgent.stream(prompt);
36+
for await (const event of streamResult) {
37+
return streamSSE(c, async (stream) => {
38+
if (
39+
event.type === 'modelStreamUpdateEvent' &&
40+
event.event.type === 'modelContentBlockDeltaEvent' &&
41+
event.event.delta.type === 'textDelta'
42+
) {
43+
await stream.write(event.event.delta.text);
44+
}
45+
});
46+
}
47+
} catch (e) {
48+
await mcp.disconnect();
49+
throw e;
50+
}
51+
},
52+
);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { app } from './app.js';
2+
import { serve } from '@hono/node-server';
3+
4+
const PORT = Number.parseInt(process.env.PORT ?? '8080');
5+
6+
const server = serve(
7+
{ ...app, port: PORT }, (info) => {
8+
console.log(`Server is running on http://localhost:${info.port}`);
9+
});
10+
11+
// graceful shutdown
12+
process.on('SIGINT', () => {
13+
server.close();
14+
process.exit(0);
15+
});
16+
process.on('SIGTERM', () => {
17+
server.close((err) => {
18+
if (err) {
19+
console.error(err);
20+
process.exit(1);
21+
}
22+
process.exit(0);
23+
});
24+
});

bedrock/agentcore-runtime-agent-example/app/src/mastra/deployer.ts

Lines changed: 0 additions & 140 deletions
This file was deleted.

0 commit comments

Comments
 (0)