
telegram mcp server
MCP server implementation for Telegram
Repository Info
About This Server
MCP server implementation for Telegram
Model Context Protocol (MCP) - This server can be integrated with AI applications to provide additional context and capabilities, enabling enhanced AI interactions and functionality.
Documentation
Telegram MCP Server
An MCP server allowing AI assistants (like Claude) to interact with your Telegram account using the user client API (not the bot API). Built with @mtproto/core and the FastMCP framework.
Features
Tools
-
listChannels- Lists available Telegram channels/chats accessible by the account, based on the server's cache.
- Parameters:
limit(number, optional): Maximum number of channels to return (default: 50).
- Output: A list of channels/chats with their ID, title, type, and access hash (if applicable).
-
searchChannels- Searches the cached channels/chats by keywords in their names.
- Parameters:
keywords(string): Keywords to search for in channel names.limit(number, optional): Maximum number of results to return (default: 100).
- Output: A list of matching channels/chats.
-
getChannelMessages- Retrieves recent messages from a specific channel/chat using its ID.
- Parameters:
channelId(number): The numeric ID of the channel/chat (obtained fromlistChannelsorsearchChannels).limit(number, optional): Maximum number of messages to return (default: 100).filterPattern(string, optional): A JavaScript-compatible regular expression to filter messages by their text content.
- Output: A list of messages containing ID, date, text, and sender ID.
Prerequisites
- Node.js: Version 18 or later recommended.
- Telegram Account:
- You need an active Telegram account.
- Two-Step Verification (2FA) must be enabled on your account (Settings → Privacy and Security → Two-Step Verification).
- Telegram API Credentials:
- Obtain an
api_idandapi_hashby creating a new application at https://core.telegram.org/api/obtaining_api_id.
- Obtain an
Installation
- Clone this repository:
git clone https://github.com/your-username/telegram-mcp-server.git # Replace with your repo URL cd telegram-mcp-server - Install dependencies:
npm install
Configuration
There are two separate configurations that need to be set up:
-
MCP Server Configuration:
Configure the Telegram MCP server using environment variables (in a
.envfile or directly in your environment):TELEGRAM_API_ID=YOUR_API_ID TELEGRAM_API_HASH=YOUR_API_HASH TELEGRAM_PHONE_NUMBER=YOUR_PHONE_NUMBER_WITH_COUNTRY_CODE # e.g., +15551234567Replace the placeholder values with your actual credentials.
-
MCP Client Configuration:
Configure client software (Claude Desktop, Cursor, etc.) to connect to the MCP server by modifying their configuration files:
{ "mcpServers": { "telegram": { "url": "http://localhost:8080/sse", "disabled": false, "timeout": 30 } } }For Claude Desktop, the config file is located at:
- On macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - On Windows:
%APPDATA%/Claude/claude_desktop_config.json
Important: Restart your MCP client to apply the changes.
- On macOS:
PostgreSQL Integration
The library can persist chat data to a PostgreSQL database. Set the DATABASE_URL environment variable to your connection string, then use the helper Database class:
import { Database } from './db.js';
import TelegramClient from './telegram-client.js';
const db = new Database(process.env.DATABASE_URL);
await db.connect();
await db.init();
const client = new TelegramClient(...);
await client.initializeDialogCache();
await client.saveDialogsToDb(db); // store chats
await client.saveChatMessagesToDb(chatId, db, { batchSize: 100, after: 0 });
saveChatMessagesToDb supports incremental saving by specifying the after timestamp to fetch only newer messages. Repeated calls can be used for continuous history updates.
Running the Server
-
Initial Login (Important First Step): The first time you run the server (or if your session expires/is invalid), it needs to authenticate with Telegram. Run it directly from your terminal:
npm start- The server will use the credentials from your
.envfile. - It will prompt you in the terminal to enter the login code sent to your Telegram account and your 2FA password if required.
- Upon successful login, a session file (
./data/session.json) will be created. This file allows the server to log in automatically in the future without requiring codes/passwords. - The server will also attempt to build or load a cache of your chats (
./data/dialog_cache.json). This can take some time on the first run, especially with many chats. Subsequent starts will be faster if the cache exists.
- The server will use the credentials from your
-
Normal Operation: You'll need to start the server manually by running
npm startin the project directory.Once the server is running, your MCP client (e.g., Claude Desktop) will connect to it via the URL specified in its configuration (
http://localhost:8080/sseby default).
Troubleshooting
- Login Prompts: If the server keeps prompting for login codes/passwords when started by the MCP client, ensure the
data/session.jsonfile exists and is valid. You might need to runnpm startmanually once to refresh the session. Also, check that the file permissions allow the user running the MCP client to read/write thedatadirectory. - Cache Issues: If channels seem outdated or missing, you can delete
./data/dialog_cache.jsonand restart the server (runnpm startmanually) to force a full refresh. This might take time. - Cannot Find Module: Ensure you run
npm installin the project directory. If the MCP client starts the server, make sure the working directory is set correctly or use absolute paths. - Other Issues: If you encounter any other problems, feel free to open an issue in this server repo.
Telegram Client Library
This repository also contains the underlying telegram-client.js library used by the MCP server. For details on using the library directly (e.g., for custom scripting), see LIBRARY.md.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Quick Start
Clone the repository
git clone https://github.com/kfastov/telegram-mcp-serverInstall dependencies
cd telegram-mcp-server
npm installFollow the documentation
Check the repository's README.md file for specific installation and usage instructions.
Repository Details
Recommended MCP Servers
Discord MCP
Enable AI assistants to seamlessly interact with Discord servers, channels, and messages.
Knit MCP
Connect AI agents to 200+ SaaS applications and automate workflows.
Apify MCP Server
Deploy and interact with Apify actors for web scraping and data extraction.
BrowserStack MCP
BrowserStack MCP Server for automated testing across multiple browsers.
Zapier MCP
A Zapier server that provides automation capabilities for various apps.