mcp-server-telegram
Integrates Telegram bots as MCP tools for sending messages to staff groups or manager chats, listing buffered messages, and viewing group members.
README
mcp-server-telegram
Model Context Protocol (MCP) server for Telegram integration. Exposes a Telegram bot as three MCP tools over the standard stdio transport.
Originally built for the YengTong 7-Scenario AI Agent demo as the second messaging channel alongside email, where:
- Staff are modeled as a single Telegram group — reminders land as @mentions.
- Escalations go to a separate manager-only chat, never visible to staff.
- A background long-poller buffers incoming messages into a local JSON store so tool calls return synchronously with low, predictable latency.
Tools
| Tool | Description |
|---|---|
telegram_send_message |
Send a text message to either the staff_group (for reminders) or manager_chat (for escalations). |
telegram_list_messages |
List incoming messages buffered from the staff group. Optional since (ISO timestamp) filter. |
telegram_list_group_members |
Setup/debug helper — list staff group members observed by the poller so far. |
Setup
1. Create a Telegram bot
- Open Telegram and message @BotFather.
- Send
/newbot, follow the prompts, and copy the bot token. - Disable privacy mode for the bot (also via BotFather →
/mybots→ select your bot → Bot Settings → Group Privacy → Turn off). Without this, the bot only sees/commandmessages in groups, not regular text — the poller won't receive staff updates.
2. Create the chats
- Staff group: create a Telegram group, add the bot as a member, add the 5 staff personas (or real staff).
- Manager chat: a separate small group or a 1:1 DM with a manager test account that has sent
/startto the bot. This is wheretelegram_send_messagewithtarget: "manager_chat"sends to.
To obtain the chat IDs, send a message in each chat and call the Telegram API manually:
curl "https://api.telegram.org/bot<TOKEN>/getUpdates"
Look for message.chat.id in the response. Group chat IDs are negative numbers (e.g. -1001234567890).
3. Configure environment
cp .env.template .env
# Edit .env with your bot token and the two chat IDs.
4. Install and build
npm install
npm run build
5. Run
npm start
The server boots, loads (or creates) the local store, starts the long-poller, and connects over stdio. Connect it from an MCP client.
6. Test
npm run build && node test_mcp.mjs
Runs an end-to-end MCP handshake (initialize → tools/list) and verifies all three tools are registered.
Environment variables
| Variable | Required | Default | Description |
|---|---|---|---|
TELEGRAM_BOT_TOKEN |
yes | — | Bot token from @BotFather |
TELEGRAM_STAFF_GROUP_CHAT_ID |
yes | — | Chat ID of the staff group (negative number for supergroups) |
TELEGRAM_MANAGER_CHAT_ID |
yes | — | Chat ID of the manager-only chat |
TELEGRAM_STORE_PATH |
no | ./telegram_store.json |
Path to the local JSON message/member store |
TELEGRAM_POLL_INTERVAL_MS |
no | 1000 |
Delay between poll cycles (ms) — only relevant after errors |
TELEGRAM_LONG_POLL_TIMEOUT |
no | 30 |
Seconds to hold each getUpdates request open (Telegram long-polling) |
Operational notes
- The poller must be running before any workflow that reads messages fires. Messages sent to the group before the poller starts are lost unless the poller's
offsetwas persisted from a previous run — on boot, the poller resumes from the last savedoffsetand Telegram redelivers any updates received while the server was down (within Telegram's 24-hour update retention window). - Attachments are flagged, not fetched.
telegram_list_messagesreturnshas_attachment: truefor messages with photos/files. Photo/document captions are captured in thetextfield so the workflow still sees the sender's words. The workflow's edge-case guard routes media-bearing messages to human review; this server intentionally doesn't fetch or interpret attachment content. - Consumption contract:
telegram_list_messagesdoes not mark messages as consumed. The workflow is responsible for tracking its own last-run timestamp (e.g. in the tracker CSV or a workspace key) and passing it as thesinceparameter each run. For a daily 09:00 schedule, passsince=yesterday_09:00to get the previous day's messages. Invalidsincevalues throw rather than silently returning all messages. - Rate limits are handled automatically — on HTTP 429 from Telegram, the server sleeps for the
Retry-Afterduration and retries up to 3 times before surfacing the error. - Graceful shutdown: SIGINT/SIGTERM aborts any in-flight long-poll immediately, stops the poller, and flushes the store to disk via an atomic temp-file-then-rename write.
Design decisions
- No external Telegram library. Uses
fetchdirectly againsthttps://api.telegram.org/bot<TOKEN>/...— keeps dependencies minimal and avoids native bindings. - JSON file store, not SQLite. Demo-scale volume (under a few dozen messages) doesn't justify SQLite's native dependency cost. Swap in SQLite by replacing the
loadStore/saveStoremethods if needed for larger deployments. - Member list is observed, not enumerated. The Telegram Bot API has no "list all group members" endpoint.
telegram_list_group_membersreturns the set of users the poller has seen send messages into the staff group, which for a demo with seeded personas is the full set. targetis an enum, not a freeform chat ID. Constrains sends to the two pre-configured chats, so workflows can't accidentally send escalations to the wrong audience by passing a wrong chat ID.
Security notes
- Bot token is a secret. Keep it in
.env, never commit it..envis in.gitignore. - Sends are constrained to configured chats. The
targetenum means tool calls can only send tostaff_groupormanager_chat— the chat IDs are server-side, not caller-controlled. - No attachment content is fetched or written to disk.
has_attachmentis a flag only; no risk of malicious-file write-through.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。