Skip to main content
Glama
Telegram MCP

Telegram MCP

README.md
# Telegram MCP

An independent MCP server for posting in a Telegram channel, editing a specific
publication and reading the subscriber count. It accepts text, one photo with
a caption or an album. The bot connection is scoped to the configured channel.
Telegram credentials are supplied by the operator.

## Run locally

Requires Node.js 20 or later. Run `npm ci`, then `npm test`. Create a bot in
BotFather, add it as an administrator of the channel and grant publishing
rights. Grant editing rights if the bot needs to edit messages.
Use the numeric channel ID when possible so that changing a public handle does
not change which channel the connection addresses.

Set `TELEGRAM_BOT_TOKEN`, `TELEGRAM_CHANNEL` and `TELEGRAM_STATE_DIR`. The last
variable points to a persistent directory writable only by the service account.
Run `node server.mjs --stdio` from an MCP client or `npm start` for Streamable HTTP
at `/mcp`. Example configuration for a local stdio client:

```json
{
  "mcpServers": {
    "telegram": {
      "command": "node",
      "args": ["/path/to/telegram-mcp/server.mjs", "--stdio"],
      "env": {
        "TELEGRAM_CHANNEL": "-1001234567890",
        "TELEGRAM_STATE_DIR": "/path/to/telegram-state"
      }
    }
  }
}
```

Supply the bot token from the client's secret store or its process environment.
For remote HTTP, put the service behind HTTPS, configure `MCP_TOKEN` and use
`MCP_PUBLIC_HOST` for the expected Host header. Set `MCP_HOST` and `MCP_PORT`
for listening. Use one persistent state directory per connection. Share it
between instances only where the filesystem supports atomic exclusive file
creation and rename. Keep the directory across restarts.

## Use the tools

- `channel_info` gives the channel, posting and editing permissions and the
  subscriber count if Telegram provides it.
- `subscriber_history` returns the collected daily subscriber samples in the
  requested date range. Requesting a fresh sample stores today’s count.
- `publish_text` and `publish_photos` return channel and message identifiers
  and links. Photos may be Telegram file IDs or base64 originals. An authorized
  BiNetwork artifact is an additional input when the operator sets
  `BN_ARTIFACT_TRANSFER_URL` to the exact trusted artifact-transfer endpoint.
- `edit_publication` changes the text, caption or photo of an addressed post.
- `publication_status` reads the last receipt for an operation. It does not
  read the current channel history.

Use a new operation identifier for a new action. Reuse the same identifier and
arguments to recover an earlier action. When Telegram may have accepted the
request and the response was lost, the receipt is `unknown`. Check the
channel before deciding on a new operation. Explicit Telegram failures include
the provider reason and any retry delay. Removing the state directory also
removes the ability to recover previous operations.

Subscriber history contains only counts sampled through this connection. Historical
reach and arbitrary channel history remain outside what a regular Bot API
connection offers. The server does not modify any existing Telegram webhook.

For a first read-only check, list tools in the client and call `publication_status` with a fresh operation ID. It returns `not_observed` without contacting Telegram. With your bot configured, `channel_info` checks the channel and bot permissions. Sending a post is a separate tool call.

If you use a browser-based HTTP client, serve it from the same origin as the MCP endpoint. For remote use the expected origin is `https://` followed by `MCP_PUBLIC_HOST`. Native MCP clients can omit the Origin header.

`npm test` opens both an independent HTTP client and a separate stdio process. Tests cover a photo album, editing, simultaneous retries and an operation with a lost reply. Telegram calls use a local test double. The protocol is documented in the [MCP transport specification](https://modelcontextprotocol.io/specification/2025-11-25/basic/transports). Channel capabilities and limits follow the [official Telegram Bot API](https://core.telegram.org/bots/api).

## License

[MIT](LICENSE). This server can be used, modified and distributed independently of BiNetwork, including commercially. Dependencies retain their original licenses.