mcp-telegram-claudecode

mcp-telegram-claudecode

Enables Claude Code to send and receive messages via Telegram for remote interaction and approval of sensitive operations.

Category
访问服务器

README

MCP-Telegram-ClaudeCode

npm version npm downloads License: MIT Node.js Version

An MCP (Model Context Protocol) server that enables Claude Code to send and receive messages via Telegram. This allows you to interact with Claude Code remotely through your Telegram app.

Features

  • Send text messages from Claude Code to Telegram
  • Receive messages from Telegram in Claude Code
  • Send photos/screenshots to Telegram
  • Proxy support for regions where Telegram is blocked
  • NEW: Remote permission approval via hooks - Approve/deny sensitive operations from your phone
  • NEW: Lock file mechanism - Prevents multiple instance conflicts

Prerequisites

Quick Start

Step 1: Create a Telegram Bot

  1. Open Telegram and search for @BotFather
  2. Send /newbot command
  3. Follow the prompts to name your bot
  4. Save the bot token - it looks like: 1234567890:ABCdefGHIjklMNOpqrsTUVwxyz

Step 2: Get Your Chat ID

  1. Open Telegram and search for @userinfobot
  2. Send any message to this bot
  3. Save the Id value from the response - it looks like: 123456789

Step 3: Start Your Bot

Important: Before Claude Code can receive your messages, you must start a conversation with your bot:

  1. Search for your bot by its username in Telegram
  2. Click "Start" or send any message to it

Step 4: Configure Claude Code

Add the MCP server to your Claude Code configuration.

Option A: Using Claude Code settings command

claude /settings

Then add the MCP server configuration.

Option B: Edit configuration file directly

The configuration file is located at:

  • Windows: %USERPROFILE%\.claude.json
  • macOS/Linux: ~/.claude.json

Configuration Examples

Without Proxy

If you can access Telegram directly:

{
  "mcpServers": {
    "telegram": {
      "command": "npx",
      "args": ["-y", "mcp-telegram-claudecode"],
      "env": {
        "TELEGRAM_BOT_TOKEN": "1234567890:ABCdefGHIjklMNOpqrsTUVwxyz",
        "TELEGRAM_CHAT_ID": "123456789"
      }
    }
  }
}

With Proxy

If you need a proxy to access Telegram:

{
  "mcpServers": {
    "telegram": {
      "command": "npx",
      "args": ["-y", "mcp-telegram-claudecode"],
      "env": {
        "TELEGRAM_BOT_TOKEN": "1234567890:ABCdefGHIjklMNOpqrsTUVwxyz",
        "TELEGRAM_CHAT_ID": "123456789",
        "HTTP_PROXY": "http://127.0.0.1:7890"
      }
    }
  }
}

Common proxy ports:

  • Clash: http://127.0.0.1:7890
  • V2Ray: http://127.0.0.1:10808
  • Shadowsocks: http://127.0.0.1:1080

Replace with your actual proxy address and port.


Environment Variables

Variable Required Description
TELEGRAM_BOT_TOKEN Yes Bot token from @BotFather
TELEGRAM_CHAT_ID Yes Your chat ID from @userinfobot
HTTP_PROXY No HTTP proxy URL (e.g., http://127.0.0.1:7890)
HTTPS_PROXY No HTTPS proxy URL (alternative to HTTP_PROXY)

Available Tools

Once configured, Claude Code will have access to these tools:

telegram_send_message

Send a text message to your Telegram.

Parameters:
- message (required): The text message to send

telegram_get_messages

Retrieve recent messages from Telegram.

Parameters:
- limit (optional): Maximum number of messages to retrieve (default: 10)

telegram_check_new

Quick check if there are new messages.

No parameters required

telegram_send_photo

Send an image file to Telegram.

Parameters:
- photo_path (required): Absolute path to the image file
- caption (optional): Caption for the photo

Usage Examples

After configuration, you can ask Claude Code to:

  • "Send me a message on Telegram saying the task is complete"
  • "Check if I sent any new messages on Telegram"
  • "Send a screenshot of the current code to my Telegram"

Troubleshooting

"TELEGRAM_BOT_TOKEN must be configured"

Make sure you've added the bot token to your .claude.json configuration.

"No new messages" but you sent messages

  1. Make sure you started a conversation with your bot first
  2. Check that your TELEGRAM_CHAT_ID is correct
  3. If using a proxy, verify the proxy is working

Connection timeout or network error

If you're in a region where Telegram is blocked:

  1. Make sure your proxy software is running
  2. Add the HTTP_PROXY environment variable to your configuration
  3. Verify the proxy port is correct

Bot not responding

  1. Check that the bot token is correct (no extra spaces)
  2. Make sure you've started a conversation with your bot
  3. Try sending a message to your bot first, then check for messages

Remote Permission Approval (Recommended)

Instead of using --dangerously-skip-permissions, you can use Claude Code hooks to approve sensitive operations remotely via Telegram.

How It Works

┌─────────────┐     PreToolUse Hook     ┌─────────────────┐     Telegram API     ┌──────────┐
│ Claude Code │ ──────────────────────► │ Hook Script     │ ◄─────────────────► │ Telegram │
│ (sensitive  │                         │ (asks approval) │                      │ (you)    │
│  operation) │ ◄────────────────────── │                 │                      │          │
└─────────────┘     approve/deny        └─────────────────┘                      └──────────┘
  1. Claude Code attempts a sensitive operation (Edit, Write, Bash)
  2. PreToolUse hook sends details to your Telegram
  3. You reply Y to approve or N to deny
  4. Hook returns the decision to Claude Code

Setup

  1. Copy the hooks to your system (included in hooks/ directory)
  2. Configure Claude Code hooks via /hooks command or edit settings:
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash|Edit|Write",
        "hooks": [
          "node /path/to/telegram-claude-mcp/hooks/pretool-approval.js"
        ]
      }
    ]
  }
}
  1. Set environment variables (same as MCP server config)

See hooks/README.md for detailed setup instructions.

Approval Responses

Approve Deny
Y, yes, 1, approve N, no, 0, deny
是, 好, 可以 否, 不, 拒绝

How It Works

Architecture

This MCP server acts as a bridge between Claude Code and Telegram:

┌─────────────┐     MCP Protocol      ┌─────────────────┐     Telegram API     ┌──────────┐
│ Claude Code │ ◄──────────────────► │ MCP Server      │ ◄─────────────────► │ Telegram │
│             │                       │ (this project)  │                      │          │
└─────────────┘                       └─────────────────┘                      └──────────┘

Auto-Polling & Terminal Injection (Experimental)

When the MCP server starts, it automatically begins polling for new Telegram messages. When a message is received, it attempts to inject the text into the active terminal window using:

  1. Clipboard: Message is copied to system clipboard
  2. SendKeys (Windows): PowerShell script simulates Ctrl+V and Enter keystrokes
  3. Window Activation: Attempts to find and activate terminal windows (Windows Terminal, cmd, PowerShell, VS Code)

This is an experimental feature - it enables "remote control" of Claude Code via Telegram, but has reliability limitations.

Tools Available

Tool Description
telegram_send_message Send text to Telegram
telegram_get_messages Retrieve recent messages
telegram_check_new Quick check for new messages
telegram_send_photo Send images to Telegram
telegram_start_polling Manually start auto-polling
telegram_stop_polling Stop auto-polling

Known Issues & Limitations

✅ Multiple Claude Code Instances (Fixed in v1.4.0)

Problem: If you run multiple Claude Code windows, each will start its own MCP server instance.

Solution: Lock file mechanism now prevents multiple instances from polling simultaneously. Only the first instance will poll; others will skip polling automatically.

✅ Injection Failure Notification (Fixed in v1.4.0)

Problem: When SendKeys injection fails, you wouldn't know about it.

Solution: Failed injections now send a notification to Telegram, so you know when to check manually.

✅ Permission Prompts (Solved with Hooks)

Problem: Cannot approve sensitive operations remotely.

Solution: Use the included PreToolUse hooks for remote approval via Telegram. See Remote Permission Approval section.

⚠️ SendKeys Reliability (Windows)

The terminal injection feature uses WriteConsoleInput API for no-focus injection:

How it works:

  • Uses Windows Console API to write directly to the console input buffer
  • Does not require window focus
  • Does not use clipboard
  • Works when other applications are active

Limitation - Single Terminal Only:

  • Works correctly when only one terminal window is open
  • If multiple terminals are open, messages may go to the wrong terminal
  • This is due to Windows Terminal's ConPTY architecture

When it may fail:

  • Multiple terminal windows open simultaneously
  • Remote desktop or virtual machine environments
  • Screen is locked

Workaround: Use hooks instead of SendKeys for more reliable operation, or ensure only one terminal is open.

⚠️ Platform Support

Platform MCP Tools Auto-Injection Hooks
Windows ✅ Full ✅ SendKeys ✅ Full
macOS ✅ Full ❌ Not implemented ✅ Full
Linux ✅ Full ❌ Not implemented ✅ Full

Recommendation: Use hooks for cross-platform remote control.


Changelog

v1.4.0

  • ✅ Added lock file mechanism to prevent multiple instance conflicts
  • ✅ Added injection failure notifications via Telegram
  • ✅ Added PreToolUse hook for remote permission approval
  • ✅ Added PostToolUse hook for error notifications
  • ✅ Improved terminal injection with WriteConsoleInput API (no focus required)
  • ✅ Improved exit cleanup (SIGINT/SIGTERM handling)
  • ⚠️ Known limitation: Single terminal mode only (multiple terminals may cause injection to wrong window)

v1.3.0

  • Added photo sending support
  • Added proxy support

v1.2.0

  • Added auto-polling and terminal injection

v1.1.0

  • Added telegram_check_new tool

v1.0.0

  • Initial release

License

MIT License - see LICENSE file for details.

Author

EthanSky

Repository

https://github.com/EthanSky2986/mcp-telegram-claudecode

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选