Production-grade MCP server for Telegram — dual-mode Bot API + MTProto, mega-tool pattern
Project description
Better Telegram MCP
mcp-name: io.github.n24q02m/better-telegram-mcp
MCP server for Telegram with dual-mode support: Bot API (httpx) for quick bot integrations and MTProto (Telethon) for full user-account access.
Features
- Dual mode -- Bot API (httpx) for bots, MTProto (Telethon) for user accounts
- 6 mega-tools with action dispatch:
messages,chats,media,contacts,config,help - Auto-detect mode -- Set bot token for bot mode, or API credentials for user mode
- Web-based OTP auth -- Browser-based authentication with remote relay support for headless environments
- Tool annotations -- Each tool declares
readOnlyHint,destructiveHint,idempotentHint,openWorldHint - MCP Resources -- Documentation available as
telegram://docs/*resources - Security hardened -- SSRF protection, path traversal prevention, error sanitization
Quick Start
Claude Code Plugin (Recommended)
Via marketplace (includes skills: /setup-bot, /channel-post):
/plugin marketplace add n24q02m/claude-plugins
/plugin install better-telegram-mcp@claude-plugins
Or install this plugin only:
/plugin marketplace add n24q02m/better-telegram-mcp
/plugin install better-telegram-mcp
Set credentials in ~/.claude/settings.local.json or shell profile. See Environment Variables.
MCP Server
Python 3.13 required -- Python 3.14+ is not supported.
Bot Mode Setup
- Open Telegram, search for @BotFather
- Send
/newbot, follow prompts to name your bot - Copy the token (format:
123456789:ABCdefGHI-JKLmnoPQRstUVwxyz)
User Mode Setup
- Go to my.telegram.org, login with your phone number
- Click "API development tools", create an app
- Note your
api_id(integer) andapi_hash(32-char hex string) - On first run, a local web page opens for OTP authentication
Option 1: uvx
{
"mcpServers": {
"telegram": {
"command": "uvx",
"args": ["--python", "3.13", "better-telegram-mcp"]
}
}
}
Other MCP clients (Cursor, Codex, Gemini CLI)
// Cursor (~/.cursor/mcp.json), Windsurf, Cline, Amp, OpenCode
{
"mcpServers": {
"telegram": {
"command": "uvx",
"args": ["--python", "3.13", "better-telegram-mcp"]
}
}
}
# Codex (~/.codex/config.toml)
[mcp_servers.telegram]
command = "uvx"
args = ["--python", "3.13", "better-telegram-mcp"]
Option 2: Docker
{
"mcpServers": {
"telegram": {
"command": "docker",
"args": [
"run", "-i", "--rm",
"-e", "TELEGRAM_BOT_TOKEN",
"-v", "telegram-data:/data",
"n24q02m/better-telegram-mcp"
]
}
}
}
Configure credentials in ~/.claude/settings.local.json or your shell profile. See Environment Variables below.
For user mode in Docker, mount the session directory with
-v ~/.better-telegram-mcp:/dataso the session persists across container restarts.
Tools
| Tool | Actions | Description |
|---|---|---|
messages |
send, edit, delete, forward, pin, react, search, history |
Send, edit, delete, forward messages. Pin, react, search, browse history |
chats |
list, info, create, join, leave, members, admin, settings, topics |
List and manage chats, groups, channels. Members, admin, forum topics |
media |
send_photo, send_file, send_voice, send_video, download |
Send photos, files, voice notes, videos. Download media from messages |
contacts |
list, search, add, block |
List, search, add contacts. Block/unblock users (user mode only) |
config |
status, set, cache_clear |
Server status, update runtime settings, clear cache |
help |
-- | Full documentation for any tool |
Mode Capabilities
| Feature | Bot | User |
|---|---|---|
| Send/Edit/Delete/Forward messages | Y | Y |
| Pin messages, React | Y | Y |
| Search messages, Browse history | -- | Y |
| List chats, Create groups/channels | -- | Y |
| Get chat info, Manage members | Y | Y |
| Send media (photo/file/voice/video) | Y | Y |
| Download media | -- | Y |
| Contacts (list/search/add/block) | -- | Y |
MCP Resources
| URI | Content |
|---|---|
telegram://docs/messages |
Message operations reference |
telegram://docs/chats |
Chat management reference |
telegram://docs/media |
Media send/download reference |
telegram://docs/contacts |
Contact management reference |
telegram://stats |
All documentation combined |
Zero-Config Setup
No environment variables needed. On first start, the server opens a setup page in your browser:
- Start the server (via plugin,
uvx, or Docker) - A setup URL appears -- open it in any browser
- Fill in your credentials on the guided form
- Credentials are encrypted and stored locally
Your credentials never leave your machine. The relay server only sees encrypted data.
For CI/automation, you can still use environment variables (see below).
Configuration
| Variable | Required | Default | Description |
|---|---|---|---|
TELEGRAM_BOT_TOKEN |
Bot mode | -- | Bot token from @BotFather |
TELEGRAM_API_ID |
User mode | -- | API ID from my.telegram.org (integer) |
TELEGRAM_API_HASH |
User mode | -- | API hash from my.telegram.org (32-char hex) |
TELEGRAM_PHONE |
User mode | -- | Phone number with country code (e.g., +84912345678) |
TELEGRAM_AUTH_URL |
No | https://better-telegram-mcp.n24q02m.com |
Auth relay URL. Set local for localhost-only mode |
TELEGRAM_SESSION_NAME |
No | default |
Session file name (useful for multiple accounts) |
TELEGRAM_DATA_DIR |
No | ~/.better-telegram-mcp |
Data directory for session files |
Mode detection: If TELEGRAM_API_ID + TELEGRAM_API_HASH are set, user mode is used. Otherwise, TELEGRAM_BOT_TOKEN is used. No silent fallback -- if neither is set, the server exits with an error.
Auth Flow (User Mode Only)
- On first run, a web-based auth UI opens in your browser (or URL is logged for headless)
- Click "Send OTP Code" -- code is sent to the Telegram app (not SMS)
- Enter the OTP code; if 2FA is enabled, enter your password
- Session file is saved at
~/.better-telegram-mcp/<name>.session(600 permissions) - Tools become active immediately -- no restart needed
| Auth Mode | TELEGRAM_AUTH_URL |
Use case |
|---|---|---|
| Remote (default) | https://better-telegram-mcp.n24q02m.com |
Headless, SSH, Docker |
| Self-hosted | https://your-domain.com |
Custom relay deployment |
| Local | local |
Desktop, offline |
Headless auth (Docker/SSH) -- use curl against the auth URL shown in logs:
curl -X POST http://127.0.0.1:PORT/send-code
curl -X POST http://127.0.0.1:PORT/verify -d '{"code":"12345"}'
curl -X POST http://127.0.0.1:PORT/verify -d '{"password":"your-2fa-password"}' # if 2FA
Security
- SSRF Protection -- All URLs validated against internal/private IP ranges, DNS rebinding blocked
- Path Traversal Prevention -- File paths validated, sensitive directories blocked
- Session File Security -- 600 permissions, 2FA via web UI only (never stored in env vars)
- Error Sanitization -- Credentials never leaked in error messages
Build from Source
git clone https://github.com/n24q02m/better-telegram-mcp.git
cd better-telegram-mcp
uv sync
uv run better-telegram-mcp
Compatible With
Also by n24q02m
| Server | Description |
|---|---|
| wet-mcp | Web search, content extraction, and documentation indexing |
| mnemo-mcp | Persistent AI memory with hybrid search and cross-machine sync |
| better-notion-mcp | Markdown-first Notion API with 9 composite tools |
| better-email-mcp | Email (IMAP/SMTP) with multi-account and auto-discovery |
| better-godot-mcp | Godot Engine 4.x with 18 tools for scenes, scripts, and shaders |
| better-code-review-graph | Knowledge graph for token-efficient code reviews |
Contributing
See CONTRIBUTING.md.
License
MIT -- See LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file better_telegram_mcp-3.3.0b1.tar.gz.
File metadata
- Download URL: better_telegram_mcp-3.3.0b1.tar.gz
- Upload date:
- Size: 124.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5aa6b1ca0085b286f409eb4fad74689b12d43e9cb39edb584e8930e2028bc342
|
|
| MD5 |
5e391dd90ae36019ec5028875fb818ed
|
|
| BLAKE2b-256 |
daf34e664189ab9f34abace80c0b395fb9d17e0e71672a818c240417833652a5
|
File details
Details for the file better_telegram_mcp-3.3.0b1-py3-none-any.whl.
File metadata
- Download URL: better_telegram_mcp-3.3.0b1-py3-none-any.whl
- Upload date:
- Size: 43.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3359cab6893ad95b9df8adc94fc0a12f1f0bbdb009de1be798504d42338d96c3
|
|
| MD5 |
5bb3a93d246f6772039d1eb630e969c0
|
|
| BLAKE2b-256 |
d741b8258a7c1eee2581436388d499de1f1ce375f10d824b5a431c5fd60208ea
|