Why is `claude -p` in a repository you didn't write riskier than an interactive Claude Code session, and how do you lock it down?
answer
- no dialog to show
- counts as trusted for settings hooks
- MCP servers connect unasked
- user-level disableAllHooks gets overridden
- --bare plus --setting-sources user
basics
~20 sclaude -p never shows the trust dialog: it runs a repository's settings-file hooks, env block and apiKeyHelper, and connects its .mcp.json servers unasked. Lock it down with --bare and --setting-sources user, or pass disableAllHooks through --settings.
solid answer
~40 sAn interactive session stops at the trust dialog; `claude -p` and SDK sessions never show it and treat the folder as trusted for hooks in settings files, the `env` block and helpers such as `apiKeyHelper`. They also connect `.mcp.json` servers without asking, approved or not, and apply project skills' `allowed-tools` and skill hooks. What `-p` still withholds without real folder trust: `permissions.allow` and `additionalDirectories` (with a `this workspace has not been trusted` warning), project subagents' frontmatter hooks and inline MCP servers, and, since 2.1.238, a server's `headersHelper`. To lock a run down, start with `--bare` (no project hooks, skills, subagents, plugins or `.mcp.json`) and add `--setting-sources user` so the project's settings files, `env` included, stay out; or pass `--settings '{"disableAllHooks": true}'` and name unwanted servers in `disabledMcpjsonServers`.
code
bash · 5 lines# Read a third-party checkout without loading what it ships
export ANTHROPIC_API_KEY="$(cat ~/.config/claude-ci-key)"
claude --bare --setting-sources user \
-p "Summarize what README.md says the build needs" \
--allowedTools "Read"go deeper
Recall that claude -p never shows the trust dialog, so a repository's hooks and .mcp.json servers can run in a script without anyone being asked.
Explain which content -p uses and which it withholds, and why claude mcp list's pending status says nothing about a -p run.
Lock a run down with the right combination, --bare with --setting-sources user or --settings disableAllHooks, and know the precedence reason user-level settings fail.
Decide when a pipeline should run on repositories it doesn't control at all, and when hand-set folder trust in ~/.claude.json is an acceptable, auditable exception.
## Why `-p` changes the trust picture The **workspace trust dialog** is how an interactive Claude Code session asks you before a repository's own configuration can act. A `claude -p` run (print mode, used in scripts and CI) and an Agent SDK session **cannot show a dialog**, so Claude Code makes a fixed decision instead: for some repository content the run **counts as trusted**, and for the rest it **withholds** the content. That is why the same clone that sits safely at a prompt in your terminal can execute its hooks the moment a script runs `claude -p` in it. The risk is sharpest in automation, because nobody is watching: a CI job or a scheduled script that runs `claude -p` across checkouts it didn't write executes whatever each repository's settings declare, with the job's environment and credentials available to those commands. A pull request that adds a hook to `.claude/settings.json` is enough. ## What a never-trusted repository still gets under `-p` | Repository content | Under `claude -p`, folder never trusted | |---|---| | Hooks in settings files, the `env` block, helpers such as `apiKeyHelper` | **Used** | | A project skill's `allowed-tools` and its hooks | **Used**; trust never gates `allowed-tools` in any session | | Servers in `.mcp.json` | **Connected without asking**, approved or not | | `permissions.allow`, `additionalDirectories` in `.claude/settings.json` | Not used; prints `this workspace has not been trusted` to stderr | | Project subagent frontmatter hooks and inline `mcpServers` | Not used | | A project `@skills-dir` plugin, `extraKnownMarketplaces` | Not used | | A server's `headersHelper` | Not run; the server connects with static `headers` and a `headersHelper not run` line is printed | One detail misleads people: `claude mcp list` in the same folder still reports such a server as **pending approval**, yet a `-p` run connects it. The listing reflects the interactive approval state, not what print mode loads. ## Locking a run down Pick controls by what they keep out: - **`--bare`** skips auto-discovery of hooks, skills, custom commands, subagents, installed plugins, MCP servers, auto memory and CLAUDE.md. It does **not** stop the project's `env` block or helpers such as `awsAuthRefresh` in its settings files, and it reads `apiKeyHelper` only from `--settings`. Bare mode never reads OAuth credentials, so set `ANTHROPIC_API_KEY` or pass an `apiKeyHelper` in `--settings`. - **`--setting-sources user`** (or the SDK's `settingSources` without project settings) means Claude Code reads neither the project's settings files nor its `.mcp.json`. - **`--settings '{"disableAllHooks": true}'`** turns hooks off for that run. Putting `disableAllHooks` in your **user** settings is not enough: the repository's project settings take precedence over yours and can set it back to `false`. Values passed with `--settings` override the settings files for the session. - **`disabledMcpjsonServers`** rejects a named `.mcp.json` server in every session type. - **`--strict-mcp-config`** limits the run to servers passed with `--mcp-config`. A tight combination for reading a third-party checkout is `--bare` with `--setting-sources user`: the first keeps the project's hooks, skills, subagents and servers out, the second keeps its settings files, `env` block included, out. ## Trusting a folder deliberately When a pipeline **should** use a repository's allow rules or helpers that need real trust, trust the folder by hand: set `projects["<path>"].hasTrustDialogAccepted` to `true` in `~/.claude.json`, with `<path>` the repository root. Claude Code prints the exact key in the stderr warning for skipped allow rules and in the `headersHelper not run` line. Do this only for repositories you control. ## How the rules got here The split has tightened across releases, which is why older blog advice disagrees with current behaviour: 1. **2.1.196**: `claude mcp list` and `claude mcp get` stopped honoring a repository's self-approval of its servers in untrusted folders. 2. **2.1.218**: project subagent frontmatter hooks began requiring the agent file's own folder trust. 3. **2.1.238**: a `.mcp.json` `headersHelper` and inline subagent MCP servers began requiring that folder's trust, also under `claude -p`. The answer above assumes the 2.1.286 behaviour. Scripting choices beyond trust (output formats, turn and budget limits, which permission mode a script runs in) belong to the headless-mode material.
- Why isn't `disableAllHooks: true` in your user settings enough to stop a repository's hooks under `claude -p`?The repository's `.claude/settings.json` takes precedence over your user settings and can set `disableAllHooks` back to `false`. Pass it with `--settings '{"disableAllHooks": true}'`, which overrides the settings files for that run, or keep project settings out with `--setting-sources user`.
- What can the project still influence in a `claude --bare -p` run?The project's `env` block and helpers such as `awsAuthRefresh` in its settings files still apply, and skills in a directory you pass with `--add-dir` still load. Bare mode reads `apiKeyHelper` only from `--settings`. Add `--setting-sources user` to keep the project's settings files out too.
- `claude mcp list` shows a repository's server as pending approval. Will a `claude -p` run in that folder connect it?Yes. Print mode can't show the per-server prompt, so it connects `.mcp.json` servers without asking, approved or not, while `claude mcp list` keeps reporting the interactive approval state. Reject the server with `disabledMcpjsonServers` or exclude project settings to keep it out.
- claude -p in an untrusted folder runs nothing from the repository until someone approves it.
- disableAllHooks in ~/.claude/settings.json reliably stops a repository's hooks under -p.
- A server listed as pending approval by claude mcp list won't connect in a -p run.
- --bare ignores everything the project ships, its env block included.
- Because -p counts as trusted, it also applies the repository's permissions.allow rules.
Assumes Claude Code 2.1.286; headersHelper and inline subagent MCP servers stopped running under -p without folder trust in 2.1.238, subagent frontmatter hooks in 2.1.218.