---
title: "Agent lesson 1: Drive the forge as an agent"
description: "Run af mcp, speak the Model Context Protocol to it as an agent does, and use the forge's tools to open a workspace, change a file, land it, look around and undo."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.forge.workreef.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Agent lesson 1: Drive the forge as an agent

**Time:** 25 minutes. **Track:** [agent builder](https://docs.forge.workreef.dev/learn/#agent-builder). **Before this:** [developer lesson 1](https://docs.forge.workreef.dev/learn/developer/first-landing/).

By the end of this lesson you will be able to:

- say what `af mcp` is, and what an agent sees of the forge through it;
- make the calls an agent makes, one at a time: open a workspace, start a change, submit, wait;
- read the forge's structured answers, look around the repository, and undo a step.

## The forge's tools, over MCP

`af mcp` is a [Model Context Protocol](https://modelcontextprotocol.io) server. An agent that speaks MCP runs it, and gets the forge's [tools](https://docs.forge.workreef.dev/reference/agent-tools/), each answering with structured data instead of text to parse. Most agents are configured to start it themselves; this lab plays the agent's part from a shell, so you can see every call and every answer.

Start a forge:

```console server
$ af-server --dev
...
```

An agent sends one JSON-RPC message per line on `af mcp`'s standard input, and reads one answer per line on its output. It sends a call, waits for its answer, then sends the next: the server takes calls as they come, so calls sent together may be answered in any order. This small client does the same: each line it reads is a call, `NAME ARGUMENTS`, or a command to run between calls, `! COMMAND`, and it prints each answer's structured content:

```console check
$ source ~/af-dev/dev/env
$ cat > ~/agent-session <<'END'
> #!/bin/bash
> # A minimal MCP client. It runs `af ... mcp DIR`, the options after DIR going to af, and makes
> # the session's calls from stdin one at a time, as an agent does: `NAME ARGUMENTS` calls a tool
> # and prints its answer; `! COMMAND` runs a command between calls.
> coproc MCP { af "${@:2}" mcp "$1"; }
> say() { printf '%s\n' "$1" >&"${MCP[1]}"; }
> say '{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"agent-session","version":"1"}}}'
> read -r _ <&"${MCP[0]}"
> say '{"jsonrpc":"2.0","method":"notifications/initialized"}'
> id=0
> while IFS= read -r step; do
>   case "$step" in
>     '! '*) eval "${step#! }" ;;
>     ?*) id=$((id + 1)); name=${step%% *}
>         say "{\"jsonrpc\":\"2.0\",\"id\":$id,\"method\":\"tools/call\",\"params\":{\"name\":\"$name\",\"arguments\":${step#* }}}"
>         read -r reply <&"${MCP[0]}"
>         case "$reply" in
>           *'"structuredContent":'*) reply=${reply#*\"structuredContent\":}; reply=${reply%,\"isError\":*} ;;
>           *'"error":'*) reply=${reply#*\"error\":}; reply=${reply%\}} ;;
>         esac
>         printf '%s -> %s\n' "$name" "$reply" ;;
>   esac
> done
> END
$ chmod +x ~/agent-session
```

## Open a workspace and land a change

Run `af mcp` as yourself, with `~/agent` as its workspace. It opens the workspace for the paths it is given, starts a change, edits a file as an agent's own file tools would, and submits it. `wait` returns when something needs the agent, here that the change landed:

```console check
$ ~/agent-session ~/agent <<'END'
> open_workspace {"patterns":["**"]}
> start_change {"description":"Add a readme","author_name":"Lesson Agent","author_email":"agent@localhost"}
> ! printf 'Written by an agent\n' > ~/agent/files/README
> diff {}
> submit {}
> wait {}
> END
open_workspace -> {"directory":"/home/you/agent/files","landing":null,"patterns":["**"],"written":0}
start_change -> {"change":"upnnqxortvlqzntxpwtqnluqyrrklpps","commit":"8963fa057eba572a98e06d5257bb8442b15597560eff4b2c738270d39ed11025"}
diff -> {"base":"0000000000000000000000000000000000000000000000000000000000000000","commit":"19bf1edd970677fb8cc7fcb529f43d7ddb04bacec9fb67fe642022f9849af30e","files":[{"after":"file","before":"none","path":"README","unified":"--- a/README\n+++ b/README\n@@ -0,0 +1 @@\n+Written by an agent\n","withheld":null}]}
submit -> {"commit":"19bf1edd970677fb8cc7fcb529f43d7ddb04bacec9fb67fe642022f9849af30e","edits":0}
wait -> {"commit":"19bf1edd970677fb8cc7fcb529f43d7ddb04bacec9fb67fe642022f9849af30e","detail":"the change is on main","event":"landed"}
```

The files are under `files` in the workspace's directory; the agent edits them with its own tools. Every answer is JSON: a change ID, a commit, a unified diff, and what `wait` saw happen.

From a terminal, it is on main like any change. The author is who `start_change` named; the writer, in the forge's record, is who ran `af mcp`, here you:

```console eventually
$ af log -r main
○  upnnqxor agent@localhost 2026-11-25T06:29:49Z 19bf1edd
│  Add a readme
~
```

## Look around

An agent reads the repository with tools too: `log` takes a revset, `search` searches main, and `read` reads a file at a commit, files outside its workspace included. The forge's search index takes a landing in a moment after it lands, so a search straight after one can miss it: if `search` answers with no files, run the session again.

```console eventually
$ ~/agent-session ~/agent <<'END'
> open_workspace {"patterns":["**"]}
> log {"revset":"::main","limit":2}
> search {"pattern":"agent"}
> read {"path":"README"}
> END
open_workspace -> {"directory":"/home/you/agent/files","landing":1,"patterns":["**"],"written":0}
log -> {"commits":[{"author":{"email":"agent@localhost","name":"Lesson Agent","time":"2026-11-25T06:29:49Z"},"change":"upnnqxortvlqzntxpwtqnluqyrrklpps","change_short":"upnnqxor","commit":"19bf1edd970677fb8cc7fcb529f43d7ddb04bacec9fb67fe642022f9849af30e","committer":{"email":"agent@localhost","name":"Lesson Agent","time":"2026-11-25T06:29:49Z"},"description":"Add a readme","edges":[{"kind":"direct","target":"0000000000000000000000000000000000000000000000000000000000000000"}],"working_copy":true},{"author":{"email":"","name":"","time":"1970-01-01T00:00:00Z"},"change":"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz","change_short":"zzzzzzzz","commit":"0000000000000000000000000000000000000000000000000000000000000000","committer":{"email":"","name":"","time":"1970-01-01T00:00:00Z"},"description":"","edges":[]}]}
search -> {"commit":"19bf1edd970677fb8cc7fcb529f43d7ddb04bacec9fb67fe642022f9849af30e","files":[{"lines":[{"number":1,"text":"Written by an agent"}],"path":"README"}]}
read -> {"commit":"19bf1edd970677fb8cc7fcb529f43d7ddb04bacec9fb67fe642022f9849af30e","kind":"file","path":"README","size":20,"text":"Written by an agent\n"}
```

## Undo a step

An agent that makes a mistake takes it back with `undo`: by default its own latest operation. Here it starts a change by mistake, and undoes the start. The answer to `undo` names the operation it published and the one it took back; this lab leaves it out, since how many digits of an ID name it alone changes with every log, and shows `status` after:

```console check
$ ~/agent-session ~/agent <<'END' | grep -v '^undo'
> open_workspace {"patterns":["**"]}
> start_change {"description":"A change I did not mean","author_name":"Lesson Agent","author_email":"agent@localhost"}
> undo {}
> status {}
> END
open_workspace -> {"directory":"/home/you/agent/files","landing":1,"patterns":["**"],"written":0}
start_change -> {"change":"kuusopppvqqqtmtrkyvlpmzpkrulsyqm","commit":"ece6c7395a1e32dd44ca92416b004f6533a7623913fd65db8bd144912220a23a"}
status -> {"change":"kuusopppvqqqtmtrkyvlpmzpkrulsyqm","commit":"ece6c7395a1e32dd44ca92416b004f6533a7623913fd65db8bd144912220a23a","conflicts":[],"findings":0,"findings_partial":false,"landing":1,"refused":[],"state":"abandoned","waiting_for":[],"withheld":[]}
```

The change the agent started is abandoned: the undo took its start back. An abandoned change waits for nothing, since it will never land.

## Exercises

1. Have the agent find every line of main that says `Written`. What does `search` return besides the lines?

   ```console check
   $ ~/agent-session ~/agent <<'END'
   > open_workspace {"patterns":["**"]}
   > search {"pattern":"^Written"}
   > END
   open_workspace -> {"directory":"/home/you/agent/files","landing":1,"patterns":["**"],"written":0}
   search -> {"commit":"19bf1edd970677fb8cc7fcb529f43d7ddb04bacec9fb67fe642022f9849af30e","files":[{"lines":[{"number":1,"text":"Written by an agent"}],"path":"README"}]}
   ```

   <details>
   <summary>Answer</summary>

   The landing of main it searched, as `commit`: main may have moved since. Each file comes with its matching lines and their numbers.

   </details>

2. Call a tool before opening the workspace. What does the forge answer?

   ```console check
   $ ~/agent-session ~/agent <<'END'
   > status {}
   > END
   status -> {"code":-32602,"message":"open the workspace first (open_workspace)"}
   ```

## Check your understanding

<details>
<summary>Why does the client wait for each answer before sending the next call?</summary>

The server takes calls as they come and may answer them in any order. A `wait` sent before its `submit` was answered could run first, and would answer `open`: the change is not submitted, so nothing would come of waiting. So an agent sends one call at a time, as MCP clients do.

</details>

<details>

<summary>

What does `wait` answer for a change that has landed, or was never submitted?

</summary>

At once, it answers `landed`, or `open` with the advice to submit. `wait` waits only on a submitted change, since only one can still land, fail its checks, conflict or be approved without the agent doing anything. Asked again about a landed change, it says `landed` again rather than hang.

</details>

<details>
<summary>The change's author is "Lesson Agent". Who does the forge record as its writer?</summary>

Whoever `af mcp` acts for: here, you. An agent that should answer for its own work needs its own identity, born from yours, so that the record names it and its line back to you. See [People and agents](https://docs.forge.workreef.dev/concepts/agents/).

</details>

## Next

[Agent lesson 2: An agent of its own](https://docs.forge.workreef.dev/learn/agents/an-agent-of-its-own/) gives an agent its own key, identity and limits.

Source: https://docs.forge.workreef.dev/learn/agents/drive-the-forge/index.mdx
