Skip to main content

⚡ telegram-mcp-cli

Modern Command-Line Interface & Bot Automation Controller for Telegram

PyPI Python 3.10+ License: MIT Telethon Test Suite

Interact with, test, and automate Telegram bots directly from your terminal.
Built on MTProto with direct Telethon .session file support, automatic environment detection, and session protection.


📑 Table of Contents


✨ Key Features

  • 🔑 Instant Session Switching (tg-cli auth <path.session>): Pass any existing Telethon .session file directly. Automatically validates SQLite integrity, detects the target server cluster, and aligns settings.
  • 🤖 Bot Testing & Automation: Send text payloads, trigger slash commands (e.g. /start), inspect responses, and click inline keyboard buttons.
  • 🛡️ Environment Mismatch Shield: Automatically detects whether your session belongs to the Test Server (Sandbox) or Production Server and protects against cross-environment auth revocation.
  • 🔒 Process-Level Session Guard: Prevents concurrent duplicate connections (/tmp/telegram-mcp.lock) to eliminate AuthKeyDuplicatedError.
  • 📁 Rich Terminal Display: Colorized output, message panels, button trees, and clean tabular diagnostics powered by rich.
  • 💬 Real-Time Interactive Chat (tg-cli chat <@bot>): Live terminal chat session with background streaming of incoming messages, inline button triggers (/click), and history scrolling.
  • Arbitrary MTProto Execution (tg-cli exec): Direct command-line evaluation of Python MTProto snippets with live client injection.

🏗️ Architecture

flowchart TD
    subgraph Terminal ["User / Agent CLI"]
        CLI["tg-cli (argparse + rich)"]
    end

    subgraph Core ["telegram-mcp-cli Engine"]
        Config["Config Manager (.env)"]
        Shield["Environment Mismatch Shield"]
        Lock["Process Lock (/tmp/telegram-mcp.lock)"]
        Controller["TelegramCliClient (Telethon)"]
    end

    subgraph Telegram ["Telegram MTProto Network"]
        TestDC["Telegram Test DC (Sandbox)"]
        ProdDC["Telegram Production DC (Live)"]
    end

    CLI --> Config
    CLI --> Controller
    Controller --> Lock
    Controller --> Shield
    Shield -->|Test Session| TestDC
    Shield -->|Prod Session| ProdDC

🚀 Quick Start

1. Installation

From PyPI (Recommended):

pip install telegram-mcp-cli

From Source:

git clone https://github.com/Telegram-mcp/telegram-mcp-cli.git
cd telegram-mcp-cli
pip install -e .

2. Configure Authentication

To set up or switch active sessions directly:

# Option A: Point to an existing Telethon .session file
tg-cli auth /path/to/my_account.session

# Option B: Run interactive phone / QR login
tg-cli auth login

3. Verify Connection

tg-cli status

💻 Command Reference

Command Description Example
auth Configure active session file or login tg-cli auth my_bot.session
status View connection, DC, and account status tg-cli status
send Send formatted text message to a bot/chat tg-cli send @mybot "Hello from CLI"
command Send /command and wait for bot reply tg-cli command @mybot /start
click Click inline button by text or index tg-cli click @mybot --button "Option 1"
chat Start interactive live chat session tg-cli chat @mybot
history Fetch recent conversation history tg-cli history @mybot --limit 10
send-file Upload photo, document, or audio tg-cli send-file @mybot doc.pdf
exec Execute MTProto Python snippet tg-cli exec "await client.get_me()"

🛡️ Safety & Session Protection

  • File Locking: tg-cli uses /tmp/telegram-mcp.lock to ensure no two processes use the session concurrently.
  • Environment Matching: Test Server sessions (DC 2 Sandbox) and Production sessions cannot be cross-connected. The CLI will abort with a clear warning before Telegram revokes the key.

🧪 Testing

Run the automated unit test suite with pytest:

python3 -m pytest tests -v

📄 License

This project is licensed under the MIT License.

Release files for telegram-mcp-cli 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for telegram-mcp-cli 0.1.1
File Size Uploaded
telegram_mcp_cli-0.1.1.tar.gz 17.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for telegram-mcp-cli 0.1.1
File Interpreter ABI Platform
telegram_mcp_cli-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 32.5 kB

Release files / telegram_mcp_cli-0.1.1.tar.gz

Download URL telegram_mcp_cli-0.1.1.tar.gz
Size 17.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ad6281577d470125c28b82242252e96be6c81133c9c755289adb7a3ac50b313d
BLAKE2b-256 checksum
How to use checksums
934a7a4073796b907cceaca8c24c218cd40cf5561b676904d7389a327dc5e516
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / telegram_mcp_cli-0.1.1-py3-none-any.whl

Download URL telegram_mcp_cli-0.1.1-py3-none-any.whl
Size 15.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e4a92fbf761a2fec7fbba67a2cb6774dfff36b0bf4d52f90458c5bef33fe46d0
BLAKE2b-256 checksum
How to use checksums
d12e71b39d8072dc1451be4c1ba1c270899d237978002917f4c0011f4fb9ca3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page