# MCP 伺服器整合

> 將 AgentFS 作為 AI 助理的 MCP 伺服器使用
> Canonical HTML: https://tursodb.cn/zh-tw/docs/agentfs/guides/mcp/
> Upstream Source (MIT): https://docs.turso.tech/agentfs/guides/mcp
> Language: zh-TW

---
AgentFS 可以作為 [MCP (Model Context Protocol)](https://modelcontextprotocol.io/) 伺服器執行，將檔案系統與鍵值操作暴露給 Claude Desktop 等 AI 助理使用。

## 啟動 MCP 伺服器

```bash
agentfs serve mcp my-agent
```

這會啟動一個透過 stdio 暴露 AgentFS 工具的 MCP 伺服器。

## 可用工具

### 檔案系統工具

| 工具 | 說明 |
| - | - |
| `read_file` | 讀取檔案內容 |
| `write_file` | 寫入檔案內容 |
| `readdir` | 列出目錄內容 |
| `mkdir` | 建立目錄 |
| `remove` | 移除檔案或目錄 |
| `rename` | 重新命名檔案或目錄 |
| `stat` | 取得檔案中繼資料 |
| `access` | 檢查檔案存取權限 |

### 鍵值工具

| 工具 | 說明 |
| - | - |
| `kv_get` | 依鍵取得值 |
| `kv_set` | 設定鍵值對 |
| `kv_delete` | 刪除鍵 |
| `kv_list` | 列出所有鍵 |

## 限制暴露的工具

為了安全考量，您可以限制可用的工具：

```bash
# Read-only mode
agentfs serve mcp my-agent --tools read_file,readdir,stat,kv_get,kv_list

# Filesystem only
agentfs serve mcp my-agent --tools read_file,write_file,readdir,mkdir,remove

# Key-value only
agentfs serve mcp my-agent --tools kv_get,kv_set,kv_delete,kv_list
```

## 安全性考量

<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">WARNING</div><div class="doc-callout-body">

MCP 伺服器會讓 AI 助理直接存取 AgentFS 檔案系統。請考慮以下安全實務：

* **限制工具** - 只暴露助理所需的工具
* **使用疊加層** - 建立疊加檔案系統，而非直接存取真實目錄
* **檢查代理資料庫** - 定期檢查 `.agentfs/*.db`

</div></div>


## 偵錯

檢查 MCP 伺服器是否正常運作：

```bash
# Run manually to see output
agentfs serve mcp my-agent

# Check if the database exists
ls -la .agentfs/my-agent.db
```

## 後續步驟

<div class="doc-cards">
<a class="doc-card" href="/zh-tw/docs/agentfs/guides/nfs/"><div class="doc-card-title"><span>NFS 伺服器</span><span class="doc-card-arrow" aria-hidden="true">→</span></div><p class="doc-card-desc">透過 NFS 進行遠端存取</p></a>
<a class="doc-card" href="/zh-tw/docs/agentfs/guides/overlay/"><div class="doc-card-title"><span>寫入時複製疊加層</span><span class="doc-card-arrow" aria-hidden="true">→</span></div><p class="doc-card-desc">隔離機制如何運作</p></a>
</div>
