Better Telegram MCP
Production-grade MCP server for Telegram with dual-mode Bot API and MTProto. 6 composite tools covering messages, chats, media, contacts management with 3-tier token optimization.
README
better-telegram-mcp
mcp-name: io.github.n24q02m/better-telegram-mcp
<a href="https://glama.ai/mcp/servers/n24q02m/better-telegram-mcp"> <img width="380" height="200" src="https://glama.ai/mcp/servers/n24q02m/better-telegram-mcp/badge" alt="better-telegram-mcp MCP server" /> </a>
Production-grade MCP server for Telegram with dual-mode support: Bot API (via httpx) for quick bot integrations and MTProto (via Telethon) for full user-account access including message search, history browsing, contact management, and group creation.
Features
- 6 mega-tools with action dispatch:
messages,chats,media,contacts,config,help - Dual mode: Bot API (httpx) for bots, MTProto (Telethon) for user accounts
- 3-tier token optimization: Only 6 tools registered instead of 30+ individual endpoints
- Tool annotations: Each tool declares
readOnlyHint,destructiveHint,idempotentHint,openWorldHint - MCP Resources: Documentation available as
telegram://docs/*resources - Auto-detect mode: Set bot token for bot mode, or API credentials for user mode
Quick Start
Bot Mode
- Open Telegram, search for @BotFather
- Send
/newbot, follow prompts to name your bot - Copy the token (format:
123456789:ABCdefGHI-JKLmnoPQRstUVwxyz) - Run:
TELEGRAM_BOT_TOKEN=123456:ABC-DEF uvx --python 3.13 better-telegram-mcp
User Mode
- Go to https://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) - Add to your MCP config with all required env vars (see examples below)
- Start using — on first run, auth happens automatically:
- Server auto-sends an OTP code to your Telegram app (not SMS, not browser)
- A terminal window opens for you to enter the OTP code directly (no AI relay needed)
- If 2FA is enabled: set
TELEGRAM_PASSWORDenv var, or enter it in the terminal prompt - In headless environments (Docker, SSH, no desktop): use
config(action='auth', code='12345')as fallback
- Done — session file persists at
~/.better-telegram-mcp/<name>.session, no more auth needed on subsequent runs
Security: The session file has
600permissions (owner-only). Treat it like a password — anyone with this file can access your Telegram account.
Configuration
All configuration is via environment variables with TELEGRAM_ prefix:
| Variable | Required | Default | Description |
|---|---|---|---|
TELEGRAM_BOT_TOKEN |
Bot mode | - | Bot token from @BotFather |
TELEGRAM_API_ID |
User mode | - | API ID from my.telegram.org |
TELEGRAM_API_HASH |
User mode | - | API hash from my.telegram.org |
TELEGRAM_PHONE |
User mode | - | Phone number with country code (e.g., +84912345678). Required for auto OTP send on startup. |
TELEGRAM_PASSWORD |
No | - | Two-Step Verification password. If set, used automatically during sign-in. If not set and 2FA is enabled, sign-in will fail with SessionPasswordNeededError. |
TELEGRAM_SESSION_NAME |
No | default |
Session file name (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 (priority). Otherwise, TELEGRAM_BOT_TOKEN is used for bot mode. No silent fallback between modes.
MCP Config Examples
Claude Code
# Bot mode
claude mcp add telegram -e TELEGRAM_BOT_TOKEN=123456:ABC-DEF -- uvx --python 3.13 better-telegram-mcp
# User mode (auto-auth on first run)
claude mcp add telegram -e TELEGRAM_API_ID=12345 -e TELEGRAM_API_HASH=abc123 -e TELEGRAM_PHONE=+84912345678 -- uvx --python 3.13 better-telegram-mcp
Claude Desktop / Cursor
Bot mode:
{
"mcpServers": {
"telegram": {
"command": "uvx",
"args": ["--python", "3.13", "better-telegram-mcp"],
"env": {
"TELEGRAM_BOT_TOKEN": "123456:ABC-DEF"
}
}
}
}
User mode:
{
"mcpServers": {
"telegram": {
"command": "uvx",
"args": ["--python", "3.13", "better-telegram-mcp"],
"env": {
"TELEGRAM_API_ID": "12345678",
"TELEGRAM_API_HASH": "your-api-hash-from-my-telegram-org",
"TELEGRAM_PHONE": "+1234567890",
"TELEGRAM_PASSWORD": "your-2fa-password-if-enabled"
}
}
}
}
VS Code Copilot
Add to .vscode/mcp.json:
{
"servers": {
"telegram": {
"command": "uvx",
"args": ["--python", "3.13", "better-telegram-mcp"],
"env": {
"TELEGRAM_BOT_TOKEN": "123456:ABC-DEF"
}
}
}
}
Docker
# Bot mode
docker run -i --rm -e TELEGRAM_BOT_TOKEN=123456:ABC-DEF n24q02m/better-telegram-mcp
# User mode (auto-auth on first run, mount session dir for persistence)
docker run -i --rm \
-e TELEGRAM_API_ID=12345 \
-e TELEGRAM_API_HASH=abcdef123456 \
-e TELEGRAM_PHONE=+84912345678 \
-v ~/.better-telegram-mcp:/data \
n24q02m/better-telegram-mcp
Docker config for MCP clients:
{
"mcpServers": {
"telegram": {
"command": "docker",
"args": [
"run", "-i", "--rm",
"-e", "TELEGRAM_BOT_TOKEN",
"n24q02m/better-telegram-mcp"
],
"env": {
"TELEGRAM_BOT_TOKEN": "123456:ABC-DEF"
}
}
}
}
Note: For user mode in Docker, mount the session directory with -v ~/.better-telegram-mcp:/data so the session persists across container restarts. On first run, complete auth via config(action='auth', code='YOUR_CODE') (terminal auth is not available in Docker).
Mode Capabilities
| Feature | Bot Mode | User Mode |
|---|---|---|
| Send messages | Y | Y |
| Edit messages | Y | Y |
| Delete messages | Y | Y |
| Forward messages | Y | Y |
| Pin messages | Y | Y |
| React to messages | Y | Y |
| Search messages | - | Y |
| Browse history | - | Y |
| List chats | - | Y |
| Get chat info | Y | Y |
| Create groups/channels | - | Y |
| Join/Leave chats | Partial | Y |
| Manage members | Y | Y |
| Admin promotion | Y | Y |
| Chat settings | Y | Y |
| Forum topics | Partial | Y |
| Send media (photo/file/voice/video) | Y | Y |
| Download media | - | Y |
| List contacts | - | Y |
| Search contacts | - | Y |
| Add contacts | - | Y |
| Block/Unblock users | - | Y |
Tool Reference
Use the help tool for full documentation:
help(topic="messages") # Message operations
help(topic="chats") # Chat management
help(topic="media") # Media send/download
help(topic="contacts") # Contact management
help(topic="all") # Everything
Troubleshooting
| Error | Cause | Fix |
|---|---|---|
No Telegram credentials found |
Neither bot token nor API credentials set | Set TELEGRAM_BOT_TOKEN or TELEGRAM_API_ID + TELEGRAM_API_HASH |
Invalid bot token |
Token revoked or wrong | Regenerate via /token in @BotFather |
Authentication required |
Session not yet authorized | Complete auth in the terminal window, or use config(action='auth', code='YOUR_CODE') in headless environments |
PhoneNumberInvalidError |
Wrong phone format | Include country code with + (e.g., +84912345678) |
SessionPasswordNeededError |
2FA enabled but no password | Set TELEGRAM_PASSWORD env var in your MCP config |
FloodWaitError |
Too many auth attempts | Wait the indicated seconds before retrying |
requires user mode |
Action not available in bot mode | Switch to user mode (API ID + Hash) |
| Session lost after Docker restart | Data volume not mounted | Add -v ~/.better-telegram-mcp:/data |
| OTP sent but where? | Code goes to Telegram app | Check the Telegram app on your phone (not SMS, not browser). Look for a message from "Telegram" with a login code. |
| How to enter OTP code? | Terminal window should open | Enter the code in the terminal window that opens automatically. If no terminal (headless/Docker), use config(action='auth', code='12345') |
| Need to re-send OTP | Code expired or not received | Call config(action='send_code') to request a new code |
Compatible With
Also by n24q02m
| Server | Description | Install |
|---|---|---|
| better-notion-mcp | Notion API for AI agents | npx -y @n24q02m/better-notion-mcp@latest |
| better-email-mcp | Email (IMAP/SMTP) for AI agents | npx -y @n24q02m/better-email-mcp@latest |
| wet-mcp | Web search, extraction, library docs | uvx --python 3.13 wet-mcp@latest |
| mnemo-mcp | Persistent AI memory with hybrid search | uvx mnemo-mcp@latest |
| better-godot-mcp | Godot Engine for AI agents | npx -y @n24q02m/better-godot-mcp@latest |
Contributing
See CONTRIBUTING.md.
License
MIT - See LICENSE.
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.