使用说明
电报mcp
生产级Telegram MCP服务器(Bot API完整域+MTProto基金会)。
特性
- 双重运输:
stdio以及可流式传输的HTTP(/mcp). - 安全默认设置:OIDC JWT身份验证(Keycloak)、RBAC+工具分配列表、静态加密的bot/MTProto凭据。
- 从生成的严格的每种方法Bot API JSON架构验证
@grammyjs/types. - 自动Bot API对生成的方法库存进行奇偶校验。
- PostgreSQL持久性,具有幂等性、审计日志、策略存储和会话/帐户记录。
- Telegram工具系列:
telegram.bot.messagestelegram.bot.mediatelegram.bot.chatstelegram.bot.memberstelegram.bot.inlinetelegram.bot.commandstelegram.bot.webhookstelegram.bot.paymentstelegram.bot.businesstelegram.bot.passporttelegram.bot.stickerstelegram.bot.forumtelegram.bot.raw(受限)telegram.mtproto.sessionstelegram.mtproto.core
- MTProto超集工具系列版本:
telegram.v2.chatstelegram.v2.messagestelegram.v2.contactstelegram.v2.profiletelegram.v2.searchtelegram.v2.privacytelegram.v2.draftstelegram.v2.inlinetelegram.v2.media(S3/MinIO电桥)telegram.v2.approval.requesttelegram.v2.approval.executetelegram.v2.approval.status
- MCP资源:
telegram://botstelegram://bot/{botId}/profiletelegram://policiestelegram://audit/recenttelegram://v2/chatstelegram://v2/contactstelegram://v2/draftstelegram://v2/approvals/recenttelegram://v2/media/{objectId}
先决条件
- Node.js 22+
- Docker+Docker Compose(适用于
--profile local) - PostgreSQL 16+(适用于
--profile external) - Telegram机器人令牌
- 电报
apiId和apiHashMTProto
快速启动(无克隆)
单线设置:
npx @mhbdev/telegram-mcp@latest setup
单行运行时(自愈):
npx @mhbdev/telegram-mcp@latest run
直接服务模式:
npx @mhbdev/telegram-mcp@latest serve --transport=stdio
档案
local(默认):写入.telegram-mcp/config.json和.telegram-mcp/.env,套auth.required=false,开始postgres + keycloak + minio + minio-init,并运行迁移,除非--skip-migrate.external:默认情况下仅写入config/env,不引导本地依赖关系。
配置和环境默认值
- 配置发现顺序:
.telegram-mcp/config.json.telegram-mcp/config.yaml.telegram-mcp/config.ymltelegram-mcp.config.jsontelegram-mcp.config.example.json
- 默认生成的文件:
.telegram-mcp/config.json.telegram-mcp/.env
- 安装格式:
- 默认
json --format yaml支持
- 默认
示例:
npx @mhbdev/telegram-mcp@latest setup --format yaml
npx @mhbdev/telegram-mcp@latest setup --profile external --non-interactive --yes
npx @mhbdev/telegram-mcp@latest run --transport http --host 127.0.0.1 --port 3000 --non-interactive --yes
命令行界面
telegram-mcp setup [--profile local|external] [--format json|yaml]telegram-mcp run [--profile local|external] [--transport stdio|http]telegram-mcp serve --transport=stdiotelegram-mcp serve --transport=http --port=3000telegram-mcp migratetelegram-mcp policy validate --file=policy.jsontelegram-mcp bot account-upsert --account-ref=... --display-name=... --token=...telegram-mcp bot webhook set --account-ref=... --url=...telegram-mcp mtproto session add --account-ref=... --display-name=... --phone=...telegram-mcp mtproto session listtelegram-mcp mtproto session revoke --account-ref=...telegram-mcp mtproto session health --account-ref=...
HTTP端点
POST|GET|DELETE /mcpGET /healthzGET /readyzGET /metrics
Docker Compose(Postgres+Keycloak+MinIO+应用程序)
cd docker
TELEGRAM_MCP_MASTER_KEY="$(openssl rand -base64 32)" docker compose up --build
竞争矩阵
docs/competitor-gap-matrix.md将竞争对手能力组映射到telegram.v2操作。
测试
npm run test
CLI端到端烟雾测试:
npm run build
npm run test:e2e:smoke
可选全栈e2e(手动/夜间风格):
npm run build
npm run test:e2e:fullstack
重新生成并验证Bot API合同:
npm run generate:bot-contract
npm run check:bot-contract
GitHub自动化:
.github/workflows/bot-contract-sync.yml在以下情况下重新生成合同工件@grammyjs/types凸起(过孔package*.json更改上main)并在生成的文件发生变化时打开自动PR。.github/workflows/e2e-fullstack.yml按计划/手动触发器运行可选的全栈CLI e2e。
故障排除
- Docker Compose丢失
setup --profile local:- 交互模式提供回退选项
- 非交互模式在没有依赖引导的情况下继续安装
- 非交互式
run缺少设置工件:- 通过
--yes因此,自愈设置可以自动运行
- 通过
- 现有的配置/env文件:
- 在交互模式下覆盖之前的设置提示
- 非交互式覆盖需要
--force
制作笔记
- 保持
TELEGRAM_MCP_MASTER_KEY在秘密管理器或密封环境中保密。 - 旋转
TELEGRAM_MCP_PREVIOUS_MASTER_KEY用于零停机解密回退。 - 保持
telegram.bot.raw限于admin/owner. - 默认情况下仅使用元数据保留;除非需要,否则避免存储消息有效载荷。