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

# 连接到 MCP Server

> 将任意 MCP 客户端连接到 Ayrshare MCP Server —— 涵盖传输方式、端点与身份认证。

Ayrshare [MCP Server](/additional/mcp-action-server) 允许 AI 代理驱动 Ayrshare API。本页介绍如何连接到它以及身份认证的工作原理。

## 端点与传输方式

生产环境 MCP Server 地址为：

`https://api.ayrshare.com/mcp`

它使用 **Streamable HTTP** 传输方式，并且是 **无状态的（stateless）** —— 各次调用之间无需维护会话。

## 身份认证

身份认证由为 REST API 提供支持的同一条 Ayrshare API 处理链执行。

<ul class="custom-bullets">
  <li>**必填：** `Authorization: Bearer YOUR_API_KEY` —— 你的账户 API 密钥（涉及配置文件与子配置文件时，需要 Business 套餐的密钥）。</li>
  <li>**可选：** `Profile-Key: YOUR_PROFILE_KEY` —— 让该连接上的每一次调用都以该子配置文件为目标。</li>
  <li>**每次调用可选：** `profileKey` 工具参数 —— 仅让本次工具调用以某个子配置文件为目标。</li>
</ul>

### 优先级：参数优先于请求头

当一次工具调用同时包含 `profileKey` 参数 **和** 连接上的 `Profile-Key` 请求头时，**本次调用的 `profileKey` 参数优先生效**。只有在未提供有效参数时，才会使用请求头。

一个例外：在 `get_platform_history` 和 `get_social_network_analytics` 上，X/Twitter 的 `userId` / `userName` 查询必须仅使用账户 API 密钥；此时如果传入 `profileKey` 参数或 `Profile-Key` 请求头，会返回 400 错误。

<Note>
  MCP 的 `initialize` 和 `tools/list` 方法可以在 **认证之前** 访问 —— 它们只返回元数据，不会执行任何操作。**所有工具调用都必须经过认证。**
</Note>

### 认证错误

在工具调用中使用未认证或无效的密钥，会返回 Ayrshare **error 403 / code 102**，错误信息为 **"API Key not valid"**。根据 MCP 规范，工具执行错误以带内（in-band）方式返回：工具结果携带 `isError: true` 及该错误信息，而 MCP 传输本身返回 **HTTP 200**。（`403` / `102` 是 Ayrshare 的应用层错误，而非传输层状态。）

## 连接

### 方式 A：Claude Code 插件

如果你使用 Claude Code，可以安装 Ayrshare 插件。该插件已内置 MCP Server 配置、初始化命令、代理、技能以及确认钩子。完整安装步骤请参见 [Claude Code 插件](/additional/mcp-claude-code-plugin) 页面。

### 方式 B：任意 MCP 客户端

对于任何支持 Streamable HTTP 的 MCP 客户端，可以直接注册服务器。在 Claude Code 中：

```bash theme={"system"}
claude mcp add --transport http ayrshare https://api.ayrshare.com/mcp --header "Authorization: Bearer YOUR_API_KEY"
```

要让每次调用都以某个子配置文件为目标，请添加可选的 `Profile-Key` 请求头：

```bash theme={"system"}
claude mcp add --transport http ayrshare https://api.ayrshare.com/mcp \
  --header "Authorization: Bearer YOUR_API_KEY" \
  --header "Profile-Key: YOUR_PROFILE_KEY"
```

<Warning>
  MCP 连接会在会话启动时初始化。安装服务器或修改密钥后，请 **重启你的 MCP 客户端** 以使新配置生效。
</Warning>

## X/Twitter 自带（BYO）凭据

从 2026 年 3 月 31 日起，通过 Ayrshare 进行的 X/Twitter 操作要求使用你自己的 OAuth 1.0a 凭据。当工具调用目标是 X/Twitter 时，请在连接中随 `Authorization`（以及可选的 `Profile-Key`）请求头一起附加以下两个请求头：

| 请求头                           | 说明                                            |
| ----------------------------- | --------------------------------------------- |
| `X-Twitter-OAuth1-Api-Key`    | 你的 OAuth 1.0a API Key（Consumer Key）           |
| `X-Twitter-OAuth1-Api-Secret` | 你的 OAuth 1.0a API Key Secret（Consumer Secret） |

这些请求头与 REST API 使用的相同 —— 每个 Ayrshare 账户对应一对 OAuth 1.0a 密钥，在每个以 X 为目标的请求中都需要发送（同一对密钥适用于所有子配置文件）。Ayrshare 在此处不使用 OAuth 2.0。有关设置、政策与故障排查，请参见 [API 概览](/apis/overview#xtwitter-byo-credentials) 和 [X BYO Key 设置指南](/dashboard/connect-social-accounts/x-twitter-byo-keys)。

<Warning>
  如果缺少这些请求头，X/Twitter 工具调用会返回错误 `419`（`x_credentials_required`）。
</Warning>

### 连接时同时配置 BYO 请求头

如需从一开始就配置 X/Twitter BYO 凭据，请在添加服务器时同时提供两个 OAuth 1.0a 请求头与 `Authorization` 请求头（如需每次调用都以某个子配置文件为目标，也请一并附上 `Profile-Key`）：

```bash theme={"system"}
claude mcp add --transport http ayrshare https://api.ayrshare.com/mcp \
  --header "Authorization: Bearer YOUR_API_KEY" \
  --header "X-Twitter-OAuth1-Api-Key: YOUR_TWITTER_CONSUMER_KEY" \
  --header "X-Twitter-OAuth1-Api-Secret: YOUR_TWITTER_CONSUMER_SECRET"
```

### 为已有连接补充 BYO 请求头

连接的请求头在添加服务器时就已固定，因此如果你之前连接时没有携带 BYO 请求头，需要先移除服务器，然后重新添加并提供完整的请求头集合（重新包含你之前已经使用的任何请求头，例如 `Profile-Key`）：

```bash theme={"system"}
claude mcp remove ayrshare
claude mcp add --transport http ayrshare https://api.ayrshare.com/mcp \
  --header "Authorization: Bearer YOUR_API_KEY" \
  --header "X-Twitter-OAuth1-Api-Key: YOUR_TWITTER_CONSUMER_KEY" \
  --header "X-Twitter-OAuth1-Api-Secret: YOUR_TWITTER_CONSUMER_SECRET"
```

修改请求头后，请重启 MCP 客户端以使新配置生效。如果你使用的是 Claude Code 插件而非直接连接？请参见 [Claude Code 插件 → X/Twitter BYO 凭据](/additional/mcp-claude-code-plugin#xtwitter-byo-credentials)。

## 后续步骤

<CardGroup cols={2}>
  <Card title="工具目录" icon="list" href="/additional/mcp-action-tools" horizontal>
    按领域分组的 27 个工具，包含作用范围与用途。
  </Card>

  <Card title="Claude Code 插件" icon="terminal" href="/additional/mcp-claude-code-plugin" horizontal>
    为 Claude Code 安装 Ayrshare 插件。
  </Card>
</CardGroup>
