Telegram Client Library and MCP Server
A Model Context Protocol server that enables AI assistants to interact with Telegram, allowing them to search channels, list available channels, retrieve messages, and filter messages by regex patterns.
kfastov
README
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
- 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
- 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
- Start the MCP server:
npm start
- The MCP server will be available at:
http://localhost:3000/mcp
- 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 IDapiHash
: Your Telegram API HashphoneNumber
: Your phone number in international formatsessionPath
: (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 chatfilterMessagesByPattern(messages, pattern)
: Filters an array of messages by a regex patternhasSession()
: Checks if a valid session exists
Files in this Repository
telegram-client.js
: The main client libraryclient.js
: An example client with additional helper functionsindex.js
: Original example using the client librarymcp-server.js
: The MCP server main entry pointtelegram-mcp.js
: The MCP server implementation with Telegram toolshttp-server.js
: The HTTP/SSE server transport layermcp-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:
- Start the MCP server
- Connect Claude to the MCP server using the MCP URL
- Ask Claude to search for channels, retrieve messages, or filter messages by pattern
License
MIT
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.
MCP Package Docs Server
Facilitates LLMs to efficiently access and fetch structured documentation for packages in Go, Python, and NPM, enhancing software development with multi-language support and performance optimization.
Claude Code MCP
An implementation of Claude Code as a Model Context Protocol server that enables using Claude's software engineering capabilities (code generation, editing, reviewing, and file operations) through the standardized MCP interface.
@kazuph/mcp-taskmanager
Model Context Protocol server for Task Management. This allows Claude Desktop (or any MCP client) to manage and execute tasks in a queue-based system.
Linear MCP Server
Enables interaction with Linear's API for managing issues, teams, and projects programmatically through the Model Context Protocol.
mermaid-mcp-server
A Model Context Protocol (MCP) server that converts Mermaid diagrams to PNG images.
Jira-Context-MCP
MCP server to provide Jira Tickets information to AI coding agents like Cursor

Linear MCP Server
A Model Context Protocol server that integrates with Linear's issue tracking system, allowing LLMs to create, update, search, and comment on Linear issues through natural language interactions.

Sequential Thinking MCP Server
This server facilitates structured problem-solving by breaking down complex issues into sequential steps, supporting revisions, and enabling multiple solution paths through full MCP integration.