telegram-mcp

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.

Category
访问服务器

README

<div align="center">

telegram-mcp

Drive Telegram as your own account from any MCP client — not a bot.

Node.js TypeScript MCP GramJS

</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_id and api_hash from 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. .env is 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 chat argument accepts a @username, a numeric id, a string id, or me (your Saved Messages). Numeric ids resolve reliably only after the entity has been seen this session — run list_dialogs first 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.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选