Tutorial 2: Describe your game's actions
Verified against gamewarden v0.2.0 (see the changelog). About 20 minutes.
The script 02-describe-your-actions.sh extracts the manifest
from this page, validates it, breaks it on purpose, and then runs it through a real gateway in
front of a small backend. CI runs that script.
You will write a manifest for a tiny REST API, validate it with gwctl, read the errors it
reports, and call your tools through a gateway. The manifest is a YAML file, which is the
easiest format to read and review. JSON works too (see the end).
Prerequisites
Section titled “Prerequisites”- Docker with Compose v2, and the repository cloned.
- Tutorial 1 helps but is not required.
The game
Section titled “The game”Pawprint Pets is a toy pet shop with four endpoints. Nothing about it is real; it only exists so
you have an API to describe. Go, Python, TypeScript, C#, Java, PHP and Elixir versions of the
backend are in examples/pet-backend-*, and you will accept signed calls
in tutorial 3.
| Endpoint | What it does |
|---|---|
GET /api/v1/shop |
coins, and the names of your pets |
GET /api/v1/pets/{name} |
one pet: species and hunger (0 full, 100 starving) |
POST /api/v1/pets/feed |
{"pet","food","amount"}: feed a pet. Kibble costs 1 coin a unit, fish 2 |
POST /api/v1/pets/adopt |
{"name","species"}: adopt a pet for 50 coins |
1. Decide what an agent may do
Section titled “1. Decide what an agent may do”Do not expose every endpoint at the same level. For each one, ask what the worst outcome of a bad call is.
- Reading the shop or a pet loses nothing: tier
read. - Feeding spends a few coins and is easy to undo: tier
safe, with a cap on how much food can be given in a day. - Adopting spends 50 coins and creates something that will want feeding: tier
medium, and the player must approve it (confirm).
The four tiers are read, safe, medium and sensitive. sensitive tools are disabled
unless the studio turns them on. The reasoning behind tiers is in
Tiers and policy.
2. Write the manifest
Section titled “2. Write the manifest”Save this block as pets.yaml in the repository folder. The next steps use that file.
version: 1game: pawprint-petstools: - name: get_shop description: The shop's coins and the names of the pets you own. Call this first. tier: read http: {method: GET, url: /api/v1/shop}
- name: get_pet description: How one pet is doing. hunger runs from 0 (full) to 100 (starving). tier: read http: {method: GET, url: "/api/v1/pets/{name}"} params: name: {type: string}
- name: feed_pet description: Feed a pet. Each unit lowers hunger by 10. kibble costs 1 coin a unit, fish costs 2. tier: safe caps: {per_day_amount: 10, amount_param: amount} http: {method: POST, url: /api/v1/pets/feed} params: pet: {type: string} food: {type: string, enum: [kibble, fish]} amount: {type: integer, min: 1, max: 5}
- name: adopt_pet description: Adopt a new pet for 50 coins. The player must approve each adoption. tier: medium confirm: true caps: {per_day_calls: 3, cooldown_seconds: 30} http: {method: POST, url: /api/v1/pets/adopt} params: name: {type: string} species: {type: string, enum: [cat, dog, rabbit]}It is also in the repository as examples/pet-shop/manifest.yaml, with a comment at the top. Small maps such as http and caps are written on one line, and each
tool is a block, so a diff shows exactly what changed. What each part does:
nameanddescription. The agent reads them to decide what to call. Write the description for the agent: say what the tool does, the units, and the limits. “Each unit lowers hunger by 10” lets the agent compute how much to feed.http. How the gateway reaches your API.{name}in the URL is filled from thenameparam. ForGETandDELETEthe other params go in the query string, for the rest they go in a JSON body.params. Every argument is checked before your backend sees it.enumrejects other values,minandmaxbound integers. Unknown arguments are refused.caps.per_day_amountadds up theamountargument, sofeed_petcan hand out at most 10 units a day however the agent splits them.per_day_callscounts calls andcooldown_secondsspaces them out.confirm. The gateway holds the call until the player approves it in your game’s own UI (tutorial 5).
The complete field list is in the manifest reference.
3. Validate it
Section titled “3. Validate it”The first docker compose run --rm dev builds the toolchain image, which takes a few minutes.
It also starts the Redis and Postgres containers the tests use. Later runs start in seconds.
docker compose run --rm dev go run ./cmd/gwctl validate pets.yamldocker compose run --rm dev go run ./cmd/gwctl tools pets.yamlok: 4 toolsread get_shopread get_petsafe feed_petmedium adopt_pet4. Read the errors
Section titled “4. Read the errors”gwctl reports every problem at once, so you fix them in one pass. Save this manifest with
mistakes as broken.yaml:
version: 1game: pawprint-petstools: - name: Feed Pet description: " " tier: admin http: {method: POST, url: api/v1/pets/feed}
- name: get_pet description: How one pet is doing. tier: read http: {method: GET, url: "/api/v1/pets/{name}"}
- name: adopt_pet description: Adopt a pet. tier: read confirm: true http: {method: POST, url: /api/v1/pets/adopt}Then validate it:
docker compose run --rm dev go run ./cmd/gwctl validate broken.yamlmanifest invalid: - tools[0] ("Feed Pet"): name must match ^[a-z][a-z0-9_]{1,63}$ - tools[0] ("Feed Pet"): description is required (the agent reads it) - tools[0] ("Feed Pet"): unknown tier "admin" - tools[0] ("Feed Pet"): http.url must be a path starting with / - tools[1] ("get_pet"): url placeholder {name} has no matching param - tools[2] ("adopt_pet"): read tools cannot require confirmationexit status 1The last line comes from go run: gwctl exited with status 1, which is what a CI job checks.
Each line names the tool and the rule. Fix them: a lowercase name, a description, a real tier, a
leading /, a name param, and a tier above read for a tool that needs approval. The
validator is strict on purpose: unknown fields and duplicate keys are errors too, so a typo
never silently does nothing. For YAML the error also points at the line. Save this as
typo.yaml and run docker compose run --rm dev go run ./cmd/gwctl validate typo.yaml:
version: 1tools: - name: get_shop description: The shop. tier: read tyer: read http: {method: GET, url: /api/v1/shop}manifest: decode yaml: [6:5] unknown field "tyer" 3 | - name: get_shop 4 | description: The shop. 5 | tier: read> 6 | tyer: read ^ 7 | http: {method: GET, url: /api/v1/shop}exit status 1A wrong indent is reported the same way, with the line and column
(mapping value is not allowed in this context).
5. Run it in front of a backend
Section titled “5. Run it in front of a backend”Start the gateway and the Go pet shop (use examples/pet-backend-python/compose.yml for the
Python one; the API is the same):
docker compose -f examples/pet-backend-go/compose.yml up --build -dThis binds 127.0.0.1:8090. The compose file loads examples/pet-shop/manifest.yaml, not your
pets.yaml, so edits to pets.yaml have no effect here. To try your own edits, copy your file
over it (cp pets.yaml examples/pet-shop/manifest.yaml), then run down and up again with the
same -f file. git checkout examples/pet-shop/manifest.yaml puts the original back.
Call the tools with the same helper as in tutorial 1:
call() { curl -s -X POST -H 'X-Dev-Player: p1' -d "$2" "http://127.0.0.1:8090/v1/tools/$1/call"; }call get_shop '{}'call get_pet '{"arguments":{"name":"rex"}}'call feed_pet '{"arguments":{"pet":"rex","food":"kibble","amount":5}}'{"ok":true,"data":{"coins":100,"pets":["rex"]}}{"ok":true,"data":{"hunger":80,"name":"rex","species":"dog"}}{"ok":true,"data":{"coins":95,"hunger":30,"pet":"rex"}}Now push on the limits. A bad enum value never reaches the backend:
call feed_pet '{"arguments":{"pet":"rex","food":"pizza","amount":1}}'{"ok":false,"code":"invalid_arguments","message":"invalid food \"pizza\": choose one of [kibble fish]"}Feed another 5, then try 1 more. The daily cap is 10 units:
call feed_pet '{"arguments":{"pet":"rex","food":"kibble","amount":5}}'call feed_pet '{"arguments":{"pet":"rex","food":"kibble","amount":1}}'{"ok":false,"code":"daily_amount_cap","message":"feed_pet: 1 needed, 0 left today"}Your own backend can refuse too. Asking about a pet you do not own is a 4xx from the backend,
and the gateway passes its short message on as backend_rejected:
call get_pet '{"arguments":{"name":"zed"}}'{"ok":false,"code":"backend_rejected","message":"no_such_pet: you do not own that pet"}A good backend refusal is a short code and a hint the agent can act on, as in
not_enough_coins: need 50, have 20.
Finally, adopt_pet is held until the player approves:
call adopt_pet '{"arguments":{"name":"milo","species":"cat"}}'{"ok":false,"code":"confirmation_required","message":"adopt_pet needs the player's approval. ...","data":{"confirmation_id":"...","expires_at":"..."}}In this dev setup you approve through the dev API. In a real game your backend does it with a signed request, which is tutorial 5:
curl -s -X POST -H 'X-Dev-Player: p1' http://127.0.0.1:8090/v1/confirmations/<confirmation_id>/approvecall adopt_pet '{"arguments":{"name":"milo","species":"cat"}}'{"ok":true,"data":{"adopted":"milo","coins":40}}Clean up with docker compose -f examples/pet-backend-go/compose.yml down.
A variant: games that run in the client
Section titled “A variant: games that run in the client”If your game logic runs in the game client, not on a server, you have no REST endpoint to map.
Mark the tool backend: relay and drop its http block. The gateway then sends the call
over a WebSocket the game client holds open. The rest of the manifest, caps and confirmation
included, works the same. See the Relay section of the protocol.
- name: feed_pet description: Feed a pet. tier: safe backend: relay params: pet: {type: string}JSON works too
Section titled “JSON works too”The gateway reads a manifest as JSON or YAML and picks the format from the file name: .yaml or
.yml is YAML, anything else is JSON. Both compile to the same structure, both reject unknown
fields, and the gateway stores every uploaded version as JSON. Use whichever your tooling
prefers. Here is get_shop in JSON:
{"name": "get_shop", "description": "The shop's coins and the names of the pets you own.", "tier": "read", "http": {"method": "GET", "url": "/api/v1/shop"}}What you learned
Section titled “What you learned”- Choose a tier by the worst outcome of a bad call, and cap what can be lost, not just how often a tool is called.
- Write descriptions for the agent: units, limits and what to call first.
gwctl validatereports every problem at once. The gateway checks arguments before your backend sees them.- A
4xxfrom your backend, with a short code and hint, reaches the agent asbackend_rejected.
To run the gateway on your own manifest and backend, see Add your manifest to the gateway.