@riddledc/riddle-mcp 0.2.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 RiddleDC
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,57 @@
1
+ # @riddledc/riddle-mcp
2
+
3
+ MCP-related utilities for RiddleDC. No secrets. No network calls unless you run the server or invoke tools.
4
+
5
+ ## Install
6
+
7
+ ```
8
+ pnpm add @riddledc/riddle-mcp
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { loadConfigFromEnv, redactSecrets } from "@riddledc/riddle-mcp";
15
+
16
+ const config = loadConfigFromEnv();
17
+ console.log(redactSecrets(config));
18
+ ```
19
+
20
+ ## MCP server (CLI)
21
+
22
+ Install and run:
23
+
24
+ ```
25
+ pnpm add @riddledc/riddle-mcp
26
+ npx riddle-mcp
27
+ ```
28
+
29
+ ### MCP config example
30
+
31
+ ```json
32
+ {
33
+ "mcpServers": {
34
+ "riddle": {
35
+ "command": "npx",
36
+ "args": ["riddle-mcp"],
37
+ "env": {
38
+ "RIDDLE_MCP_GATEWAY_URL": "https://api.riddledc.com",
39
+ "RIDDLE_AUTH_TOKEN": "your_login_token_here"
40
+ }
41
+ }
42
+ }
43
+ }
44
+ ```
45
+
46
+ ## Environment variables
47
+
48
+ - `RIDDLE_BASE_URL`
49
+ - `RIDDLE_API_URL`
50
+ - `RIDDLE_TOKEN`
51
+ - `RIDDLE_MCP_GATEWAY_URL`
52
+ - `RIDDLE_AUTH_TOKEN`
53
+ - `RIDDLE_API_KEY`
54
+
55
+ ## Notes
56
+ - Supply credentials via env vars or your own config layer.
57
+ - No assumptions about other integrations.
package/dist/index.cjs ADDED
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ loadConfigFromEnv: () => loadConfigFromEnv,
24
+ redactSecrets: () => redactSecrets
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var DEFAULT_ENV_NAMES = {
28
+ baseUrl: "RIDDLE_BASE_URL",
29
+ apiUrl: "RIDDLE_API_URL",
30
+ token: "RIDDLE_TOKEN"
31
+ };
32
+ function loadConfigFromEnv(envNames = {}) {
33
+ const names = { ...DEFAULT_ENV_NAMES, ...envNames };
34
+ return {
35
+ baseUrl: process.env[names.baseUrl],
36
+ apiUrl: process.env[names.apiUrl],
37
+ token: process.env[names.token]
38
+ };
39
+ }
40
+ function redactSecrets(config) {
41
+ return {
42
+ baseUrl: config.baseUrl,
43
+ apiUrl: config.apiUrl,
44
+ token: config.token ? "[REDACTED]" : void 0
45
+ };
46
+ }
47
+ // Annotate the CommonJS export names for ESM import in node:
48
+ 0 && (module.exports = {
49
+ loadConfigFromEnv,
50
+ redactSecrets
51
+ });
@@ -0,0 +1,14 @@
1
+ type RiddleMcpConfig = {
2
+ baseUrl?: string;
3
+ apiUrl?: string;
4
+ token?: string;
5
+ };
6
+ type RiddleEnvNames = {
7
+ baseUrl: string;
8
+ apiUrl: string;
9
+ token: string;
10
+ };
11
+ declare function loadConfigFromEnv(envNames?: Partial<RiddleEnvNames>): RiddleMcpConfig;
12
+ declare function redactSecrets(config: RiddleMcpConfig): RiddleMcpConfig;
13
+
14
+ export { type RiddleEnvNames, type RiddleMcpConfig, loadConfigFromEnv, redactSecrets };
@@ -0,0 +1,14 @@
1
+ type RiddleMcpConfig = {
2
+ baseUrl?: string;
3
+ apiUrl?: string;
4
+ token?: string;
5
+ };
6
+ type RiddleEnvNames = {
7
+ baseUrl: string;
8
+ apiUrl: string;
9
+ token: string;
10
+ };
11
+ declare function loadConfigFromEnv(envNames?: Partial<RiddleEnvNames>): RiddleMcpConfig;
12
+ declare function redactSecrets(config: RiddleMcpConfig): RiddleMcpConfig;
13
+
14
+ export { type RiddleEnvNames, type RiddleMcpConfig, loadConfigFromEnv, redactSecrets };
package/dist/index.js ADDED
@@ -0,0 +1,25 @@
1
+ // src/index.ts
2
+ var DEFAULT_ENV_NAMES = {
3
+ baseUrl: "RIDDLE_BASE_URL",
4
+ apiUrl: "RIDDLE_API_URL",
5
+ token: "RIDDLE_TOKEN"
6
+ };
7
+ function loadConfigFromEnv(envNames = {}) {
8
+ const names = { ...DEFAULT_ENV_NAMES, ...envNames };
9
+ return {
10
+ baseUrl: process.env[names.baseUrl],
11
+ apiUrl: process.env[names.apiUrl],
12
+ token: process.env[names.token]
13
+ };
14
+ }
15
+ function redactSecrets(config) {
16
+ return {
17
+ baseUrl: config.baseUrl,
18
+ apiUrl: config.apiUrl,
19
+ token: config.token ? "[REDACTED]" : void 0
20
+ };
21
+ }
22
+ export {
23
+ loadConfigFromEnv,
24
+ redactSecrets
25
+ };
@@ -0,0 +1,436 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ // src/server.ts
5
+ var import_server = require("@modelcontextprotocol/sdk/server/index.js");
6
+ var import_stdio = require("@modelcontextprotocol/sdk/server/stdio.js");
7
+ var import_types = require("@modelcontextprotocol/sdk/types.js");
8
+ var import_fs = require("fs");
9
+ var DEFAULT_GATEWAY_URL = "https://api.riddledc.com";
10
+ var RIDDLE_API = process.env.RIDDLE_MCP_GATEWAY_URL || DEFAULT_GATEWAY_URL;
11
+ function saveToTmp(buffer, name) {
12
+ const path = `/tmp/${name}.png`;
13
+ (0, import_fs.writeFileSync)(path, buffer);
14
+ return path;
15
+ }
16
+ var RiddleClient = class {
17
+ constructor() {
18
+ this.token = process.env.RIDDLE_AUTH_TOKEN || process.env.RIDDLE_API_KEY;
19
+ if (!this.token) {
20
+ console.error("Warning: RIDDLE_AUTH_TOKEN or RIDDLE_API_KEY not set");
21
+ }
22
+ }
23
+ async screenshotSync(url, viewport) {
24
+ const response = await fetch(`${RIDDLE_API}/v1/run`, {
25
+ method: "POST",
26
+ headers: {
27
+ Authorization: `Bearer ${this.token ?? ""}`,
28
+ "Content-Type": "application/json"
29
+ },
30
+ body: JSON.stringify({ url, options: { viewport } })
31
+ });
32
+ if (!response.ok) {
33
+ if (response.status === 408) {
34
+ const data = await response.json().catch(() => null);
35
+ const jobId = data?.job_id;
36
+ if (!jobId) {
37
+ throw new Error(`API error: ${response.status}`);
38
+ }
39
+ await this.waitForJob(jobId);
40
+ const artifacts = await this.getArtifacts(jobId);
41
+ const png = artifacts.artifacts?.find((a) => a.name?.endsWith(".png"));
42
+ if (!png?.url) {
43
+ throw new Error("No screenshot artifact found");
44
+ }
45
+ const buffer2 = await this.downloadArtifact(png.url);
46
+ return Buffer.from(buffer2).toString("base64");
47
+ }
48
+ const text = await response.text().catch(() => "");
49
+ throw new Error(`API error: ${response.status}${text ? `: ${text}` : ""}`);
50
+ }
51
+ const buffer = await response.arrayBuffer();
52
+ return Buffer.from(buffer).toString("base64");
53
+ }
54
+ async runScript(url, script, viewport) {
55
+ const response = await fetch(`${RIDDLE_API}/v1/run`, {
56
+ method: "POST",
57
+ headers: {
58
+ Authorization: `Bearer ${this.token ?? ""}`,
59
+ "Content-Type": "application/json"
60
+ },
61
+ body: JSON.stringify({ url, script, viewport, sync: false })
62
+ });
63
+ if (!response.ok) {
64
+ throw new Error(`API error: ${response.status}`);
65
+ }
66
+ return response.json();
67
+ }
68
+ async getJob(jobId) {
69
+ const response = await fetch(`${RIDDLE_API}/v1/jobs/${jobId}`, {
70
+ headers: { Authorization: `Bearer ${this.token ?? ""}` }
71
+ });
72
+ return response.json();
73
+ }
74
+ async getArtifacts(jobId) {
75
+ const response = await fetch(`${RIDDLE_API}/v1/jobs/${jobId}/artifacts`, {
76
+ headers: { Authorization: `Bearer ${this.token ?? ""}` }
77
+ });
78
+ return response.json();
79
+ }
80
+ async waitForJob(jobId, maxAttempts = 30, intervalMs = 2e3) {
81
+ for (let i = 0; i < maxAttempts; i += 1) {
82
+ const job = await this.getJob(jobId);
83
+ if (job.status === "completed" || job.status === "complete") {
84
+ return job;
85
+ }
86
+ if (job.status === "failed") {
87
+ throw new Error(`Job failed: ${job.error || "Unknown error"}`);
88
+ }
89
+ await new Promise((resolve) => setTimeout(resolve, intervalMs));
90
+ }
91
+ throw new Error("Job timed out");
92
+ }
93
+ async downloadArtifact(url) {
94
+ const response = await fetch(url);
95
+ if (!response.ok) throw new Error(`Download failed: ${response.status}`);
96
+ return Buffer.from(await response.arrayBuffer());
97
+ }
98
+ async runScriptSync(url, script, viewport, timeoutSec = 60) {
99
+ const response = await fetch(`${RIDDLE_API}/v1/run`, {
100
+ method: "POST",
101
+ headers: {
102
+ Authorization: `Bearer ${this.token ?? ""}`,
103
+ "Content-Type": "application/json"
104
+ },
105
+ body: JSON.stringify({
106
+ url,
107
+ script,
108
+ viewport,
109
+ sync: false,
110
+ timeout_sec: timeoutSec
111
+ })
112
+ });
113
+ if (!response.ok) {
114
+ const text = await response.text();
115
+ throw new Error(`API error ${response.status}: ${text}`);
116
+ }
117
+ const { job_id } = await response.json();
118
+ if (!job_id) throw new Error("No job_id returned");
119
+ await this.waitForJob(job_id);
120
+ const artifacts = await this.getArtifacts(job_id);
121
+ const results = [];
122
+ let consoleLogs = null;
123
+ let networkHar = null;
124
+ for (const artifact of artifacts.artifacts || []) {
125
+ if (artifact.url) {
126
+ const buffer = await this.downloadArtifact(artifact.url);
127
+ if (artifact.name === "console.json") {
128
+ try {
129
+ consoleLogs = JSON.parse(buffer.toString("utf8"));
130
+ } catch (error) {
131
+ consoleLogs = null;
132
+ }
133
+ } else if (artifact.name === "network.har") {
134
+ try {
135
+ networkHar = JSON.parse(buffer.toString("utf8"));
136
+ } catch (error) {
137
+ networkHar = null;
138
+ }
139
+ }
140
+ results.push({
141
+ name: artifact.name,
142
+ type: artifact.name?.endsWith(".png") ? "image" : "file",
143
+ buffer
144
+ });
145
+ }
146
+ }
147
+ return { job_id, artifacts: results, consoleLogs, networkHar };
148
+ }
149
+ };
150
+ var server = new import_server.Server({ name: "riddle-mcp-server", version: "1.0.0" }, { capabilities: { tools: {} } });
151
+ var client = new RiddleClient();
152
+ var devices = {
153
+ desktop: { width: 1280, height: 720, hasTouch: false },
154
+ ipad: { width: 820, height: 1180, hasTouch: true, isMobile: true },
155
+ iphone: { width: 390, height: 844, hasTouch: true, isMobile: true }
156
+ };
157
+ server.setRequestHandler(import_types.ListToolsRequestSchema, async () => ({
158
+ tools: [
159
+ {
160
+ name: "riddle_screenshot",
161
+ description: "Take a screenshot of a URL using the Riddle API. Returns base64-encoded PNG image.",
162
+ inputSchema: {
163
+ type: "object",
164
+ properties: {
165
+ url: { type: "string", description: "URL to screenshot" },
166
+ width: { type: "number", description: "Viewport width (default: 1280)" },
167
+ height: { type: "number", description: "Viewport height (default: 720)" },
168
+ device: {
169
+ type: "string",
170
+ enum: ["desktop", "ipad", "iphone"],
171
+ description: "Device preset (overrides width/height)"
172
+ }
173
+ },
174
+ required: ["url"]
175
+ }
176
+ },
177
+ {
178
+ name: "riddle_batch_screenshot",
179
+ description: "Screenshot multiple URLs. Returns array of base64 images.",
180
+ inputSchema: {
181
+ type: "object",
182
+ properties: {
183
+ urls: { type: "array", items: { type: "string" }, description: "URLs to screenshot" },
184
+ device: { type: "string", enum: ["desktop", "ipad", "iphone"] }
185
+ },
186
+ required: ["urls"]
187
+ }
188
+ },
189
+ {
190
+ name: "riddle_run_script",
191
+ description: "Run a Playwright script on a page (async). Returns job_id to check status later.",
192
+ inputSchema: {
193
+ type: "object",
194
+ properties: {
195
+ url: { type: "string", description: "Starting URL" },
196
+ script: { type: "string", description: "Playwright script (page object available)" },
197
+ width: { type: "number" },
198
+ height: { type: "number" }
199
+ },
200
+ required: ["url", "script"]
201
+ }
202
+ },
203
+ {
204
+ name: "riddle_get_job",
205
+ description: "Get status and artifacts of a Riddle job",
206
+ inputSchema: {
207
+ type: "object",
208
+ properties: {
209
+ job_id: { type: "string", description: "Job ID to check" }
210
+ },
211
+ required: ["job_id"]
212
+ }
213
+ },
214
+ {
215
+ name: "riddle_automate",
216
+ description: "Run a Playwright script, wait for completion, and return all artifacts. Includes console logs and network HAR. Full sync automation - one call does everything.",
217
+ inputSchema: {
218
+ type: "object",
219
+ properties: {
220
+ url: { type: "string", description: "Starting URL" },
221
+ script: {
222
+ type: "string",
223
+ description: "Playwright script. Use 'page' object. Example: await page.click('button'); await page.screenshot({path: 'result.png'});"
224
+ },
225
+ device: { type: "string", enum: ["desktop", "ipad", "iphone"], description: "Device preset" },
226
+ timeout_sec: { type: "number", description: "Max execution time in seconds (default: 60)" },
227
+ force_clicks: {
228
+ type: "boolean",
229
+ description: "Add { force: true } to all click() calls to bypass stability checks on animated elements (default: true)"
230
+ }
231
+ },
232
+ required: ["url", "script"]
233
+ }
234
+ },
235
+ {
236
+ name: "riddle_click_and_screenshot",
237
+ description: "Simple automation: load URL, click a selector, take screenshot. Good for testing button clicks, game starts, etc. Uses force-click by default to handle animated buttons.",
238
+ inputSchema: {
239
+ type: "object",
240
+ properties: {
241
+ url: { type: "string", description: "URL to load" },
242
+ click: { type: "string", description: "CSS selector to click (e.g., 'button.start', '.play-btn')" },
243
+ wait_ms: { type: "number", description: "Wait time after click before screenshot (default: 1000)" },
244
+ device: { type: "string", enum: ["desktop", "ipad", "iphone"] },
245
+ force: { type: "boolean", description: "Force click even on animating elements (default: true)" }
246
+ },
247
+ required: ["url", "click"]
248
+ }
249
+ }
250
+ ]
251
+ }));
252
+ server.setRequestHandler(import_types.CallToolRequestSchema, async (request) => {
253
+ const { name } = request.params;
254
+ const args = request.params.arguments ?? {};
255
+ try {
256
+ if (name === "riddle_screenshot") {
257
+ const device = args.device;
258
+ const viewport = device ? devices[device] : { width: args.width || 1280, height: args.height || 720 };
259
+ const base64 = await client.screenshotSync(String(args.url), viewport);
260
+ return {
261
+ content: [
262
+ {
263
+ type: "image",
264
+ data: base64,
265
+ mimeType: "image/png"
266
+ }
267
+ ]
268
+ };
269
+ }
270
+ if (name === "riddle_batch_screenshot") {
271
+ const device = args.device;
272
+ const viewport = device ? devices[device] : devices.desktop;
273
+ const results = [];
274
+ const urls = Array.isArray(args.urls) ? args.urls : [];
275
+ for (const url of urls) {
276
+ try {
277
+ const base64 = await client.screenshotSync(String(url), viewport);
278
+ results.push({ url, success: true, image: base64 });
279
+ } catch (error) {
280
+ results.push({ url: String(url), success: false, error: error.message });
281
+ }
282
+ }
283
+ return {
284
+ content: [
285
+ {
286
+ type: "text",
287
+ text: JSON.stringify(
288
+ results.map((result) => ({
289
+ url: result.url,
290
+ success: result.success,
291
+ error: result.error
292
+ })),
293
+ null,
294
+ 2
295
+ )
296
+ },
297
+ ...results.filter((result) => result.success).map((result) => ({
298
+ type: "image",
299
+ data: result.image,
300
+ mimeType: "image/png"
301
+ }))
302
+ ]
303
+ };
304
+ }
305
+ if (name === "riddle_run_script") {
306
+ const viewport = { width: args.width || 1280, height: args.height || 720 };
307
+ const result = await client.runScript(String(args.url), String(args.script), viewport);
308
+ return {
309
+ content: [{ type: "text", text: JSON.stringify(result, null, 2) }]
310
+ };
311
+ }
312
+ if (name === "riddle_get_job") {
313
+ const job = await client.getJob(String(args.job_id));
314
+ const artifacts = await client.getArtifacts(String(args.job_id));
315
+ return {
316
+ content: [
317
+ {
318
+ type: "text",
319
+ text: JSON.stringify({ job, artifacts }, null, 2)
320
+ }
321
+ ]
322
+ };
323
+ }
324
+ if (name === "riddle_automate") {
325
+ const device = args.device;
326
+ const viewport = device ? devices[device] : devices.desktop;
327
+ let script = String(args.script ?? "");
328
+ const forceClicks = args.force_clicks !== false;
329
+ if (forceClicks) {
330
+ script = script.replace(
331
+ /\.click\(\s*(['"`])([^'"`]+)\1\s*\)(?!\s*;?\s*\/\/\s*no-force)/g,
332
+ ".click($1$2$1, { force: true })"
333
+ );
334
+ }
335
+ const { job_id, artifacts, consoleLogs, networkHar } = await client.runScriptSync(
336
+ String(args.url),
337
+ script,
338
+ viewport,
339
+ args.timeout_sec || 60
340
+ );
341
+ const images = [];
342
+ const savedPaths = [];
343
+ for (const artifact of artifacts) {
344
+ if (artifact.type === "image") {
345
+ const base64 = artifact.buffer.toString("base64");
346
+ images.push({ type: "image", data: base64, mimeType: "image/png" });
347
+ const path = saveToTmp(artifact.buffer, artifact.name?.replace(".png", "") || "artifact");
348
+ savedPaths.push(path);
349
+ }
350
+ }
351
+ let consoleOutput = null;
352
+ if (consoleLogs?.entries) {
353
+ consoleOutput = {
354
+ summary: consoleLogs.summary,
355
+ logs: consoleLogs.entries.log?.slice(-20) || [],
356
+ errors: consoleLogs.entries.error || [],
357
+ warns: consoleLogs.entries.warn || []
358
+ };
359
+ }
360
+ let networkSummary = null;
361
+ if (networkHar?.log?.entries) {
362
+ const entries = networkHar.log.entries;
363
+ networkSummary = {
364
+ total_requests: entries.length,
365
+ failed: entries.filter((entry) => entry.response?.status >= 400).length,
366
+ requests: entries.slice(-10).map((entry) => ({
367
+ url: entry.request?.url?.substring(0, 80),
368
+ status: entry.response?.status,
369
+ time: entry.time
370
+ }))
371
+ };
372
+ }
373
+ return {
374
+ content: [
375
+ {
376
+ type: "text",
377
+ text: JSON.stringify(
378
+ {
379
+ job_id,
380
+ saved_to: savedPaths,
381
+ console: consoleOutput,
382
+ network: networkSummary
383
+ },
384
+ null,
385
+ 2
386
+ )
387
+ },
388
+ ...images
389
+ ]
390
+ };
391
+ }
392
+ if (name === "riddle_click_and_screenshot") {
393
+ const device = args.device;
394
+ const viewport = device ? devices[device] : devices.desktop;
395
+ const waitMs = args.wait_ms || 1e3;
396
+ const forceClick = args.force !== false;
397
+ const clickSelector = String(args.click ?? "");
398
+ const script = `
399
+ await page.waitForLoadState('networkidle');
400
+ await page.click('${clickSelector.replace(/'/g, "\\'")}', { force: ${forceClick} });
401
+ await page.waitForTimeout(${waitMs});
402
+ await page.screenshot({ path: 'after-click.png', fullPage: false });
403
+ `;
404
+ const { job_id, artifacts } = await client.runScriptSync(String(args.url), script, viewport, 30);
405
+ const images = [];
406
+ for (const artifact of artifacts) {
407
+ if (artifact.type === "image") {
408
+ const base64 = artifact.buffer.toString("base64");
409
+ images.push({ type: "image", data: base64, mimeType: "image/png" });
410
+ saveToTmp(artifact.buffer, "click-result");
411
+ }
412
+ }
413
+ return {
414
+ content: [
415
+ {
416
+ type: "text",
417
+ text: JSON.stringify({ job_id, clicked: clickSelector }, null, 2)
418
+ },
419
+ ...images
420
+ ]
421
+ };
422
+ }
423
+ throw new Error(`Unknown tool: ${name}`);
424
+ } catch (error) {
425
+ return {
426
+ content: [{ type: "text", text: `Error: ${error.message}` }],
427
+ isError: true
428
+ };
429
+ }
430
+ });
431
+ async function main() {
432
+ const transport = new import_stdio.StdioServerTransport();
433
+ await server.connect(transport);
434
+ console.error("Riddle MCP server running");
435
+ }
436
+ main().catch(console.error);
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
package/dist/server.js ADDED
@@ -0,0 +1,435 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/server.ts
4
+ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
5
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
6
+ import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
7
+ import { writeFileSync } from "fs";
8
+ var DEFAULT_GATEWAY_URL = "https://api.riddledc.com";
9
+ var RIDDLE_API = process.env.RIDDLE_MCP_GATEWAY_URL || DEFAULT_GATEWAY_URL;
10
+ function saveToTmp(buffer, name) {
11
+ const path = `/tmp/${name}.png`;
12
+ writeFileSync(path, buffer);
13
+ return path;
14
+ }
15
+ var RiddleClient = class {
16
+ constructor() {
17
+ this.token = process.env.RIDDLE_AUTH_TOKEN || process.env.RIDDLE_API_KEY;
18
+ if (!this.token) {
19
+ console.error("Warning: RIDDLE_AUTH_TOKEN or RIDDLE_API_KEY not set");
20
+ }
21
+ }
22
+ async screenshotSync(url, viewport) {
23
+ const response = await fetch(`${RIDDLE_API}/v1/run`, {
24
+ method: "POST",
25
+ headers: {
26
+ Authorization: `Bearer ${this.token ?? ""}`,
27
+ "Content-Type": "application/json"
28
+ },
29
+ body: JSON.stringify({ url, options: { viewport } })
30
+ });
31
+ if (!response.ok) {
32
+ if (response.status === 408) {
33
+ const data = await response.json().catch(() => null);
34
+ const jobId = data?.job_id;
35
+ if (!jobId) {
36
+ throw new Error(`API error: ${response.status}`);
37
+ }
38
+ await this.waitForJob(jobId);
39
+ const artifacts = await this.getArtifacts(jobId);
40
+ const png = artifacts.artifacts?.find((a) => a.name?.endsWith(".png"));
41
+ if (!png?.url) {
42
+ throw new Error("No screenshot artifact found");
43
+ }
44
+ const buffer2 = await this.downloadArtifact(png.url);
45
+ return Buffer.from(buffer2).toString("base64");
46
+ }
47
+ const text = await response.text().catch(() => "");
48
+ throw new Error(`API error: ${response.status}${text ? `: ${text}` : ""}`);
49
+ }
50
+ const buffer = await response.arrayBuffer();
51
+ return Buffer.from(buffer).toString("base64");
52
+ }
53
+ async runScript(url, script, viewport) {
54
+ const response = await fetch(`${RIDDLE_API}/v1/run`, {
55
+ method: "POST",
56
+ headers: {
57
+ Authorization: `Bearer ${this.token ?? ""}`,
58
+ "Content-Type": "application/json"
59
+ },
60
+ body: JSON.stringify({ url, script, viewport, sync: false })
61
+ });
62
+ if (!response.ok) {
63
+ throw new Error(`API error: ${response.status}`);
64
+ }
65
+ return response.json();
66
+ }
67
+ async getJob(jobId) {
68
+ const response = await fetch(`${RIDDLE_API}/v1/jobs/${jobId}`, {
69
+ headers: { Authorization: `Bearer ${this.token ?? ""}` }
70
+ });
71
+ return response.json();
72
+ }
73
+ async getArtifacts(jobId) {
74
+ const response = await fetch(`${RIDDLE_API}/v1/jobs/${jobId}/artifacts`, {
75
+ headers: { Authorization: `Bearer ${this.token ?? ""}` }
76
+ });
77
+ return response.json();
78
+ }
79
+ async waitForJob(jobId, maxAttempts = 30, intervalMs = 2e3) {
80
+ for (let i = 0; i < maxAttempts; i += 1) {
81
+ const job = await this.getJob(jobId);
82
+ if (job.status === "completed" || job.status === "complete") {
83
+ return job;
84
+ }
85
+ if (job.status === "failed") {
86
+ throw new Error(`Job failed: ${job.error || "Unknown error"}`);
87
+ }
88
+ await new Promise((resolve) => setTimeout(resolve, intervalMs));
89
+ }
90
+ throw new Error("Job timed out");
91
+ }
92
+ async downloadArtifact(url) {
93
+ const response = await fetch(url);
94
+ if (!response.ok) throw new Error(`Download failed: ${response.status}`);
95
+ return Buffer.from(await response.arrayBuffer());
96
+ }
97
+ async runScriptSync(url, script, viewport, timeoutSec = 60) {
98
+ const response = await fetch(`${RIDDLE_API}/v1/run`, {
99
+ method: "POST",
100
+ headers: {
101
+ Authorization: `Bearer ${this.token ?? ""}`,
102
+ "Content-Type": "application/json"
103
+ },
104
+ body: JSON.stringify({
105
+ url,
106
+ script,
107
+ viewport,
108
+ sync: false,
109
+ timeout_sec: timeoutSec
110
+ })
111
+ });
112
+ if (!response.ok) {
113
+ const text = await response.text();
114
+ throw new Error(`API error ${response.status}: ${text}`);
115
+ }
116
+ const { job_id } = await response.json();
117
+ if (!job_id) throw new Error("No job_id returned");
118
+ await this.waitForJob(job_id);
119
+ const artifacts = await this.getArtifacts(job_id);
120
+ const results = [];
121
+ let consoleLogs = null;
122
+ let networkHar = null;
123
+ for (const artifact of artifacts.artifacts || []) {
124
+ if (artifact.url) {
125
+ const buffer = await this.downloadArtifact(artifact.url);
126
+ if (artifact.name === "console.json") {
127
+ try {
128
+ consoleLogs = JSON.parse(buffer.toString("utf8"));
129
+ } catch (error) {
130
+ consoleLogs = null;
131
+ }
132
+ } else if (artifact.name === "network.har") {
133
+ try {
134
+ networkHar = JSON.parse(buffer.toString("utf8"));
135
+ } catch (error) {
136
+ networkHar = null;
137
+ }
138
+ }
139
+ results.push({
140
+ name: artifact.name,
141
+ type: artifact.name?.endsWith(".png") ? "image" : "file",
142
+ buffer
143
+ });
144
+ }
145
+ }
146
+ return { job_id, artifacts: results, consoleLogs, networkHar };
147
+ }
148
+ };
149
+ var server = new Server({ name: "riddle-mcp-server", version: "1.0.0" }, { capabilities: { tools: {} } });
150
+ var client = new RiddleClient();
151
+ var devices = {
152
+ desktop: { width: 1280, height: 720, hasTouch: false },
153
+ ipad: { width: 820, height: 1180, hasTouch: true, isMobile: true },
154
+ iphone: { width: 390, height: 844, hasTouch: true, isMobile: true }
155
+ };
156
+ server.setRequestHandler(ListToolsRequestSchema, async () => ({
157
+ tools: [
158
+ {
159
+ name: "riddle_screenshot",
160
+ description: "Take a screenshot of a URL using the Riddle API. Returns base64-encoded PNG image.",
161
+ inputSchema: {
162
+ type: "object",
163
+ properties: {
164
+ url: { type: "string", description: "URL to screenshot" },
165
+ width: { type: "number", description: "Viewport width (default: 1280)" },
166
+ height: { type: "number", description: "Viewport height (default: 720)" },
167
+ device: {
168
+ type: "string",
169
+ enum: ["desktop", "ipad", "iphone"],
170
+ description: "Device preset (overrides width/height)"
171
+ }
172
+ },
173
+ required: ["url"]
174
+ }
175
+ },
176
+ {
177
+ name: "riddle_batch_screenshot",
178
+ description: "Screenshot multiple URLs. Returns array of base64 images.",
179
+ inputSchema: {
180
+ type: "object",
181
+ properties: {
182
+ urls: { type: "array", items: { type: "string" }, description: "URLs to screenshot" },
183
+ device: { type: "string", enum: ["desktop", "ipad", "iphone"] }
184
+ },
185
+ required: ["urls"]
186
+ }
187
+ },
188
+ {
189
+ name: "riddle_run_script",
190
+ description: "Run a Playwright script on a page (async). Returns job_id to check status later.",
191
+ inputSchema: {
192
+ type: "object",
193
+ properties: {
194
+ url: { type: "string", description: "Starting URL" },
195
+ script: { type: "string", description: "Playwright script (page object available)" },
196
+ width: { type: "number" },
197
+ height: { type: "number" }
198
+ },
199
+ required: ["url", "script"]
200
+ }
201
+ },
202
+ {
203
+ name: "riddle_get_job",
204
+ description: "Get status and artifacts of a Riddle job",
205
+ inputSchema: {
206
+ type: "object",
207
+ properties: {
208
+ job_id: { type: "string", description: "Job ID to check" }
209
+ },
210
+ required: ["job_id"]
211
+ }
212
+ },
213
+ {
214
+ name: "riddle_automate",
215
+ description: "Run a Playwright script, wait for completion, and return all artifacts. Includes console logs and network HAR. Full sync automation - one call does everything.",
216
+ inputSchema: {
217
+ type: "object",
218
+ properties: {
219
+ url: { type: "string", description: "Starting URL" },
220
+ script: {
221
+ type: "string",
222
+ description: "Playwright script. Use 'page' object. Example: await page.click('button'); await page.screenshot({path: 'result.png'});"
223
+ },
224
+ device: { type: "string", enum: ["desktop", "ipad", "iphone"], description: "Device preset" },
225
+ timeout_sec: { type: "number", description: "Max execution time in seconds (default: 60)" },
226
+ force_clicks: {
227
+ type: "boolean",
228
+ description: "Add { force: true } to all click() calls to bypass stability checks on animated elements (default: true)"
229
+ }
230
+ },
231
+ required: ["url", "script"]
232
+ }
233
+ },
234
+ {
235
+ name: "riddle_click_and_screenshot",
236
+ description: "Simple automation: load URL, click a selector, take screenshot. Good for testing button clicks, game starts, etc. Uses force-click by default to handle animated buttons.",
237
+ inputSchema: {
238
+ type: "object",
239
+ properties: {
240
+ url: { type: "string", description: "URL to load" },
241
+ click: { type: "string", description: "CSS selector to click (e.g., 'button.start', '.play-btn')" },
242
+ wait_ms: { type: "number", description: "Wait time after click before screenshot (default: 1000)" },
243
+ device: { type: "string", enum: ["desktop", "ipad", "iphone"] },
244
+ force: { type: "boolean", description: "Force click even on animating elements (default: true)" }
245
+ },
246
+ required: ["url", "click"]
247
+ }
248
+ }
249
+ ]
250
+ }));
251
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
252
+ const { name } = request.params;
253
+ const args = request.params.arguments ?? {};
254
+ try {
255
+ if (name === "riddle_screenshot") {
256
+ const device = args.device;
257
+ const viewport = device ? devices[device] : { width: args.width || 1280, height: args.height || 720 };
258
+ const base64 = await client.screenshotSync(String(args.url), viewport);
259
+ return {
260
+ content: [
261
+ {
262
+ type: "image",
263
+ data: base64,
264
+ mimeType: "image/png"
265
+ }
266
+ ]
267
+ };
268
+ }
269
+ if (name === "riddle_batch_screenshot") {
270
+ const device = args.device;
271
+ const viewport = device ? devices[device] : devices.desktop;
272
+ const results = [];
273
+ const urls = Array.isArray(args.urls) ? args.urls : [];
274
+ for (const url of urls) {
275
+ try {
276
+ const base64 = await client.screenshotSync(String(url), viewport);
277
+ results.push({ url, success: true, image: base64 });
278
+ } catch (error) {
279
+ results.push({ url: String(url), success: false, error: error.message });
280
+ }
281
+ }
282
+ return {
283
+ content: [
284
+ {
285
+ type: "text",
286
+ text: JSON.stringify(
287
+ results.map((result) => ({
288
+ url: result.url,
289
+ success: result.success,
290
+ error: result.error
291
+ })),
292
+ null,
293
+ 2
294
+ )
295
+ },
296
+ ...results.filter((result) => result.success).map((result) => ({
297
+ type: "image",
298
+ data: result.image,
299
+ mimeType: "image/png"
300
+ }))
301
+ ]
302
+ };
303
+ }
304
+ if (name === "riddle_run_script") {
305
+ const viewport = { width: args.width || 1280, height: args.height || 720 };
306
+ const result = await client.runScript(String(args.url), String(args.script), viewport);
307
+ return {
308
+ content: [{ type: "text", text: JSON.stringify(result, null, 2) }]
309
+ };
310
+ }
311
+ if (name === "riddle_get_job") {
312
+ const job = await client.getJob(String(args.job_id));
313
+ const artifacts = await client.getArtifacts(String(args.job_id));
314
+ return {
315
+ content: [
316
+ {
317
+ type: "text",
318
+ text: JSON.stringify({ job, artifacts }, null, 2)
319
+ }
320
+ ]
321
+ };
322
+ }
323
+ if (name === "riddle_automate") {
324
+ const device = args.device;
325
+ const viewport = device ? devices[device] : devices.desktop;
326
+ let script = String(args.script ?? "");
327
+ const forceClicks = args.force_clicks !== false;
328
+ if (forceClicks) {
329
+ script = script.replace(
330
+ /\.click\(\s*(['"`])([^'"`]+)\1\s*\)(?!\s*;?\s*\/\/\s*no-force)/g,
331
+ ".click($1$2$1, { force: true })"
332
+ );
333
+ }
334
+ const { job_id, artifacts, consoleLogs, networkHar } = await client.runScriptSync(
335
+ String(args.url),
336
+ script,
337
+ viewport,
338
+ args.timeout_sec || 60
339
+ );
340
+ const images = [];
341
+ const savedPaths = [];
342
+ for (const artifact of artifacts) {
343
+ if (artifact.type === "image") {
344
+ const base64 = artifact.buffer.toString("base64");
345
+ images.push({ type: "image", data: base64, mimeType: "image/png" });
346
+ const path = saveToTmp(artifact.buffer, artifact.name?.replace(".png", "") || "artifact");
347
+ savedPaths.push(path);
348
+ }
349
+ }
350
+ let consoleOutput = null;
351
+ if (consoleLogs?.entries) {
352
+ consoleOutput = {
353
+ summary: consoleLogs.summary,
354
+ logs: consoleLogs.entries.log?.slice(-20) || [],
355
+ errors: consoleLogs.entries.error || [],
356
+ warns: consoleLogs.entries.warn || []
357
+ };
358
+ }
359
+ let networkSummary = null;
360
+ if (networkHar?.log?.entries) {
361
+ const entries = networkHar.log.entries;
362
+ networkSummary = {
363
+ total_requests: entries.length,
364
+ failed: entries.filter((entry) => entry.response?.status >= 400).length,
365
+ requests: entries.slice(-10).map((entry) => ({
366
+ url: entry.request?.url?.substring(0, 80),
367
+ status: entry.response?.status,
368
+ time: entry.time
369
+ }))
370
+ };
371
+ }
372
+ return {
373
+ content: [
374
+ {
375
+ type: "text",
376
+ text: JSON.stringify(
377
+ {
378
+ job_id,
379
+ saved_to: savedPaths,
380
+ console: consoleOutput,
381
+ network: networkSummary
382
+ },
383
+ null,
384
+ 2
385
+ )
386
+ },
387
+ ...images
388
+ ]
389
+ };
390
+ }
391
+ if (name === "riddle_click_and_screenshot") {
392
+ const device = args.device;
393
+ const viewport = device ? devices[device] : devices.desktop;
394
+ const waitMs = args.wait_ms || 1e3;
395
+ const forceClick = args.force !== false;
396
+ const clickSelector = String(args.click ?? "");
397
+ const script = `
398
+ await page.waitForLoadState('networkidle');
399
+ await page.click('${clickSelector.replace(/'/g, "\\'")}', { force: ${forceClick} });
400
+ await page.waitForTimeout(${waitMs});
401
+ await page.screenshot({ path: 'after-click.png', fullPage: false });
402
+ `;
403
+ const { job_id, artifacts } = await client.runScriptSync(String(args.url), script, viewport, 30);
404
+ const images = [];
405
+ for (const artifact of artifacts) {
406
+ if (artifact.type === "image") {
407
+ const base64 = artifact.buffer.toString("base64");
408
+ images.push({ type: "image", data: base64, mimeType: "image/png" });
409
+ saveToTmp(artifact.buffer, "click-result");
410
+ }
411
+ }
412
+ return {
413
+ content: [
414
+ {
415
+ type: "text",
416
+ text: JSON.stringify({ job_id, clicked: clickSelector }, null, 2)
417
+ },
418
+ ...images
419
+ ]
420
+ };
421
+ }
422
+ throw new Error(`Unknown tool: ${name}`);
423
+ } catch (error) {
424
+ return {
425
+ content: [{ type: "text", text: `Error: ${error.message}` }],
426
+ isError: true
427
+ };
428
+ }
429
+ });
430
+ async function main() {
431
+ const transport = new StdioServerTransport();
432
+ await server.connect(transport);
433
+ console.error("Riddle MCP server running");
434
+ }
435
+ main().catch(console.error);
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@riddledc/riddle-mcp",
3
+ "version": "0.2.0",
4
+ "description": "MCP-related utilities for RiddleDC (no secrets).",
5
+ "license": "MIT",
6
+ "author": "RiddleDC",
7
+ "type": "module",
8
+ "main": "./dist/index.cjs",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js",
15
+ "require": "./dist/index.cjs"
16
+ },
17
+ "./server": {
18
+ "types": "./dist/server.d.ts",
19
+ "import": "./dist/server.js",
20
+ "require": "./dist/server.cjs"
21
+ }
22
+ },
23
+ "bin": {
24
+ "riddle-mcp": "./dist/server.cjs"
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "sideEffects": false,
30
+ "engines": {
31
+ "node": ">=18"
32
+ },
33
+ "dependencies": {
34
+ "@modelcontextprotocol/sdk": "^1.0.0"
35
+ },
36
+ "devDependencies": {
37
+ "@types/node": "^22.0.0",
38
+ "tsup": "^8.0.1",
39
+ "typescript": "^5.4.5"
40
+ },
41
+ "scripts": {
42
+ "build": "tsup src/index.ts src/server.ts --format cjs,esm --dts --out-dir dist",
43
+ "clean": "rm -rf dist",
44
+ "lint": "echo 'lint: (not configured)'",
45
+ "test": "echo 'test: (not configured)'"
46
+ }
47
+ }