Storyエージェントの実装と設定
タスク1: Storyエージェントの実装
Section titled “タスク1: Storyエージェントの実装”Storyエージェントは、モジュール1で--protocol=ag-uiを使用して生成されたStrandsエージェントです。これにより、UIはCopilotKitを介してAgent-User Interactionプロトコル経由でストリーミングできます。Inventory MCP Serverを使用してプレイヤーのアイテムを管理し、モジュール1でジェネレーターがプロビジョニングしたsession.pyを介して会話履歴を既に永続化しています。デプロイされると、これはStrandsの組み込みS3SessionManagerであり、Game APIのqueryActionsが読み取るのと同じS3バケットに書き込むため、ブラウザは再訪時にトランスクリプトを再構築できます。
エージェントの実装
Section titled “エージェントの実装”packages/story/dungeon_adventure_story/agentのagent.pyを更新します:
from contextlib import contextmanager
from dungeon_adventure_agent_connection import InventoryMcpServerClientStrands, log_model_errors, log_tool_errorsfrom strands import Agentfrom strands.hooks import HookCallback, HookProvider
AGENT_HOOKS: list[HookProvider | HookCallback] = [log_model_errors, log_tool_errors]
@contextmanagerdef get_agent(): inventory_mcp_server = InventoryMcpServerClientStrands.create() with ( inventory_mcp_server, ): yield Agent( system_prompt="""You are running a text adventure game for a lone adventurer. When a new storybegins, the first user message will tell you the player's name and the genre(one of 'medieval', 'zombie', 'superhero'). Greet the player by name, set thescene in the chosen genre, and populate their inventory with a few startingitems. On subsequent turns, advance the story in response to the player'sactions and keep item state in sync with the narrative.Use the tools to manage the player's inventory as items are obtained or lost.Item names in the inventory must be Title Case — match them exactly.Only use list-inventory-items if you are unsure of the exact item name before modifying it.IMPORTANT: Only call ONE tool per response turn. Never batch multiple tool calls in a single turn.Ensure you specify a suitable emoji when adding items if available.Items should be a key part of the narrative.Keep responses under 100 words.""", tools=[*inventory_mcp_server.list_tools_sync()], hooks=AGENT_HOOKS, )from contextlib import contextmanager
from dungeon_adventure_agent_connection import InventoryMcpServerClientStrands, log_model_errors, log_tool_errorsfrom strands import Agent, toolfrom strands import Agentfrom strands.hooks import HookCallback, HookProviderfrom strands_tools import current_time
@tooldef subtract(a: int, b: int) -> int: return a - b
AGENT_HOOKS: list[HookProvider | HookCallback] = [log_model_errors, log_tool_errors]
@contextmanagerdef get_agent(): inventory_mcp_server = InventoryMcpServerClientStrands.create() with ( inventory_mcp_server, ): yield Agent( name="StoryAgent", description="StoryAgent Strands Agent", system_prompt="""You are a mathematical wizard.Use your tools for mathematical tasks.Refer to tools as your 'spellbook'.You are running a text adventure game for a lone adventurer. When a new storybegins, the first user message will tell you the player's name and the genre(one of 'medieval', 'zombie', 'superhero'). Greet the player by name, set thescene in the chosen genre, and populate their inventory with a few startingitems. On subsequent turns, advance the story in response to the player'sactions and keep item state in sync with the narrative.Use the tools to manage the player's inventory as items are obtained or lost.Item names in the inventory must be Title Case — match them exactly.Only use list-inventory-items if you are unsure of the exact item name before modifying it.IMPORTANT: Only call ONE tool per response turn. Never batch multiple tool calls in a single turn.Ensure you specify a suitable emoji when adding items if available.Items should be a key part of the narrative.Keep responses under 100 words.""", tools=[subtract, current_time, *inventory_mcp_server.list_tools_sync()], tools=[*inventory_mcp_server.list_tools_sync()], hooks=AGENT_HOOKS, )agent.pyはサンプルのsubtractツールを削除し、システムプロンプトをダンジョンマスター用のものに置き換えます。これは、最初のユーザーメッセージでプレイヤーの名前とジャンルを述べるよう促し、Inventory MCP Serverのツールを使用します。main.pyはここでは変更不要です。エージェントが生成された時点でセッション管理は既に配線されています。
タスク2: エージェントをローカルでテストする
Section titled “タスク2: エージェントをローカルでテストする”コードのビルド
Section titled “コードのビルド”コードをビルドするには:
pnpm buildyarn buildnpm run buildbun buildエージェントとチャットする
Section titled “エージェントとチャットする”生成されたagent-chatターゲットは、エージェントに対してインタラクティブなREPLを開きます。これはスタンドアロンで実行され、ローカルで実行中のエージェントに接続するため、まず1つのターミナルでエージェントのローカルサーバーを起動します:
pnpm nx agent-dev storyyarn nx agent-dev storynpx nx agent-dev storybunx nx agent-dev story次に、2つ目のターミナルでチャットを開始します:
pnpm nx agent-chat storyyarn nx agent-chat storynpx nx agent-chat storybunx nx agent-chat story最初のメッセージでは、エージェントにヒーローの名前とジャンルを伝える必要があります(例: My name is Alice. Start my zombie adventure.)。ストーリーがストリーミングで返されます。
おめでとうございます。最初のStrandsエージェントをローカルでビルドしてテストしました!🎉🎉🎉