# MCP 服务器

Pocket Alert 支持 [Model Context Protocol](https://modelcontextprotocol.io)，因此 AI 智能体可以通过普通的工具调用为你发送推送通知、读取消息历史并查看投递统计。

实际用法是：智能体启动一个耗时较长的任务，在不确定的时间点完成后，直接给你的手机推送通知，而不必让你盯着终端等待。

```
https://mcp.pocketalert.app/mcp
```

## 连接

身份验证使用 Bearer 令牌，即你在 [API 密钥](https://pocketalert.app/account/apikeys)页面获取的 API 密钥，通过 `Authorization: Bearer <token>` 发送。

**Claude Desktop / Claude Code**

```json
{
  "mcpServers": {
    "pocketalert": {
      "command": "npx",
      "args": [
        "mcp-remote",
        "https://mcp.pocketalert.app/mcp",
        "--header",
        "Authorization: Bearer ${POCKETALERT_API_TOKEN}"
      ],
      "env": {
        "POCKETALERT_API_TOKEN": "your-api-key"
      }
    }
  }
}
```

**Native HTTP transport**

```json
{
  "mcpServers": {
    "pocketalert": {
      "type": "http",
      "url": "https://mcp.pocketalert.app/mcp",
      "headers": {
        "Authorization": "Bearer your-api-key"
      }
    }
  }
}
```

之后重启客户端，然后让它列出可用工具，此时应能看到 `pocketalert`。

## 可用工具

| 工具 | 功能 |
|------|--------------|
| `list_applications` | 列出你的应用 |
| `get_application` | 通过 TID 获取单个应用 |
| `list_devices` | 列出已注册的设备 |
| `get_device` | 通过 TID 获取单个设备 |
| `list_messages` | 读取消息历史 |
| `get_message` | 通过 TID 获取单条消息 |
| `create_message` | 发送推送通知 |
| `create_message_with_attachment` | 发送带图片附件的推送 |
| `delete_message` | 删除消息 |
| `get_message_stats` | 投递统计 |

`create_message` 接受与 REST API 相同的 `level` 和 `actions` 参数，因此智能体可以将发送的通知提升为[关键告警](https://info.pocketalert.app/features/priority-levels.md)，或附加[操作按钮](https://info.pocketalert.app/features/action-buttons.md)用于审批。

## 值得配置的场景

**长时间运行的任务。** 迁移、训练任务、批量导入等。任务完成时智能体会通知你，你无需反复查看终端。

**需要人工介入的审批。** 为智能体提供 `create_message` 工具，并让它在执行任何不可逆操作前发送两个 `http` 操作按钮。你可以直接在锁屏上批准，智能体随后继续执行。

**读取自身的历史记录。** 能调用 `list_messages` 的智能体可以回答"昨晚什么出了问题？"，无需你转发任何内容。

> **Warning: 为密钥限定范围**
>
> MCP 服务器是你交给模型的一种能力。请为它单独签发一个 API 密钥，而不要复用生产脚本正在使用的那个。这样，撤销智能体的访问权限就不会影响其他任何东西。

## 相关链接

- [优先级](https://info.pocketalert.app/features/priority-levels.md)：`level` 会如何影响已送达的告警
- [操作按钮](https://info.pocketalert.app/features/action-buttons.md)：基于通知的审批流程
- [创建消息](https://info.pocketalert.app/api/messages/create.md)：`create_message` 对应的 REST 接口
