Why we moved Telegram MCP security enforcement inside the MTProto client
gramcp did not begin as a Rust Telegram client. It began as an HTTP security proxy sitting in front of someone else's Node.js Telegram MCP server. That proxy worked well enough to teach us where external MCP gateways break down.
The first architecture: an external MCP proxy
When you attach an off-the-shelf Telegram MCP server to Claude, the server exposes everything your personal MTProto session can do: read private chats, send messages anywhere, delete history, ban members, leave groups, terminate sessions, and spend Telegram Stars.
The obvious first fix was a wrapper proxy: Streamable HTTP with a bearer token on the outside, stdio to the upstream Node.js process on the inside, a tool allowlist, a send destination whitelist, a single outbox folder, an hourly rate cap, and Cloudflare Access around the endpoint.
That stopped obvious abuse, but writing the proxy's own threat model exposed three flaws that no JSON-RPC wrapper could close cleanly.
Gap 1: You cannot reliably hide a chat from outside the client
An external proxy only sees tool names and JSON arguments going in, and arbitrary formatted strings coming back out. It can reject read_messages(chat="Family") if the caller spells the ID the way the proxy expects.
It fails on everything else:
- Alternate ID spellings: Telegram identifies the same peer as
-100123456,channel:123456,me, or a numeric user ID. Basic groups upgraded to supergroups gain a second ID while keeping history. - Indirect leaks in collection responses:
list_chats,search_messages,get_drafts, forwarded messages, reply quotes, poll voters, andget_updatesall return structured Telegram objects. Stripping blacklisted peers from free-form upstream text is fragile; dropping them from typed MTProto structs before JSON serialization is exact. - Cross-chat message ID lookups: Telegram does not bind private-chat message IDs to a specific peer in every RPC. Verifying whether a message belongs to a blacklisted sender requires querying Telegram first inside the client before returning media, polls, or metadata.
Gap 2: Username pinning and atomic rate limits need live MTProto state
If a user whitelists @colleague in a proxy, the proxy only checks the string "@colleague". If that handle is later released and registered by an attacker, the proxy happily forwards the message to the wrong person. Pinning @username → canonical peer_id requires calling resolve_username on the live MTProto session, comparing the returned peer ID against SQLite, and failing closed with username_mismatch on drift.
Similarly, an in-memory proxy counter resets on every restart and only counts tool calls (so forwarding 50 messages in one call looks like "1"). Moving limits into SQLite inside the client lets gramcp reserve N slots for an N-message forward or album inside an IMMEDIATE transaction, roll back on upstream error, and disarm drop-rollback once MTProto dispatch starts so HTTP cancellation cannot refund spent quota.
Gap 3: Upstream schema drift breaks proxy-level argument checks
A proxy has to guess which JSON fields carry destinations, file paths, or destructive flags for an upstream server it does not own. Whenever the upstream adds a parameter alias or changes response structure, the proxy's enforcement silently drifts open.
Inside gramcp, every tool handler deserializes into a Rust struct marked #[serde(deny_unknown_fields)], routes through PolicyEngine before touching TelegramBackend, and records a SQLite audit_log row in an IMMEDIATE transaction before any mutation runs.
What moving inside the client unlocked
Rewriting the server as one Rust binary on grammers collapsed the whole stack:
ReadBlacklistGuardfilters typed structs across dialogs, messages, search, members, forwards, stories, and reply quotes before anything reaches JSON.- Native UTF-16
MessageEntityparsing appends the— Sent by AIfooter after body entities close, so unclosed HTML/Markdown tags cannot hide attribution. MockBackendimplements the exact sameTelegramBackendtrait as liveGrammersBackend, so every prompt-injection refusal, username-drift check, symlink escape, and OAuth token-reuse path is covered by deterministic integration tests in CI.
Next read: Building a fail-closed security boundary between an LLM and Telegram in Rust, or inspect src/security/policy.rs directly.