---
title: "Use varsafe safely with coding agents"
sidebar:
  icon: shield-check
  label: "Agent safety"
description: "Harden Claude Code, Cursor or Codex around your secrets: keep values out of the conversation, narrow both the MCP grant and the CLI login, and add client-side guardrails."
---

<PageMeta />

A coding agent can do real secrets work for you: compare environments, generate keys, run your code with the right environment loaded. It also reads whatever lands in its context and acts on it. This guide shows how to set things up so that the agent gets the work done with as little access as possible, and so that secret values stay out of the conversation.

For connecting a client in the first place, see [AI agents (MCP)](/guides/mcp).

## What can go wrong

- **Anything in the agent's context can be logged.** A value the agent reads through a tool, a file it opens or a command output it sees is sent to the model. It can be kept in your client's history and in the model provider's logs, outside varsafe's control.
- **Content can steer the agent.** A README, an issue, a code comment or a web page the agent reads can contain instructions written to make it do something else (prompt injection). varsafe tells agents to treat secret names and values as data, but it cannot stop an agent from following text it found elsewhere.
- **An agent with a shell has your CLI login.** The MCP grant limits MCP tools only. When the agent runs `varsafe` in its shell, the CLI uses the credential stored on that machine, usually your own `varsafe login`, with everything that login allows. Those commands are audited as **CLI** activity under your name, not as agent activity.

The rest of this guide narrows each of these: what the agent asks for, what the MCP grant allows, what the CLI credential allows, and what the client lets it run.

## Keep values out of the conversation

The hosted MCP endpoint sends every client [a set of rules](/guides/mcp#what-your-agent-is-told) that point agents at the safe paths. Ask for work that follows them:

- **The agent runs commands through `varsafe run`.** Rather than reading values to build a command, it runs `varsafe run -p <project> -e <environment> -- <command>` in its shell. The values reach that process as environment variables, not the conversation. Whatever the command prints still does, so it must not print secrets or dump the environment.
- **The agent generates keys with `varsafe_generate_secret`.** Rather than writing a key or password into a secret, it lets varsafe generate and store the value, and sees only its fingerprint.
- **You set third-party keys yourself.** Rather than pasting one into the chat, run `varsafe set <KEY>` in your terminal. The value goes from your terminal to varsafe and never passes through the agent.
- **You check a value in your own terminal.** Rather than having the agent read it, run `varsafe get <KEY>`. The value is shown to you, not to the agent.

`varsafe_get_secret_value`, `varsafe_get_secret_values` and `varsafe_export_secrets` are the MCP tools that return plaintext values in the tool result. `varsafe_export_secrets` returns a whole environment's values to the agent; it does not inject them into a process. A grant on **Read only** exposes none of them.

For example, a value only you have:

```bash
varsafe set STRIPE_SECRET_KEY -p my-api -e staging
```

At a terminal, `varsafe set` asks for the value with a masked prompt.

## Narrow the MCP grant

You choose the grant on the consent screen when the client first connects. See [Choose what the agent may do](/guides/mcp#choose-what-the-agent-may-do) for every option.

1. **Pick Read only when you can.** The agent can list projects, environments and secret names and compare environments. It cannot read a value or change anything. Reading values comes only with **Read & write**; there is no separate switch for it.
2. **Pick specific projects.** Leave a team on all projects only if the agent needs all of them.
3. **Pick specific environments.** Narrow each project to the environments the agent works in, for example `development` and `staging`, and leave `production` out. An environment outside the grant is refused, and left out of every list the agent sees.
4. **Cover only the teams the agent needs.** One approval can cover several teams; select only those.

To change a grant later, revoke the connection on the **Agents** page and approve the consent screen again.

## Narrow the CLI credential

Narrowing the MCP grant changes nothing for commands the agent runs in its shell. Check what the CLI on the agent's machine can do:

```bash
varsafe whoami
```

`whoami` shows the type of credential in use (a CLI login or an API token), its scopes, and the projects and environments it covers.

### Sign in at a lower access level

`varsafe login` opens an approval page in your browser with three access levels:

| Level | What the CLI can do |
| --- | --- |
| **Read only** | Browse projects, environments and secret names. `varsafe run`, `get` and `export` do not work. |
| **Standard** | What `varsafe run`, `get` and `export` need, values included. Nothing can change. |
| **Full access** | Adds creating, updating and deleting secrets, so `varsafe set` and `unset` work. |

On a machine where an agent runs commands, approve **Standard** rather than **Full access** unless the agent has to change secrets, and narrow the teams, projects and environments on the same page. Keep `varsafe set` for yourself, from a terminal where you approved **Full access**, or from the dashboard.

To replace an existing login with a narrower one:

```bash
varsafe login --force
```

`--force` starts a new browser approval even though you are already signed in. Once the new credential is stored, the CLI revokes the one it replaced.

**Standard** still lets the agent run `varsafe get` or `varsafe export --plain` and see values. To stop that, use the client-side guardrails below, or a credential that does not reach the environments that matter.

### Or give the machine a scoped API token

An [API token](/reference/api-tokens) can be limited to one project, chosen environments and **Read secrets**. Owners and admins create tokens in the dashboard under **API tokens**. The CLI reads it from `VARSAFE_API_TOKEN`:

```bash
export VARSAFE_API_TOKEN="vs_at_..."
varsafe whoami
```

:::warning[A stored login wins over the token]
The CLI uses a stored `varsafe login` first and reads `VARSAFE_API_TOKEN` only when no login is stored. On a machine where you are signed in, setting the variable changes nothing. Use a token where there is no stored login, such as a dev container or a separate user account, or run `varsafe logout` first. Then check with `varsafe whoami` that the token is the credential in use.
:::

`varsafe run` removes `VARSAFE_API_TOKEN` and `VARSAFE_TOKEN` from the environment of the command it starts, unless you pass `--pass-credential`, so the command cannot reuse the token.

## Add client-side guardrails

Your client can refuse commands before they run. In Claude Code, permission rules in `.claude/settings.json` do this. For example, to block the commands that print values and reads of `.env` files:

```json
{
  "permissions": {
    "deny": [
      "Bash(varsafe get:*)",
      "Bash(varsafe export:*)",
      "Read(./.env)",
      "Read(./.env.*)"
    ]
  }
}
```

This is Claude Code's configuration format; see Claude Code's documentation on permissions for the full rule syntax. In Cursor, Codex and other clients, use their own command-approval settings to require your approval for `varsafe get` and `varsafe export`.

:::note[A guardrail, not a security boundary]
Deny rules match how a command is written. An agent can reach the same values another way, for example `varsafe run -- printenv` or a script that calls the CLI. Treat client rules as a safety net for mistakes, and rely on the MCP grant and the CLI credential for what the agent can actually reach.
:::

## Review what the agent did

- **Audit log.** In the dashboard, open **Audit log**. Entries made through MCP tools are marked **Agent · MCP** and attributed to you, or to the token the client used. Value reads, changes and secret-name listings are recorded; comparing environments is not. Commands the agent ran through the CLI appear as **CLI** under the credential that machine used. See [Audit trail](/guides/mcp#audit-trail).
- **Agents.** Open **Agents** in the sidebar to see every connection you approved: the app and its publisher, the teams it covers, its scopes, and when it was last used. **Revoke** cuts an agent off at once.
- **CLI Sessions.** Your **Profile** lists every machine signed in with `varsafe login` as you. Revoke a machine you no longer trust.
- **API tokens.** Revoke a token on the **API tokens** page when the machine no longer needs it.

## Checklist

<div class="vs-checks" data-tone="yes">

- The MCP grant is **Read only**, or **Read & write** only where the agent has to change secrets.
- The grant covers specific projects and environments, and leaves out `production`.
- The CLI login on the agent's machine was approved at **Standard** or narrower: `varsafe whoami` lists no `write` scope, and only the environments the agent works in.
- If the machine uses an API token, no `varsafe login` is stored there, and `whoami` shows the token.
- You ask the agent to use `varsafe run` and `varsafe_generate_secret`, and you set third-party values yourself with `varsafe set`.
- Your client asks before running `varsafe get` or `varsafe export`, or denies them.
- `.env` files with real values are not readable by the agent.
- You check the audit log and the **Agents** page from time to time, and revoke what you no longer use.

</div>

## Related

- [AI agents (MCP)](/guides/mcp): connect a client, access levels and audit
- [API tokens](/reference/api-tokens): scoped credentials for machines
- [Authentication](/reference/authentication): CLI login, storage and sign-out
- [Roles and permissions](/reference/roles): what each role can do
