![[アップデート] Strands AgentsでのTypeScriptサポート(プレビュー版)が発表されました #AWSreInvent](https://images.ctfassets.net/ct0aopd36mqt/33a7q65plkoztFWVfWxPWl/a718447bea0d93a2d461000926d65428/reinvent2025_devio_update_w1200h630.png?w=3840&fm=webp)
[アップデート] Strands AgentsでのTypeScriptサポート(プレビュー版)が発表されました #AWSreInvent
こんにちは、筧(@takaakikakei)です。
Strands Agentsは、AWSが提供するエージェントフレームワークです。これまでPythonでの開発が主流でしたが、AWS re:Invent 2025での発表により、TypeScriptでの開発がプレビュー版としてサポートされるようになりました。完全な型安全性とasync/awaitに対応し、最新のパターンを活用したTypeScriptネイティブな開発が可能です。TypeScriptを普段から使用している開発者にとっては嬉しいアップデートです。今回は、この新しいTypeScript SDKを使って簡単なエージェントを作成し、実行してみます。
公式ドキュメント
今回のアップデートに関連する公式ドキュメントは以下の通りです。
やってみた
プロジェクト作成
今回、パッケージ管理にはpnpmを使用します。
pnpm init
ワークスペースを有効にするため、pnpm-workspace.yamlファイルを作成します。
touch pnpm-workspace.yaml
pnpm-workspace.yamlの内容は以下の通りです。minimumReleaseAgeは、パッケージ公開からインストールまでの最低経過時間を分単位で指定できるオプションです。ここでは5日間(7200分)に設定しています。サプライチェーン攻撃などの緩和に役立ちますが、最新のパッケージをすぐに利用できなくなるため、適宜調整してください。
packages:
- 'apps/*'
minimumReleaseAge: 7200
LinterとFormatterには、Biomeを使用します。
pnpm add -D -E @biomejs/biome
pnpm exec biome init
package.jsonに以下のスクリプトを追加します。これにより、pnpm run check:fixコマンドでBiomeのチェックと自動修正が実行できるようになります。
"scripts": {
"check:fix": "biome check --write"
}
Strands Agentsの追加
Strands AgentsのTypeScript SDKと関連ライブラリをインストールします。TypeScript SDKはリリースされたばかりなので、適宜minimumReleaseAgeの設定を調整してください。
pnpm add @strands-agents/sdk zod
pnpm add -D @types/node typescript tsx
apps/agent.tsを作成します。
mkdir apps
touch apps/agent.ts
現在のフォルダ構成は、以下のようになります。
.
├── apps
│ └── agent.ts
├── biome.json
├── node_modules
├── package.json
├── pnpm-lock.yaml
└── pnpm-workspace.yaml
公式ドキュメントの実装を参考に、apps/agent.tsに以下のコードを記述します。
// Define a custom tool as a TypeScript function
import { Agent, tool } from '@strands-agents/sdk'
import z from 'zod'
const letterCounter = tool({
name: 'letter_counter',
description: 'Count occurrences of a specific letter in a word. Performs case-insensitive matching.',
// Zod schema for letter counter input validation
inputSchema: z
.object({
word: z.string().describe('The input word to search in'),
letter: z.string().describe('The specific letter to count'),
})
.refine((data) => data.letter.length === 1, {
message: "The 'letter' parameter must be a single character",
}),
callback: (input) => {
const { word, letter } = input
// Convert both to lowercase for case-insensitive comparison
const lowerWord = word.toLowerCase()
const lowerLetter = letter.toLowerCase()
// Count occurrences
let count = 0
for (const char of lowerWord) {
if (char === lowerLetter) {
count++
}
}
// Return result as string (following the pattern of other tools in this project)
return `The letter '${letter}' appears ${count} time(s) in '${word}'`
},
})
// Create an agent with tools with our custom letterCounter tool
const agent = new Agent({
tools: [letterCounter],
})
// Ask the agent a question that uses the available tools
const message = `Tell me how many letter R's are in the word "strawberry" 🍓`
const result = await agent.invoke(message)
console.log(result.lastMessage)
ローカルで開発サーバーを起動するためのスクリプトを、package.jsonに追加します。これにより、pnpm run devコマンドでapps/agent.tsを実行できるようになります。
"scripts": {
"dev": "tsx apps/agent.ts",
}
また、前述のコードはトップレベルのawaitを使用していますが、プロジェクトがCommonJS形式のため未サポートです。package.jsonに"type": "module"を追加してESMとして扱うようにします。
{
"type": "module",
}
Strands Agentsの実行
AWSプロファイルを切り替えた上で、以下のコマンドでエージェントを実行します。
pnpm run dev
以下のような出力が得られれば成功です。コード上では特にモデルを指定していませんが、現時点ではGlobalのAnthropic Claude Sonnet 4.5がデフォルトで使用されます。
🔧 Tool #1: letter_counter
✓ Tool completed
Auto-detected includeToolResultStatus=true for model: global.anthropic.claude-sonnet-4-5-20250929-v1:0
There are **3** letter R's in the word "strawberry" 🍓
They appear in these positions:
- st**r**awberry (position 3)
- strawbe**rr**y (positions 8 and 9)Message {
type: 'message',
role: 'assistant',
content: [
TextBlock {
type: 'textBlock',
text: `There are **3** letter R's in the word "strawberry" 🍓\n` +
'\n' +
'They appear in these positions:\n' +
'- st**r**awberry (position 3)\n' +
'- strawbe**rr**y (positions 8 and 9)'
}
]
}
ここまでの変更は、以下のGitHubリポジトリで確認できます。
モデルを指定する
@strands-agents/sdkのBedrockModelを使用して、明示的にモデルを指定することも可能です。以下のように、Agentのコンストラクタにmodelオプションを追加します。今回は新しく登場した、日本のクロスリージョン推論用のAmazon Nova 2 Liteを指定してみます。
import { BedrockModel } from '@strands-agents/sdk'
// ..snip..
const bedrockModel = new BedrockModel({
modelId: "jp.amazon.nova-2-lite-v1:0",
region: "ap-northeast-1",
});
// Create an agent with tools with our custom letterCounter tool
const agent = new Agent({
model: bedrockModel,
tools: [letterCounter],
});
pnpm run devで再度実行すると、Nova 2 Liteが使用されていることが分かります。
🔧 Tool #1: letter_counter
✓ Tool completed
Auto-detected includeToolResultStatus=false for model: jp.amazon.nova-2-lite-v1:0
The letter **R** appears **3 time(s)** in the word **"strawberry"**. 🍓Message {
type: 'message',
role: 'assistant',
content: [
TextBlock {
type: 'textBlock',
text: 'The letter **R** appears **3 time(s)** in the word **"strawberry"**. 🍓'
}
]
}
ここまでの変更は、以下のGitHubリポジトリで確認できます。
MCPの追加
Toolとして、MCP(Model Context Protocol)を使った機能も追加してみましょう。今回は、AWS Knowledge MCP Serverを使用して、最新のAWSドキュメント情報を取得できるようにします。
まず、MCP SDKをインストールします。
pnpm add @modelcontextprotocol/sdk
次に、apps/agent.tsに以下のコードを追加・修正します。
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
import { McpClient } from "@strands-agents/sdk";
// ..snip..
const awsKnowledgeMcpClient = new McpClient({
transport: new StreamableHTTPClientTransport(
new URL("https://knowledge-mcp.global.api.aws"),
),
});
// ..snip..
const agent = new Agent({
model: bedrockModel,
tools: [letterCounter, awsKnowledgeMcpClient],
});
const message = `AgentCore Policyとは何ですか?`;
pnpm run devで再度実行すると、冒頭のTool呼び出しでAWS Knowledge MCP Serverからの情報を基に応答が生成されていることが分かります。
🔧 Tool #1: aws___search_documentation
✓ Tool completed
Auto-detected includeToolResultStatus=false for model: jp.amazon.nova-2-lite-v1:0
### AgentCore Policyとは?
**AgentCore Policy**は、AWS Bedrock AgentCoreで提供されるセキュリティソリューションであり、AIエージェントがツールとやり取りする際のセキュリティコントロールを定義し、施行するためのものです。これは、AIエージェントの柔軟性によってもたらされる新しいセキュリティ課題に対処するために、エージェントの操作周囲に保護バリアを設けることを目的としています。
#### 主な特徴と機能
1. **保護バリアの作成**
- エージェント操作周囲に保護バリアを設け、セキュリティリスクを軽減します。
2. **ポリシーエンジンによる評価**
- Amazon Bedrock AgentCore Gatewayを介してエージェントトラフィックをインターセプトし、各リクエストを定義されたポリシーに対して評価します。
3. **Cedar言語または自然言語プロンプトによるポリシーの定義**
- エージェントのアクションを正確に指定し、きめ細かい制御が可能になります。
4. **細かなアクセス制御**
- エージェントのアクションに対する細かな制御が可能で、決定的な施行と強い保証を提供します。
5. **単純でアクセスしやすい著述**
- 組織全体で一貫性のあるポリシーの作成が容易になります。
#### 主要な機能
- **ポリシーに基づくガバナンス**
- ポリシーの施行、アクセス制御、ポリシーの著述、ポリシーの監視、インフラストラクチャ統合、オーディットログなどを提供します。
- **Cedarポリシー言語**
- ポリシーはCedar言語または自然言語プロンプトを使用して作成され、具体的なアクションと条件を指定できます。
- **エージェントへの適用**
- エージェントがGatewayを介してツールにアクセスする際に、ポリシーを評価し、許可または拒否を決定します。
- **監視とログ記録**
- ポリシーの実行状況を監視し、オーディットログを記録してセキュリティイベントを追跡します。
#### 適用例
- **リファンダ処理ツールの制限**
- リファンダ金額が特定の限度以下の場合にのみ、リファンダを処理するようポリシーを設定できます。
- **ユーザーのロールベースのアクセス制御**
- 特定のユーザーロールだけが特定のツールにアクセスできるように制限できます。
#### 利点
- **細かな制御**
- エージェントのアクションを正確に制御できます。
- **決定的な施行**
- ポリシーは確実に施行され、セキュリティリスクを軽減します。
- **アクセスしやすい著述**
- 自然言語プロンプトを使用してポリシーを簡単に作成できます。
- **組織全体での一貫性**
- 組織全体のセキュリティポリシーが一貫して適用されます。
#### 使用方法の概要
1. **ポリシーの作成**
- Cedar言語または自然言語プロンプトを使用してポリシーを定義します。
2. **ポリシーエンジンの設定**
- ポリシーエンジンをGatewayにアタッチし、ENFORCEモードで設定します。
3. **エージェントのテスト**
- エージェントがGatewayを介してツールにアクセスする際のポリシーをテストします。
4. **監視とログ記録**
- ポリシーの実行状況を監視し、オーディットログを記録します。
AgentCore Policyは、AIエージェントのセキュリティを強化し、組織のセキュリティポリシーを確実に適用するための強力なツールです。
ここまでの変更は、以下のGitHubリポジトリで確認できます。
さいごに
最後までお読みいただき、ありがとうございました。
まだプレビュー版のため、本番環境での利用には注意が必要ですが、TypeScriptユーザーにとって、今回のアップデートは喜ばしいものだったのではないでしょうか。本記事が皆様のお役に立てれば幸いです。
それではまた!
参考リンク










