Skip to main content

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

CI codecov PyPI Docker License: MIT Python Telegram MCP Registry Glama semantic-release Renovate

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

  1. Open Telegram, search for @BotFather
  2. Send /newbot, follow prompts to name your bot
  3. Copy the token (format: 123456789:ABCdefGHI-JKLmnoPQRstUVwxyz)
  4. Run:
TELEGRAM_BOT_TOKEN=123456:ABC-DEF uvx --python 3.13 better-telegram-mcp

User Mode

  1. Go to https://my.telegram.org
  2. Login with your phone number (OTP sent via Telegram, not SMS)
  3. Click "API development tools"
  4. Create an app, note api_id (integer) and api_hash (32-char hex)
  5. Run the server with your credentials:
TELEGRAM_API_ID=12345 TELEGRAM_API_HASH=a1b2c3d4e5... TELEGRAM_PHONE=+84912345678 \
  uvx --python 3.13 better-telegram-mcp

On first run, the server automatically sends an OTP code to your Telegram app. When the AI agent calls any tool, it receives an auth prompt. Complete authentication via:

config(action='auth', code='YOUR_CODE')

The session is saved locally. Subsequent runs authenticate automatically.

2FA handling: If your account has Two-Step Verification enabled, set TELEGRAM_PASSWORD env var. The server uses it automatically during sign-in.

Session file: Stored at ~/.better-telegram-mcp/<name>.session with 600 permissions (owner-only). Treat this file like a password.

Auth CLI (Legacy)

For environments where interactive auth is preferred, the auth CLI command is still available:

TELEGRAM_API_ID=... TELEGRAM_API_HASH=... uvx --python 3.13 better-telegram-mcp auth

This is optional. The recommended approach is automatic runtime auth via the config tool as described above.

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 for auto-auth (e.g., +84912345678)
TELEGRAM_PASSWORD No - 2FA password (if enabled)
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

{
  "mcpServers": {
    "telegram": {
      "command": "uvx",
      "args": ["--python", "3.13", "better-telegram-mcp"],
      "env": {
        "TELEGRAM_BOT_TOKEN": "123456:ABC-DEF"
      }
    }
  }
}

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').

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 Use config(action='auth', code='YOUR_CODE') after receiving OTP
PhoneNumberInvalidError Wrong phone format Include country code with + (e.g., +84912345678)
SessionPasswordNeededError 2FA enabled Set TELEGRAM_PASSWORD env var
FloodWaitError Too many auth attempts Wait the indicated seconds
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

Compatible With

Claude Desktop Claude Code Cursor VS Code Copilot Antigravity Gemini CLI OpenAI Codex OpenCode

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 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

better_telegram_mcp-1.1.0.tar.gz (74.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

better_telegram_mcp-1.1.0-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

Details for the file better_telegram_mcp-1.1.0.tar.gz.

File metadata

  • Download URL: better_telegram_mcp-1.1.0.tar.gz
  • Upload date:
  • Size: 74.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","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

Hashes for better_telegram_mcp-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ecb989c324eb85827041964ea8c5c123e2cdc388ba8bb67b9955cb0e6f10e224
MD5 9fa28c25c0636ee267b850d416cf6576
BLAKE2b-256 5a64206c97fcd24dee7b1b2239777bb5090bbbf1dbb39d6f367f3e0b88b0de74

See more details on using hashes here.

File details

Details for the file better_telegram_mcp-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: better_telegram_mcp-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 27.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","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

Hashes for better_telegram_mcp-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a1fe87bd7976fe2e05e51eea776f87bace12e6bcee4ec93d2462c1f27de0493a
MD5 9dacfcac7c6eb0f2d7f4f3866ad42c1a
BLAKE2b-256 cea405410d37ecdf70c3bbb37ee118c05b63da737aeb0b0c1578a19c83b8f5ae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page