> For the complete documentation index, see [llms.txt](https://gitbook.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.com/docs/documentation/zh/ai-yu-sou-suo/mcp-servers-for-published-docs.md).

# 已发布文档的 MCP 服务器

每个已发布的 GitBook 站点都会自动包含一个模型上下文协议（MCP）服务器。

AI 工具可以使用它直接读取你已发布的文档。这适用于 Claude、Claude Code、Cursor、Codex、VS Code 以及其他 MCP 客户端。

### 选择正确的端点 <a href="#choose-the-right-endpoint" id="choose-the-right-endpoint"></a>

你的 MCP 服务器地址是你的已发布站点 URL 加上以下端点之一：

| 如果你的站点是……                       | 使用此 URL                             | 示例                                           |
| ------------------------------- | ----------------------------------- | -------------------------------------------- |
| 公开，通过共享链接共享且公开所有已发布内容，或完全经过身份验证 | `{docs-site-url}/~gitbook/mcp`      | `https://gitbook.com/docs/~gitbook/mcp`      |
| 部分经过身份验证，仍暴露一些公开内容或共享链接内容       | `{docs-site-url}/~gitbook/mcp/auth` | `https://gitbook.com/docs/~gitbook/mcp/auth` |

对于完全经过身份验证的站点，客户端通过 MCP 发现和 OAuth 登录。更多详情请参见 [MCP 授权流程](https://modelcontextprotocol.io/docs/tutorials/security/authorization#the-authorization-flow-step-by-step).

{% hint style="info" %}
如果你在浏览器中打开此 URL，你会看到错误。请在能够发出 HTTP 请求的工具中使用它，例如 AI 助手或 IDE。
{% endhint %}

{% hint style="info" %}
**页面操作** 必须启用，MCP 服务器才能工作。如果你关闭 **站点自定义** → **页面操作**，GitBook 会禁用 `~gitbook/mcp` 并且该端点返回 `404`. **连接 MCP 服务器** 仅控制 MCP 链接是否显示在页面操作菜单中。
{% endhint %}

### 连接 AI 工具

{% stepper %}
{% step %}

#### 查找你的 MCP 服务器 URL

从你已发布的文档 URL 开始。然后添加来自以下内容的端点： [选择正确的端点](#choose-the-right-endpoint).

例如，如果你的文档站点是 `https://gitbook.com/docs`，你的 MCP 服务器 URL 是 `https://gitbook.com/docs/~gitbook/mcp`.
{% endstep %}

{% step %}

#### 将服务器添加到你的工具中

使用下面与你的工具对应的选项卡。将 `{docs-site-url}` 替换为你自己的已发布站点 URL。

如果你的站点使用第二个端点，请将 `/~gitbook/mcp` 替换为 `/~gitbook/mcp/auth`.
{% endstep %}

{% step %}

#### 提出一个测试问题

运行以下提示词之一： [试试看](#try-it)。如果工具可以搜索你的文档并据此回答，说明连接成功。
{% endstep %}
{% endstepper %}

{% tabs %}
{% tab title="Claude" %}
这些步骤适用于网页端 Claude 和 Claude Desktop。

打开 **设置** → **连接器**.

点击 **添加自定义连接器**。然后粘贴你的 MCP 服务器 URL，例如 `{docs-site-url}/~gitbook/mcp`.

示例：

```
https://gitbook.com/docs/~gitbook/mcp
```

如果 Claude 没有显示远程连接器，你当前的套餐或分批推出可能暂时还不支持它们。
{% endtab %}

{% tab title="Claude Code" %}
在终端中运行此命令：

```shell
claude mcp add --transport http my-docs https://gitbook.com/docs/~gitbook/mcp
```

将 `my-docs` 替换为你想要的任何服务器名称。

如果你的站点使用经过身份验证的公共端点，请将 URL 后缀替换为 `/~gitbook/mcp/auth`.
{% endtab %}

{% tab title="Cursor" %}
打开 **设置** → **MCP**.

点击 **添加新的 MCP 服务器**。然后粘贴 `{docs-site-url}/~gitbook/mcp`.

你也可以在以下位置添加此文件： `.cursor/mcp.json`:

```json
{
  "mcpServers": {
    "my-docs": {
      "url": "https://gitbook.com/docs/~gitbook/mcp"
    }
  }
}
```

将 `my-docs` 如果需要，也可以替换为你自己的服务器名称。
{% endtab %}

{% tab title="Codex" %}
使用 `codex mcp add` 命令，或者将服务器添加到你的 `config.toml`.

命令示例：

```shell
codex mcp add my-docs https://gitbook.com/docs/~gitbook/mcp
```

配置示例：

```toml
[mcp_servers.my-docs]
url = "https://gitbook.com/docs/~gitbook/mcp"
```

将 `my-docs` 如果需要，也可以替换为你自己的服务器名称。
{% endtab %}

{% tab title="VS Code（Copilot）" %}
打开你的 `mcp.json` 文件。然后添加一个使用 HTTP 传输的服务器条目：

```json
{
  "servers": {
    "my-docs": {
      "type": "http",
      "url": "https://gitbook.com/docs/~gitbook/mcp"
    }
  }
}
```

将 `my-docs` 如果需要，也可以替换为你自己的服务器名称。
{% endtab %}
{% endtabs %}

### 试试看 <a href="#try-it" id="try-it"></a>

将以下提示词之一粘贴到你的助手中：

* `使用 my-docs MCP 服务器，如何设置经过身份验证的访问？`
* `在我的文档中搜索所有关于自定义域名的内容并总结步骤。`
* `列出 my-docs MCP 服务器公开的工具。然后使用它们找到关于页面操作的页面。`

如果你使用的是代理式工具，也可以向它提供以下设置提示词：

```
添加一个名为 my-docs 的 MCP 服务器，使用 HTTP 传输，地址为 https://gitbook.com/docs/~gitbook/mcp。然后通过列出其可用工具来验证它是否已连接。
```

### 要求

要使用 MCP 服务器：

* 你的站点必须已发布。MCP 服务器只公开已发布内容。
* **页面操作** 必须在以下位置启用： **站点自定义** → **页面操作**.
* 你的工具必须支持通过 HTTP 使用 MCP。
* 如果你的站点使用 [经过身份验证的访问](/docs/documentation/zh/zhan-dian-fang-wen-quan-xian/authenticated-access.md)，你的工具必须支持 [MCP 授权规范](https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization).
* 如果你的站点使用共享链接，请使用共享链接站点 URL，然后添加来自以下内容的端点： [选择正确的端点](#choose-the-right-endpoint).
* GitBook 仅支持 HTTP 传输。 `stdio` 和 `SSE` 不受支持。

在 **页面操作** 的以下部分中： [自定义](/docs/documentation/zh/docs-site/customization.md) 设置中，你可以启用 **连接 MCP 服务器** 选项。这样访客就可以从以下位置复制你站点的 MCP 服务器 URL： [页面操作菜单](/docs/documentation/zh/docs-site/customization/extra-configuration.md#page-actions).

### 将 MCP 链接添加到你的站点

在 [站点自定义](/docs/documentation/zh/docs-site/customization.md)中，打开 [页面操作](/docs/documentation/zh/docs-site/customization/extra-configuration.md#page-actions)。确保 **页面操作** 已开启。然后开启 **连接 MCP 服务器**.

这会在页面操作菜单中添加一个可复制的 MCP 链接。它不会改变你的工具使用的端点。

### 隐私与访问

使用来自以下内容的端点： [选择正确的端点](#choose-the-right-endpoint).

MCP 服务器对你已发布的文档提供只读访问。

隐藏的页面仍可通过 MCP 访问。隐藏页面只会将其从已发布的目录中移除。

它不会公开账户数据、分析数据或 GitBook 内部数据。

它只提供最新发布的版本。草稿和未发布的更改保持私密。

### 故障排查

如果工具无法连接：

* 确认你的已发布站点可访问。
* 确认该 URL 使用的是来自以下内容的端点： [选择正确的端点](#choose-the-right-endpoint).
* 如果站点使用身份验证，请使用支持以下内容的客户端： [MCP 授权规范](https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization).
* 如果该工具需要 `stdio` 或 `SSE`，那么它将无法与 GitBook 一起使用。

### 在经过身份验证的站点上使用 MCP

如果你的 GitBook 站点使用 [经过身份验证的访问](/docs/documentation/zh/zhan-dian-fang-wen-quan-xian/authenticated-access.md)，位于以下地址的 MCP 服务器： `/~gitbook/mcp` 使用相同的身份验证。支持以下内容的 MCP 客户端 [MCP 授权规范](https://modelcontextprotocol.io/docs/tutorials/security/authorization) ——包括 Claude 和 Claude Code——可以使用 OAuth 和动态客户端注册（DCR）自动连接。

如果你的站点改为使用共享链接，请使用完整的共享链接站点 URL，然后添加来自以下内容的端点： [选择正确的端点](#choose-the-right-endpoint).

GitBook 不支持仅使用共享链接的站点，也不支持使用以静态请求头传递的访客身份验证令牌进行 MCP 身份验证的站点。

受支持的 MCP 客户端——包括 Claude——遵循 [MCP 授权规范](https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization) 以进行连接。

{% stepper %}
{% step %}

#### 发现 OAuth 服务器

在 MCP 握手过程中，客户端会发现你站点的 OAuth 服务器。
{% endstep %}

{% step %}

#### 通过 DCR 注册客户端

客户端通过动态客户端注册（Dynamic Client Registration）注册 OAuth 客户端。

你无需手动创建客户端 ID。
{% endstep %}

{% step %}

#### 使用你站点的身份验证提供商登录

客户端会将你重定向到你站点的身份验证提供商。

你使用文档站点已经在用的同一提供商登录。
{% endstep %}

{% step %}

#### 将代码兑换为令牌

登录后，客户端会将授权码交换为访问令牌。
{% endstep %}

{% step %}

#### 重复使用该令牌

客户端会在后续的 MCP 请求中携带该令牌，直到它过期。
{% endstep %}
{% endstepper %}

此流程适用于以下经过身份验证的访问后端：

* [Auth0](/docs/documentation/zh/zhan-dian-fang-wen-quan-xian/authenticated-access/setting-up-auth0.md)
* [Azure AD](/docs/documentation/zh/zhan-dian-fang-wen-quan-xian/authenticated-access/setting-up-azure-ad.md)
* [Okta](/docs/documentation/zh/zhan-dian-fang-wen-quan-xian/authenticated-access/setting-up-okta.md)
* [AWS Cognito](/docs/documentation/zh/zhan-dian-fang-wen-quan-xian/authenticated-access/setting-up-aws-cognito.md)
* [OIDC](/docs/documentation/zh/zhan-dian-fang-wen-quan-xian/authenticated-access/setting-up-oidc.md)
* [自定义后端](/docs/documentation/zh/zhan-dian-fang-wen-quan-xian/authenticated-access/setting-up-a-custom-backend.md) 并配置了回退 URL

{% hint style="warning" %}
MCP 身份验证不支持仅依赖在请求头中使用静态访客身份验证令牌的站点。

请改用上面列出的某一种经过身份验证的访问后端。
{% endhint %}

要进行设置，请从以下内容开始： [经过身份验证的访问](/docs/documentation/zh/zhan-dian-fang-wen-quan-xian/authenticated-access.md) 和 [启用经过身份验证的访问](/docs/documentation/zh/zhan-dian-fang-wen-quan-xian/authenticated-access/enabling-authenticated-access.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://gitbook.com/docs/documentation/zh/ai-yu-sou-suo/mcp-servers-for-published-docs.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
