Sign inSign up

dkasss/mcp-telegram-server

By dkasss

•Updated over 1 year ago

Image
0

799

dkasss/mcp-telegram-server repository overview

⁠MCP Telegram Server

šŸ¤– A Model Context Protocol (MCP) server that provides Telegram messaging capabilities via Server-Sent Events (SSE). Send messages to Telegram through any MCP-compatible client like Cursor, Claude Desktop, or custom applications.

⁠Features

  • šŸ”Œ SSE-based MCP Server - Modern event-driven architecture
  • šŸ“± Telegram Integration - Send text messages with Markdown formatting
  • 🐳 Docker Ready - Containerized for easy deployment
  • šŸ”’ Privacy First - Bot token and chat ID configured via environment variables
  • ⚔ Real-time - Bidirectional communication via Server-Sent Events
  • šŸ›”ļø Production Ready - Health checks, logging, and auto-restart

⁠Quick Start with Docker

⁠1. Setup Telegram Bot
  1. Message @BotFather⁠ on Telegram
  2. Create a new bot with /newbot
  3. Save the bot token provided
⁠2. Get Your Chat ID

Send a message to your bot, then visit:

https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates

Look for chat.id in the response.

⁠3. Configure Environment

Create a .env file:

TELEGRAM_BOT_TOKEN=your_bot_token_here
TELEGRAM_CHAT_ID=your_chat_id_here
⁠4. Run with Docker Compose

Create docker-compose.yml:

services:
  telegram-mcp:
    image: mcp-telegram-server
    build: .
    ports:
      - "8008:8008"
    environment:
      - TELEGRAM_BOT_TOKEN=${TELEGRAM_BOT_TOKEN}
      - TELEGRAM_CHAT_ID=${TELEGRAM_CHAT_ID}
      - HOST=0.0.0.0
      - PORT=8008
    env_file:
      - .env
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8008/"]
      interval: 30s
      timeout: 10s
      retries: 3

Start the server:

docker-compose up -d

⁠MCP Client Configuration

⁠Cursor IDE

Add to your MCP settings:

{
  "mcpServers": {
    "telegram": {
      "url": "http://localhost:8008/sse"
    }
  }
}
⁠Claude Desktop

Add to claude_desktop_config.json:

{
  "mcpServers": {
    "telegram": {
      "url": "http://localhost:8008/sse"
    }
  }
}

⁠Available Tools

⁠send_message

Send a text message to your configured Telegram chat.

Parameters:

  • message (required): The text message to send
  • parse_mode (optional): "Markdown", "MarkdownV2", or "HTML"

Example:

{
  "message": "Hello from MCP! šŸš€\n\n**Bold text** and *italic text*",
  "parse_mode": "Markdown"
}

⁠Environment Variables

VariableDescriptionRequiredDefault
TELEGRAM_BOT_TOKENYour Telegram bot token from BotFatherāœ… Yes-
TELEGRAM_CHAT_IDTarget chat ID for messagesāœ… Yes-
HOSTServer bind addressNo127.0.0.1
PORTServer portNo8008
DEBUGEnable debug loggingNofalse

⁠Docker Commands

⁠Build and Run
# Build image
docker build -t mcp-telegram-server .

# Run container
docker run -d \
  --name mcp-telegram-server \
  -p 8008:8008 \
  --env-file .env \
  mcp-telegram-server
⁠Management
# View logs
docker logs -f mcp-telegram-server

# Restart server
docker restart mcp-telegram-server

# Stop server
docker stop mcp-telegram-server

# Remove container
docker rm mcp-telegram-server
⁠With Docker Compose
# Start services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down

# Rebuild and restart
docker-compose up -d --build

⁠API Endpoints

EndpointMethodDescription
/GETServer info and health check
/sseGETSSE endpoint for MCP clients
/messagePOSTJSON-RPC message endpoint

⁠Testing

Test the server is working:

# Health check
curl http://localhost:8008/

# Expected response:
{
  "name": "Telegram MCP Server",
  "version": "1.0.0",
  "endpoints": {
    "sse": "/sse",
    "message": "/message"
  }
}

⁠Development

⁠Local Development
# Clone repository
git clone <repository-url>
cd mcp-telegram

# Create virtual environment
python -m venv .venv
source .venv/bin/activate  # Linux/Mac
# or
.venv\Scripts\activate     # Windows

# Install dependencies
pip install -r requirements.txt

# Configure environment
cp env.example .env
# Edit .env with your credentials

# Run server
python main.py
⁠Project Structure
mcp-telegram/
ā”œā”€ā”€ main.py              # Main server application
ā”œā”€ā”€ requirements.txt     # Python dependencies
ā”œā”€ā”€ Dockerfile          # Docker image definition
ā”œā”€ā”€ docker-compose.yml  # Docker Compose configuration
ā”œā”€ā”€ .env                # Environment variables (create from env.example)
ā”œā”€ā”€ env.example         # Environment template
└── README.md           # This file

⁠Security Considerations

  • Environment Variables: Never commit .env files to version control
  • Network Access: The server runs on all interfaces (0.0.0.0) when containerized
  • Bot Token: Keep your Telegram bot token secure and rotate regularly
  • Chat ID: Only the configured chat ID can receive messages

⁠Troubleshooting

⁠Common Issues

Container won't start:

# Check logs
docker logs mcp-telegram-server

# Common causes:
# - Missing .env file
# - Invalid bot token
# - Invalid chat ID

MCP client can't connect:

# Verify server is running
curl http://localhost:8008/

# Check SSE endpoint
curl -N -H "Accept: text/event-stream" http://localhost:8008/sse

Messages not sending:

  • Verify bot token is correct
  • Confirm chat ID is valid (try both positive and negative values for groups)
  • Check that the bot can send messages to the target chat

⁠License

MIT License - see LICENSE file for details.

⁠Support

Tag summary

Content type

Image

Digest

sha256:83a87ece3…

Size

62.5 MB

Last updated

over 1 year ago

docker pull dkasss/mcp-telegram-server