This project provides both a Telegram client library and an MCP (Model Context Protocol) server for AI assistants to interact with Telegram.
Create a .env file with your Telegram API credentials:
plaintext
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() {
// 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
Start the MCP server:
shell
npm start
The MCP server will be available at:
plaintext
http://localhost:3000/mcp
You can test the MCP server using the included client:
shell
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.
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')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 existstelegram-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 serverThe 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:
MIT
Mirror of https://github.com/kfastov/telegram-mcp-server
Readme
Activity
Custom properties
No releases published
No packages published