Cover image
Try Now
2025-03-19

Mirror ofhttps://github.com/kfastov/telegram-mcp-server

3 years

Works with Finder

0

Github Watches

0

Github Forks

1

Github Stars

Telegram Client Library and MCP Server

This project provides both a Telegram client library and an MCP (Model Context Protocol) server for AI assistants to interact with Telegram.

Features

Telegram Client Library

  • Authentication with Telegram (including 2FA support)
  • Session management (automatic reuse of existing sessions)
  • Retrieving chats/dialogs
  • Fetching messages from specific chats
  • Filtering messages by pattern (e.g., regex)

MCP Server

  • Search channels by keywords - Find Telegram channels by searching for keywords in their names
  • List available channels - View all accessible channels
  • Get messages from channels - Retrieve messages from any accessible channel
  • Filter messages by pattern - Apply regex patterns to filter messages

Setup

  1. Create a .env file with your Telegram API credentials:
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. Install dependencies:
npm install

Usage

Using the Telegram Client Library

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);

Run the example client:

npm run client

Using the MCP Server

  1. Start the MCP server:
npm start
  1. The MCP server will be available at:
http://localhost:3000/mcp
  1. You can test the MCP server using the included client:
npm run mcp-client

For more details about the MCP server, see MCP-README.md.
For information about the code architecture, see CODE_STRUCTURE.md.

API Reference

TelegramClient

Constructor

const client = new TelegramClient(apiId, apiHash, phoneNumber, sessionPath);
  • apiId: Your Telegram API ID
  • apiHash: Your Telegram API Hash
  • phoneNumber: Your phone number in international format
  • sessionPath: (Optional) Path to save the session file (default: './data/session.json')

Methods

  • login(): Authenticates with Telegram (handles both new logins and session reuse)
  • getDialogs(limit, offset): Gets a list of dialogs (chats)
  • getChatMessages(chat, limit): Gets messages from a specific chat
  • filterMessagesByPattern(messages, pattern): Filters an array of messages by a regex pattern
  • hasSession(): Checks if a valid session exists

Files in this Repository

  • telegram-client.js: The main client library
  • client.js: An example client with additional helper functions
  • index.js: Original example using the client library
  • mcp-server.js: The MCP server main entry point
  • telegram-mcp.js: The MCP server implementation with Telegram tools
  • http-server.js: The HTTP/SSE server transport layer
  • mcp-client-example.js: A simple client to test the MCP server

Using with Claude or other MCP-compatible Assistants

The MCP server can be used with Claude or other MCP-compatible assistants. When connected, the assistant will have access to your Telegram channels and messages through the tools provided by the server.

Example workflow:

  1. Start the MCP server
  2. Connect Claude to the MCP server using the MCP URL
  3. Ask Claude to search for channels, retrieve messages, or filter messages by pattern

License

MIT

相关推荐

  • NiKole Maxwell
  • I craft unique cereal names, stories, and ridiculously cute Cereal Baby images.

  • Bora Yalcin
  • Evaluator for marketplace product descriptions, checks for relevancy and keyword stuffing.

  • Joshua Armstrong
  • Confidential guide on numerology and astrology, based of GG33 Public information

  • https://suefel.com
  • Latest advice and best practices for custom GPT development.

  • Emmet Halm
  • Converts Figma frames into front-end code for various mobile frameworks.

  • Elijah Ng Shi Yi
  • Advanced software engineer GPT that excels through nailing the basics.

  • https://maiplestudio.com
  • Find Exhibitors, Speakers and more

  • Lists Tailwind CSS classes in monospaced font

  • lumpenspace
  • Take an adjectivised noun, and create images making it progressively more adjective!

  • Yasir Eryilmaz
  • AI scriptwriting assistant for short, engaging video content.

  • apappascs
  • Discover the most comprehensive and up-to-date collection of MCP servers in the market. This repository serves as a centralized hub, offering an extensive catalog of open-source and proprietary MCP servers, complete with features, documentation links, and contributors.

  • ShrimpingIt
  • Micropython I2C-based manipulation of the MCP series GPIO expander, derived from Adafruit_MCP230xx

  • huahuayu
  • A unified API gateway for integrating multiple etherscan-like blockchain explorer APIs with Model Context Protocol (MCP) support for AI assistants.

  • deemkeen
  • control your mbot2 with a power combo: mqtt+mcp+llm

  • jae-jae
  • MCP server for fetch web page content using Playwright headless browser.

    Reviews

    2 (1)
    Avatar
    user_xTW8fvOE
    2025-04-17

    As a dedicated user of the kfastov_telegram-mcp-server by MCP-Mirror, I must say this server is impressive! It provides seamless integration and efficient performance for managing Telegram interactions. The clear documentation and user-friendly interface make it an excellent choice for both beginners and experienced developers. Highly recommend checking it out at the given GitHub link.