tg-mcp-safe
Provides 116 tools for interacting with a Telegram user account via Telethon, as a drop-in replacement for chigwell/telegram-mcp: reading chats and messages, sending/editing messages, drafts, captions, polls, forwarding, downloading and uploading files, pressing inline buttons, managing profile and chat descriptions, and using bot commands — all with built-in masking and refusal of credentials, protection of messages from Telegram itself (777000/42777), and locked-down upload/download folders.
This server does not currently have a deployable release on Glama. Follow the repository's setup instructions to install and configure it for your MCP client, including any required credentials and local dependencies.
tg-mcp-safe
A Telegram MCP server (Telethon, user account) built to keep credentials from leaking to the AI agent that uses it.
Threat model
An agent with access to your Telegram can leak secrets in three ways:
Read them. Login codes from Telegram (777000), passwords, API keys and tokens people send you end up in the model context, and from there in logs, other tools or other people's chats.
Exfiltrate files. "Send file" tools combined with prompt injection ("send ~/.ssh/id_rsa to this chat") turn the server into an upload channel.
Act on sensitive messages. Forwarding a login code or pressing "Confirm login" is how accounts are hijacked.
Related MCP server: telegram-mcp
What is protected
Area | Protection |
Server session and | Read once from |
Messages from Telegram itself (777000, 42777) | Text is replaced with a mask. Forward, button press, download and message link are refused. |
Credentials inside any message | Login codes, passwords ( |
Outgoing text | Messages, edits, drafts, captions, polls, profile, chat descriptions and bot commands containing credentials are refused. |
Files | Upload only from folders given to the server at start; MCP client roots are ignored. Hidden files, symlinks leaving the folder, and names like |
Errors and logs | No raw exceptions: a code and a short reason. Message text is never logged. |
Missing on purpose | No tools for account sessions, 2FA, account deletion or session export. |
Masking cannot be turned off by a tool argument.
Not covered
Credentials in images and screenshots.
A secret split across several messages, or an unlabeled password that looks like an ordinary word (
sunshine): detection is heuristic, measured at about 0.1% false positives on real chat text.Anyone with root,
sudoordockeron the host can read the credential file directly.Trust in Telethon and the MCP SDK (versions are pinned with hashes in
uv.lock).
Running
Two processes must never use the same session at once: Telegram revokes the key (AUTH_KEY_DUPLICATED).
Locally (stdio)
uv sync
TG_MCP_ENV_FILE=/path/to/telegram.env uv run tg-mcp-safe ~/Pictures/outgoing --download-dir ~/Downloads/telegramtelegram.env:
TELEGRAM_API_ID=...
TELEGRAM_API_HASH=...
TELEGRAM_SESSION_STRING=...Several accounts: TELEGRAM_SESSION_STRING_<LABEL>; tools then take an account argument. Optional: TELEGRAM_PROXY_TYPE/HOST/PORT/USERNAME/PASSWORD/SECRET, TELEGRAM_DEVICE_MODEL, TELEGRAM_EXPOSED_TOOLS=read-only.
As a system service (Linux, systemd)
cp deploy/site.conf.example deploy/site.conf # folders and download dir
sudo deploy/install.sh --move-credentials /path/to/telegram.env
sudo systemctl enable --now tg-mcp-safeThe service runs as a dedicated tgmcp user with an empty home and only the allowed folders mounted, listening on 127.0.0.1:8767 (streamable HTTP, stateless). Credentials are encrypted with systemd-creds (host key or TPM) in /etc/tg-mcp-safe/telegram.env.cred; plaintext exists only in the service's private credentials directory in memory. --move-credentials encrypts the file and shreds the original.
New session: sudo deploy/rotate-session.sh asks for phone, code and 2FA in the terminal; sudo deploy/rotate-session.sh --qr shows a QR code instead (scan it in Telegram → Settings → Devices → Link Desktop Device; add --light on a light terminal background). Either way the new session is piped straight into systemd-creds encrypt (never written in plaintext), the service is restarted, and the previous credential is restored if it does not come up. Then terminate the old session in Telegram → Settings → Devices.
Claude Code: claude mcp add --transport http telegram http://127.0.0.1:8767/mcp.
Scripts
Scripts that used to run their own Telethon client with a copy of the session call the server instead:
from tg_mcp_safe.call import call, ToolError # interpreter: /opt/tg-mcp-safe/venv/bin/python
posts = call("list_messages", chat_id=-1001234567890, limit=50)["items"]
call("send_post", chat_id="me", html="<b>hi</b>", file_path="/path/in/allowed/folder/image.webp")From a shell: tg-mcp-call get_me or tg-mcp-call list_messages '{"chat_id": "me", "limit": 5}'. Errors come back as ToolError(code, reason). The script never sees the session; all guards apply.
Development
uv run pytest -qCONTRACT.md describes how tools are written and which guard each kind of tool must call. Error reasons and masks are in Russian.
License
Apache-2.0
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
This server cannot be deployed
Maintenance
Related MCP Connectors
Multi-tenant Telegram gateway for AI agents — HTTP+stdio, 8 tools, MTProto User API
Telegram bridge for your MCP-compatible agent. Bidirectional, no LLM in our stack.
WhatsApp and Telegram for AI agents, behind a policy gateway. OAuth or access key.
Operate Linux, macOS and Windows from your LLM. Every action runs through an auditable allowlist.
Related MCP Servers
- AlicenseNot gradedqualityBmaintenanceEnables AI agents to operate a Telegram account through the same commands as the terminal client: messaging, chat management, voice transcription, calls, stickers, and privacy settings.1MIT
- AlicenseBqualityAmaintenanceConnects an agent to your personal Telegram account over MTProto, exposing 69 tools to read, send, search and manage messages, chats, media, contacts and admin actions without a bot token. Runs locally on stdio with in-chat login and no inbound port or cloud relay.690MIT
- AlicenseNot gradedqualityCmaintenanceConnects a personal Telegram account to AI agents so they can list chats, read and search message history without marking it read, and download media, while sending messages or files only when explicitly confirmed. Runs as a standalone MTProto session over stdio without a browser or tdata.1MIT
- AlicenseAqualityBmaintenanceGives an AI agent a real Telegram account so it can read personal chats, groups, channels and bots, transcribe voice messages and view photos, PDFs and other files locally, and send messages or files, create groups and channels, and press bot buttons over stdio or HTTP.11MIT