# fs_write

Create a file or replace it whole, atomically (a half-written file is never seen). Text or base64, up to 8 MiB. Replacing an existing file needs expect_sha256, its SHA-256 from fs_stat with hash=true or the sha256 the last fs_write or fs_edit of it returned: without it the answer is hash_required and nothing is written, and a file changed since is refused. create_only refuses an existing file. An existing file keeps its permissions. Paths holding keys and credentials are refused.

**In short:** Risk class [`write`](https://docs.cmdop.ai/reference/tools.md#risk-classes). Over MCP call `fs_write`; over HTTP send `"tool": "fs_write"` to [`POST /api/v1/call`](https://docs.cmdop.ai/reference/http/post-call.md).

## Arguments

Over MCP every tool also takes `machine`: the machine's name or id, optional when only one machine is online.

| Name | Type | Required | Meaning |
| --- | --- | --- | --- |
| `create_only` | `boolean` | no | Refuse when the file already exists. Default: `false`. |
| `data` | `string` | yes | The whole new content. |
| `encoding` | `"utf8" | "base64"` | no | How `data` is written: `utf8` (default) or `base64`. |
| `expect_sha256` | `string` | no | The file's SHA-256 from fs_stat with hash=true, or as the last fs_write or fs_edit of it returned. Required to replace an existing file; the write is refused when the file changed since or does not exist. |
| `mkdirs` | `boolean` | no | Create missing parent folders. Default: `false`. |
| `path` | `string` | yes | File on the machine. Absolute, or relative to the user's home folder. |

## Result

| Name | Type | Required | Meaning |
| --- | --- | --- | --- |
| `created` | `boolean` | yes | The file did not exist before. |
| `path` | `string` | yes | The file, as an absolute path. |
| `sha256` | `string` | yes | SHA-256 of the new content. |
| `size` | `integer` | yes | Bytes written. |

## Examples

**MCP**

```json
{
  "name": "fs_write",
  "arguments": {
    "machine": "<machine>",
    "path": "<path>",
    "data": "<data>"
  }
}
```

**HTTP**

```bash
curl -sS -X POST "https://<contour>.cmdop.ai/api/v1/call" \
  -H "Authorization: Bearer $CMDOP_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"machine":"<machine>","tool":"fs_write","args":{"path":"<path>","data":"<data>"}}'
```

## Limits

The call times out after 60 s (`timed_out`). Available since version 0.0.1. A refused or failed call returns one of the [error codes](https://docs.cmdop.ai/reference/errors.md).
