Skip to content

Agentic AI Dungeon Game

We are going to start by creating a new monorepo. From within your desired directory, run the following command:

Terminal window
npx create-nx-workspace@21.6.4 dungeon-adventure --pm=pnpm --preset=@aws/nx-plugin --iacProvider=CDK --ci=skip --aiAgents

This will set up a NX monorepo within the dungeon-adventure directory which you can then open in vscode. It should look like the following:

  • Directory.nx/
  • Directory.vscode/
  • Directorynode_modules/
  • Directorypackages/ this is where your sub-projects will reside
  • .gitignore
  • .npmrc
  • .prettierignore
  • .prettierrc
  • nx.json configures the Nx CLI and monorepo defaults
  • package.json all node dependencies are defined here
  • pnpm-lock.yaml or bun.lock, yarn.lock, package-lock.json depending on package manager
  • pnpm-workspace.yaml if using pnpm
  • README.md
  • tsconfig.base.json all node based sub-projects extend this
  • tsconfig.json
  • aws-nx-plugin.config.mts configuraton for the Nx Plugin for AWS

Now we are ready to start creating our different sub-projects using the @aws/nx-plugin.

First let’s create our Game API. To do this, let’s create a tRPC API called GameApi by following the below steps:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - ts#trpc-api
  5. Fill in the required parameters
    • name: GameApi
  6. Click Generate

You should see some new files have appeared in your file tree.

Click here to examine these files in more detail.

Now let’s create our Story Agent. To do this, let’s first create a Python Project:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - py#project
  5. Fill in the required parameters
    • name: story
  6. Click Generate

You should see some new files have appeared in your file tree.

Click here to examine these files in more detail.

Next, let’s add a Strands agent to the project with the py#strands-agent generator:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - py#strands-agent
  5. Fill in the required parameters
    • project: story
  6. Click Generate

You should see some new files have appeared in your file tree.

Click here to examine these files in more detail.

Now let’s create an MCP server which will provide tools for our Story Agent to manage a player’s inventory.

First, we create a TypeScript project:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - ts#project
  5. Fill in the required parameters
    • name: inventory
  6. Click Generate

This will create an empty TypeScript project.

Click here to examine these files in more detail.

Next we’ll add an MCP server to our TypeScript project:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - ts#mcp-server
  5. Fill in the required parameters
    • project: inventory
  6. Click Generate

This will add

Click here to examine these files in more detail.

Now let’s create the UI which will enable you to interact with the game. To do this, let’s create a website called GameUI by following the below steps:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - ts#react-website
  5. Fill in the required parameters
    • name: GameUI
  6. Click Generate

You should see some new files have appeared in your file tree.

Click here to examine these files in more detail.

Now let’s configure our Game UI to require authenticated access via Amazon Cognito by following the below steps:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - ts#react-website#auth
  5. Fill in the required parameters
    • cognitoDomain: game-ui
    • project: @dungeon-adventure/game-ui
    • allowSignup: true
  6. Click Generate

You should see some new files have appeared/changed in your file tree.

Click here to examine these files in more detail.

Now let’s configure our Game UI to connect to our previously created Game API:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - api-connection
  5. Fill in the required parameters
    • sourceProject: @dungeon-adventure/game-ui
    • targetProject: @dungeon-adventure/game-api
  6. Click Generate

You should see some new files have appeared/changed in your file tree.

Click here to examine these files in more detail.

Now the final sub-project we need to create is for the CDK infrastructure. To create this, follow the below steps:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - ts#infra
  5. Fill in the required parameters
    • name: infra
  6. Click Generate

You should see some new files have appeared/changed in your file tree.

Click here to examine these files in more detail.

Let’s make an update to our packages/infra/src/stacks/application-stack.ts to instantiate some of our already generated constructs:

import {
GameApi,
GameUI,
InventoryMcpServer,
RuntimeConfig,
StoryAgent,
UserIdentity,
} from ':dungeon-adventure/common-constructs';
import { Stack, StackProps, CfnOutput } from 'aws-cdk-lib';
import { Construct } from 'constructs';
export class ApplicationStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const userIdentity = new UserIdentity(this, 'UserIdentity');
const gameApi = new GameApi(this, 'GameApi', {
integrations: GameApi.defaultIntegrations(this).build(),
});
const { userPool, userPoolClient } = userIdentity;
const mcpServer = new InventoryMcpServer(this, 'InventoryMcpServer');
// Use Cognito for user authentication with the agent
const storyAgent = new StoryAgent(this, 'StoryAgent', {
authorizerConfiguration: {
customJwtAuthorizer: {
discoveryUrl: `https://cognito-idp.${Stack.of(userPool).region}.amazonaws.com/${userPool.userPoolId}/.well-known/openid-configuration`,
allowedAudience: [userPoolClient.userPoolClientId],
},
},
environment: {
INVENTORY_MCP_ARN: mcpServer.agentCoreRuntime.arn,
},
});
// Add the Story Agent ARN to runtime-config.json so it can be referenced by the website
RuntimeConfig.ensure(this).config.agentArn =
storyAgent.agentCoreRuntime.arn;
new CfnOutput(this, 'StoryAgentArn', {
value: storyAgent.agentCoreRuntime.arn,
});
new CfnOutput(this, 'InventoryMcpArn', {
value: mcpServer.agentCoreRuntime.arn,
});
// Grant the agent permissions to invoke our mcp server
mcpServer.agentCoreRuntime.grantInvoke(storyAgent.agentCoreRuntime);
// Grant the authenticated role access to invoke the api
gameApi.grantInvokeAccess(userIdentity.identityPool.authenticatedRole);
// Ensure this is instantiated last so our runtime-config.json can be automatically configured
new GameUI(this, 'GameUI');
}
}

Notice here that we supply default integrations for our Game API. By default, each operation in our API is mapped to an individual lambda function to handle that operation.

Now it's time for us to build our code for the first time
Terminal window
pnpm nx run-many --target build --all

You should be prompted with the following:

Terminal window
NX The workspace is out of sync
[@nx/js:typescript-sync]: Some TypeScript configuration files are missing project references to the projects they depend on or contain outdated project references.
This will result in an error in CI.
? Would you like to sync the identified changes to get your workspace up to date?
Yes, sync the changes and run the tasks
No, run the tasks without syncing the changes

This message indicates that NX has detected some files which can be updated automatically for you. In this case, it is referring to the tsconfig.json files which do not have Typescript references set up on references projects. Select the Yes, sync the changes and run the tasks option to proceed. You should notice all of you IDE related import errors get automatically resolved as the sync generator will add the missing typescript references automatically!

All built artifacts are now available within the dist/ folder located at the root of the monorepo. This is a standard practice when using projects generated by the @aws/nx-plugin as it does not pollute your file-tree with generated files. In the event you want to clean yor files, you can simply delete the dist/ folder without having to worry about generated files being littered throughout the file tree.

Congratulations! You’ve created all of the required sub-projects needed to start implementing the core of our Dunegeon Adventure game. 🎉🎉🎉