telegram-mcp
Enables AI agents to control a real Telegram user account via MTProto, allowing message sending, chat reading/searching, and message management through MCP tools.
README
<div align="center">
telegram-mcp
Drive Telegram as your own account from any MCP client — not a bot.
</div>
An MCP server that connects an AI agent to Telegram through your real user account over MTProto (via GramJS), authenticated with a saved session string. Because it acts as you and not a bot, it can read and search chat history and message anyone — things the Bot API cannot do.
It exposes 11 focused tools over stdio for sending, reading, searching, and managing messages, chats, and files.
Features
- You, not a bot — message people who never contacted you and post to any chat you belong to.
- Read & search history — pull recent messages or full-text search a conversation.
- Send anything — text with Markdown/HTML, replies, files by local path or URL.
- Manage messages — edit, delete (for everyone or just you), and forward across chats.
- Discover chats — list your dialogs with unread counts and resolve
@username→ id. - Safe by default — returned content is sanitized against prompt injection; the client stays silent on stdout so it never corrupts the protocol stream.
Prerequisites
- Node.js ≥ 22.14
- A Telegram account
- An
api_idandapi_hashfrom my.telegram.org/apps (these identify the app, not your account)
Installation
git clone https://github.com/naim30/telegram-mcp.git
cd telegram-mcp
npm install
npm run build
Configuration
Copy the example environment file and add your app credentials:
cp .env.example .env
| Variable | Required | Description |
|---|---|---|
TELEGRAM_API_ID |
yes | App api_id from my.telegram.org |
TELEGRAM_API_HASH |
yes | App api_hash from my.telegram.org |
TELEGRAM_SESSION |
yes* | Session string for your account (generated below) |
TELEGRAM_DEFAULT_CHAT |
no | Fallback chat (@username, id, or me) when a tool omits chat |
<sub>*Set TELEGRAM_SESSION after running the login step below.</sub>
Login
Generate a session string once. The script prompts for your phone number, the login code Telegram sends you, and your two-factor password if you have one:
npm run login
Copy the printed string into .env as TELEGRAM_SESSION=.... That's it — the session is reused on every start, so you never log in again.
[!WARNING] The session string grants full access to your account. Never commit it or share it.
.envis already gitignored.
Connect to an MCP client
Point your client at the built server. Example .mcp.json:
{
"mcpServers": {
"telegram": {
"type": "stdio",
"command": "node",
"args": ["/absolute/path/to/telegram-mcp/dist/server.js"],
"env": {
"TELEGRAM_API_ID": "1234567",
"TELEGRAM_API_HASH": "0123456789abcdef0123456789abcdef",
"TELEGRAM_SESSION": "1BQANOTEuMTA...",
"TELEGRAM_DEFAULT_CHAT": "me"
}
}
}
}
Values in env override .env. The server connects to Telegram lazily on the first tool call and reuses one authorized client for its lifetime.
Tools
| Tool | Description |
|---|---|
get_me |
Your account profile — doubles as a connectivity/auth check |
list_dialogs |
List your chats (DMs, groups, channels) with unread counts and last message |
resolve_entity |
Look up a user/group/channel by @username or id |
get_messages |
Read a chat's recent history, newest first, with pagination |
search_messages |
Full-text search a single chat's history |
send_message |
Send a text message (Markdown/HTML, replies, silent) |
edit_message |
Edit a message you previously sent |
delete_message |
Delete messages for everyone or just your local copy |
forward_message |
Forward messages from one chat to another |
mark_read |
Clear a chat's unread counter |
send_file |
Send a photo or document by local path or URL |
[!NOTE] The
chatargument accepts a@username, a numeric id, a string id, orme(your Saved Messages). Numeric ids resolve reliably only after the entity has been seen this session — runlist_dialogsfirst to warm the cache, or prefer@username.
How it works
MCP client ──stdio──▶ server.ts ──▶ tool handler ──▶ GramJS client ──▶ Telegram (MTProto)
│
└─▶ serialize + sanitize ──▶ JSON result
src/
server.ts Registers all tools, connects the stdio transport
login.ts `npm run login` — interactive session-string generator
config/config.ts Typed environment config (envalid + dotenv)
lib/
client.ts Lazy, authorized GramJS client
register-tool.ts Wraps each tool: JSON output, errors → isError
sanitize.ts Strips control/zero-width characters from returned text
serialize.ts Compact, BigInt-safe summaries of GramJS objects
fields.ts Shared zod input fields
tools/ account · messages · files (one object per tool)
Scripts
npm run build # Compile TypeScript to dist/
npm run login # Generate a session string
npm start # Run the server (needs a valid .env)
[!CAUTION] This automates a user account. Bulk or spammy behavior can get your account limited or banned by Telegram. Keep actions deliberate and user-initiated.
Acknowledgements
Built on GramJS and the Model Context Protocol SDK. The content-sanitization approach is adapted from the Telethon-based chigwell/telegram-mcp.
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.