pcade-mcp 1.0.0
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 +21 -0
- package/README.md +255 -0
- package/bin/cli.mjs +441 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 PPC Ad Editor
|
|
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
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
# pcade-mcp
|
|
2
|
+
|
|
3
|
+
Create professional Google, Meta & LinkedIn ad previews from any AI assistant.
|
|
4
|
+
|
|
5
|
+
`pcade-mcp` is an [MCP](https://modelcontextprotocol.io) server for
|
|
6
|
+
[PPC Ad Editor](https://www.ppcadeditor.com). It connects Claude, Cursor,
|
|
7
|
+
Windsurf, and other MCP clients to the PPC Ad Editor preview engine.
|
|
8
|
+
|
|
9
|
+
**Works out of the box — no account needed.** Guest mode gives you 3 previews
|
|
10
|
+
per day. Add an API key for higher limits.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Quick Setup (guest mode — no account needed)
|
|
15
|
+
|
|
16
|
+
### Claude Desktop
|
|
17
|
+
|
|
18
|
+
Add to `~/.claude/claude_desktop_config.json`:
|
|
19
|
+
|
|
20
|
+
```json
|
|
21
|
+
{
|
|
22
|
+
"mcpServers": {
|
|
23
|
+
"pcade": {
|
|
24
|
+
"command": "npx",
|
|
25
|
+
"args": ["-y", "pcade-mcp"]
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Claude Desktop (Windows)
|
|
32
|
+
|
|
33
|
+
```json
|
|
34
|
+
{
|
|
35
|
+
"mcpServers": {
|
|
36
|
+
"pcade": {
|
|
37
|
+
"command": "cmd",
|
|
38
|
+
"args": ["/c", "npx", "-y", "pcade-mcp"]
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Cursor
|
|
45
|
+
|
|
46
|
+
Add to `.cursor/mcp.json` (project) or `~/.cursor/mcp.json` (global):
|
|
47
|
+
|
|
48
|
+
```json
|
|
49
|
+
{
|
|
50
|
+
"mcpServers": {
|
|
51
|
+
"pcade": {
|
|
52
|
+
"command": "npx",
|
|
53
|
+
"args": ["-y", "pcade-mcp"]
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Windsurf
|
|
60
|
+
|
|
61
|
+
Add to `~/.codeium/windsurf/mcp_config.json`:
|
|
62
|
+
|
|
63
|
+
```json
|
|
64
|
+
{
|
|
65
|
+
"mcpServers": {
|
|
66
|
+
"pcade": {
|
|
67
|
+
"command": "npx",
|
|
68
|
+
"args": ["-y", "pcade-mcp"]
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Claude Code
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
claude mcp add pcade -- npx -y pcade-mcp
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Any client (via add-mcp)
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
npx add-mcp pcade-mcp
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
After adding the config, **fully quit and reopen your editor**. MCP servers
|
|
87
|
+
load at startup.
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Authenticated Setup (higher limits)
|
|
92
|
+
|
|
93
|
+
Get an API key at <https://studio.ppcadeditor.com/dashboard/settings>, then
|
|
94
|
+
pass it via the `PCADE_API_KEY` environment variable.
|
|
95
|
+
|
|
96
|
+
### Claude Desktop
|
|
97
|
+
|
|
98
|
+
```json
|
|
99
|
+
{
|
|
100
|
+
"mcpServers": {
|
|
101
|
+
"pcade": {
|
|
102
|
+
"command": "npx",
|
|
103
|
+
"args": ["-y", "pcade-mcp"],
|
|
104
|
+
"env": { "PCADE_API_KEY": "pcade_live_xxx" }
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Claude Desktop (Windows)
|
|
111
|
+
|
|
112
|
+
```json
|
|
113
|
+
{
|
|
114
|
+
"mcpServers": {
|
|
115
|
+
"pcade": {
|
|
116
|
+
"command": "cmd",
|
|
117
|
+
"args": ["/c", "npx", "-y", "pcade-mcp"],
|
|
118
|
+
"env": { "PCADE_API_KEY": "pcade_live_xxx" }
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Cursor
|
|
125
|
+
|
|
126
|
+
```json
|
|
127
|
+
{
|
|
128
|
+
"mcpServers": {
|
|
129
|
+
"pcade": {
|
|
130
|
+
"command": "npx",
|
|
131
|
+
"args": ["-y", "pcade-mcp"],
|
|
132
|
+
"env": { "PCADE_API_KEY": "pcade_live_xxx" }
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Windsurf
|
|
139
|
+
|
|
140
|
+
```json
|
|
141
|
+
{
|
|
142
|
+
"mcpServers": {
|
|
143
|
+
"pcade": {
|
|
144
|
+
"command": "npx",
|
|
145
|
+
"args": ["-y", "pcade-mcp"],
|
|
146
|
+
"env": { "PCADE_API_KEY": "pcade_live_xxx" }
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Claude Code
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
claude mcp add pcade --env PCADE_API_KEY=pcade_live_xxx -- npx -y pcade-mcp
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Any client (via add-mcp)
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
npx add-mcp pcade-mcp --env PCADE_API_KEY=pcade_live_xxx
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
## Faster startup (optional)
|
|
167
|
+
|
|
168
|
+
`npx` downloads the package on first use. For faster startup on repeated
|
|
169
|
+
launches, install globally:
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
npm i -g pcade-mcp
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Then use `pcade-mcp` directly in your config instead of `npx -y pcade-mcp`.
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
## Available tools
|
|
180
|
+
|
|
181
|
+
| Tool | Description |
|
|
182
|
+
|------|-------------|
|
|
183
|
+
| `get_ad_formats` | Returns ad format specs — field counts, character limits, supported media. Call this first. |
|
|
184
|
+
| `create_and_share` | Creates ad previews from structured content and returns a shareable link. |
|
|
185
|
+
|
|
186
|
+
### Example conversation
|
|
187
|
+
|
|
188
|
+
> **You:** Create Google Search ads for a Portland bakery at portlandsourdough.com
|
|
189
|
+
>
|
|
190
|
+
> **AI:** _(calls `get_ad_formats` to learn RSA constraints, then `create_and_share`)_
|
|
191
|
+
>
|
|
192
|
+
> Here's your ad preview: https://studio.ppcadeditor.com/shared/abc123
|
|
193
|
+
> You can edit it in the dashboard: https://studio.ppcadeditor.com/dashboard/...
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## Rate limits
|
|
198
|
+
|
|
199
|
+
| Mode | Previews | Share link expiry |
|
|
200
|
+
|------|----------|-------------------|
|
|
201
|
+
| Guest (no API key) | 3 per day | 24 hours |
|
|
202
|
+
| Free account | 20 total | 7 days |
|
|
203
|
+
| Paid account | Plan-based | Permanent |
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
## CLI options
|
|
208
|
+
|
|
209
|
+
```
|
|
210
|
+
--api-key <key> API key (prefer PCADE_API_KEY env var)
|
|
211
|
+
--endpoint <url> Override server URL
|
|
212
|
+
--debug Log requests/responses to stderr
|
|
213
|
+
--help Show help
|
|
214
|
+
--version Show version
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
## Troubleshooting
|
|
220
|
+
|
|
221
|
+
**Tools not showing up?**
|
|
222
|
+
Fully quit and reopen your editor (not just close the tab). MCP servers only
|
|
223
|
+
load at startup.
|
|
224
|
+
|
|
225
|
+
**Claude Desktop: "npx: command not found"?**
|
|
226
|
+
Install [Node.js](https://nodejs.org) (v18.13+), then fully quit and reopen
|
|
227
|
+
Claude Desktop.
|
|
228
|
+
|
|
229
|
+
**Timeout or WAF errors?**
|
|
230
|
+
The server may be temporarily behind a security checkpoint. Wait a minute and
|
|
231
|
+
retry. If persistent, run with `--debug` for diagnostics.
|
|
232
|
+
|
|
233
|
+
**Slow startup?**
|
|
234
|
+
`npx` downloads on first run. Install globally with `npm i -g pcade-mcp` for
|
|
235
|
+
instant startup.
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
## Security
|
|
240
|
+
|
|
241
|
+
- **Use env vars for API keys** — the `PCADE_API_KEY` environment variable is
|
|
242
|
+
not visible in process listings, unlike CLI flags.
|
|
243
|
+
- **Zero dependencies** — no supply chain risk from third-party packages.
|
|
244
|
+
- **Credentials stay safe** — API keys are never sent to custom endpoints
|
|
245
|
+
(only to the default PPC Ad Editor server).
|
|
246
|
+
- **No data stored locally** — the proxy is stateless; nothing is written to
|
|
247
|
+
disk.
|
|
248
|
+
|
|
249
|
+
---
|
|
250
|
+
|
|
251
|
+
## Links
|
|
252
|
+
|
|
253
|
+
- [PPC Ad Editor](https://www.ppcadeditor.com)
|
|
254
|
+
- [Get an API key](https://studio.ppcadeditor.com/dashboard/settings)
|
|
255
|
+
- [MCP Protocol](https://modelcontextprotocol.io)
|
package/bin/cli.mjs
ADDED
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createInterface } from "readline";
|
|
3
|
+
import { createRequire } from "module";
|
|
4
|
+
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
const { version } = require("../package.json");
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Constants
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
const DEFAULT_ENDPOINT = "https://studio.ppcadeditor.com/api/mcp";
|
|
13
|
+
const FETCH_TIMEOUT_MS = 15_000;
|
|
14
|
+
const MAX_LINE_BYTES = 262_144; // 256 KB — server caps at 32 KB anyway
|
|
15
|
+
const MAX_CONCURRENT = 5;
|
|
16
|
+
const SECRET_PATTERN = /pcade_(live|test)_[A-Za-z0-9_\-+/=]+/g;
|
|
17
|
+
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// Argument parsing (zero-dep)
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
const args = process.argv.slice(2);
|
|
23
|
+
|
|
24
|
+
function flag(name) {
|
|
25
|
+
return args.includes(`--${name}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function flagValue(name) {
|
|
29
|
+
const idx = args.indexOf(`--${name}`);
|
|
30
|
+
if (idx === -1 || idx + 1 >= args.length) return undefined;
|
|
31
|
+
return args[idx + 1];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const showHelp = flag("help") || flag("h");
|
|
35
|
+
const showVersion = flag("version") || flag("v");
|
|
36
|
+
const debug = flag("debug");
|
|
37
|
+
const cliApiKey = flagValue("api-key");
|
|
38
|
+
const cliEndpoint = flagValue("endpoint");
|
|
39
|
+
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
// --help / --version → print and exit
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
if (showVersion) {
|
|
45
|
+
process.stdout.write(`${version}\n`);
|
|
46
|
+
process.exit(0);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (showHelp) {
|
|
50
|
+
process.stdout.write(`pcade-mcp v${version} — MCP server for PPC Ad Editor
|
|
51
|
+
|
|
52
|
+
Create professional Google, Meta & LinkedIn ad previews from any AI assistant.
|
|
53
|
+
|
|
54
|
+
Usage:
|
|
55
|
+
npx pcade-mcp [options]
|
|
56
|
+
|
|
57
|
+
Options:
|
|
58
|
+
--api-key <key> API key (or set PCADE_API_KEY env var — recommended)
|
|
59
|
+
--endpoint <url> Override server URL (default: ${DEFAULT_ENDPOINT})
|
|
60
|
+
--debug Log all requests/responses to stderr
|
|
61
|
+
--help Show this help message
|
|
62
|
+
--version Show version number
|
|
63
|
+
|
|
64
|
+
Setup — Claude Desktop (~/.claude/claude_desktop_config.json):
|
|
65
|
+
{
|
|
66
|
+
"mcpServers": {
|
|
67
|
+
"pcade": {
|
|
68
|
+
"command": "npx",
|
|
69
|
+
"args": ["-y", "pcade-mcp"],
|
|
70
|
+
"env": { "PCADE_API_KEY": "pcade_live_xxx" }
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
Setup — Claude Desktop on Windows:
|
|
76
|
+
{
|
|
77
|
+
"mcpServers": {
|
|
78
|
+
"pcade": {
|
|
79
|
+
"command": "cmd",
|
|
80
|
+
"args": ["/c", "npx", "-y", "pcade-mcp"],
|
|
81
|
+
"env": { "PCADE_API_KEY": "pcade_live_xxx" }
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
Setup — Cursor (.cursor/mcp.json):
|
|
87
|
+
{
|
|
88
|
+
"mcpServers": {
|
|
89
|
+
"pcade": {
|
|
90
|
+
"command": "npx",
|
|
91
|
+
"args": ["-y", "pcade-mcp"],
|
|
92
|
+
"env": { "PCADE_API_KEY": "pcade_live_xxx" }
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
Setup — Windsurf (~/.codeium/windsurf/mcp_config.json):
|
|
98
|
+
{
|
|
99
|
+
"mcpServers": {
|
|
100
|
+
"pcade": {
|
|
101
|
+
"command": "npx",
|
|
102
|
+
"args": ["-y", "pcade-mcp"],
|
|
103
|
+
"env": { "PCADE_API_KEY": "pcade_live_xxx" }
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
Setup — Claude Code:
|
|
109
|
+
claude mcp add pcade -- npx -y pcade-mcp
|
|
110
|
+
claude mcp add pcade --env PCADE_API_KEY=pcade_live_xxx -- npx -y pcade-mcp
|
|
111
|
+
|
|
112
|
+
Setup — Universal (via add-mcp, any client):
|
|
113
|
+
npx add-mcp pcade-mcp
|
|
114
|
+
npx add-mcp pcade-mcp --env PCADE_API_KEY=pcade_live_xxx
|
|
115
|
+
|
|
116
|
+
Guest mode (no API key — 3 previews/day):
|
|
117
|
+
{
|
|
118
|
+
"mcpServers": {
|
|
119
|
+
"pcade": {
|
|
120
|
+
"command": "npx",
|
|
121
|
+
"args": ["-y", "pcade-mcp"]
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
Get an API key: https://studio.ppcadeditor.com/dashboard/settings
|
|
127
|
+
`);
|
|
128
|
+
process.exit(0);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ---------------------------------------------------------------------------
|
|
132
|
+
// Resolve auth + endpoint
|
|
133
|
+
// ---------------------------------------------------------------------------
|
|
134
|
+
|
|
135
|
+
const apiKey =
|
|
136
|
+
cliApiKey ||
|
|
137
|
+
process.env.PCADE_API_KEY ||
|
|
138
|
+
process.env.MCP_PROXY_API_KEY ||
|
|
139
|
+
null;
|
|
140
|
+
|
|
141
|
+
const endpoint =
|
|
142
|
+
cliEndpoint ||
|
|
143
|
+
process.env.MCP_PROXY_URL ||
|
|
144
|
+
DEFAULT_ENDPOINT;
|
|
145
|
+
|
|
146
|
+
const isCustomEndpoint = endpoint !== DEFAULT_ENDPOINT;
|
|
147
|
+
|
|
148
|
+
// S1: Strip auth on custom endpoints to prevent credential exfiltration
|
|
149
|
+
const sendAuth = apiKey && !isCustomEndpoint;
|
|
150
|
+
|
|
151
|
+
// Vercel WAF bypass (internal use only — not documented publicly)
|
|
152
|
+
const vercelBypass = process.env.VERCEL_PROTECTION_BYPASS || null;
|
|
153
|
+
|
|
154
|
+
// ---------------------------------------------------------------------------
|
|
155
|
+
// Logging helpers
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
|
|
158
|
+
function redact(str) {
|
|
159
|
+
return String(str).replace(SECRET_PATTERN, (m) => m.slice(0, 11) + "***REDACTED***");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function log(msg) {
|
|
163
|
+
process.stderr.write(`[pcade-mcp] ${redact(msg)}\n`);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function debugLog(msg) {
|
|
167
|
+
if (debug) log(`[debug] ${msg}`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ---------------------------------------------------------------------------
|
|
171
|
+
// Startup banner
|
|
172
|
+
// ---------------------------------------------------------------------------
|
|
173
|
+
|
|
174
|
+
log(`v${version}`);
|
|
175
|
+
log(`Endpoint: ${endpoint}`);
|
|
176
|
+
|
|
177
|
+
if (sendAuth) {
|
|
178
|
+
log(`Auth: API key (${apiKey.slice(0, 11)}...)`);
|
|
179
|
+
} else if (apiKey && isCustomEndpoint) {
|
|
180
|
+
log("Auth: withheld (custom endpoint)");
|
|
181
|
+
} else {
|
|
182
|
+
log("Running in guest mode (3 previews/day). Set PCADE_API_KEY for higher limits.");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// S5: Warn when API key passed via CLI flag (visible in process listings)
|
|
186
|
+
if (cliApiKey) {
|
|
187
|
+
log("WARNING: API key passed via --api-key flag is visible in process listings. Use PCADE_API_KEY env var instead.");
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// S1: Warn if custom endpoint is used with an API key
|
|
191
|
+
if (apiKey && isCustomEndpoint) {
|
|
192
|
+
log(`WARNING: Custom endpoint detected. API key will NOT be sent to ${endpoint}. Use the default endpoint or set PCADE_API_KEY on the target server.`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
log("Ready — waiting for MCP client requests on stdin");
|
|
196
|
+
|
|
197
|
+
// ---------------------------------------------------------------------------
|
|
198
|
+
// Session tracking
|
|
199
|
+
// ---------------------------------------------------------------------------
|
|
200
|
+
|
|
201
|
+
let mcpSessionId = null;
|
|
202
|
+
|
|
203
|
+
// ---------------------------------------------------------------------------
|
|
204
|
+
// Concurrency limiter
|
|
205
|
+
// ---------------------------------------------------------------------------
|
|
206
|
+
|
|
207
|
+
let inFlight = 0;
|
|
208
|
+
const waiting = [];
|
|
209
|
+
|
|
210
|
+
function acquireSlot() {
|
|
211
|
+
if (inFlight < MAX_CONCURRENT) {
|
|
212
|
+
inFlight++;
|
|
213
|
+
return Promise.resolve();
|
|
214
|
+
}
|
|
215
|
+
return new Promise((resolve) => waiting.push(resolve));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function releaseSlot() {
|
|
219
|
+
inFlight--;
|
|
220
|
+
if (waiting.length > 0) {
|
|
221
|
+
inFlight++;
|
|
222
|
+
waiting.shift()();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// ---------------------------------------------------------------------------
|
|
227
|
+
// JSON-RPC helpers
|
|
228
|
+
// ---------------------------------------------------------------------------
|
|
229
|
+
|
|
230
|
+
function jsonRpcError(id, code, message, data) {
|
|
231
|
+
const err = { jsonrpc: "2.0", id: id ?? null, error: { code, message } };
|
|
232
|
+
if (data !== undefined) err.error.data = data;
|
|
233
|
+
return err;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function send(obj) {
|
|
237
|
+
process.stdout.write(JSON.stringify(obj) + "\n");
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// ---------------------------------------------------------------------------
|
|
241
|
+
// Core proxy loop
|
|
242
|
+
// ---------------------------------------------------------------------------
|
|
243
|
+
|
|
244
|
+
const rl = createInterface({ input: process.stdin });
|
|
245
|
+
|
|
246
|
+
rl.on("line", async (line) => {
|
|
247
|
+
// S3: Reject oversized input
|
|
248
|
+
if (Buffer.byteLength(line) > MAX_LINE_BYTES) {
|
|
249
|
+
log(`Rejected oversized input (${Buffer.byteLength(line)} bytes, max ${MAX_LINE_BYTES})`);
|
|
250
|
+
send(jsonRpcError(null, -32600, `Request too large (max ${MAX_LINE_BYTES / 1024}KB)`));
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
let request;
|
|
255
|
+
try {
|
|
256
|
+
request = JSON.parse(line);
|
|
257
|
+
} catch {
|
|
258
|
+
log(`Invalid JSON received`);
|
|
259
|
+
debugLog(`Parse error on: ${line.slice(0, 200)}`);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Skip notifications (no id = no response expected)
|
|
264
|
+
if (
|
|
265
|
+
request.method?.startsWith("notifications/") ||
|
|
266
|
+
request.id === undefined ||
|
|
267
|
+
request.id === null
|
|
268
|
+
) {
|
|
269
|
+
debugLog(`Skipping notification: ${request.method}`);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
debugLog(`→ ${request.method} (id: ${request.id})`);
|
|
274
|
+
|
|
275
|
+
await acquireSlot();
|
|
276
|
+
|
|
277
|
+
const ac = new AbortController();
|
|
278
|
+
const timer = setTimeout(() => ac.abort(), FETCH_TIMEOUT_MS);
|
|
279
|
+
|
|
280
|
+
const headers = {
|
|
281
|
+
"Content-Type": "application/json",
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
if (sendAuth) {
|
|
285
|
+
headers["Authorization"] = `Bearer ${apiKey}`;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (vercelBypass) {
|
|
289
|
+
headers["x-vercel-protection-bypass"] = vercelBypass;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (mcpSessionId) {
|
|
293
|
+
headers["Mcp-Session-Id"] = mcpSessionId;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
try {
|
|
297
|
+
const response = await fetch(endpoint, {
|
|
298
|
+
method: "POST",
|
|
299
|
+
headers,
|
|
300
|
+
body: JSON.stringify(request),
|
|
301
|
+
signal: ac.signal,
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
clearTimeout(timer);
|
|
305
|
+
|
|
306
|
+
if (response.status === 204) {
|
|
307
|
+
debugLog(`← 204 No Content`);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const contentType = response.headers.get("content-type") || "";
|
|
312
|
+
|
|
313
|
+
// WAF / non-JSON detection
|
|
314
|
+
if (!contentType.includes("application/json")) {
|
|
315
|
+
const snippet = (await response.text()).slice(0, 200);
|
|
316
|
+
const isWaf =
|
|
317
|
+
response.status === 429 ||
|
|
318
|
+
response.status === 403 ||
|
|
319
|
+
snippet.includes("Vercel Security");
|
|
320
|
+
|
|
321
|
+
log(
|
|
322
|
+
`Non-JSON response ${response.status} (${contentType}): ${snippet.slice(0, 100)}`,
|
|
323
|
+
);
|
|
324
|
+
|
|
325
|
+
send(
|
|
326
|
+
jsonRpcError(
|
|
327
|
+
request.id,
|
|
328
|
+
-32603,
|
|
329
|
+
isWaf
|
|
330
|
+
? `WAF blocked (HTTP ${response.status}). The Vercel security checkpoint is active. Wait a moment and retry.`
|
|
331
|
+
: `Upstream returned HTTP ${response.status} with non-JSON body`,
|
|
332
|
+
{ http_status: response.status, content_type: contentType },
|
|
333
|
+
),
|
|
334
|
+
);
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// HTTP error with JSON body
|
|
339
|
+
if (response.status >= 400) {
|
|
340
|
+
let body;
|
|
341
|
+
try {
|
|
342
|
+
body = await response.json();
|
|
343
|
+
} catch {
|
|
344
|
+
body = null;
|
|
345
|
+
}
|
|
346
|
+
log(`HTTP ${response.status}: ${redact(JSON.stringify(body)).slice(0, 200)}`);
|
|
347
|
+
send(
|
|
348
|
+
jsonRpcError(
|
|
349
|
+
request.id,
|
|
350
|
+
-32603,
|
|
351
|
+
`Upstream HTTP ${response.status}: ${body?.message || body?.error || "Unknown error"}`,
|
|
352
|
+
{ http_status: response.status, upstream: body },
|
|
353
|
+
),
|
|
354
|
+
);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Track session
|
|
359
|
+
const newSessionId = response.headers.get("mcp-session-id");
|
|
360
|
+
if (newSessionId) {
|
|
361
|
+
mcpSessionId = newSessionId;
|
|
362
|
+
debugLog(`Session: ${mcpSessionId}`);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const result = await response.json();
|
|
366
|
+
|
|
367
|
+
// Fix null id from server when client sent a real id
|
|
368
|
+
if (
|
|
369
|
+
result.id === null &&
|
|
370
|
+
request.id !== undefined &&
|
|
371
|
+
request.id !== null
|
|
372
|
+
) {
|
|
373
|
+
result.id = request.id;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
debugLog(`← ${request.method} (${response.status})`);
|
|
377
|
+
|
|
378
|
+
// Surface guest rate limit hits to stderr for visibility
|
|
379
|
+
if (result?.result?.isError && result?.result?.content?.[0]?.text) {
|
|
380
|
+
const text = result.result.content[0].text;
|
|
381
|
+
if (text.includes("guest_limit_reached") || text.includes("3 previews")) {
|
|
382
|
+
log("Guest limit reached (3/3 today). Get an API key: https://studio.ppcadeditor.com/dashboard/settings");
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
send(result);
|
|
387
|
+
} catch (err) {
|
|
388
|
+
clearTimeout(timer);
|
|
389
|
+
const isTimeout = err.name === "AbortError";
|
|
390
|
+
log(
|
|
391
|
+
`${isTimeout ? "Timeout" : "Fetch error"}: ${redact(String(err))}`,
|
|
392
|
+
);
|
|
393
|
+
send(
|
|
394
|
+
jsonRpcError(
|
|
395
|
+
request.id,
|
|
396
|
+
isTimeout ? -32001 : -32603,
|
|
397
|
+
isTimeout
|
|
398
|
+
? `Request timed out after ${FETCH_TIMEOUT_MS / 1000}s — endpoint may be down or WAF is blocking`
|
|
399
|
+
: `Network error: ${err.message}`,
|
|
400
|
+
{ error_type: isTimeout ? "timeout" : "network" },
|
|
401
|
+
),
|
|
402
|
+
);
|
|
403
|
+
} finally {
|
|
404
|
+
releaseSlot();
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
// ---------------------------------------------------------------------------
|
|
409
|
+
// Graceful shutdown
|
|
410
|
+
// ---------------------------------------------------------------------------
|
|
411
|
+
|
|
412
|
+
let stdinClosed = false;
|
|
413
|
+
|
|
414
|
+
function exitWhenIdle() {
|
|
415
|
+
if (inFlight === 0) {
|
|
416
|
+
process.exit(0);
|
|
417
|
+
}
|
|
418
|
+
// In-flight requests pending — they will call releaseSlot → re-check
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const origRelease = releaseSlot;
|
|
422
|
+
releaseSlot = function () {
|
|
423
|
+
origRelease();
|
|
424
|
+
if (stdinClosed) exitWhenIdle();
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
rl.on("close", () => {
|
|
428
|
+
stdinClosed = true;
|
|
429
|
+
debugLog("stdin closed — draining in-flight requests");
|
|
430
|
+
exitWhenIdle();
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
process.on("SIGINT", () => {
|
|
434
|
+
debugLog("SIGINT received — exiting");
|
|
435
|
+
process.exit(0);
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
process.on("SIGTERM", () => {
|
|
439
|
+
debugLog("SIGTERM received — exiting");
|
|
440
|
+
process.exit(0);
|
|
441
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pcade-mcp",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "MCP server for PPC Ad Editor — create Google, Meta & LinkedIn ad previews from AI assistants",
|
|
5
|
+
"bin": {
|
|
6
|
+
"pcade-mcp": "bin/cli.mjs"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=18.13"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"bin/",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"mcp",
|
|
19
|
+
"model-context-protocol",
|
|
20
|
+
"ppc",
|
|
21
|
+
"google-ads",
|
|
22
|
+
"meta-ads",
|
|
23
|
+
"linkedin-ads",
|
|
24
|
+
"ad-preview",
|
|
25
|
+
"claude",
|
|
26
|
+
"cursor",
|
|
27
|
+
"ai-tools"
|
|
28
|
+
],
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"homepage": "https://www.ppcadeditor.com",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/ppcadeditor/pcade2-sip.git",
|
|
34
|
+
"directory": "packages/mcp-proxy"
|
|
35
|
+
}
|
|
36
|
+
}
|