Telegram MCP Server

by kfastov
Verified

local-only server

The server can only run on the client’s local machine because it depends on local resources.

Integrations

  • Handles configuration management through environment variables, specifically for storing Telegram API credentials including API ID, API hash, and phone number.

  • Enables interaction with Telegram messaging platform, providing tools to search channels by keywords, list available channels, retrieve messages from channels, and filter messages by regex patterns.

Telegram 客户端库和 MCP 服务器

该项目为 AI 助手与 Telegram 交互提供了 Telegram 客户端库和 MCP(模型上下文协议)服务器。

特征

Telegram 客户端库

  • 使用 Telegram 进行身份验证(包括 2FA 支持)
  • 会话管理(自动重用现有会话)
  • 检索聊天/对话
  • 从特定聊天中获取消息
  • 按模式过滤消息(例如正则表达式)

MCP 服务器

  • 通过关键字搜索频道- 通过搜索名称中的关键字来查找 Telegram 频道
  • 列出可用频道- 查看所有可访问的频道
  • 从频道获取消息- 从任何可访问的频道检索消息
  • 按模式过滤消息- 应用正则表达式模式来过滤消息

设置

  1. 使用您的 Telegram API 凭证创建一个.env文件:
TELEGRAM_API_ID=your_api_id TELEGRAM_API_HASH=your_api_hash TELEGRAM_PHONE_NUMBER=your_phone_number PORT=3000 # Optional, defaults to 3000 for MCP server
  1. 安装依赖项:
npm install

用法

使用 Telegram 客户端库

const TelegramClient = require("./telegram-client"); const dotenv = require("dotenv"); dotenv.config(); async function main() { // Create a new client instance const client = new TelegramClient( process.env.TELEGRAM_API_ID, process.env.TELEGRAM_API_HASH, process.env.TELEGRAM_PHONE_NUMBER ); // Login to Telegram await client.login(); // Get all chats/dialogs const { chats } = await client.getDialogs(); // Print all chats chats.forEach((chat) => { if (chat.title) { console.log(`Chat: ${chat.title}`); } }); } main().catch(console.error);

运行示例客户端:

npm run client

使用 MCP 服务器

  1. 启动 MCP 服务器:
npm start
  1. MCP 服务器将在以下位置开放:
http://localhost:3000/mcp
  1. 您可以使用附带的客户端测试 MCP 服务器:
npm run mcp-client

有关 MCP 服务器的更多详细信息,请参阅MCP-README.md
有关代码架构的信息,请参阅CODE_STRUCTURE.md

API 参考

Telegram客户端

构造函数

const client = new TelegramClient(apiId, apiHash, phoneNumber, sessionPath);
  • apiId :您的 Telegram API ID
  • apiHash :你的 Telegram API 哈希
  • phoneNumber :您的国际格式的电话号码
  • sessionPath :(可选)保存会话文件的路径(默认值:'./data/session.json')

方法

  • login() :使用 Telegram 进行身份验证(处理新登录和会话重用)
  • getDialogs(limit, offset) :获取对话(聊天)列表
  • getChatMessages(chat, limit) :从特定聊天获取消息
  • filterMessagesByPattern(messages, pattern) :通过正则表达式模式过滤消息数组
  • hasSession() :检查是否存在有效会话

此存储库中的文件

  • telegram-client.js :主客户端库
  • client.js :具有附加辅助功能的示例客户端
  • index.js :使用客户端库的原始示例
  • mcp-server.js :MCP 服务器主入口点
  • telegram-mcp.js :使用 Telegram 工具实现的 MCP 服务器
  • http-server.js :HTTP/SSE 服务器传输层
  • mcp-client-example.js :一个用于测试 MCP 服务器的简单客户端

与 Claude 或其他 MCP 兼容助手一起使用

MCP 服务器可与 Claude 或其他兼容 MCP 的助手一起使用。连接后,助手将通过服务器提供的工具访问您的 Telegram 频道和消息。

工作流程示例:

  1. 启动 MCP 服务器
  2. 使用 MCP URL 将 Claude 连接到 MCP 服务器
  3. 要求 Claude 搜索频道、检索消息或按模式过滤消息

执照

麻省理工学院

-
security - not tested
F
license - not found
-
quality - not tested

模型上下文协议服务器使 AI 助手能够与 Telegram 交互,允许他们搜索频道、列出可用频道、检索消息以及通过正则表达式模式过滤消息。

  1. Features
    1. Telegram Client Library
    2. MCP Server
  2. Setup
    1. Usage
      1. Using the Telegram Client Library
      2. Using the MCP Server
    2. API Reference
      1. TelegramClient
    3. Files in this Repository
      1. Using with Claude or other MCP-compatible Assistants
        1. License
          ID: b91b7wxzlu