Navigation
Telegram Client & MCP Server: Bot Automation & Enterprise Messaging - MCP Implementation

Telegram Client & MCP Server: Bot Automation & Enterprise Messaging

Build scalable Telegram apps effortlessly with our lightweight client library & MCP server—seamless bot automation, enterprise messaging, and rock-solid reliability. Mirror success.

Developer Tools
4.4(187 reviews)
280 saves
130 comments

This tool saved users approximately 5761 hours last month!

About Telegram Client & MCP Server

What is Telegram Client & MCP Server: Bot Automation & Enterprise Messaging?

This solution combines a Telegram client library with an MCP (Model Context Protocol) server, enabling seamless integration of AI assistants with Telegram's messaging ecosystem. The client handles authentication, session management, and message retrieval, while the MCP server provides standardized endpoints for enterprise-grade automation tasks like channel discovery, message filtering, and real-time communication orchestration.

How to use Telegram Client & MCP Server: Bot Automation & Enterprise Messaging?

Follow these streamlined steps to deploy the system:

  1. Create a .env file with Telegram API credentials and port settings
  2. Install dependencies via npm install
  3. Launch the MCP server (npm start) and test it with npm run mcp-client
  4. Use the client library API to programmatically interact with Telegram chats and messages

Full examples are provided in the client.js and mcp-client-example.js files.

Telegram Client & MCP Server Features

Key Features of Telegram Client & MCP Server: Bot Automation & Enterprise Messaging?

Telegram Client Library Highlights

  • 2FA-enabled authentication with automatic session reuse
  • Advanced pattern matching using regex for message filtering
  • Efficient chat/dialog management with pagination support

MCP Server Capabilities

  • Channel search by keyword with real-time results
  • Granular message filtering through pattern matching
  • RESTful API endpoints for enterprise workflows

Use Cases of Telegram Client & MCP Server: Bot Automation & Enterprise Messaging?

Common applications include:

  • Automated monitoring: Track channel activity using regex-based alerts
  • Content aggregation: Centralize messages from multiple chats into analytics pipelines
  • Enterprise communication: Connect AI assistants to internal Telegram channels for ticketing systems
  • Real-time notifications: Trigger actions based on keyword detection in public channels

Telegram Client & MCP Server FAQ

FAQ from Telegram Client & MCP Server: Bot Automation & Enterprise Messaging?

  • How does 2FA handling work? The client automatically manages login codes through Telegram's official API flow.
  • Can I customize message filters? Yes - regex patterns can be tailored to specific business requirements.
  • Does this support multi-user environments? Session files are user-specific, allowing concurrent use with separate configurations.
  • What happens if the connection drops? Built-in retry logic handles transient network issues gracefully.
  • Are there security safeguards? API credentials are environment-variable based, and sessions are encrypted by default.

Content

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

Related MCP Servers & Clients