telegram-api-mcp
<div align="center">
# telegram-mcp-worker
**Ultimate MCP server for Telegram Bot API — 185 methods, full v10.2 coverage, rich messages, meta-mode, rate limiting, circuit breaker — running on Cloudflare Workers.**
[](https://github.com/EngDawood/telegram-mcp-worker/stargazers)
[](LICENSE)
[](https://core.telegram.org/bots/api)
[](https://workers.cloudflare.com)
</div>
185/185 Bot API methods with Zod validation, token masking, tool annotations, and zero bloat (2 dependencies) — served over stateless Streamable HTTP. The same Worker also receives bot updates through a built-in webhook endpoint, so one deployment covers both sending and receiving. The live deployment runs in [meta-mode](#meta-mode): the 191 tools collapse to 8, saving ~99% of the context tokens the tool definitions used to take.
**Live deployment:** `https://telegram-mcp-worker.engdawood.workers.dev`
| Endpoint | Method | Purpose |
|----------|--------|---------|
| `/` or `/health` | GET | Plain-text status check |
| `/mcp` | POST | MCP endpoint (Streamable HTTP, stateless) |
| `/webhook` | POST | Telegram webhook receiver for bot updates |
| `/webhook` | GET | Webhook endpoint status check |
## What this fork adds
- **Worker entry point** (`src/worker.ts`) — the original MCP server served
over Streamable HTTP on Cloudflare Workers, a fresh server per request.
- **Webhook receiver** (`POST /webhook`) — acknowledges updates and writes
them to the Worker log (Cloudflare observability / `wrangler tail`),
optionally verified with Telegram's secret-token header.
- **Token auth for `/mcp`** — when `MCP_AUTH_TOKEN` is set, clients must
send the token in any form their connector supports:
`Authorization: Bearer <MCP_AUTH_TOKEN>`, `x-api-key: <MCP_AUTH_TOKEN>`,
`api-key: <MCP_AUTH_TOKEN>` or `?token=<MCP_AUTH_TOKEN>` in the URL
(same token, four options; headers preferred — URLs get logged).
- **GitHub-connected deployment** — pushes to `main` deploy automatically
from the Cloudflare dashboard.
Everything else — the tool registry with Zod validation, rate limiting,
circuit breaker, retry with backoff, token masking, meta-mode and the
`telegram_format` helper — is inherited from the original project (see the
fork notice at the end).
## Connect an MCP client
Point any MCP client at the hosted endpoint:
```json
{
"mcpServers": {
"telegram": {
"url": "https://telegram-mcp-worker.engdawood.workers.dev/mcp",
"headers": {
"Authorization": "Bearer <your MCP_AUTH_TOKEN>"
}
}
}
}
```
## Meta-mode
The live deployment runs in meta-mode (`TELEGRAM_META_MODE=true` in
`wrangler.toml` under `[vars]`). Instead of one MCP tool per Bot API method
(191 tools, each carrying its full input schema), the server exposes only 8
tools — `telegram_find`, `telegram_call`, `telegram_format`,
`download_file`, `get_message_file` and the TRAIL tools (`get_trail`,
`mark_trail`, `get_trail_stats`) — which saves ~99% of the context tokens
the tool definitions used to take. The full Bot API is still available
through the two meta-tools:
1. `telegram_find` — search methods by keyword, e.g.
`{"query": "webhook"}` finds `setWebhook`, `deleteWebhook` and
`getWebhookInfo` with their parameters.
2. `telegram_call` — call a method by its camelCase Bot API name with a
`params` object, e.g. `{"method": "getMe"}` or
`{"method": "sendMessage", "params": {"chat_id": "...", "text": "..."}}`.
To enable it on your own deployment, set `TELEGRAM_META_MODE` to `"true"`
as a plain Variable (not a Secret) — in `wrangler.toml` under `[vars]` or in
the dashboard under Settings → Variables — and redeploy. Remove it (or set
it to `"false"`) to go back to the standard 191-tool mode.
## Claude plugin
This repo is also a Claude plugin (and its own plugin marketplace). In
Claude Code:
```text
/plugin marketplace add EngDawood/telegram-mcp-worker
/plugin install telegram-mcp-worker@telegram-mcp-worker
```
The plugin registers the hosted MCP server from `.mcp.json`. Export your
token first so Claude can expand it:
```bash
export MCP_AUTH_TOKEN=<your MCP_AUTH_TOKEN>
```
To deploy and use your own Worker instead, change the `url` in `.mcp.json`
to your deployment.
The plugin also ships a skill at
[`skills/telegram-mcp-worker/SKILL.md`](skills/telegram-mcp-worker/SKILL.md)
(linked from `.claude/skills/`), which teaches the agent the working rules
above: verify with `telegram_call` (`getMe`), ask `telegram_format` before complex payloads,
and work within the Worker limits.
## Deploy your own
### Option A — Cloudflare dashboard (GitHub-connected)
1. Fork or clone this repo to your GitHub account.
2. In the Cloudflare dashboard: **Workers & Pages → Create → Import a
repository** and pick the repo. Pushes to `main` now deploy
automatically.
3. Add the secrets below under **Settings → Variables and Secrets**.
### Option B — Wrangler
```bash
npm install
npx wrangler secret put TELEGRAM_BOT_TOKEN # from @BotFather
npx wrangler secret put MCP_AUTH_TOKEN # bearer token for /mcp
npx wrangler secret put WEBHOOK_SECRET # webhook verification token
npm run worker:deploy
```
### Secrets
| Secret | Required | Purpose |
|--------|:---:|---------|
| `TELEGRAM_BOT_TOKEN` | Yes | Bot token from [@BotFather](https://t.me/BotFather) |
| `MCP_AUTH_TOKEN` | Recommended | Token clients must send to `POST /mcp` as `Authorization: Bearer`, `x-api-key`, `api-key` or `?token=`. Without it the endpoint is open. |
| `WEBHOOK_SECRET` | Optional | When set, `POST /webhook` only accepts updates whose `X-Telegram-Bot-Api-Secret-Token` header matches it. Register the same value as the `setWebhook` `secret_token`. |
Optional plain variables (`TELEGRAM_DEFAULT_CHAT_ID`,
`TELEGRAM_DEFAULT_THREAD_ID`, `TELEGRAM_META_MODE` and the tuning knobs) are
documented in `src/config.ts`; `TELEGRAM_META_MODE` is covered in
[Meta-mode](#meta-mode) above.
## Webhook
Register the webhook through the MCP server itself by calling `setWebhook`
through `telegram_call` (in standard mode the same call is the `set_webhook`
tool):
```json
{
"method": "setWebhook",
"params": {
"url": "https://<your-worker>.workers.dev/webhook",
"secret_token": "<same value as the WEBHOOK_SECRET secret>",
"allowed_updates": ["message", "edited_message", "channel_post", "edited_channel_post", "callback_query", "inline_query"]
}
}
```
Notes:
- While a webhook is registered, Telegram rejects `get_updates` calls.
Call `telegram_call` with `deleteWebhook` to go back to polling.
- `telegram_call` with `getWebhookInfo` shows the registered URL, pending update count and
the last delivery error, if any.
- The receiver currently logs updates; it does not reply to messages.
## Local development
```bash
npm install
npm run worker:dev # wrangler dev — Worker build on localhost
npm run build # tsc — also builds the original stdio server
npm test # vitest
npm run typecheck
```
Create a `.dev.vars` file (git-ignored) with the same secret names for
local runs.
The original stdio build is unchanged and still available:
```bash
npm run build && npm start
```
## From source
The original stdio build still runs locally from source:
```bash
git clone https://github.com/EngDawood/telegram-mcp-worker.git
cd telegram-mcp-worker
npm install
npm run build
TELEGRAM_BOT_TOKEN=<your bot token> npm start
```
To run the Worker build locally from the same checkout instead:
```bash
npm run worker:dev
```
Put the secrets in a `.dev.vars` file first (see Local development above).
## Limitations on Workers
Features of the original Node build that rely on a local filesystem return
errors on Workers:
- Uploading files from local paths (sending by `file_id`, URL or bytes
works as before)
- The TRAIL log tools
- Mirroring URL media to disk
## Temporary file storage
`download_file` and `get_message_file` need somewhere to put the file. On
Workers that is an R2 bucket bound as `FILES` (already declared in
`wrangler.toml`):
```bash
wrangler r2 bucket create telegram-mcp-files
```
```bash
wrangler r2 bucket lifecycle add telegram-mcp-files expire-tmp tmp/ --expire-days 1
```
Both tools then return a download URL served by the Worker at
`GET /files/<uuid>/<name>`. The URL needs no auth header — the random
UUID is the secret — and stops working after `FILES_TTL_SECONDS`
(default 3600). The lifecycle rule removes files nobody fetched. Without
the binding the tools return the `file_id` and a clear error instead of a
URL.
`get_message_file` exists because the Bot API cannot read a message by
ID. Given a chat and message ID (for `t.me/c/1234567890/150`:
`from_chat_id` `-1001234567890`, `message_id` `150`) it forwards the
message silently to a scratch chat (default: `TELEGRAM_DEFAULT_CHAT_ID`),
reads the `file_id`, deletes the forwarded copy and downloads the file.
## Architecture
```text
MCP client ──POST /mcp (Bearer)──▶ Worker ──fetch──▶ api.telegram.org
Telegram ──POST /webhook───────▶ Worker ──▶ update written to Worker log
```
- **Stateless per request.** Each `POST /mcp` builds a fresh MCP server on
a web-standard Streamable HTTP transport with JSON responses. No session
state is kept between requests, which is what makes the design fit
Workers.
- **One server builder, two transports.** `buildServer(config)` in
`src/mcp/server.ts` assembles the MCP server and the Telegram client from the
configuration; the Worker entry (`src/worker.ts`) and the original stdio
entry (`src/index.ts`) both use it unchanged.
- **Declarative tool registry.** All 185 Bot API methods are defined as
data — name, Bot API endpoint, Zod input schema — and served by one
generic handler, alongside the `telegram_format` helper, `download_file`,
`get_message_file` and the TRAIL tools. With `TELEGRAM_META_MODE=true` the registry
collapses into two meta-tools (`telegram_find` / `telegram_call`), plus
`telegram_format`, `download_file`, `get_message_file` and the TRAIL tools
— 8 tools in total on the live deployment.
- **Telegram client.** A plain `fetch`-based Bot API client
(`src/telegram/telegram-client.ts`) with global and per-chat rate limiting, a
circuit breaker and retry with backoff. It also masks the bot token in
every response and error.
- **Configuration.** `loadConfig` in `src/core/config.ts` maps the Worker's
secrets/vars (or environment variables in the stdio build) into one
validated config object.
- **Webhook path.** `POST /webhook` verifies the secret-token header when
`WEBHOOK_SECRET` is set, writes the update JSON to the Worker log and
acknowledges immediately with `{"ok": true}`.
| Path | What it is |
|------|------------|
| `src/worker.ts` | Cloudflare Worker entry: `/mcp`, `/webhook`, `/health` |
| `src/mcp/` | MCP server: `server.ts`, `method-registry.ts`, `formats.ts`, `methods/` |
| `src/telegram/` | `telegram-client.ts`, the Bot API client |
| `src/core/` | `config.ts`, `rate-limiter.ts`, `circuit-breaker.ts` |
| `src/trail/` | TRAIL protocol: `trail.ts`, `post-log.ts` |
| `src/index.ts` | Original stdio entry point |
| `src/config.ts` | Environment/secrets loading and tuning knobs |
| `docs/` | Documentation: `project/` (this codebase), `bot-api/` (official docs mirror), `history/` |
## License
MIT — see [LICENSE](LICENSE).
---
## Acknowledgments
This project is a fork of
[timoncool/telegram-api-mcp](https://github.com/timoncool/telegram-api-mcp)
(based on commit `34af6ba`), and it would not exist without the original
author's work. All credit for the original MCP server — the tool registry
covering the full Telegram Bot API and the stdio build — goes to
[@timoncool](https://github.com/timoncool). Thank you for building it and
for releasing it under the MIT license, which is preserved in
[LICENSE](LICENSE). This fork only adds the Cloudflare Worker conversion
(`src/worker.ts`), the `/webhook` receiver, and the deployment setup used
to run it at `telegram-mcp-worker.engdawood.workers.dev`.
> **This fork (`telegram-mcp-worker`)** converts the server below to a
> **Cloudflare Worker**: the full Bot API served over stateless
> Streamable HTTP at `POST /mcp` (`src/worker.ts`). Based on
> [timoncool/telegram-api-mcp](https://github.com/timoncool/telegram-api-mcp)
> @ `34af6ba` (MIT — see LICENSE).
TDQS
Scored across 190 tools
Most tools have distinct verb+resource purposes, but the sheer number of similar variants creates real ambiguity: send_message vs send_message_draft vs send_rich_message, edit_message_text vs edit_ephemeral_message_text, delete_message vs delete_messages, and forward/copy singular vs plural. Descriptions help distinguish them, but an agent must still parse many closely related options before choosing.
The surface overwhelmingly follows a predictable snake_case verb_noun pattern (send_message, get_chat, delete_sticker_from_set). Minor deviations exist, such as bare verbs 'close' and 'log_out', plus domain-specific names like 'telegram_format' and 'get_trail', but these do not seriously undermine readability.
190 tools is an extreme mismatch for a usable MCP surface, far beyond the typical 3-15 range and well past the 50+ threshold. Even though the tools mirror the Telegram Bot API, the volume creates severe selection and context burdens for an agent.
The tool set covers an unusually broad Telegram Bot API surface: messaging, media, chats, members, stickers, payments, gifts, business accounts, webhooks, inline mode, and formatting helpers. No obvious lifecycle or domain gap stands out for the stated purpose.