Skip to content

Agentic AI Dungeon Game

The Story Agent is a Strands agent which, given a Game and a list of Actions for context, will progress a story. We will configure the agent to interact with our Inventory MCP Server to manage the player’s available items.

Let’s implement our agent. Update the following files in packages/story/dungeon_adventure_story/agent:

import os
from bedrock_agentcore.runtime import BedrockAgentCoreApp
from .agent import get_agent
app = BedrockAgentCoreApp()
PORT = int(os.environ.get("PORT", "8080"))
@app.entrypoint
async def invoke(payload, context):
"""Handler for agent invocation"""
player_name = payload.get("playerName")
genre = payload.get("genre")
actions = payload.get("actions")
messages = [{"role": "user", "content": [{"text": "Continue or create a new story..."}]}]
for action in actions:
messages.append({"role": action["role"], "content": [{"text": action["content"]}]})
with get_agent(player_name, genre, session_id=context.session_id) as agent:
stream = agent.stream_async(messages)
async for event in stream:
print(event)
yield (event)
if __name__ == "__main__":
app.run(port=PORT)

This configures the following:

  • Extracting the player, genre and actions from the agent payload
  • Constructing a client which the Agent can use to invoke our MCP server with SigV4 Authentication
  • Constructing the agent with a system prompt and the MCP server’s tools

First, lets build the codebase:

Terminal window
pnpm nx run-many --target build --all

Your application can now be deployed by running the following command:

Terminal window
pnpm nx deploy infra dungeon-adventure-infra-sandbox/*

This deployment will take around 2 minutes to complete.

Once the deployment completes, you should see some outputs similar to the following (some values have been redacted):

Terminal window
dungeon-adventure-infra-sandbox-Application
dungeon-adventure-infra-sandbox-Application: deploying... [2/2]
dungeon-adventure-infra-sandbox-Application
Deployment time: 354s
Outputs:
dungeon-adventure-infra-sandbox-Application.ElectroDbTableTableNameXXX = dungeon-adventure-infra-sandbox-Application-ElectroDbTableXXX-YYY
dungeon-adventure-infra-sandbox-Application.GameApiEndpointXXX = https://xxx.execute-api.region.amazonaws.com/prod/
dungeon-adventure-infra-sandbox-Application.GameUIDistributionDomainNameXXX = xxx.cloudfront.net
dungeon-adventure-infra-sandbox-Application.InventoryMcpArn = arn:aws:bedrock-agentcore:region:xxxxxxx:runtime/dungeonadventureventoryMcpServerXXXX-YYYY
dungeon-adventure-infra-sandbox-Application.StoryAgentArn = arn:aws:bedrock-agentcore:region:xxxxxxx:runtime/dungeonadventurecationStoryAgentXXXX-YYYY
dungeon-adventure-infra-sandbox-Application.UserIdentityUserIdentityIdentityPoolIdXXX = region:xxx
dungeon-adventure-infra-sandbox-Application.UserIdentityUserIdentityUserPoolIdXXX = region_xxx

We can test our API by either:

  • Starting a local instance of the Agent server and invoking it using curl.
  • Calling the deployed API using curl with a JWT token.

Start your local Agent server by running the following command:

Terminal window
PORT=9999 INVENTORY_MCP_ARN=arn:aws:bedrock-agentcore:region:xxxxxxx:runtime/dungeonadventureventoryMcpServerXXXX-YYYY AWS_REGION=<region> pnpm nx run dungeon_adventure.story:agent-serve

Once the Agent server is up and running (you won’t see any output!), call it by running the following command:

Terminal window
curl -N -X POST http://127.0.0.1:9999/invocations \
-d '{"genre":"superhero", "actions":[], "playerName":"UnnamedHero"}' \
-H "Content-Type: application/json" \
-H "X-Amzn-Bedrock-AgentCore-Runtime-Session-Id: abcdefghijklmnopqrstuvwxyz-123456789"

If the command executes successfully, you should see events being streamed similar to:

data: {"init_event_loop": true}
data: {"start": true}
data: {"start_event_loop": true}
data: {"event": {"messageStart": {"role": "assistant"}}}
data: {"event": {"contentBlockDelta": {"delta": {"text": "Welcome"}, "contentBlockIndex": 0}}}
...

Congratulations. You have built and deployed your first Strands Agent on Bedrock AgentCore Runtime! 🎉🎉🎉