telegram-ads-mcp 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Furkan Koykiran
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,426 @@
1
- # Temporary Holding Version
1
+ <p align="center">
2
+ <img src="assets/logo.svg" width="112" height="112" alt="Telegram Ads MCP logo">
3
+ </p>
2
4
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
5
+ <h1 align="center">Telegram Ads MCP</h1>
6
+
7
+ <p align="center">
8
+ <a href="https://github.com/furkankoykiran/telegram-ads-mcp/actions/workflows/ci.yml"><img src="https://github.com/furkankoykiran/telegram-ads-mcp/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
9
+ <a href="https://registry.modelcontextprotocol.io/?q=io.github.furkankoykiran%2Ftelegram-ads"><img src="https://img.shields.io/badge/MCP%20Registry-listed-6f42c1" alt="MCP Registry"></a>
10
+ <a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License: MIT"></a>
11
+ <a href="https://nodejs.org"><img src="https://img.shields.io/badge/node-%3E%3D22.18-339933?logo=node.js&logoColor=white" alt="Node.js 22.18 or newer"></a>
12
+ <a href="https://www.typescriptlang.org"><img src="https://img.shields.io/badge/TypeScript-strict-3178C6?logo=typescript&logoColor=white" alt="TypeScript strict"></a>
13
+ </p>
14
+
15
+ An open-source [Model Context Protocol](https://modelcontextprotocol.io) server for the **official
16
+ Telegram Ads API**. Manage accounts, advertisements, budgets, statistics, audiences, pixels and
17
+ targeting from Claude, Cursor, Codex, OpenCode or any MCP-compatible client.
18
+
19
+ - **Official API only.** No cookies, browser automation or private endpoints.
20
+ - **Local-first.** Runs over stdio on your machine. Your token never leaves it except to call
21
+ `promoteapi.telegram.org`.
22
+ - **Safe by default.** Read-only mode, explicit write confirmation, budget caps, idempotency keys and
23
+ an optional JSONL audit log.
24
+ - **Built for agents.** 24 focused tools, a searchable catalog of all 45 official methods, guided
25
+ prompts, and an optional sandboxed Code Mode for multi-step work.
26
+
27
+ > This project is unofficial and is not affiliated with, endorsed by or sponsored by Telegram. The
28
+ > Telegram name and logo are trademarks of their respective owners.
29
+
30
+ ## Contents
31
+
32
+ - [Quick start](#quick-start)
33
+ - [Configuration](#configuration)
34
+ - [Safety model](#safety-model)
35
+ - [Tools](#tools)
36
+ - [Code Mode](#code-mode)
37
+ - [CLI](#cli)
38
+ - [Development](#development)
39
+ - [Troubleshooting](#troubleshooting)
40
+ - [Security](#security)
41
+ - [License](#license)
42
+
43
+ ## Quick start
44
+
45
+ ### Prerequisites
46
+
47
+ - Node.js **22.18 or newer** (24 LTS recommended)
48
+ - A Telegram Ads account with access to the API
49
+ - An API token from <https://ads.telegram.org/account/api>
50
+
51
+ ### 1. Connect the server
52
+
53
+ The package is published on npm as `telegram-ads-mcp` and listed in the official
54
+ [MCP Registry](https://registry.modelcontextprotocol.io/?q=io.github.furkankoykiran%2Ftelegram-ads)
55
+ as `io.github.furkankoykiran/telegram-ads`. Run:
56
+
57
+ ```bash
58
+ npx -y telegram-ads-mcp doctor
59
+ ```
60
+
61
+ To run the latest commit from GitHub instead of the npm release, use:
62
+
63
+ ```bash
64
+ npx -y github:furkankoykiran/telegram-ads-mcp doctor
65
+ ```
66
+
67
+ Or build a local checkout:
68
+
69
+ ```bash
70
+ git clone https://github.com/furkankoykiran/telegram-ads-mcp.git
71
+ cd telegram-ads-mcp
72
+ npm install
73
+ node dist/cli.js doctor
74
+ ```
75
+
76
+ `doctor` checks the token against the API and prints the account currency and budget. It is the
77
+ fastest way to confirm everything works.
78
+
79
+ ### 2. Write your client configuration
80
+
81
+ `init` writes the correct file for each client and keeps a `.bak` copy of an existing config:
82
+
83
+ ```bash
84
+ npx -y telegram-ads-mcp init --client claude-desktop --token <your-token>
85
+ npx -y telegram-ads-mcp init --client cursor --token <your-token>
86
+ npx -y telegram-ads-mcp init --client opencode --token <your-token>
87
+ npx -y telegram-ads-mcp init --client all --token <your-token>
88
+ ```
89
+
90
+ To launch the GitHub package instead of the npm release, add `--github`:
91
+
92
+ ```bash
93
+ npx -y github:furkankoykiran/telegram-ads-mcp init --client all --github --token <your-token>
94
+ ```
95
+
96
+ For a local checkout, add `--local .` so the generated config launches `node dist/cli.js` instead of
97
+ `npx`:
98
+
99
+ ```bash
100
+ node dist/cli.js init --client claude-desktop --local . --token <your-token>
101
+ ```
102
+
103
+ Prefer to edit files by hand? `--print` prints the merged configuration without writing anything.
104
+ Claude Code and Codex are CLI-based; `init` prints the exact `claude mcp add` / `codex mcp add`
105
+ command instead of editing their config.
106
+
107
+ <details>
108
+ <summary>Claude Desktop / Cursor JSON</summary>
109
+
110
+ ```json
111
+ {
112
+ "mcpServers": {
113
+ "telegram-ads": {
114
+ "command": "npx",
115
+ "args": ["-y", "telegram-ads-mcp"],
116
+ "env": {
117
+ "TELEGRAM_ADS_TOKEN": "your-token",
118
+ "TG_ADS_MODE": "read-only",
119
+ "TG_ADS_CODE_MODE": "read-only"
120
+ }
121
+ }
122
+ }
123
+ }
124
+ ```
125
+
126
+ </details>
127
+
128
+ <details>
129
+ <summary>OpenCode JSON</summary>
130
+
131
+ ```json
132
+ {
133
+ "mcp": {
134
+ "servers": {
135
+ "telegram-ads": {
136
+ "type": "local",
137
+ "command": ["npx", "-y", "telegram-ads-mcp"],
138
+ "environment": {
139
+ "TELEGRAM_ADS_TOKEN": "your-token",
140
+ "TG_ADS_MODE": "read-only",
141
+ "TG_ADS_CODE_MODE": "read-only"
142
+ },
143
+ "codemode": false
144
+ }
145
+ }
146
+ }
147
+ }
148
+ ```
149
+
150
+ OpenCode V2 uses the `mcp.servers` shape. `codemode: false` keeps the 24 tools directly callable;
151
+ use the server's own `tg_execute` when you want sandboxed scripts.
152
+
153
+ </details>
154
+
155
+ <details>
156
+ <summary>Claude Code and Codex</summary>
157
+
158
+ ```bash
159
+ claude mcp add telegram-ads --scope user \
160
+ --env TELEGRAM_ADS_TOKEN=<your-token> \
161
+ --env TG_ADS_MODE=read-only \
162
+ -- npx -y telegram-ads-mcp
163
+
164
+ codex mcp add telegram-ads \
165
+ --env TELEGRAM_ADS_TOKEN=<your-token> \
166
+ --env TG_ADS_MODE=read-only \
167
+ -- npx -y telegram-ads-mcp
168
+ ```
169
+
170
+ </details>
171
+
172
+ ### 3. Ask your agent to start
173
+
174
+ Restart the client and ask:
175
+
176
+ > Use `tg_get_account` to show my Telegram Ads account and currency.
177
+
178
+ Then try a read-only report:
179
+
180
+ > Use `tg_get_stats` for the last 30 days, daily interval, and summarize spend, views, CTR and CPM.
181
+
182
+ ## Configuration
183
+
184
+ All configuration is environment-based. The server never reads `.env` files implicitly; load them
185
+ explicitly with `node --env-file=.env` if you prefer a file.
186
+
187
+ | Variable | Default | Description |
188
+ | ------------------------- | ------------ | ----------------------------------------------------------------------- |
189
+ | `TELEGRAM_ADS_TOKEN` | required | API token from <https://ads.telegram.org/account/api>. |
190
+ | `TG_ADS_TOKEN` | — | Alias for `TELEGRAM_ADS_TOKEN`. |
191
+ | `TG_ADS_MODE` | `read-only` | `read-only` or `write`. Write tools are blocked unless this is `write`. |
192
+ | `TG_ADS_CODE_MODE` | `read-only` | `off`, `read-only` or `write`. Controls `tg_execute`. |
193
+ | `TG_ADS_ALLOWED_ACCOUNTS` | all | Comma-separated allowlist for explicit `account_id` values. |
194
+ | `TG_ADS_MAX_CPM` | no cap | Maximum CPM accepted by the server, in account currency. |
195
+ | `TG_ADS_MAX_AD_BUDGET` | no cap | Maximum ad budget or budget transfer per call. |
196
+ | `TG_ADS_MAX_DAILY_BUDGET` | no cap | Maximum daily budget limit per call. |
197
+ | `TG_ADS_AUDIT_LOG` | disabled | Path to a JSONL audit log. Parent directories are created. |
198
+ | `TG_ADS_TIMEOUT_MS` | `30000` | Per-request timeout in milliseconds. |
199
+ | `TG_ADS_BASE_URL` | official API | Override only for testing. The token is sent to this URL. |
200
+
201
+ Caps apply **per call**. They are a guardrail, not a full budgeting system: a loop of approved
202
+ calls can still spend more than a single cap. Use `TG_ADS_ALLOWED_ACCOUNTS` and the read-only
203
+ default when you want hard limits on what an agent can touch.
204
+
205
+ ### Currency precision
206
+
207
+ Telegram rejects values with too many decimals. The server validates them before sending:
208
+
209
+ | Currency | CPM and budgets | Amounts (transfers, account budget) |
210
+ | -------- | --------------- | ----------------------------------- |
211
+ | EUR | 2 decimals | 5 decimals |
212
+ | TON | 2 decimals | 5 decimals |
213
+ | XTR | 0 decimals | 3 decimals |
214
+
215
+ `tg_get_account` returns the account currency. `tg_create_ad` and `tg_ad_budget` refuse mismatched
216
+ precision with a clear error.
217
+
218
+ ## Safety model
219
+
220
+ The server is designed to be useful to an agent without giving it uncontrolled access to money.
221
+
222
+ 1. **Read-only by default.** `TG_ADS_MODE` defaults to `read-only`. Every write tool fails before it
223
+ reaches the API.
224
+ 2. **Per-call confirmation.** In write mode, every write also requires `confirm: true`. The model
225
+ must decide to pass it, and the call is visible in the transcript.
226
+ 3. **New ads start paused with zero budget.** `tg_create_ad` defaults `is_paused` to `true` and
227
+ `initial_budget` to `0`. Funding is a separate, explicit step.
228
+ 4. **Dry runs.** `tg_create_ad` and `tg_update_ad` accept `dry_run: true` to show the exact payload
229
+ and currency without calling the API.
230
+ 5. **Account allowlist.** `TG_ADS_ALLOWED_ACCOUNTS` rejects explicit `account_id` values outside the
231
+ list.
232
+ 6. **Budget caps.** `TG_ADS_MAX_CPM`, `TG_ADS_MAX_AD_BUDGET` and `TG_ADS_MAX_DAILY_BUDGET` reject
233
+ oversized values before the request is sent.
234
+ 7. **Idempotency.** Every write carries an `Idempotency-Key`; retries reuse the same key. Tools
235
+ accept an explicit key when the caller needs to retry safely.
236
+ 8. **Audit log.** Set `TG_ADS_AUDIT_LOG` to keep a JSONL record of tool calls. Phone numbers, emails
237
+ and legal names are summarized, and ad text/code is reduced to its length.
238
+ 9. **No token in logs.** The token is never included in tool output, errors or the audit log.
239
+
240
+ Code Mode adds a second gate: writes inside `tg_execute` require `TG_ADS_MODE=write`,
241
+ `TG_ADS_CODE_MODE=write` **and** `confirm: true` on the `tg_execute` call.
242
+
243
+ ## Tools
244
+
245
+ Every tool returns JSON text. Errors are returned with `isError: true` and a stable code such as
246
+ `WRITE_DISABLED`, `CONFIRMATION_REQUIRED`, `UNAUTHORIZED` or `CHANNEL_ID_UNKNOWN`.
247
+
248
+ ### Account
249
+
250
+ | Tool | Access | Purpose |
251
+ | --------------------- | ------ | --------------------------------------------------------------------- |
252
+ | `tg_get_account` | read | Current account, currency, budgets. Optionally list related accounts. |
253
+ | `tg_get_account_info` | read | Billing/profile details (company, email, phone, country, city). |
254
+ | `tg_account_budget` | write | Increase or decrease a related account's budget. |
255
+
256
+ ### Advertisements
257
+
258
+ | Tool | Access | Purpose |
259
+ | ----------------- | ------ | -------------------------------------------------------------------- |
260
+ | `tg_list_ads` | read | Paged ad list with status, budgets and lifetime metrics. |
261
+ | `tg_get_ads` | read | Up to 100 ads by ID, including decline reasons. |
262
+ | `tg_create_ad` | write | Create a paused, zero-budget ad. Supports `dry_run`. |
263
+ | `tg_update_ad` | write | Edit fields, pause/resume, or submit for review. Supports `dry_run`. |
264
+ | `tg_ad_budget` | write | Move funds into or out of an ad. |
265
+ | `tg_delete_ad` | write | Delete an inactive ad with no remaining budget. |
266
+ | `tg_upload_media` | write | Upload ad photos, videos or website photos from a local path. |
267
+
268
+ ### Statistics
269
+
270
+ | Tool | Access | Purpose |
271
+ | --------------- | ------ | ----------------------------------------------------- |
272
+ | `tg_get_stats` | read | Account or per-ad stats, with totals and CTR/CPM/CPC. |
273
+ | `tg_get_report` | read | Telegram's monthly account report. |
274
+
275
+ ### Targeting
276
+
277
+ | Tool | Access | Purpose |
278
+ | ------------------- | ------ | ----------------------------------------------------------- |
279
+ | `tg_list_targeting` | read | Languages, topics, countries, locations, channels and bots. |
280
+
281
+ ### Audiences and pixels
282
+
283
+ | Tool | Access | Purpose |
284
+ | ---------------------- | ------ | ----------------------------------------------- |
285
+ | `tg_list_audiences` | read | List or fetch retargeting audiences. |
286
+ | `tg_manage_audience` | write | Create, edit or delete a phone-number audience. |
287
+ | `tg_get_pixel` | read | Pixel ID and installation snippet. |
288
+ | `tg_list_pixel_events` | read | List or fetch conversion events. |
289
+ | `tg_manage_pixel` | write | Create the pixel, create/rename/delete events. |
290
+
291
+ ### Transactions
292
+
293
+ | Tool | Access | Purpose |
294
+ | ---------------------- | ------ | ---------------------------------------------- |
295
+ | `tg_list_transactions` | read | Account-level or ad-level transaction history. |
296
+ | `tg_get_transaction` | read | Status of a budget transaction. |
297
+
298
+ ### Advanced
299
+
300
+ | Tool | Access | Purpose |
301
+ | ------------- | ---------- | -------------------------------------------------------------- |
302
+ | `tg_search` | read | Search all official methods by name, description or parameter. |
303
+ | `tg_get_sdk` | read | TypeScript declarations for the Code Mode `tg` namespace. |
304
+ | `tg_execute` | sandboxed | Run JavaScript with sequential API calls in QuickJS. |
305
+ | `tg_api_call` | read/write | Escape hatch for any method without a dedicated tool. |
306
+
307
+ The server also exposes:
308
+
309
+ - **Resources:** `telegram-ads://guide`, `telegram-ads://method-catalog`, `telegram-ads://sdk.d.ts`
310
+ - **Prompts:** `launch-ad`, `audit-account`, `weekly-report`
311
+
312
+ ## Code Mode
313
+
314
+ Code Mode lets an agent run one short JavaScript program instead of dozens of tool calls: bulk
315
+ edits, pagination, joins, cross-ad analysis and compact reports.
316
+
317
+ ```js
318
+ const ads = await tg.getAdsList({ limit: 100 });
319
+ const active = ads.ads.filter((ad) => ad.status === "active");
320
+ return active
321
+ .sort((a, b) => (b.spent_budget ?? 0) - (a.spent_budget ?? 0))
322
+ .map((ad) => ({
323
+ id: ad.ad_id,
324
+ title: ad.title,
325
+ spent: ad.spent_budget,
326
+ cpm: ad.cpm,
327
+ }));
328
+ ```
329
+
330
+ Limits and guarantees:
331
+
332
+ - QuickJS (WebAssembly) sandbox with **no network, filesystem, process or environment access**.
333
+ - Only the `tg` object and `console` are exposed.
334
+ - Calls run **sequentially**, so scripts are deterministic and cannot burst the API.
335
+ - At most **25 API calls**, **15 seconds**, **64 MB** of memory and **64 KB** of output per run.
336
+ - Read-only unless `TG_ADS_MODE=write` **and** `TG_ADS_CODE_MODE=write`, and `tg_execute` is called
337
+ with `confirm: true`.
338
+
339
+ See [docs/code-mode.md](docs/code-mode.md) for examples and the full security model.
340
+
341
+ ## CLI
342
+
343
+ ```text
344
+ telegram-ads-mcp serve Start the MCP server over stdio (default)
345
+ telegram-ads-mcp doctor Check configuration and verify the token
346
+ telegram-ads-mcp init Write MCP client configuration
347
+ telegram-ads-mcp --version Print the version
348
+ ```
349
+
350
+ `init` options:
351
+
352
+ ```text
353
+ --client <name...> claude-desktop, cursor, opencode, claude-code, codex, or all
354
+ --token <token> Defaults to TELEGRAM_ADS_TOKEN
355
+ --mode <mode> read-only or write (default: read-only)
356
+ --code-mode <mode> off, read-only or write (default: read-only)
357
+ --local <path> Launch a built local checkout instead of npx
358
+ --github Launch github:furkankoykiran/telegram-ads-mcp instead of the npm package
359
+ --print Print the configuration without writing files
360
+ ```
361
+
362
+ The token is stored in plain text in the generated client config. Keep that file private and revoke
363
+ the token at <https://ads.telegram.org/account/api> if it is exposed.
364
+
365
+ ## Development
366
+
367
+ ```bash
368
+ npm install
369
+ npm run typecheck
370
+ npm run lint
371
+ npm test
372
+ npm run build
373
+ ```
374
+
375
+ | Script | Purpose |
376
+ | ---------------------- | --------------------------------------------------------------- |
377
+ | `npm run dev` | Run the CLI from source with `tsx`. |
378
+ | `npm run check` | Typecheck, lint and test in one pass. |
379
+ | `npm run sync-catalog` | Re-parse the official docs and regenerate `src/api/catalog.ts`. |
380
+ | `npm run format` | Format the repository with Prettier. |
381
+
382
+ Project layout:
383
+
384
+ ```text
385
+ src/
386
+ api/ Catalog, shared types and the HTTP client
387
+ code-mode/ QuickJS sandbox and generated SDK declarations
388
+ tools/ MCP tool registrations
389
+ cli.ts serve / doctor / init
390
+ server.ts McpServer assembly
391
+ policy.ts Write gates, account allowlist and budget caps
392
+ audit.ts Optional JSONL audit log
393
+ tests/
394
+ unit/ Money, client, policy, sandbox, catalog, schemas
395
+ contract/ End-to-end MCP client/server contract tests
396
+ ```
397
+
398
+ The method catalog is generated from <https://ads.telegram.org/docs/api>. If Telegram ships a new
399
+ API revision, run `npm run sync-catalog`, review the diff and update the tools that need new fields.
400
+ Do not edit `src/api/catalog.ts` by hand.
401
+
402
+ Release steps and the one-time trusted publishing setup are documented in
403
+ [docs/releasing.md](docs/releasing.md).
404
+
405
+ ## Troubleshooting
406
+
407
+ | Symptom | Fix |
408
+ | --------------------------------------- | ---------------------------------------------------------------------------------------------------- |
409
+ | `UNAUTHORIZED` / HTTP 401 | The token is invalid or revoked. Create a new one at <https://ads.telegram.org/account/api>. |
410
+ | `WRITE_DISABLED` | Set `TG_ADS_MODE=write` in the server environment and restart the client. |
411
+ | `CONFIRMATION_REQUIRED` | Re-run the write with `confirm: true` after checking the parameters. |
412
+ | `CHANNEL_ID_UNKNOWN` / `BOT_ID_UNKNOWN` | Use `@username` first so the account resolves it, then numeric IDs will work. |
413
+ | `IDEMPOTENT_PARAM_MISMATCH` | The key was already used with different parameters. Retry with a new key. |
414
+ | `IDEMPOTENT_REQUEST_IN_PROGRESS` | An identical request is still running. Wait and retry with the same key. |
415
+ | Precision errors | Check the account currency with `tg_get_account`; EUR/TON use 2 decimals for CPM, XTR uses 0. |
416
+ | Stats range error | Use an interval of 300 or 86400 seconds and at most 1000 intervals; the server splits larger ranges. |
417
+ | Client does not see the server | Restart the client after editing its config, and check that `node --version` is 22.18 or newer. |
418
+
419
+ ## Security
420
+
421
+ Please read [SECURITY.md](SECURITY.md) before using write mode in production. Report vulnerabilities
422
+ through GitHub's private vulnerability reporting, not in a public issue.
423
+
424
+ ## License
425
+
426
+ MIT © Furkan Koykiran. See [LICENSE](LICENSE).
@@ -0,0 +1,18 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512" role="img" aria-labelledby="title">
2
+ <title id="title">Telegram Ads MCP</title>
3
+ <rect width="512" height="512" rx="112" fill="#0B1220"/>
4
+
5
+ <!-- Ascending analytics bars -->
6
+ <g id="analytics-bars">
7
+ <rect x="108" y="318" width="44" height="72" rx="12" fill="#FFB86B"/>
8
+ <rect x="168" y="250" width="44" height="140" rx="12" fill="#FFB86B"/>
9
+ <rect x="228" y="182" width="44" height="208" rx="12" fill="#FFB86B"/>
10
+ </g>
11
+
12
+ <!-- Original origami paper plane -->
13
+ <g id="paper-plane">
14
+ <polygon points="428,92 316,230 254,162" fill="#4EECD0"/>
15
+ <polygon points="428,92 316,230 296,272" fill="#149D7C"/>
16
+ <polygon points="428,92 398,272 316,230" fill="#2DE2B7"/>
17
+ </g>
18
+ </svg>
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node