# CLI 参考

> AgentFS 命令行界面完整参考
> Canonical HTML: https://tursodb.cn/zh-cn/docs/agentfs/reference/cli/
> Upstream Source (MIT): https://docs.turso.tech/agentfs/reference/cli
> Language: zh-CN

---
`agentfs` 命令行工具的完整参考。

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">注意</div><div class="doc-callout-body">

如需最新参考，请查看仓库中的 [MANUAL.md](https://github.com/tursodatabase/agentfs/blob/main/MANUAL.md)。

</div></div>


## 安装

```bash
curl -fsSL https://github.com/tursodatabase/agentfs/releases/latest/download/agentfs-installer.sh | sh
```

## 命令

### agentfs init

初始化一个新的 agent 文件系统。

```bash
agentfs init [OPTIONS] [ID]
```

**参数：**

* `ID` - Agent 标识符（默认值：`agent-{timestamp}`）

**选项：**

| 选项 | 说明 |
| - | - |
| `--force` | 覆盖已存在的 agent 文件系统 |
| `--base <PATH>` | 叠加文件系统的基础目录 |
| `--sync-remote-url <URL>` | 远程 Turso 数据库 URL |

**示例：**

```bash
agentfs init my-agent
agentfs init my-overlay --base /path/to/project
```

***

### agentfs run

在沙箱环境中执行程序。

```bash
agentfs run [OPTIONS] <COMMAND> [ARGS]...
```

**选项：**

| 选项 | 说明 |
| - | - |
| `--session <ID>` | 用于持久化的命名会话 |
| `--allow <PATH>` | 允许对指定目录的写访问 |
| `--no-default-allows` | 禁用默认允许的目录 |
| `--experimental-sandbox` | 使用 ptrace 沙箱（Linux） |
| `--strace` | 显示被拦截的系统调用 |

**示例：**

```bash
agentfs run /bin/bash
agentfs run --session my-project python3 agent.py
agentfs run --allow /tmp --allow ~/.cache /bin/bash
```

***

### agentfs mount

挂载 agent 文件系统或列出挂载项。

```bash
agentfs mount [OPTIONS] [ID_OR_PATH] [MOUNT_POINT]
```

不带参数时，列出所有已挂载的文件系统。

**选项：**

| 选项 | 说明 |
| - | - |
| `-a, --auto-unmount` | 退出时自动卸载 |
| `--allow-root` | 允许 root 用户访问 |
| `-f, --foreground` | 在前台运行 |
| `--uid <UID>` | 文件的用户 ID |
| `--gid <GID>` | 文件的组 ID |

**示例：**

```bash
agentfs mount                              # List mounts
agentfs mount my-agent ./mnt               # Mount agent
agentfs mount my-agent ./mnt -a -f         # Foreground with auto-unmount
```

**卸载：**

* Linux：`fusermount -u <MOUNT_POINT>`
* macOS：`umount <MOUNT_POINT>`

***

### agentfs serve mcp

启动 MCP（Model Context Protocol）服务器。

```bash
agentfs serve mcp <ID_OR_PATH> [OPTIONS]
```

**选项：**

| 选项 | 说明 |
| - | - |
| `--tools <TOOLS>` | 以逗号分隔的工具列表 |

**可用工具：**

| 类别 | 工具 |
| - | - |
| 文件系统 | `read_file`, `write_file`, `readdir`, `mkdir`, `remove`, `rename`, `stat`, `access` |
| 键值存储 | `kv_get`, `kv_set`, `kv_delete`, `kv_list` |

**示例：**

```bash
agentfs serve mcp my-agent
agentfs serve mcp my-agent --tools read_file,readdir,stat
```

***

### agentfs serve nfs

启动 NFS 服务器。

```bash
agentfs serve nfs <ID_OR_PATH> [OPTIONS]
```

**选项：**

| 选项 | 说明 |
| - | - |
| `--bind <IP>` | 绑定的 IP（默认值：`127.0.0.1`） |
| `--port <PORT>` | 端口（默认值：`11111`） |

**示例：**

```bash
agentfs serve nfs my-agent --bind 0.0.0.0 --port 2049
```

**挂载：**

```bash
mount -t nfs -o vers=3,tcp,port=11111,mountport=11111,nolock localhost:/ /mnt
```

***

### agentfs sync

与远程 Turso 数据库同步。

```bash
agentfs sync <ID_OR_PATH> <SUBCOMMAND>
```

**子命令：**

| 命令 | 说明 |
| - | - |
| `pull` | 拉取远程变更 |
| `push` | 推送本地变更 |
| `stats` | 查看同步统计信息 |
| `checkpoint` | 创建检查点 |

**示例：**

```bash
agentfs sync my-agent pull
agentfs sync my-agent push
```

***

### agentfs fs

对 agent 数据库执行文件系统操作。

#### agentfs fs ls

```bash
agentfs fs ls <ID_OR_PATH> [FS_PATH]
```

列出文件。输出：文件显示为 `f <name>`，目录显示为 `d <name>`。

#### agentfs fs cat

```bash
agentfs fs cat <ID_OR_PATH> <FILE_PATH>
```

显示文件内容。

#### agentfs fs write

```bash
agentfs fs write <ID_OR_PATH> <FILE_PATH> <CONTENT>
```

将内容写入文件。

**示例：**

```bash
agentfs fs ls my-agent
agentfs fs cat my-agent /config.json
agentfs fs write my-agent /hello.txt "Hello, world!"
```

***

### agentfs diff

显示叠加模式下的文件系统变更。

```bash
agentfs diff <ID_OR_PATH>
```

***

### agentfs timeline

显示 agent 操作时间线。

```bash
agentfs timeline [OPTIONS] <ID_OR_PATH>
```

**选项：**

| 选项 | 说明 |
| - | - |
| `--limit <N>` | 限制条目数量（默认值：100） |
| `--filter <TOOL>` | 按工具名称过滤 |
| `--status <STATUS>` | 过滤条件：`pending`、`success`、`error` |
| `--format <FORMAT>` | 输出格式：`table`、`json` |

**示例：**

```bash
agentfs timeline my-agent
agentfs timeline my-agent --filter web_search --status error
agentfs timeline my-agent --format json
```

***

### agentfs completions

管理 shell 自动补全。

```bash
agentfs completions install [SHELL]
agentfs completions uninstall [SHELL]
agentfs completions show
```

支持：`bash`、`zsh`、`fish`、`powershell`

## 环境变量

在沙箱内设置的环境变量：

| 变量 | 说明 |
| - | - |
| `AGENTFS` | 在沙箱内设置为 `1` |
| `AGENTFS_SANDBOX` | 沙箱类型 |
| `AGENTFS_SESSION` | 会话 ID |

## 文件

| 路径 | 说明 |
| - | - |
| `.agentfs/<ID>.db` | Agent 文件系统数据库 |
| `~/.config/agentfs/` | 配置目录 |

## 另请参阅

<div class="doc-cards">
<a class="doc-card" href="/zh-cn/docs/agentfs/installation/"><div class="doc-card-title"><span>安装</span><span class="doc-card-arrow" aria-hidden="true">→</span></div><p class="doc-card-desc">安装说明</p></a>
<a class="doc-card" href="/zh-cn/docs/agentfs/guides/sandbox/"><div class="doc-card-title"><span>沙箱指南</span><span class="doc-card-arrow" aria-hidden="true">→</span></div><p class="doc-card-desc">在沙箱中运行 agent</p></a>
</div>
