Skip to content

Game APIとInventory MCPサーバーの実装

ゲームAPIを実装する前に、これらの5つのAPIを作成する必要があります:

  1. saveGame - ゲームの作成または更新
  2. queryGames - 保存済みゲームのページネーション付きリストを返す
  3. saveAction - 指定したゲームのアクションを保存
  4. queryActions - ゲームに関連する全アクションのページネーション付きリストを返す
  5. queryInventory - プレイヤーのインベントリ内アイテムのページネーション付きリストを返す

APIの入力と出力を定義するため、packages/game-api/src/schemaディレクトリ内でZodを使用してスキーマを作成します:

import { z } from 'zod';
export const ActionSchema = z.object({
playerName: z.string(),
timestamp: z.iso.datetime(),
role: z.enum(['assistant', 'user']),
content: z.string(),
});
export type IAction = z.TypeOf<typeof ActionSchema>;

このプロジェクトで使用しないため、packages/game-api/src/schema/echo.tsファイルは削除できます。

これがアプリケーションのER図です。

dungeon-adventure-er.png

DynamoDBでデータベースを実装し、ElectroDB DynamoDBクライアントライブラリを使用して簡素化します。electrodbとDynamoDBクライアントをインストールするには、次のコマンドを実行します:

Terminal window
pnpm add -w electrodb@3.5.0 @aws-sdk/client-dynamodb@3.914.0

ER図からElectroDBエンティティを定義するため、packages/game-api/src/entitiesフォルダ内に以下のファイルを作成します:

import { Entity } from 'electrodb';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
export const createActionEntity = (client?: DynamoDBClient) =>
new Entity(
{
model: {
entity: 'Action',
version: '1',
service: 'game',
},
attributes: {
playerName: { type: 'string', required: true, readOnly: true },
timestamp: {
type: 'string',
required: true,
readOnly: true,
set: () => new Date().toISOString(),
default: () => new Date().toISOString(),
},
role: { type: 'string', required: true, readOnly: true },
content: { type: 'string', required: true, readOnly: true },
},
indexes: {
primary: {
pk: { field: 'pk', composite: ['playerName'] },
sk: { field: 'sk', composite: ['timestamp'] },
},
},
},
{ client, table: process.env.TABLE_NAME },
);

ElectroDBでは型定義だけでなく、タイムスタンプなどの特定の値にデフォルト値を設定できます。またElectroDBはDynamoDB使用時のベストプラクティスであるシングルテーブル設計に従います。

MCPサーバーがインベントリと連携できるよう、packages/game-api/src/index.tsでインベントリエンティティをエクスポートします:

export type { AppRouter } from './router.js';
export { appRouter } from './router.js';
export type { Context } from './init.js';
export * from './client/index.js';
export * from './schema/index.js';
export * from './entities/inventory.js';

tRPCコンテキストへのDynamoDBクライアント追加

Section titled “tRPCコンテキストへのDynamoDBクライアント追加”

各プロシージャでDynamoDBクライアントにアクセスする必要があるため、コンテキスト経由で渡せるクライアントの単一インスタンスを作成します。これを行うため、packages/game-api/src内で以下の変更を加えます:

import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { initTRPC } from '@trpc/server';
export interface IDynamoDBContext {
dynamoDb?: DynamoDBClient;
}
export const createDynamoDBPlugin = () => {
const t = initTRPC.context<IDynamoDBContext>().create();
return t.procedure.use(async (opts) => {
const dynamoDb = new DynamoDBClient();
const response = await opts.next({
ctx: {
...opts.ctx,
dynamoDb,
},
});
return response;
});
};

DynamoDBClientを作成しコンテキストに注入するプラグインです。

APIメソッドを実装するため、packages/game-api/src/procedures内で以下の変更を加えます:

import { createActionEntity } from '../entities/action.js';
import {
ActionSchema,
IAction,
QueryInputSchema,
createPaginatedQueryOutput,
} from '../schema/index.js';
import { publicProcedure } from '../init.js';
import { z } from 'zod';
export const queryActions = publicProcedure
.input(QueryInputSchema.extend({ playerName: z.string() }))
.output(createPaginatedQueryOutput(ActionSchema))
.query(async ({ input, ctx }) => {
const actionEntity = createActionEntity(ctx.dynamoDb);
const result = await actionEntity.query
.primary({ playerName: input.playerName })
.go({ cursor: input.cursor, count: input.limit });
return {
items: result.data as IAction[],
cursor: result.cursor,
};
});
import { ActionSchema, IAction } from '../schema/index.js';
import { publicProcedure } from '../init.js';
import { createActionEntity } from '../entities/action.js';
import { createGameEntity } from '../entities/game.js';
export const saveAction = publicProcedure
.input(ActionSchema.omit({ timestamp: true }))
.output(ActionSchema)
.mutation(async ({ input, ctx }) => {
const actionEntity = createActionEntity(ctx.dynamoDb);
const gameEntity = createGameEntity(ctx.dynamoDb);
const action = await actionEntity.put(input).go();
await gameEntity
.update({ playerName: input.playerName })
.set({ lastUpdated: action.data.timestamp })
.go();
return action.data as IAction;
});

このプロジェクトで使用しないため、echo.tsファイル(packages/game-api/src/procedures内)は削除できます。

プロシージャを定義した後、APIに接続するため、以下のファイルを更新します:

import {
awsLambdaRequestHandler,
CreateAWSLambdaContextOptions,
} from '@trpc/server/adapters/aws-lambda';
import { t } from './init.js';
import { APIGatewayProxyEvent } from 'aws-lambda';
import { queryActions } from './procedures/query-actions.js';
import { saveAction } from './procedures/save-action.js';
import { queryGames } from './procedures/query-games.js';
import { saveGame } from './procedures/save-game.js';
import { queryInventory } from './procedures/query-inventory.js';
export const router = t.router;
export const appRouter = router({
actions: router({
query: queryActions,
save: saveAction,
}),
games: router({
query: queryGames,
save: saveGame,
}),
inventory: router({
query: queryInventory,
}),
});
export const handler = awsLambdaRequestHandler({
router: appRouter,
createContext: (
ctx: CreateAWSLambdaContextOptions<APIGatewayProxyEvent>,
) => ctx,
responseMeta: () => ({
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
},
}),
});
export type AppRouter = typeof appRouter;

タスク2: インベントリMCPサーバーの作成

Section titled “タスク2: インベントリMCPサーバーの作成”

エージェントがプレイヤーのインベントリのアイテムを管理できるMCPサーバーを作成します。

エージェント用に以下のツールを定義します:

  • list-inventory-items - プレイヤーの現在のインベントリアイテムを取得
  • add-to-inventory - プレイヤーのインベントリにアイテムを追加
  • remove-from-inventory - プレイヤーのインベントリからアイテムを削除

時間節約のため、すべてのツールをインラインで定義します:

import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import z from 'zod-v3';
import { createInventoryEntity } from ':dungeon-adventure/game-api';
/**
* Create the MCP Server
*/
export const createServer = () => {
const server = new McpServer({
name: 'inventory-mcp-server',
version: '1.0.0',
});
const dynamoDb = new DynamoDBClient();
const inventory = createInventoryEntity(dynamoDb);
server.tool(
'list-inventory-items',
"List items in the player's inventory. Leave cursor blank unless you are requesting subsequent pages",
{
playerName: z.string(),
cursor: z.string().optional(),
},
async ({ playerName }) => {
const results = await inventory.query
.primary({
playerName,
})
.go();
return {
content: [{ type: 'text', text: JSON.stringify(results) }],
};
},
);
server.tool(
'add-to-inventory',
"Add an item to the player's inventory. Quantity defaults to 1 if omitted.",
{
playerName: z.string(),
itemName: z.string(),
emoji: z.string(),
quantity: z.number().optional().default(1),
},
async ({ playerName, itemName, emoji, quantity = 1 }) => {
await inventory
.put({
playerName,
itemName,
quantity,
emoji,
})
.go();
return {
content: [
{
type: 'text',
text: `Added ${itemName} (x${quantity}) to inventory`,
},
],
};
},
);
server.tool(
'remove-from-inventory',
"Remove an item from the player's inventory. If quantity is omitted, all items are removed.",
{
playerName: z.string(),
itemName: z.string(),
quantity: z.number().optional(),
},
async ({ playerName, itemName, quantity }) => {
// If quantity is omitted, remove the entire item
if (quantity === undefined) {
try {
await inventory.delete({ playerName, itemName }).go();
return {
content: [
{ type: 'text', text: `${itemName} removed from inventory.` },
],
} as const;
} catch {
return {
content: [
{ type: 'text', text: `${itemName} not found in inventory` },
],
} as const;
}
}
// If quantity is specified, fetch current quantity and update
const item = await inventory.get({ playerName, itemName }).go();
if (!item.data) {
return {
content: [
{ type: 'text', text: `${itemName} not found in inventory` },
],
} as const;
}
const newQuantity = item.data.quantity - quantity;
if (newQuantity <= 0) {
await inventory.delete({ playerName, itemName }).go();
return {
content: [
{ type: 'text', text: `${itemName} removed from inventory.` },
],
} as const;
}
await inventory
.put({
playerName,
itemName,
quantity: newQuantity,
emoji: item.data.emoji,
})
.go();
return {
content: [
{
type: 'text',
text: `Removed ${itemName} (x${quantity}) from inventory. ${newQuantity} remaining.`,
},
],
};
},
);
return server;
};

ツール数が増えた場合、必要に応じて別ファイルにリファクタリングできます。

packages/inventory/src/mcp-server内のtoolsresourcesディレクトリは使用しないため削除してください。

タスク3: インフラストラクチャの更新

Section titled “タスク3: インフラストラクチャの更新”

最後のステップは、DynamoDBテーブルを作成し、Game APIからの操作権限を付与するためインフラストラクチャを更新することです。 これを行うため、packages/infra/srcを以下のように更新します:

import { CfnOutput } from 'aws-cdk-lib';
import {
AttributeType,
BillingMode,
ProjectionType,
Table,
TableProps,
} from 'aws-cdk-lib/aws-dynamodb';
import { Construct } from 'constructs';
import { suppressRules } from ':dungeon-adventure/common-constructs';
export type ElectrodbDynamoTableProps = Omit<
TableProps,
'partitionKey' | 'sortKey' | 'billingMode'
>;
export class ElectrodbDynamoTable extends Table {
constructor(scope: Construct, id: string, props?: ElectrodbDynamoTableProps) {
super(scope, id, {
partitionKey: {
name: 'pk',
type: AttributeType.STRING,
},
sortKey: {
name: 'sk',
type: AttributeType.STRING,
},
billingMode: BillingMode.PAY_PER_REQUEST,
...props,
});
this.addGlobalSecondaryIndex({
indexName: 'gsi1pk-gsi1sk-index',
partitionKey: {
name: 'gsi1pk',
type: AttributeType.STRING,
},
sortKey: {
name: 'gsi1sk',
type: AttributeType.STRING,
},
projectionType: ProjectionType.ALL,
});
// Suppress checkov rules that expect a KMS customer managed key and backup to be enabled
suppressRules(this, ['CKV_AWS_119', 'CKV_AWS_28'], 'No need for custom encryption or backup');
new CfnOutput(this, 'TableName', { value: this.tableName });
}
}

コードベースをビルドするには:

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

アプリケーションをデプロイするには、以下のコマンドを実行します:

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

初回デプロイは約8分かかります。以降のデプロイは約2分です。

デプロイ完了後、以下のような出力が表示されます(一部値は編集済み):

Terminal window
dungeon-adventure-sandbox-Application
dungeon-adventure-sandbox-Application: deploying... [2/2]
dungeon-adventure-sandbox-Application
Deployment time: 354s
Outputs:
dungeon-adventure-sandbox-Application.ElectroDbTableTableNameXXX = dungeon-adventure-sandbox-Application-ElectroDbTableXXX-YYY
dungeon-adventure-sandbox-Application.GameApiEndpointXXX = https://xxx.execute-api.region.amazonaws.com/prod/
dungeon-adventure-sandbox-Application.GameUIDistributionDomainNameXXX = xxx.cloudfront.net
dungeon-adventure-sandbox-Application.StoryApiEndpointXXX = https://xxx.execute-api.region.amazonaws.com/prod/
dungeon-adventure-sandbox-Application.UserIdentityUserIdentityIdentityPoolIdXXX = region:xxx
dungeon-adventure-sandbox-Application.UserIdentityUserIdentityUserPoolIdXXX = region_xxx

APIは以下の方法でテストできます:

  • tRPCバックエンドのローカルインスタンスを起動しcurlでAPIを呼び出す
  • デプロイ済みAPIをsigv4対応curlで呼び出す

ローカルのgame-apiサーバーを起動するには、以下のコマンドを実行します:

Terminal window
TABLE_NAME=dungeon-adventure-infra-sandbox-Application-ElectroDbTableXXX-YYY pnpm nx run @dungeon-adventure/game-api:serve

サーバーが起動したら、以下のコマンドで呼び出せます:

Terminal window
curl -X GET 'http://localhost:2022/games.query?input=%7B%7D'

コマンドが成功すると、以下のようなレスポンスが返ります:

{"result":{"data":{"items":[],"cursor":null}}}

おめでとうございます!tRPCを使用した最初のAPIの構築とデプロイに成功しました! 🎉🎉🎉