> 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-for-your-readers/mcp-servers-for-published-docs.md).

# 已发布文档的 MCP 服务器

发布在 GitBook 上的文档会自动生成一个 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).

中的端点。 `例如，如果你的文档站点是`，你的 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/publish/site-audience/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/manage-your-site/customization.md) 设置中，你可以启用 **连接 MCP 服务器** 选项。这样访问者就可以从 [页面操作菜单](/docs/documentation/zh/manage-your-site/customization/extra-configuration.md#page-actions).

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

在 [站点自定义](/docs/documentation/zh/manage-your-site/customization.md)，请打开 [页面操作](/docs/documentation/zh/manage-your-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/publish/site-audience/authenticated-access.md)，位于 `/~gitbook/mcp` 的 MCP 服务器会使用相同的身份验证。支持 [MCP 授权规范](https://modelcontextprotocol.io/docs/tutorials/security/authorization) 的 MCP 客户端——包括 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 注册客户端

客户端使用动态客户端注册注册一个 OAuth 客户端。

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

{% step %}

#### 使用你网站的身份验证提供方登录

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

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

{% step %}

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

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

{% step %}

#### 重用令牌

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

此流程适用于以下已认证访问后端：

* [Auth0](/docs/documentation/zh/publish/site-audience/authenticated-access/setting-up-auth0.md)
* [Azure AD](/docs/documentation/zh/publish/site-audience/authenticated-access/setting-up-azure-ad.md)
* [Okta](/docs/documentation/zh/publish/site-audience/authenticated-access/setting-up-okta.md)
* [AWS Cognito](/docs/documentation/zh/publish/site-audience/authenticated-access/setting-up-aws-cognito.md)
* [OIDC](/docs/documentation/zh/publish/site-audience/authenticated-access/setting-up-oidc.md)
* [自定义后端](/docs/documentation/zh/publish/site-audience/authenticated-access/setting-up-a-custom-backend.md) 配合配置好的回退 URL

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

请改用上面列出的某种已认证访问后端。
{% endhint %}

要进行设置，请从 [经过身份验证的访问](/docs/documentation/zh/publish/site-audience/authenticated-access.md) 以及 [启用已认证访问](/docs/documentation/zh/publish/site-audience/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-for-your-readers/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.
