> ## Documentation Index
> Fetch the complete documentation index at: https://mcp.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 连接到本地 MCP 服务器

> 了解如何使用本地 MCP 服务器扩展 Claude Desktop，以启用文件系统访问和其他强大的集成

模型上下文协议（MCP）服务器通过为本地资源和工具提供安全、受控的访问，扩展了 AI 应用程序的能力。许多客户端支持 MCP，从而能够在不同平台和应用程序之间实现多样化的集成可能性。

本指南将以 Claude Desktop 为例，演示如何连接到本地 MCP 服务器；Claude Desktop 是支持 MCP 的众多客户端之一。虽然我们重点介绍 Claude Desktop 的实现，但这些概念同样适用于其他兼容 MCP 的客户端。到本教程结束时，Claude 将能够与你电脑上的文件交互，创建新文档，整理文件夹，并搜索你的文件系统——所有操作都需要你对每一步给予明确许可。

<Frame>
  <img src="https://mintcdn.com/mcp-zhcndoc/e93uqR4nmQj7tWn4/images/quickstart-filesystem.png?fit=max&auto=format&n=e93uqR4nmQj7tWn4&q=85&s=f155f41124e6d33b44bbf7c5d7d3d947" alt="Claude Desktop with filesystem integration showing file management capabilities" width="1732" height="2060" data-path="images/quickstart-filesystem.png" />
</Frame>

## 前提条件

在开始本教程之前，请确保你的系统已安装以下内容：

### Claude Desktop

为你的操作系统下载并安装 [Claude Desktop](https://claude.ai/download)。Claude Desktop 适用于 macOS 和 Windows。

如果你已经安装了 Claude Desktop，请通过点击 Claude 菜单并选择“检查更新...”来确认你运行的是最新版本。

### Node.js

Filesystem Server 和许多其他 MCP 服务器都需要 Node.js 才能运行。要验证你的 Node.js 安装，请打开终端或命令提示符并运行：

```bash theme={null}
node --version
```

如果尚未安装 Node.js，请从 [nodejs.org](https://nodejs.org/) 下载。我们建议使用 LTS（长期支持）版本以获得更稳定的体验。

## 理解 MCP 服务器

MCP 服务器是在你的计算机上运行的程序，它们通过标准化协议向 Claude Desktop 提供特定功能。每个服务器都会暴露一些工具，Claude 可以在你的批准下使用这些工具来执行操作。我们将要安装的 Filesystem Server 提供的工具包括：

* 读取文件内容和目录结构
* 创建新文件和目录
* 移动和重命名文件
* 按名称或内容搜索文件

所有操作在执行前都需要你的明确批准，确保你始终完全掌控 Claude 可以访问和修改的内容。

## 安装文件系统服务器

这个过程涉及配置 Claude Desktop，使其在你启动应用程序时自动启动 Filesystem Server。此配置通过一个 JSON 文件完成，该文件告诉 Claude Desktop 需要运行哪些服务器以及如何连接到它们。

<Steps>
  <Step title="打开 Claude Desktop 设置">
    首先访问 Claude Desktop 的设置。点击系统菜单栏中的 Claude 菜单（不是 Claude 窗口本身里的设置），然后选择“Settings...”

    在 macOS 上，它显示在顶部菜单栏中：

    <Frame style={{ textAlign: "center" }}>
      <img src="https://mintcdn.com/mcp-zhcndoc/e93uqR4nmQj7tWn4/images/quickstart-menu.png?fit=max&auto=format&n=e93uqR4nmQj7tWn4&q=85&s=9a39a930e975d08d129c9d5d486c4a4f" width="400" alt="Claude Desktop 菜单显示 Settings 选项" data-path="images/quickstart-menu.png" />
    </Frame>

    这将打开 Claude Desktop 配置窗口，它与你的 Claude 账户设置是分开的。
  </Step>

  <Step title="访问开发者设置">
    在设置窗口中，导航到左侧边栏里的“Developer”选项卡。该部分包含用于配置 MCP 服务器和其他开发者功能的选项。

    点击“Edit Config”按钮打开配置文件：

    <Frame>
      <img src="https://mintcdn.com/mcp-zhcndoc/e93uqR4nmQj7tWn4/images/quickstart-developer.png?fit=max&auto=format&n=e93uqR4nmQj7tWn4&q=85&s=622b1686c5a15d55f0cdce0ce49a919b" alt="开发者设置显示 Edit Config 按钮" width="1688" height="534" data-path="images/quickstart-developer.png" />
    </Frame>

    如果配置文件不存在，此操作会创建一个新的配置文件；否则会打开你现有的配置。该文件位于：

    * **macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
    * **Windows**: `%APPDATA%\Claude\claude_desktop_config.json`
  </Step>

  <Step title="配置文件系统服务器">
    将配置文件内容替换为以下 JSON 结构。此配置会告诉 Claude Desktop 启动 Filesystem Server，并允许其访问特定目录：

    <CodeGroup>
      ```json macOS theme={null}
      {
        "mcpServers": {
          "filesystem": {
            "command": "npx",
            "args": [
              "-y",
              "@modelcontextprotocol/server-filesystem",
              "/Users/username/Desktop",
              "/Users/username/Downloads"
            ]
          }
        }
      }
      ```

      ```json Windows theme={null}
      {
        "mcpServers": {
          "filesystem": {
            "command": "npx",
            "args": [
              "-y",
              "@modelcontextprotocol/server-filesystem",
              "C:\\Users\\username\\Desktop",
              "C:\\Users\\username\\Downloads"
            ]
          }
        }
      }
      ```
    </CodeGroup>

    将 `username` 替换为你实际的计算机用户名。`args` 数组中列出的路径指定了 Filesystem Server 可以访问哪些目录。你可以根据需要修改这些路径或添加其他目录。

    <Tip>
      **理解此配置**

      * `"filesystem"`：服务器在 Claude Desktop 中显示的友好名称
      * `"command": "npx"`：使用 Node.js 的 npx 工具运行服务器
      * `"-y"`：自动确认安装服务器包
      * `"@modelcontextprotocol/server-filesystem"`：Filesystem Server 的包名
      * 其余参数：服务器被允许访问的目录
    </Tip>

    <Warning>
      **安全注意事项**

      只授予你愿意让 Claude 读取和修改的目录访问权限。该服务器会以你的用户账户权限运行，因此它可以执行任何你手动能够执行的文件操作。
    </Warning>
  </Step>

  <Step title="重启 Claude Desktop">
    保存配置文件后，完全退出 Claude Desktop 并重新启动它。应用程序需要重启才能加载新配置并启动 MCP 服务器。

    成功重启后，点击对话输入框左下角的“Add files, connectors and more”指示器 <img src="https://mintcdn.com/mcp-zhcndoc/D7P41PxycwiS1Q3N/images/claude-add-files-connectors-and-more.png?fit=max&auto=format&n=D7P41PxycwiS1Q3N&q=85&s=9f14ef739bddd5f5445987ebefd3c138" style={{display: 'inline', margin: 0, height: '1.3em'}} width="33" height="33" data-path="images/claude-add-files-connectors-and-more.png" />：

    <Frame>
      <img src="https://mintcdn.com/mcp-zhcndoc/D7P41PxycwiS1Q3N/images/quickstart-slider.png?fit=max&auto=format&n=D7P41PxycwiS1Q3N&q=85&s=9d898332a5aaeec00fdda01701447c49" alt="Claude Desktop 界面显示 MCP 服务器指示器" width="1414" height="410" data-path="images/quickstart-slider.png" />
    </Frame>

    点击该指示器，然后滚动到“Connectors”并点击“Manage connectors”。从连接器列表中选择“filesystem”以查看 Filesystem Server 可用的工具：

    <Frame style={{ textAlign: "center" }}>
      <img src="https://mintcdn.com/mcp-zhcndoc/D7P41PxycwiS1Q3N/images/quickstart-tools.png?fit=max&auto=format&n=D7P41PxycwiS1Q3N&q=85&s=16f6687452746b5bdedbf1b95f42a403" width="400" alt="Claude Desktop 中可用的文件系统工具" data-path="images/quickstart-tools.png" />
    </Frame>

    如果 Filesystem Server 无法连接，请参阅 [故障排除](#troubleshooting) 部分以获取调试步骤。
  </Step>
</Steps>

## 使用文件系统服务器

连接文件系统服务器后，Claude 现在可以与你的文件系统交互。试试这些示例请求来探索其功能：

### 文件管理示例

* **“你能写一首诗并把它保存到我的桌面吗？”** - Claude 将创作一首诗，并在你的桌面上创建一个新的文本文件
* **“我的下载文件夹里有哪些与工作相关的文件？”** - Claude 将扫描你的下载内容并识别与工作相关的文档
* **“请把我桌面上的所有图片整理到一个名为‘Images’的新文件夹里”** - Claude 将创建一个文件夹，并把图片文件移动进去

### 审批如何工作

在执行任何文件系统操作之前，Claude 会请求你的批准。这可以确保你始终掌控所有操作：

<Frame style={{ textAlign: "center" }}>
  <img src="https://mintcdn.com/mcp-zhcndoc/e93uqR4nmQj7tWn4/images/quickstart-approve.png?fit=max&auto=format&n=e93uqR4nmQj7tWn4&q=85&s=66957a5652cc2048ea800e5140db26a4" width="500" alt="Claude 请求批准以执行文件操作" data-path="images/quickstart-approve.png" />
</Frame>

在批准之前，请仔细审查每个请求。如果你对所提议的操作不放心，随时都可以拒绝。

## 故障排除

如果你在设置或使用 Filesystem Server 时遇到问题，以下解决方案可以帮助你处理常见问题：

<AccordionGroup>
  <Accordion title="Server not showing up in Claude / hammer icon missing">
    1. 完全重启 Claude Desktop
    2. 检查你的 `claude_desktop_config.json` 文件语法
    3. 确保 `claude_desktop_config.json` 中包含的文件路径有效，并且它们是绝对路径而不是相对路径
    4. 查看 [日志](#getting-logs-from-claude-for-desktop) 以了解服务器为什么没有连接
    5. 在命令行中，尝试手动运行服务器（将 `username` 替换为你在 `claude_desktop_config.json` 中使用的值），看看是否有任何错误：

    <CodeGroup>
      ```bash macOS/Linux theme={null}
      npx -y @modelcontextprotocol/server-filesystem /Users/username/Desktop /Users/username/Downloads
      ```

      ```powershell Windows theme={null}
      npx -y @modelcontextprotocol/server-filesystem C:\Users\username\Desktop C:\Users\username\Downloads
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Getting logs from Claude Desktop">
    与 MCP 相关的 Claude.app 日志会写入以下位置的日志文件中：

    * macOS: `~/Library/Logs/Claude`

    * Windows: `%APPDATA%\Claude\logs`

    * `mcp.log` 将包含有关 MCP 连接和连接失败的一般日志信息。

    * 名为 `mcp-server-SERVERNAME.log` 的文件将包含来自指定服务器的错误（stderr）日志。

    你可以运行以下命令来列出最近的日志并持续跟踪新日志（在 Windows 上，它只会显示最近的日志）：

    <CodeGroup>
      ```bash macOS/Linux theme={null}
      tail -n 20 -f ~/Library/Logs/Claude/mcp*.log
      ```

      ```powershell Windows theme={null}
      type "%APPDATA%\Claude\logs\mcp*.log"
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Tool calls failing silently">
    如果 Claude 尝试使用这些工具但它们失败了：

    1. 检查 Claude 的日志以查看错误
    2. 验证你的服务器是否能构建并正常运行且没有错误
    3. 尝试重启 Claude Desktop
  </Accordion>

  <Accordion title="None of this is working. What do I do?">
    请参考我们的 [调试指南](/docs/2024-11-05/tools/debugging) 以获得更好的调试工具和更详细的指导。
  </Accordion>

  <Accordion title="ENOENT error and `${APPDATA}` in paths on Windows">
    如果你配置的服务器加载失败，并且你在其日志中看到路径里引用了 `${APPDATA}` 的错误，你可能需要将 `%APPDATA%` 的展开值添加到 `claude_desktop_config.json` 的 `env` 键中：

    ```json theme={null}
    {
      "brave-search": {
        "command": "npx",
        "args": ["-y", "@modelcontextprotocol/server-brave-search"],
        "env": {
          "APPDATA": "C:\\Users\\user\\AppData\\Roaming\\",
          "BRAVE_API_KEY": "..."
        }
      }
    }
    ```

    完成此更改后，再次启动 Claude Desktop。

    <Warning>
      **npm 应全局安装**

      如果你没有全局安装 npm，`npx` 命令可能会继续失败。如果 npm 已经全局安装，你会发现系统中存在 `%APPDATA%\npm`。如果没有，你可以通过运行以下命令全局安装 npm：

      ```bash theme={null}
      npm install -g npm
      ```
    </Warning>
  </Accordion>
</AccordionGroup>

## 下一步

既然你已经成功将 Claude Desktop 连接到本地 MCP 服务器，可以探索以下选项来扩展你的设置：

<CardGroup cols={2}>
  <Card title="探索其他服务器" icon="grid" href="https://github.com/modelcontextprotocol/servers">
    浏览我们的官方和社区创建的 MCP 服务器集合，以获得
    额外功能
  </Card>

  <Card title="构建你自己的服务器" icon="code" href="/docs/2024-11-05/develop/build-server">
    创建适合你特定工作流程和
    集成的自定义 MCP 服务器
  </Card>

  <Card title="连接到远程服务器" icon="cloud" href="/docs/2024-11-05/develop/connect-remote-servers">
    了解如何将 Claude 连接到远程 MCP 服务器，以使用基于云的工具和
    服务
  </Card>

  <Card title="理解协议" icon="book" href="/docs/2024-11-05/learn/architecture">
    深入了解 MCP 的工作原理及其架构
  </Card>
</CardGroup>
