Telegram MCP Server
Telegram MCP Server
用于通过 Telegram Bot API 与 Telegram 交互的 MCP 服务器。该服务器为 LLM 客户端提供一组工具,用于发送消息、读取最近可用的消息以及获取聊天信息。
功能特性
服务器提供三个 MCP 工具:
send_message— 向指定的 Telegram 聊天发送文本消息。get_recent_messages— 获取指定聊天中最近可用的消息。get_chat_info— 返回 Telegram 聊天的基本信息。
服务器使用 stdio 传输,因此可以连接到 MCP Inspector 和其他 MCP 客户端,而无需单独的 HTTP 服务器。
Related MCP server: agent-telegram-mcp
架构
MCP client / MCP Inspector
│
│ MCP over stdio
▼
src/server.py
│
▼
src/telegram_client.py
│
│ HTTPS
▼
Telegram Bot APIserver.py 负责 MCP 接口和工具注册。
telegram_client.py 封装了与 Telegram Bot API 的 HTTP 交互。
config.py 从环境变量加载令牌。
技术栈
Python 3.10+
MCP Python SDK 2.x
Telegram Bot API
httpxpython-dotenv
项目结构
telegram-mcp/
├── src/
│ ├── __init__.py
│ ├── config.py
│ ├── telegram_client.py
│ └── server.py
├── .env.example
├── .gitignore
├── requirements.txt
└── README.md要求
Python 3.10 或更高版本
通过
@BotFather创建的 Telegram 机器人Node.js 和
npx— 仅当通过mcp dev使用 MCP Inspector 时需要
安装
克隆仓库并进入其目录:
git clone <repository-url>
cd telegram-mcp创建虚拟环境:
python3 -m venv .venv
source .venv/bin/activate安装依赖:
pip install -r requirements.txt配置 Telegram 机器人
打开 Telegram 并找到
@BotFather。执行
/newbot。创建机器人并获取 Bot API 令牌。
不要将令牌添加到源代码或 Git。
创建 .env 文件:
cp .env.example .env指定令牌:
TELEGRAM_BOT_TOKEN=your_telegram_bot_token_here.env 已添加到 .gitignore。
准备聊天
私聊
打开已创建的机器人。
点击
Start或向其发送消息。为了测试
get_recent_messages,请发送几条文本消息。
群组
创建一个测试群组。
将机器人添加到群组中。
如果机器人需要查看群组的普通消息,请通过
@BotFather关闭其隐私模式(/setprivacy→Disable)。向群组发送几条消息。
为了获取 chat_id,可以在机器人从目标聊天收到消息后,先调用 get_chat_info 或 get_recent_messages。
运行
从项目根目录:
python src/server.py服务器通过 stdio 运行并等待 MCP 连接。因此,启动后终端没有常规输出属于正常现象。
可以使用 MCP CLI 进行开发和测试:
mcp dev src/server.py该命令启动服务器和 MCP Inspector。Inspector 使用 npx,因此 Node.js 必须在 PATH 中可用。
MCP 工具
send_message
向 Telegram 发送文本消息。
参数:
chat_id: string — ID чата
text: string — текст сообщения示例:
chat_id: 123456789
text: Привет! Сообщение отправлено через MCP.服务器在响应中返回发送确认和 message_id。
get_recent_messages
获取指定聊天的最近可用消息。
参数:
chat_id: string — ID чата
limit: integer — количество сообщений, по умолчанию 10limit 限制在 1 到 100 的范围内。
示例:
chat_id: 123456789
limit: 10结果包含每条可用消息的发送者和文本。
get_chat_info
获取聊天的基本信息。
参数:
chat_id: string — ID чата响应中显示可用字段,包括 ID、类型、名称、username、名字和姓氏。
消息获取的工作原理
Telegram Bot API 不向机器人提供单独的方法来读取任意聊天历史。为了获取传入消息,服务器使用 getUpdates。
get_recent_messages 请求最多 100 条最近可用的 updates,然后按 chat_id 过滤。因此,该工具处理的是 Telegram 通过 updates 队列提供给机器人的消息,而不是完整的聊天历史。
这意味着该工具不能替代可以访问完整聊天记录的 Telegram 客户端。要测试,只需在将机器人添加到聊天后发送消息,然后调用 get_recent_messages 即可。
重要:getUpdates 不能与活动的 webhook 同时使用。如果机器人配置了 webhook,请先删除它,以便通过 getUpdates 的 long polling 能够接收 updates。
示例场景
启动 MCP Inspector。
连接
src/server.py。确保以下工具可用:
send_messageget_recent_messagesget_chat_info
调用
get_chat_info以验证与聊天的连接。调用
send_message并确认消息已出现在 Telegram 中。在 Telegram 中发送几条消息。
调用
get_recent_messages并检查返回的消息列表。
安全性
Telegram Bot API 令牌仅通过环境变量 TELEGRAM_BOT_TOKEN 传递。
真实的 .env 文件不应提交到 Git。仓库中只保留不包含实际令牌的 .env.example。
限制
机器人无法通过 Bot API 访问完整的 Telegram 聊天历史。
get_recent_messages处理可用的 bot updates。在群组中,机器人收到的消息集合取决于 Telegram 的隐私设置。
getUpdates和 webhook 是互斥的获取 updates 的方式。
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceEnables AI agents to read, send, and organize Telegram messages and chats. Supports tools for listing chats, fetching messages, sending/reply, archiving, muting, and folder management.1MIT
- AlicenseAqualityDmaintenanceEnables AI agents to send and receive messages, media, and files on Telegram, and manage chats via a bot token.17MIT
- AlicenseNot gradedqualityDmaintenanceEnables AI assistants to interact with the Telegram Bot API, allowing them to send messages, forward messages, get bot information, and receive updates.273MIT
- AlicenseNot gradedqualityDmaintenanceEnables LLMs to send notifications and receive responses via Telegram.174MIT
Related MCP Connectors
Telegram bridge for your MCP-compatible agent. Bidirectional, no LLM in our stack.
Multi-tenant Telegram gateway for AI agents — HTTP+stdio, 8 tools, MTProto User API
Gateway between LLM agents and world data through eight tools and a bundled endpoint catalog.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/polinenysh/telegram-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server