Run Claude Code locally
claude_run_localRuns Claude Code headless in an existing local checkout when tasks need the local toolchain or hardware, returning a background job ID to poll for results.
Instructions
Run Claude Code headless on this machine (claude -p) in an existing checkout, instead of a cloud session: for work that needs the local toolchain or hardware. cwd must be inside a CLAUDE_MCP_LOCAL_ROOTS directory after resolving symlinks; with no roots configured every call fails with LOCAL_RUNS_DISABLED. Unless CLAUDE_MCP_LOCAL_ALLOW_SHELL is set, the run uses Claude Code's --restricted mode: no shell or code tools, no WebFetch, no MCP servers, and the checkout's settings, hooks and .mcp.json are not loaded (needs Claude Code 2.1.248 or later). The run is a background job and this call returns within a few seconds: if status is not done, poll claude_get_session with job_id until local_job.status is done or the call returns an error. The final result has the session_id (pass it as resume to continue), the result text, turns, cost and usage, denied tools, and the files git status lists. permission_mode defaults to dontAsk, which denies anything not read-only or in allowed_tools; permission bypass is never used. Pre-approving Bash or using auto mode runs shell commands unasked and fails with LOCAL_SHELL_DISABLED unless CLAUDE_MCP_LOCAL_ALLOW_SHELL is set. claude_cancel_session stops a run.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"cwd": "<cwd>",
"prompt": "<prompt>"
},
"name": "claude_run_local"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cwd | Yes | Absolute directory to run in: an existing checkout inside a CLAUDE_MCP_LOCAL_ROOTS directory. Compared after resolving symlinks. | |
| model | No | Model alias (opus, sonnet, haiku, fable) or full model name. Omit for the CLI's default. | |
| prompt | Yes | Task for Claude Code. | |
| resume | No | session_id of an earlier local run to continue (claude --resume). Omit to start a new session. | |
| max_turns | No | Stop after this many agentic turns. | |
| timeout_s | No | Time limit for the run, in seconds. Omit to use CLAUDE_MCP_TIMEOUT_MS. | |
| allowed_tools | No | Built-in tools that run without asking (--allowedTools). Bash allows every shell command and needs CLAUDE_MCP_LOCAL_ALLOW_SHELL. | |
| permission_mode | No | dontAsk (default): only reads and allowed_tools run; anything that would prompt is denied. acceptEdits: also file edits and common filesystem commands in cwd. plan: read-only exploration. auto: a background classifier reviews each action, including shell commands; needs CLAUDE_MCP_LOCAL_ALLOW_SHELL. Permission bypass is not available. |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cli | Yes | exit_code, and the tail of stderr. stdout is empty when the result line was read (it is the session transcript), otherwise its last 4,000 characters. | |
| cwd | Yes | Directory the CLI ran in, after resolving symlinks. | |
| args | Yes | ||
| files | Yes | Paths git status lists after the run, plus paths that were listed before and are clean now. Changes Claude committed don't appear. Null until done, and when cwd is not a git work tree. | |
| usage | Yes | ||
| errors | Yes | Errors the CLI reported on an error result. | |
| job_id | Yes | Background job for this run. Pass it to claude_get_session until status is done. | |
| parsed | Yes | True when the CLI's final result line was read. | |
| result | Yes | Text of the final assistant message. | |
| status | Yes | starting: claude -p is running and has not reported a session yet. running: session_id is known and the CLI has not exited. done: the CLI exited and the result below is final. cancelled: claude_cancel_session stopped the run. | |
| subtype | Yes | Result subtype, such as success or error_max_turns. | |
| is_error | Yes | True when the run ended in an error state (max turns, an API error, a refusal). | |
| num_turns | Yes | ||
| elapsed_ms | Yes | Time since the run started, or its total duration once done. | |
| rate_limit | Yes | The latest rate-limit event the run reported, or null if none. | |
| restricted | Yes | True when the run used Claude Code's --restricted mode (CLAUDE_MCP_LOCAL_ALLOW_SHELL off): no shell or code tools, no WebFetch, no MCP servers, and the checkout's settings, hooks and .mcp.json not loaded. | |
| session_id | Yes | Local session id. Pass it as `resume` to continue this session. | |
| total_cost_usd | Yes | ||
| permission_mode | Yes | ||
| assistant_messages | Yes | Assistant messages seen so far. | |
| permission_denials | Yes | Tools whose calls were denied, by name. |