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

# CometAPI MCP

> 将 Claude Code、Codex 和其他 MCP 客户端连接到 CometAPI，即可使用一个 API 密钥搜索模型、生成文本和创建图像。

CometAPI MCP 服务器将 [模型上下文协议](https://modelcontextprotocol.io) 客户端（例如 Claude Code 和 Codex）连接到 CometAPI。CometAPI 托管该服务器，因此无需安装任何内容。连接后的助手可以在对话中搜索模型目录、查看模型详情、生成文本和创建图像。

该服务器使用与 CometAPI API 和 [CometAPI CLI](https://apidoc.cometapi.com/zh-Hans/developer-tools/cli/overview.md)相同的 API 密钥。身份验证只需一个请求头，无需通过浏览器登录。

| 设置 | 值 |
| - | - |
| URL | `https://mcp.cometapi.com/mcp` |
| 传输方式 | Streamable HTTP |
| 身份验证 | `Authorization: Bearer <COMETAPI_KEY>` 请求头 |

## 功能

* 使用一个 URL 和 CometAPI API 密钥即可连接，无需安装任何内容
* 免费搜索 API 密钥可访问的模型目录并查看模型详情
* 使用 `gpt-6-astra` 生成文本，使用 `gpt-image-2` 创建图像，价格与 CometAPI API 请求相同
* 断开连接后恢复结果：免费重新读取文本回执或图像任务
* 控制费用：服务器对每个生成请求只发送一次，不会重试

## 前提条件

* 从 [API 密钥页面](https://www.cometapi.com/console/token)获取 CometAPI API 密钥。要限制助手的可用额度，请为 MCP 服务器创建专用 API 密钥：禁用 **无限额度** 并设置额度。
* 支持通过 Streamable HTTP 和自定义请求头连接远程服务器的 MCP 客户端，例如 Claude Code 或 Codex

## 连接客户端

**1. 设置 API 密钥**

为避免将 API 密钥存入配置文件，请将其存储在 `COMETAPI_KEY` 环境变量中。将 `<COMETAPI_KEY>` 替换为你的 CometAPI API 密钥：

```bash theme={null}
export COMETAPI_KEY="<COMETAPI_KEY>"
```

**2. 添加服务器**

> [tab] Claude Code
>
> 在项目根目录中创建 `.mcp.json` 文件，即启动 Claude Code 的目录。如果该目录中已有 `.mcp.json` 文件，请将 `cometapi` 条目添加到该文件的 `mcpServers` 对象中。使用以下内容：
>
> ```json theme={null}
> {
>   "mcpServers": {
>     "cometapi": {
>       "type": "http",
>       "url": "https://mcp.cometapi.com/mcp",
>       "headers": {
>         "Authorization": "Bearer ${COMETAPI_KEY}"
>       }
>     }
>   }
> }
> ```
>
> Claude Code 会将 `${COMETAPI_KEY}` 替换为环境变量的值，因此文件中不包含 API 密钥。从已导出 `COMETAPI_KEY` 的 shell 中，在项目根目录启动 Claude Code。当 Claude Code 提示时，批准使用 `cometapi` 服务器。

> [tab] Codex
>
> 使用 Codex CLI 添加服务器：
>
> ```bash theme={null}
> codex mcp add cometapi --url https://mcp.cometapi.com/mcp \
>   --bearer-token-env-var COMETAPI_KEY
> ```
>
> `--bearer-token-env-var` 接受的是环境变量的名称，而不是 API 密钥本身。Codex 会存储环境变量的名称，并在启动时读取 API 密钥。从已导出 `COMETAPI_KEY` 的 shell 中启动 Codex。

> [tab] 其他客户端
>
> 使用本页顶部表格中的 URL、传输方式和请求头添加远程 MCP 服务器。将 API 密钥存储在客户端的密钥或环境设置中。

**3. 验证连接**

检查客户端是否将 `cometapi` 服务器列为已连接。在 Claude Code 和 Codex 中，`/mcp` 命令会显示服务器状态。

然后让助手搜索模型目录。此次调用不计费：

```text theme={null}
Use the cometapi search_models tool to find models that match "gpt".
Show five results.
```

结果会列出你的 API 密钥可以访问的模型。

## 尝试示例 Prompt

要在发起计费请求前检查支持的参数，请使用此 Prompt。此次调用不计费：

```text theme={null}
Use the cometapi get_model_schema tool to show the parameters that
gpt-image-2 accepts.
```

要生成文本，请使用此 Prompt。请求按 CometAPI API 请求的相同价格计费：

```text theme={null}
Use the cometapi generate_text tool with gpt-6-astra to write a 50-word
product description for a reusable water bottle. Set max_output_tokens to 512.
```

要生成较长的文本，请使用回执。回执允许生成过程运行最多 120 秒，并在回执过期前保持结果可读取：

```text theme={null}
Use the cometapi prepare_text_generation tool to reserve a receipt. Then call
generate_text with that recovery_id and gpt-6-astra to explain what an API key
is in 100 words. Set max_output_tokens to 512. Read the result with
get_text_result.
```

要创建图像，请使用此 Prompt。任务在图像生成时计费：

```text theme={null}
Use the cometapi generate_image tool with gpt-image-2 to create a
minimalist product photo of a stainless steel water bottle on a light
background. Use the low quality and the 1024x1024 size. Read the task with
get_task until the task finishes, then show the task ID.
```

要将图像保存为文件，请使用任务 ID 和相关命令，详见 [工具参考](https://apidoc.cometapi.com/zh-Hans/developer-tools/mcp/tools.md).

## 工具

| 工具 | 用途 | 费用 |
| - | - | - |
| `search_models` | 搜索 API 密钥可访问的模型目录 | 免费 |
| `get_model_schema` | 查看单个模型及其生成参数 | 免费 |
| `generate_text` | 使用以下方式生成文本： `gpt-6-astra` | 收费 |
| `prepare_text_generation` | 为文本请求预留回执 | 免费 |
| `get_text_result` | 读取带有回执的文本请求结果 | 免费 |
| `generate_image` | 提交图像任务，适用于 `gpt-image-2` | 生成图像时收费 |
| `get_task` | 读取图像任务的状态和结果 | 免费 |

要查看进度或再次读取结果，请使用 `get_text_result` 或 `get_task`。这些工具不收费。请勿为此再次调用 `generate_text` 或 `generate_image`，因为重复调用可能会发起第二个计费请求。

当 API 密钥可以访问 `gpt-6-astra` 时，客户端会列出 `generate_text` 和 `prepare_text_generation`。当 API 密钥可以访问 `gpt-image-2` 时，客户端会列出 `generate_image` 和 `get_task`。

有关各工具的参数、结果和错误，请参阅 [工具参考](https://apidoc.cometapi.com/zh-Hans/developer-tools/mcp/tools.md).

## 选择 MCP 服务器、API 或 CLI

| 使用方式 | 适用场景 |
| - | - |
| MCP 服务器 | 聊天或 IDE 会话中的助手需要在对话期间搜索模型、生成文本或图像 |
| [CometAPI CLI](https://apidoc.cometapi.com/zh-Hans/developer-tools/cli/overview.md) | 在终端或脚本中工作，或需要余额和使用日志等账户数据 |
| [CometAPI API](https://apidoc.cometapi.com/zh-Hans/quickstarts/index.md) | 调用 MCP 工具未覆盖的模型，或构建自己的集成 |

MCP 服务器和 CLI 使用相同的 API 密钥，因此可以同时使用两者。在 CLI 中，查询使用日志和其他账户数据的命令还需要访问令牌。

## 计费

目录搜索、模型查询、回执和任务读取均不收费。`generate_text` 和 `generate_image` 会消耗 CometAPI 余额，费用与通过 CometAPI API 发起相同请求的费用一致。MCP 服务器不收取额外费用。有关模型价格，请参阅 [模型目录](https://www.cometapi.com/models/).

服务器对每个生成请求只发送一次。超时或断开连接后，请求仍可能完成并产生费用。再次发送请求前，请检查第一次请求：

| 请求 | 检查方式 |
| - | - |
| 带有回执的文本请求 | 使用以下工具读取回执： `get_text_result` |
| 没有回执的文本请求 | 查看 [使用日志](https://www.cometapi.com/console/log) 中是否有 `gpt-6-astra` 条目。该条目会在生成结束时出现，可能在请求发出后超过一分钟才出现。如果该条目存在，说明请求已计费，且生成的文本无法再次获取。 |
| 带有任务 ID 的图像任务 | 使用以下工具读取任务： `get_task` |
| 没有任务 ID 的图像任务 | 查看 [使用日志](https://www.cometapi.com/console/log) 中是否有 `gpt-image-2` 条目。该条目会在图像生成时出现，图像生成可能需要几分钟。 |

要限制助手的支出，请使用设置了配额的专用 API 密钥。有关如何通过 API 设置配额或过期时间，请参阅 [创建 API 密钥](https://apidoc.cometapi.com/zh-Hans/api/api-keys/create-api-key.md).

## 故障排查

> [accordion] 服务器返回 401 和 authentication\_error
>
> 检查启动客户端的环境中是否设置了 `COMETAPI_KEY`。然后检查该值是否为完整的 CometAPI API 密钥，并确认密钥已启用、未过期且有剩余额度。要复制 API 密钥，请打开 [API 密钥页面](https://www.cometapi.com/console/token)。服务器不接受账户访问令牌。

> [accordion] 服务器返回 403 和 authentication\_error
>
> CometAPI 拒绝了该 API 密钥的访问请求。通过 MCP 服务器发出的请求并非来自您自己的 IP 地址，因此 API 密钥上的 IP 允许列表会阻止这些请求。如果 API 密钥配置了 IP 允许列表，请为 MCP 服务器创建一个不带 IP 允许列表的专用 API 密钥，并为该密钥设置额度。否则，请检查 API 密钥的分组权限。

> [accordion] 客户端未列出生成工具
>
> 只有当您的 API 密钥可以访问生成工具所用的模型时，客户端才会列出该工具。要检查访问权限，请读取 `gpt-6-astra` 或 `gpt-image-2`，并使用 `get_model_schema`。当结果显示 `schemaStatus` 为 `verified` 时，客户端会列出该模型的生成工具。

## 链接

* [工具参考](https://apidoc.cometapi.com/zh-Hans/developer-tools/mcp/tools.md)
* [CometAPI CLI](https://apidoc.cometapi.com/zh-Hans/developer-tools/cli/overview.md)
* [模型 API 快速入门](https://apidoc.cometapi.com/zh-Hans/quickstarts/index.md)
* [模型目录](https://www.cometapi.com/models/)
* [帮助中心](https://apidoc.cometapi.com/zh-Hans/support/help-center.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.