Sponsored by Deepsite.site

Telegram

Created By
kdoronin9 months ago
Content

Telegram MCP Server

English version | Русская версия

Server for working with Telegram API through Model Context Protocol (MCP). Allows AI agents and other MCP clients to interact with Telegram.

Requirements

  • Node.js 14+ (or higher)
  • npm
  • API ID and API Hash from Telegram

Installation

  1. Clone the repository:

    git clone https://github.com/kdoronin/telegram_mcp
    cd telegram-mcp
    
  2. Install dependencies:

    npm install
    

    (This will install the necessary packages: gramjs, @modelcontextprotocol/sdk, zod, etc.)

  3. Create and configure the .env file:

    # Copy the example .env file
    cp .env.example .env
    

    Edit the .env file and specify your API_ID and API_HASH.

  4. Get API ID and API Hash:

    • Go to my.telegram.org
    • Log in to your account
    • Navigate to "API development tools"
    • Create a new application
    • Copy api_id and api_hash to your .env file

Running

Important note about running

The main server file mcp-server.js is located in the project root, which allows running it without path issues.

npm run mcp    # Run from the project root directory

Option 2: Direct node execution

# Run from the project root directory
node mcp-server.js

Option 3: As an executable file

# From the project root
./mcp-server.js

Option 4: After global installation

npm install -g .   # Install the package globally
telegram-mcp       # Run the globally installed package

Integration with MCP clients

Configuration in Cursor

  1. Open Cursor settings
  2. Go to Features -> MCP Servers
  3. Click "Add new MCP server"
  4. Configure the server:
    • Name: telegram (or any other)
    • Type: command
    • Command: node /full/path/to/project/mcp-server.js
    • Alternatively: npx telegram-mcp (after global installation)

Configuration in Claude Desktop

  1. Open Claude Desktop settings
  2. Go to Tools -> MCP
  3. Click "Add New Server"
  4. Configure the server:
    • Name: telegram (or any other)
    • Type: command
    • Command: node /full/path/to/project/mcp-server.js
    • Alternatively: npx telegram-mcp (after global installation)

Checking functionality

When the server starts:

  1. It will load settings from the .env file
  2. Check for saved sessions
  3. If there are no sessions, it will offer to create a new one (you will need to enter a phone number and confirmation code)

Available Tools

The MCP server provides the following tools:

  • getDialogs: Gets a list of user dialogs (chats).
    • Parameters: session (string, required), limit (integer, optional, default: 100).
  • getMessages: Gets messages from the specified chat.
    • Parameters: session (string, required), chatId (string, required), limit (integer, optional, default: 100).
  • sendMessage: Sends a message to the specified chat.
    • Parameters: session (string, required), chatId (string, required), message (string, required).
  • executeMethod: Executes an arbitrary Telegram API method (use with caution).
    • Parameters: session (string, required), method (string, required), params (object, optional).

Note: The session parameter is usually the user's phone number in international format (e.g., +79001234567).

Usage examples in prompts

Using the telegram.getDialogs tool for session +79001234567, show me the last 5 chats.
With telegram.sendMessage for session +79001234567, send the message "Hello from my AI assistant!" to chat with ID 'username_or_chat_id'.

Authorization

When using a session (phone number) for the first time, the server will request a confirmation code in the console where it is running. Enter the code received from Telegram to authorize the session.

If you have two-factor authentication (2FA) enabled, you will also need to enter your Telegram password. If the password is entered incorrectly, the system will prompt you to enter it again (up to 3 attempts).

After successful authorization, the session will be saved to a file inside the sessions/ directory and will be used for subsequent requests.

Session file structure

Sessions are stored in the sessions/ directory as JSON files named after the phone number (e.g., +79001234567.json). Each file contains:

  • A session string (encrypted authorization token)
  • A timestamp of creation/update

Troubleshooting

  1. Error "Your API ID or Hash cannot be empty or undefined"

    • Check that the .env file is in the project root
    • Make sure API_ID and API_HASH are correctly specified in it
    • Run the server from the project root directory
  2. Server doesn't see the saved session

    • Check the execution path (should be from the project root)
    • Check for the session file in the sessions/ directory
    • Try running with npm run mcp
  3. Authorization error

    • If you entered an incorrect 2FA password, the system will prompt you to re-enter it
    • If all attempts are exhausted, delete the session file and try again

Security

  • Do not share your API ID and API Hash with third parties.
  • Run the server in a trusted environment.
  • Session files contain sensitive data. Store them in a secure location and do not share them.
  • The executeMethod tool allows executing any Telegram API methods. Use it with caution, as it can perform destructive actions.

License

MIT

Server Config

{
  "mcpServers": {
    "Telegram": {
      "command": "node",
      "args": [
        "/Users/dka/Documents/Solo/git/telegram_mcp/mcp-server.js"
      ]
    }
  }
}
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Amap Maps高德地图官方 MCP Server
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Tavily Mcp
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Serper MCP ServerA Serper MCP Server
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
WindsurfThe new purpose-built IDE to harness magic
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
DeepChatYour AI Partner on Desktop
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Playwright McpPlaywright MCP server
ChatWiseThe second fastest AI chatbot™
CursorThe AI Code Editor