The Telegram MCP Server is a powerful implementation that combines a Telegram client library with an MCP (Model Context Protocol) server. This project enables AI assistants to interact with Telegram, providing features like message retrieval, channel search, and pattern-based filtering.
Create a .env
file with your Telegram API credentials:
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
Install dependencies:
shell
npm install
const TelegramClient = require("./telegram-client");
const dotenv = require("dotenv");
dotenv.config();
async function main() {
const client = new TelegramClient(
process.env.TELEGRAM_API_ID,
process.env.TELEGRAM_API_HASH,
process.env.TELEGRAM_PHONE_NUMBER
);
await client.login();
const { chats } = await client.getDialogs();
chats.forEach((chat) => {
if (chat.title) {
console.log(`Chat: ${chat.title}`);
}
});
}
main().catch(console.error);
Run the example client:
npm run client
Start the MCP server:
shell
npm start
The MCP server will be available at:
http://localhost:3000/mcp
Test the MCP server using the included client:
shell
npm run mcp-client
For more details, see MCP-README.md and CODE_STRUCTURE.md.
const client = new TelegramClient(apiId, apiHash, phoneNumber, sessionPath);
login()
: Authenticates with Telegram.getDialogs(limit, offset)
: Gets a list of dialogs (chats).getChatMessages(chat, limit)
: Gets messages from a specific chat.filterMessagesByPattern(messages, pattern)
: Filters messages by a regex pattern.hasSession()
: Checks if a valid session exists.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.The MCP server can be integrated with Claude or other MCP-compatible assistants. Once connected, the assistant can access Telegram channels and messages through the server.
MIT
This project provides an MCP server implementation for Telegram, enabling AI assistants to interact with Telegram channels and messages efficiently.