Safety Controls for Scripts and Agents
A script or an AI agent that holds your Google token can do anything the token allows, including sending mail in your name. gog checks commands and requests at several points before they reach Google.
Use this chapter to choose an OAuth grant, block sending at 3 levels, restrict available commands, and compile restrictions into a binary. The agent setup at the end allows reading and drafting while blocking sending.
Every refusal message and exit code in this chapter was captured from gog v0.48.1 in a throwaway home directory (GOG_HOME=$(mktemp -d)) with no Google account signed in. The one exception is exit code 6 for a missing scope, which needs a real token; it comes from the way gog maps an HTTP 403 response in internal/cmd/exit_codes.go.
The checks at a glance
When gog parses a command, it runs a fixed series of checks before any data leaves your machine, and Google adds a final check of its own. The order matters: an earlier check stops the command before a later one is consulted, so the first refusal you see is not necessarily the only one that would apply. The order below follows the run function in internal/cmd/root.go.
Figure 8.1. A command meets the baked profile, the command lists and the send guard before its handler runs; a dry run ends inside the handler; the read-only guard inspects each HTTP request; Google finally checks the OAuth scopes of the token.
| Check | Set by | Who can change it | Refusal exit code |
|---|---|---|---|
| Baked safety profile | build-safe.sh at build time | only a rebuild | 2 |
| Command lists | --enable-commands, --enable-commands-exact, --disable-commands or their environment variables | the caller, on every run | 2 |
| Gmail send guard | --gmail-no-send, GOG_GMAIL_NO_SEND, config key gmail_no_send, gog config no-send set | the caller for the flag; for the config entries, whoever can edit config.json or point gog at another config directory | 2 |
| Runtime read-only guard | --readonly, GOG_READONLY | the caller, on every run | 2 |
| OAuth scopes | gog auth add | only a new authorization | 6 (from Google) |
An OAuth scope is a named permission that you approve on Google’s consent screen. It is written as a URL such as https://www.googleapis.com/auth/gmail.readonly. Google limits the actions a token can authorize to its granted scopes.
OAuth scopes chosen at gog auth add
Scopes are the only check that Google enforces, so they are the one control that still holds if a caller swaps the gog binary or edits its configuration. You choose them when you authorize an account, and the stored refresh token carries exactly that grant. A refresh token is the long-lived credential that gog exchanges for short-lived access tokens; gog keeps it in its keyring, which is either the operating system’s secure credential store or an encrypted file. Authentication and Accounts covers the sign-in flow; this section concentrates on the flags that narrow the grant.
The scopes are decided by 3 flags of gog auth add:
--servicesselects which Google services to authorize, for examplegmail,calendar. The default valueusermeans all default user services, which is far more than a mail script needs.--gmail-scopeselects the Gmail grant:full,readonly,sendorread-send.- The root flag
--readonlyasks for the read-only variant of every selected service that has one.
gog auth add accepts --dry-run, which prints the exact scopes it would request without opening a browser. That makes it a reliable way to compare grants offline:
$ gog --dry-run --json --readonly auth add you@example.com --services gmail,calendar --no-input | jq '.request.scopes'
[
"email",
"https://www.googleapis.com/auth/calendar.readonly",
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/userinfo.email",
"openid"
]
Without --readonly, the same command requests calendar, gmail.modify, gmail.settings.basic and gmail.settings.sharing. The 4 Gmail modes request the following Gmail scopes (captured the same way, with --services gmail):
--gmail-scope | Gmail scopes requested | Suits |
|---|---|---|
full (default) | gmail.modify, gmail.settings.basic, gmail.settings.sharing | reading, labels, drafts and sending |
readonly | gmail.readonly | reading only |
send | gmail.send | sending only |
read-send | gmail.readonly, gmail.send | reading and sending, but no drafts |
The gog documentation implies 2 limits. First, a gmail.readonly grant cannot create drafts: docs/gmail-workflows.md states that draft writes need gmail.compose, gmail.modify or https://mail.google.com/, and a token with only gmail.send cannot write drafts either.
Second, gog offers no Gmail scope mode that allows drafts while excluding sending, so a job that must draft but never send has to rely on gog’s own guards for the send block. You can add individual scope URLs with --extra-scopes, but how Google treats each extra scope is outside what gog documents.
Tokens are stored per OAuth client name, so one account can hold a narrow token and a broad token side by side. An OAuth client is the client ID and secret, downloaded from the Google Cloud console as a JSON file, that identify gog to Google during sign-in. The client name is the label you pass with --client (or GOG_CLIENT); it selects both a credentials file and a separate token bucket, as described in docs/auth-clients.md:
gog --client reader auth credentials ~/Downloads/client_secret.json
gog --client reader --readonly auth add you@example.com --services gmail,calendar
Every later command that passes --client reader uses the read-only token. The recommended agent setup at the end of this chapter builds on this.
The runtime read-only guard
The root flag --readonly (or GOG_READONLY=1) has a second job besides narrowing auth add. On every other command it inspects each HTTP request just before gog sends it and rejects mutating requests; GET, HEAD, OPTIONS and a small allowlist of query endpoints that Google implements as POST still pass (docs/automation.md). The guard checks the HTTP request regardless of its OAuth scopes or the command that generated it.
The guard checks requests at the HTTP layer after gog loads credentials. In a home with no credentials, --readonly gmail send therefore fails earlier, with the missing-credentials error and exit code 10. To capture the guard’s own message, the run below passes a dummy direct access token (--access-token), which lets gog reach the dispatch step:
$ gog --account you@example.com --access-token fake-token --readonly gmail send --to friend@example.com --subject t --body b
Note: Using direct access token (expires in ~1 hour; no auto-refresh)
Post "https://gmail.googleapis.com/gmail/v1/users/me/messages/send?alt=json&prettyPrint=false": request blocked by --readonly: POST /gmail/v1/users/me/messages/send
$ echo $?
2
Dry runs and raw-message drafts reach different checks.
--dry-runends a command inside its handler, before any request is built, sogog --readonly --dry-run gmail send ...prints its plan and exits 0. A dry run stops before testing the read-only guard.- Raw-message drafts (
gmail drafts create --raw-file) are refused earlier, before authentication, withrequest blocked by --readonly: Gmail draft mutations are disabledand exit code 2.
The Gmail send guard
The send guard blocks every gog command that can deliver mail. It recognizes these command paths, listed in internal/cmd/gmail_no_send.go: the root shortcut send, gmail send, gmail reply, gmail reply-all, gmail forward, gmail autoreply and gmail drafts send. Draft commands such as gmail drafts create, gmail drafts reply and gmail drafts forward remain available.
You can switch the guard on in 4 places:
| Where | How | Scope |
|---|---|---|
| Flag | --gmail-no-send | one run |
| Environment | GOG_GMAIL_NO_SEND=1 | every run in that environment |
| Config key | gog config set gmail_no_send true | every run that reads this config.json |
| Per account | gog config no-send set you@example.com | every run for that account |
The guard runs before the command handler, so it holds under --dry-run as well:
$ gog --account you@example.com --gmail-no-send gmail send --to friend@example.com --subject t --body b
Gmail sending is blocked by --gmail-no-send
$ echo $?
2
$ gog --account you@example.com --gmail-no-send --dry-run gmail send --to friend@example.com --subject t --body b
Gmail sending is blocked by --gmail-no-send
$ echo $?
2
With --json, the same refusal also appears on standard output as an error object whose class is usage:
{"error":{"class":"usage","code":2,"message":"Gmail sending is blocked by --gmail-no-send"}}
The config entries name their source in the message:
$ gog config no-send set you@example.com
No-send enabled for you@example.com
$ gog --account you@example.com gmail send --to friend@example.com --subject t --body b
Gmail sending is blocked for you@example.com (config no-send)
$ gog config set gmail_no_send true
Set gmail_no_send = true
$ gog --account you@example.org gmail send --to friend@example.com --subject t --body b
Gmail sending is blocked by config gmail_no_send
These runs show 2 behaviors that matter in practice:
- The flag cannot cancel a config entry.
gog --gmail-no-send=false --account you@example.org gmail send ...still fails withGmail sending is blocked by config gmail_no_send, because the flag can only add the guard. The entries live in one file, though: a caller that setsGOG_CONFIG_DIRorGOG_HOME, or passes--home, to another directory runs without them, and the same dry-run send then exits 0. WithGOG_CONFIG_DIRalone, the tokens in the data directory stay usable. Config entries therefore guard against mistakes in commands; against a caller that controls its own environment, only a baked profile and the token’s scopes hold. - Register per-account guards under the email address. An account alias is a short name for an address, set with
gog auth alias set. In v0.48.1,gog config no-send set workwith the aliasworkforyou@example.orgis accepted and listed, yet a latergog --account work --dry-run gmail send ...is not blocked. The same guard stored asyou@example.orgblocks the account whether you select it by alias or by address.
Command allowlists and denylists
The command lists decide which gog commands may run at all. They use dot paths, so gmail.drafts.create names the command gmail drafts create.
| Flag | Environment variable | Meaning |
|---|---|---|
--enable-commands | GOG_ENABLE_COMMANDS | allow these commands and everything below them (prefix match) |
--enable-commands-exact | GOG_ENABLE_COMMANDS_EXACT | allow exactly these commands; a parent does not enable its children |
--disable-commands | GOG_DISABLE_COMMANDS | block these commands and everything below them |
If either allowlist is set, a command must match one of them; the denylist is applied afterward. The refusals look like this (internal/cmd/enabled_commands.go):
$ gog --account you@example.com --enable-commands-exact gmail.search gmail send --to friend@example.com --subject t --body b
command "gmail send" is not enabled (set --enable-commands or --enable-commands-exact to allow it)
$ echo $?
2
$ gog --account you@example.com --disable-commands gmail.send gmail send --to friend@example.com --subject t --body b
command "gmail send" is disabled (blocked by --disable-commands)
$ echo $?
2
gog matches the parsed command path. These 3 examples were verified with --dry-run.
--disable-commands gmail.senddoes not block the root shortcutsend, norgmail reply,gmail reply-allorgmail forward. All of these deliver mail and have their own paths.--enable-commands gmail.draftsis a prefix rule, so it admitsgmail drafts send.--enable-commands-exact gmailadmits only the baregmailcommand;gmail sendis still refused.
A denylist therefore has to anticipate every path that leads to the action you want to prevent, while an exact allowlist only has to name what the job needs. For unattended jobs, prefer --enable-commands-exact with leaf commands, and combine it with the send guard, which already knows all the send paths.
Safety profiles and baked builds
A caller can omit runtime flags. A safety profile is a YAML policy that build-safe.sh compiles into a dedicated gog binary. Flags, environment variables, and configuration files cannot relax that policy (docs/safety-profiles.md).
The source tree ships 3 presets in safety-profiles/:
| Profile | Allows | Blocks |
|---|---|---|
agent-safe | reading, searching, drafting, labels, archiving and other recoverable actions, plus schema | sending, deleting, sharing changes, admin operations and auth writes |
readonly | read, list, search and get commands, plus schema | every mutation, including drafts and local config writes |
full | everything | nothing; useful only to test the build |
A profile mirrors the command tree. true allows a path, false blocks it, an explicit block beats an allowed parent, and once a profile contains any allow rule, everything it does not mention is blocked. An excerpt of agent-safe.yaml:
name: agent-safe
gmail:
search: true
send: false
drafts:
list: true
create: true
reply: true
send: false
A profile can also pin boolean flags under locked-flags, for example sanitize-content: true or no-input: true, so that an agent receives sanitized output whether or not it asks for it.
To build a binary, run the script from a checkout of the source at the release you use; it needs a Go toolchain:
./build-safe.sh safety-profiles/agent-safe.yaml -o bin/gog-agent-safe
The script validates the profile, generates a Go file with the rules, builds with the safety_profile tag, runs the result with --version and deletes the generated file. A binary built from v0.48.1 reports its version with a -safe suffix. Its refusals name the profile, and runtime flags cannot override them:
$ bin/gog-agent-safe --account you@example.com gmail send --to friend@example.com --subject t --body b
command "gmail send" is blocked by baked safety profile "agent-safe"
$ echo $?
2
$ bin/gog-agent-safe --account you@example.com --enable-commands gmail.send gmail send --to friend@example.com --subject t --body b
command "gmail send" is blocked by baked safety profile "agent-safe"
$ bin/gog-agent-safe --account you@example.com --dry-run gmail drafts send r123
command "gmail drafts send" is blocked by baked safety profile "agent-safe"
$ bin/gog-readonly --account you@example.com --dry-run gmail drafts create --to friend@example.com --subject t --body b
command "gmail drafts create" is blocked by baked safety profile "readonly"
The same agent-safe binary accepts --dry-run gmail drafts create ... and prints the plan with exit code 0. Asking it for help on a blocked command prints the block message instead of the help page, and gog schema on such a binary lists only the allowed commands.
How a baked profile differs from the runtime flags:
| Property | Runtime flags and config | Baked profile |
|---|---|---|
| Where the policy lives | the command line, the environment or config.json | inside the executable |
| Who can relax it | anyone who runs gog or edits its config | only someone who rebuilds the binary |
Effect on help and gog schema | none | blocked commands disappear from both |
| When it is checked | after parsing, after the profile | first, right after parsing |
| Cost | none | a Go build per profile and per release |
A baked profile restricts commands in that binary. Use narrow scopes because any other program with access to the token can do everything Google allows under those scopes.
Checking the effective policy
gog schema prints gog’s machine-readable description of its commands, flags, exit codes and output formats, and it also reports the safety state of the invocation it is part of, under .automation.safety (docs/automation.md). Pass the same global flags that the real job uses:
$ gog --gmail-no-send --readonly --enable-commands-exact schema,gmail.search,gmail.drafts.create schema | jq '.automation.safety | {gmail_no_send, readonly, command_rules, baked_profile}'
{
"gmail_no_send": true,
"readonly": true,
"command_rules": {
"enabled_prefixes": [],
"enabled_exact": [
"gmail.drafts.create",
"gmail.search",
"schema"
],
"disabled": []
},
"baked_profile": {
"enabled": false
}
}
On the agent-safe binary, baked_profile reads {"enabled": true, "name": "agent-safe"}. A preflight step in a script can assert these values with jq -e before it runs the real command, as Automation and Troubleshooting shows. The snapshot describes one invocation only; it does not prove that the token is valid or what its scopes are.
Giving gog to an AI agent
An agent can reach gog in 2 ways: by running the CLI through a shell tool, guided by an agent skill, or by calling typed tools through gog’s MCP server. Both paths retain the checks in Figure 8.1. The shell exposes the CLI, and the server exposes a fixed set of tools.
Agent skills
An agent skill is a short instruction file that teaches a coding agent which commands exist and how to use them. gog publishes a core skill, one skill per service and a few hand-written workflow skills, such as gog-inbox-triage (prioritize unread mail without sending), in its repository; docs/agent-skills.md gives the install command for clients that support the Agent Skills format:
npx skills add https://github.com/openclaw/gogcli --skill gog gog-gmail
A skill only describes commands. The agent still runs whatever binary and flags its shell provides, so pair skills with an environment that enforces the policy: a baked binary on the agent’s PATH, GOG_GMAIL_NO_SEND=1 and an exact command allowlist in its environment.
The MCP server
gog mcp runs a Model Context Protocol server over standard input and output. MCP is a protocol through which an agent client discovers and calls tools that a server describes with fixed argument schemas. gog’s server exposes a small set of typed tools, each mapped to one gog operation, and deliberately offers no tool that runs arbitrary gog commands (docs/mcp.md).
--list-tools lists the registered tools without starting the server, which makes it easy to verify a configuration offline. With no other options, only read tools are registered:
$ gog --account you@example.com mcp --list-tools | jq -r '.tools[].name'
gmail_search
gmail_get_message
gmail_get_thread
drive_search
drive_get
docs_get
sheets_read_range
calendar_events
gmail_list_drafts
gmail_get_draft
gmail_list_labels
These server flags control which tools are registered.
--allow-toolnarrows the set to named tools or services, for examplegmailorgmail_search,calendar_events.--allow-writeadds write tools that match--allow-tool. For Gmail these includegmail_create_draft,gmail_update_draft, label changes, archiving and trashing.--allow-gmail-sendalso registersgmail_send_messageandgmail_send_draft;--allow-gmail-deleteregisters permanent deletion.
The root safety flags apply as well. --readonly removes every write tool from the list, and the server passes --gmail-no-send and the command lists on to each child command it runs.
When --allow-gmail-send and --gmail-no-send are both present, the send tools stay listed, and a call to gmail_send_message returns exit_code 2 with Gmail sending is blocked by --gmail-no-send in stderr. Selecting only write tools without --allow-write registers nothing:
$ gog --account you@example.com mcp --allow-tool gmail_create_draft --list-tools
no MCP tools enabled
$ echo $?
2
Configuring an agent for reads and drafts
The aim is an agent that can read mail and calendars, prepare reply drafts, and has no path to sending. Because no Gmail scope mode allows drafts without sending, the setup splits the work across 2 tokens and adds gog’s guards on top:
- Reading uses a read-only token. Authorize a separate client name with
--readonly, as shown earlier:gog --client reader --readonly auth add you@example.com --services gmail,calendar. Run every read with--client reader --readonly. - Drafting uses the default token under guards. The default
fullGmail grant can write drafts. Register a per-account guard withgog config no-send set you@example.com, run draft commands with--gmail-no-send, and restrict them with--enable-commands-exact gmail.drafts.create(addgmail.drafts.replyorgmail.drafts.updateif the agent needs them). - The agent’s binary cannot send. Give the agent a binary built from
safety-profiles/agent-safe.yaml, so that a forgotten flag or a different config directory still meets the baked block. - After reviewing a draft, send it in Gmail or run
gog gmail drafts sendfrom your own shell with the stock binary. Run the agent in an environment, such as a container or a separate user account, that contains the baked binary but not the stock one, since an agent with a shell can call any executable it can reach.
For an MCP client, the same policy becomes the server command line. Each MCP tool runs one gog child command (gmail_search runs gmail messages search, gmail_get_thread runs gmail thread get, calendar_events runs calendar events, and the draft tools run gmail drafts list and gmail drafts create), so an exact allowlist must name mcp itself plus those child commands. This configuration registers 5 tools, 1 of which writes, and keeps the send tools unregistered:
{
"command": "gog",
"args": [
"--account", "you@example.com",
"--gmail-no-send",
"--enable-commands-exact", "mcp,gmail.messages.search,gmail.thread.get,gmail.drafts.list,gmail.drafts.create,calendar.events",
"--no-input",
"mcp",
"--allow-write",
"--allow-tool", "gmail_search,gmail_get_thread,gmail_list_drafts,gmail_create_draft,calendar_events"
]
}
Run the same command with --list-tools to see the registered tools. The server registers gmail_search, gmail_get_thread, calendar_events, and gmail_list_drafts as read, and gmail_create_draft as write. With the root flag --dry-run added, a gmail_create_draft call returns the dry-run plan with exit_code 0, which shows that the allowlist admits the child command.
The preset agent-safe profile does not list mcp, so a binary baked from it refuses to start the server with command "mcp" is not included in baked safety profile "agent-safe". To bake an MCP policy, copy agent-safe.yaml, add mcp: true and build your own binary.
The worked example in Worked Example and Glossary applies the same restrictions to a shell script.