@pineforge/codegen-mcp 0.8.1 → 0.8.3

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/dist/server.js ADDED
@@ -0,0 +1,758 @@
1
+ /**
2
+ * @pineforge/codegen-mcp — shared MCP server factory.
3
+ *
4
+ * Single source of truth for all tool logic. Both entrypoints (docker via
5
+ * `index.ts`, local/in-container via `index.local.ts`) build their server here;
6
+ * they differ only in which EngineRunner they construct and which tool surface
7
+ * they request via `opts.imageTools`. Tool COPY is made mode-aware off
8
+ * `runner.mode` so descriptions stay accurate per backend.
9
+ */
10
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
11
+ import { z } from "zod";
12
+ import { mkdtemp, mkdir, writeFile, rm, stat, readFile } from "node:fs/promises";
13
+ import { tmpdir } from "node:os";
14
+ import { join, resolve, isAbsolute, dirname } from "node:path";
15
+ import { VERSION } from "./version.js";
16
+ import { DEFAULT_IMAGE, stringifyParams, } from "./engine.js";
17
+ // ─── Config ───────────────────────────────────────────────────────────────
18
+ const ALLOW_ANYWHERE = process.env.PINEFORGE_ALLOW_ANYWHERE === "1";
19
+ const BINANCE_SPOT_BASE = "https://api.binance.com";
20
+ const BINANCE_FAPI_BASE = "https://fapi.binance.com";
21
+ const BINANCE_KLINES_LIMIT = 1000;
22
+ const BINANCE_PAGE_DELAY_MS = 200;
23
+ async function runBacktest(runner, args) {
24
+ const csvPath = await resolveCsvPath(args.ohlcv_csv_path);
25
+ const image = args.image ?? DEFAULT_IMAGE;
26
+ const cpp = await runner.transpile(args.source, args.image);
27
+ const tmp = await mkdtemp(join(tmpdir(), "pineforge-bt-"));
28
+ const cppPath = join(tmp, "strategy.cpp");
29
+ await writeFile(cppPath, cpp, "utf8");
30
+ try {
31
+ const report = await runner.backtest({
32
+ cppPath,
33
+ csvPath,
34
+ image: args.image,
35
+ inputs: args.inputs,
36
+ overrides: args.overrides,
37
+ runtime: args.runtime,
38
+ });
39
+ return {
40
+ ...report,
41
+ _meta: { strategy_cpp_bytes: cpp.length, image: runner.mode === "docker" ? image : "local" },
42
+ };
43
+ }
44
+ finally {
45
+ rm(tmp, { recursive: true, force: true }).catch(() => undefined);
46
+ }
47
+ }
48
+ async function runBacktestGrid(runner, args) {
49
+ const csvPath = await resolveCsvPath(args.ohlcv_csv_path);
50
+ const image = args.image ?? DEFAULT_IMAGE;
51
+ const includeTrades = args.include_trades === true;
52
+ const sortBy = args.sort_by ?? "net_pnl";
53
+ const maxCombos = args.max_combinations ?? 64;
54
+ const concurrency = Math.max(1, Math.min(args.concurrency ?? 1, 8));
55
+ const combos = buildCombinations(args.inputs, args.overrides, args.fixed_inputs, args.fixed_overrides);
56
+ if (combos.length === 0) {
57
+ throw new Error("no parameter combinations produced — provide at least one inputs/overrides axis");
58
+ }
59
+ if (combos.length > maxCombos) {
60
+ throw new Error(`${combos.length} combinations exceeds max_combinations=${maxCombos}. ` +
61
+ `Either reduce the grid or raise max_combinations.`);
62
+ }
63
+ const cpp = await runner.transpile(args.source, args.image);
64
+ const tmp = await mkdtemp(join(tmpdir(), "pineforge-grid-"));
65
+ const cppPath = join(tmp, "strategy.cpp");
66
+ await writeFile(cppPath, cpp, "utf8");
67
+ try {
68
+ const rows = await pMap(combos, concurrency, async (combo) => {
69
+ try {
70
+ const report = await runner.backtest({
71
+ cppPath, csvPath,
72
+ image: args.image,
73
+ inputs: combo.inputs, overrides: combo.overrides,
74
+ runtime: args.runtime,
75
+ });
76
+ return {
77
+ ok: true,
78
+ inputs: combo.inputs,
79
+ overrides: combo.overrides,
80
+ summary: report.summary,
81
+ applied_inputs: report.applied_inputs,
82
+ applied_overrides: report.applied_overrides,
83
+ elapsed_seconds: report.elapsed_seconds,
84
+ ...(includeTrades ? { trades: report.trades } : {}),
85
+ };
86
+ }
87
+ catch (e) {
88
+ return {
89
+ ok: false,
90
+ inputs: combo.inputs,
91
+ overrides: combo.overrides,
92
+ error: e instanceof Error ? e.message : String(e),
93
+ };
94
+ }
95
+ });
96
+ const succeeded = rows.filter(r => r.ok);
97
+ const failed = rows.filter(r => !r.ok);
98
+ const sortValue = (r) => {
99
+ if (!r.ok || !r.summary)
100
+ return -Infinity;
101
+ const v = r.summary[sortBy];
102
+ return typeof v === "number" ? v : -Infinity;
103
+ };
104
+ succeeded.sort((a, b) => sortValue(b) - sortValue(a));
105
+ return {
106
+ total_combinations: combos.length,
107
+ succeeded: succeeded.length,
108
+ failed: failed.length,
109
+ sort_by: sortBy,
110
+ image: runner.mode === "docker" ? image : "local",
111
+ _meta: { strategy_cpp_bytes: cpp.length, concurrency },
112
+ best: succeeded[0] ?? null,
113
+ results: [...succeeded, ...failed],
114
+ };
115
+ }
116
+ finally {
117
+ rm(tmp, { recursive: true, force: true }).catch(() => undefined);
118
+ }
119
+ }
120
+ // ─── Param-grid helpers ───────────────────────────────────────────────────
121
+ function cartesianProduct(grid) {
122
+ const keys = Object.keys(grid);
123
+ if (keys.length === 0)
124
+ return [{}];
125
+ let acc = [{}];
126
+ for (const k of keys) {
127
+ const values = grid[k] ?? [];
128
+ const next = [];
129
+ for (const partial of acc) {
130
+ for (const v of values) {
131
+ next.push({ ...partial, [k]: String(v) });
132
+ }
133
+ }
134
+ acc = next;
135
+ }
136
+ return acc;
137
+ }
138
+ function buildCombinations(inputsGrid, overridesGrid, fixedInputs, fixedOverrides) {
139
+ const inputCombos = cartesianProduct(inputsGrid ?? {});
140
+ const overrideCombos = cartesianProduct(overridesGrid ?? {});
141
+ const fixIn = stringifyParams(fixedInputs);
142
+ const fixOv = stringifyParams(fixedOverrides);
143
+ const out = [];
144
+ for (const i of inputCombos) {
145
+ for (const o of overrideCombos) {
146
+ out.push({ inputs: { ...fixIn, ...i }, overrides: { ...fixOv, ...o } });
147
+ }
148
+ }
149
+ return out;
150
+ }
151
+ async function pMap(items, n, fn) {
152
+ const results = new Array(items.length);
153
+ let next = 0;
154
+ const worker = async () => {
155
+ while (true) {
156
+ const i = next++;
157
+ if (i >= items.length)
158
+ return;
159
+ results[i] = await fn(items[i], i);
160
+ }
161
+ };
162
+ await Promise.all(Array.from({ length: Math.min(n, items.length) }, worker));
163
+ return results;
164
+ }
165
+ // ─── Path / CSV helpers ───────────────────────────────────────────────────
166
+ function resolveScopedPath(p, label) {
167
+ const abs = isAbsolute(p) ? p : resolve(process.cwd(), p);
168
+ if (!ALLOW_ANYWHERE && !abs.startsWith(process.cwd() + "/") && abs !== process.cwd()) {
169
+ throw new Error(`${label} path '${abs}' is outside cwd '${process.cwd()}'. ` +
170
+ `Set PINEFORGE_ALLOW_ANYWHERE=1 to override.`);
171
+ }
172
+ return abs;
173
+ }
174
+ async function resolveCsvPath(p) {
175
+ const abs = resolveScopedPath(p, "OHLCV");
176
+ const st = await stat(abs).catch(() => null);
177
+ if (!st || !st.isFile())
178
+ throw new Error(`OHLCV file not found: ${abs}`);
179
+ const head = (await readFile(abs, "utf8")).slice(0, 200).split(/\r?\n/)[0] ?? "";
180
+ const expected = ["timestamp", "open", "high", "low", "close", "volume"];
181
+ const cols = head.toLowerCase().split(",").map((s) => s.trim());
182
+ if (expected.some((c, i) => cols[i] !== c)) {
183
+ throw new Error(`OHLCV header mismatch. Expected: ${expected.join(",")}\nGot: ${head}`);
184
+ }
185
+ return abs;
186
+ }
187
+ // ─── Binance public-API client ────────────────────────────────────────────
188
+ const BINANCE_INTERVAL_MS = {
189
+ "1s": 1_000,
190
+ "1m": 60_000, "3m": 180_000, "5m": 300_000, "15m": 900_000, "30m": 1_800_000,
191
+ "1h": 3_600_000, "2h": 7_200_000, "4h": 14_400_000, "6h": 21_600_000,
192
+ "8h": 28_800_000, "12h": 43_200_000,
193
+ "1d": 86_400_000, "3d": 259_200_000,
194
+ "1w": 604_800_000,
195
+ "1M": 30 * 86_400_000, // approximate
196
+ };
197
+ const BINANCE_INTERVALS = Object.keys(BINANCE_INTERVAL_MS);
198
+ function binanceKlinesUrl(market) {
199
+ return market === "spot"
200
+ ? `${BINANCE_SPOT_BASE}/api/v3/klines`
201
+ : `${BINANCE_FAPI_BASE}/fapi/v1/klines`;
202
+ }
203
+ function binanceExchangeInfoUrl(market) {
204
+ return market === "spot"
205
+ ? `${BINANCE_SPOT_BASE}/api/v3/exchangeInfo`
206
+ : `${BINANCE_FAPI_BASE}/fapi/v1/exchangeInfo`;
207
+ }
208
+ async function binanceGet(url) {
209
+ const resp = await fetch(url, {
210
+ headers: { "user-agent": `pineforge-codegen-mcp/${VERSION}` },
211
+ });
212
+ const text = await resp.text();
213
+ if (!resp.ok) {
214
+ throw new Error(`Binance ${resp.status} for ${url}: ${text.slice(0, 500)}`);
215
+ }
216
+ try {
217
+ return JSON.parse(text);
218
+ }
219
+ catch {
220
+ throw new Error(`Binance non-JSON response: ${text.slice(0, 200)}`);
221
+ }
222
+ }
223
+ async function fetchKlinesPage(market, symbol, interval, startTime, endTime, limit) {
224
+ const params = new URLSearchParams({
225
+ symbol: symbol.toUpperCase(),
226
+ interval,
227
+ limit: String(limit),
228
+ });
229
+ if (startTime !== undefined)
230
+ params.set("startTime", String(startTime));
231
+ if (endTime !== undefined)
232
+ params.set("endTime", String(endTime));
233
+ const url = `${binanceKlinesUrl(market)}?${params.toString()}`;
234
+ const data = await binanceGet(url);
235
+ if (!Array.isArray(data))
236
+ throw new Error(`Binance unexpected klines payload: ${JSON.stringify(data).slice(0, 200)}`);
237
+ return data;
238
+ }
239
+ async function fetchBinanceOhlcv(args) {
240
+ const intervalMs = BINANCE_INTERVAL_MS[args.interval];
241
+ if (intervalMs === undefined) {
242
+ throw new Error(`Unknown interval '${args.interval}'. Valid: ${Object.keys(BINANCE_INTERVAL_MS).join(", ")}`);
243
+ }
244
+ if (args.limit <= 0)
245
+ throw new Error("limit must be > 0");
246
+ if (args.limit > 100_000)
247
+ throw new Error("limit must be ≤ 100000 (sanity cap)");
248
+ const outAbs = resolveScopedPath(args.output_path, "output");
249
+ await mkdir(dirname(outAbs), { recursive: true });
250
+ const now = Date.now();
251
+ const endTime = args.end_time ?? now;
252
+ const startTime = args.start_time ?? Math.max(0, endTime - args.limit * intervalMs);
253
+ const collected = [];
254
+ let cursor = startTime;
255
+ let pages = 0;
256
+ while (collected.length < args.limit && cursor <= endTime) {
257
+ const remaining = args.limit - collected.length;
258
+ const pageLimit = Math.min(BINANCE_KLINES_LIMIT, remaining);
259
+ const page = await fetchKlinesPage(args.market, args.symbol, args.interval, cursor, endTime, pageLimit);
260
+ pages++;
261
+ if (page.length === 0)
262
+ break;
263
+ // Dedup against previous tail (Binance is inclusive on startTime).
264
+ const tail = collected[collected.length - 1];
265
+ const lastSeen = tail ? tail[0] : -1;
266
+ for (const k of page) {
267
+ if (k[0] > lastSeen)
268
+ collected.push(k);
269
+ }
270
+ if (page.length < pageLimit)
271
+ break;
272
+ const lastPage = page[page.length - 1];
273
+ cursor = lastPage[0] + intervalMs;
274
+ if (collected.length < args.limit && cursor <= endTime) {
275
+ await sleep(BINANCE_PAGE_DELAY_MS);
276
+ }
277
+ }
278
+ if (collected.length === 0) {
279
+ throw new Error(`Binance returned 0 bars for ${args.symbol} ${args.interval} (${args.market})`);
280
+ }
281
+ const lines = ["timestamp,open,high,low,close,volume"];
282
+ for (const k of collected) {
283
+ const ts = Number(k[0]);
284
+ if (!Number.isFinite(ts))
285
+ continue;
286
+ const o = sanitizeNumeric(k[1]);
287
+ const h = sanitizeNumeric(k[2]);
288
+ const l = sanitizeNumeric(k[3]);
289
+ const c = sanitizeNumeric(k[4]);
290
+ const v = sanitizeNumeric(k[5]);
291
+ lines.push(`${ts},${o},${h},${l},${c},${v}`);
292
+ }
293
+ const csv = lines.join("\n") + "\n";
294
+ await writeFile(outAbs, csv, "utf8");
295
+ const first = collected[0];
296
+ const last = collected[collected.length - 1];
297
+ return {
298
+ output_path: outAbs,
299
+ market: args.market,
300
+ symbol: args.symbol.toUpperCase(),
301
+ interval: args.interval,
302
+ bars: collected.length,
303
+ pages,
304
+ first_open_time: first[0],
305
+ last_open_time: last[0],
306
+ first_open_iso: new Date(first[0]).toISOString(),
307
+ last_open_iso: new Date(last[0]).toISOString(),
308
+ bytes: Buffer.byteLength(csv, "utf8"),
309
+ };
310
+ }
311
+ function sanitizeNumeric(raw) {
312
+ // Binance returns numeric strings; we keep them as strings to avoid
313
+ // float round-trip loss but reject anything non-numeric so the CSV
314
+ // stays parseable downstream.
315
+ const s = String(raw);
316
+ if (!/^-?\d+(\.\d+)?([eE][-+]?\d+)?$/.test(s)) {
317
+ throw new Error(`Non-numeric kline field: ${s}`);
318
+ }
319
+ return s;
320
+ }
321
+ function sleep(ms) {
322
+ return new Promise((r) => setTimeout(r, ms));
323
+ }
324
+ // Cache exchangeInfo for 5 min — symbol list rarely changes.
325
+ const SYMBOL_CACHE_TTL_MS = 5 * 60_000;
326
+ const symbolCache = new Map();
327
+ async function listBinanceSymbols(market) {
328
+ const now = Date.now();
329
+ const cached = symbolCache.get(market);
330
+ if (cached && now - cached.ts < SYMBOL_CACHE_TTL_MS)
331
+ return cached.symbols;
332
+ const data = await binanceGet(binanceExchangeInfoUrl(market));
333
+ if (!Array.isArray(data?.symbols)) {
334
+ throw new Error("Binance exchangeInfo: unexpected payload shape");
335
+ }
336
+ symbolCache.set(market, { ts: now, symbols: data.symbols });
337
+ return data.symbols;
338
+ }
339
+ async function binanceSymbols(args) {
340
+ const all = await listBinanceSymbols(args.market);
341
+ const limit = Math.max(1, Math.min(args.limit ?? 200, 2000));
342
+ const q = args.query?.toUpperCase();
343
+ const quote = args.quote_asset?.toUpperCase();
344
+ const base = args.base_asset?.toUpperCase();
345
+ const status = args.status?.toUpperCase();
346
+ const ct = args.contract_type?.toUpperCase();
347
+ const filtered = all.filter((s) => {
348
+ if (q && !s.symbol.toUpperCase().includes(q))
349
+ return false;
350
+ if (quote && s.quoteAsset?.toUpperCase() !== quote)
351
+ return false;
352
+ if (base && s.baseAsset?.toUpperCase() !== base)
353
+ return false;
354
+ if (status && s.status?.toUpperCase() !== status)
355
+ return false;
356
+ if (ct && s.contractType?.toUpperCase() !== ct)
357
+ return false;
358
+ return true;
359
+ });
360
+ const truncated = filtered.length > limit;
361
+ const slice = filtered.slice(0, limit).map((s) => ({
362
+ symbol: s.symbol,
363
+ base: s.baseAsset,
364
+ quote: s.quoteAsset,
365
+ status: s.status,
366
+ ...(s.contractType ? { contract_type: s.contractType } : {}),
367
+ }));
368
+ return {
369
+ market: args.market,
370
+ total_symbols: all.length,
371
+ matched: filtered.length,
372
+ returned: slice.length,
373
+ truncated,
374
+ symbols: slice,
375
+ };
376
+ }
377
+ // ─── MCP server wiring ────────────────────────────────────────────────────
378
+ const asTextResult = (value) => ({
379
+ content: [{ type: "text", text: JSON.stringify(value, null, 2) }],
380
+ });
381
+ const ParamMapSchema = z.record(z.string(), z.union([z.string(), z.number(), z.boolean()]));
382
+ const ParamGridSchema = z.record(z.string(), z.array(z.union([z.string(), z.number(), z.boolean()])).min(1));
383
+ const MarketSchema = z.enum(["spot", "usdt_perp"]);
384
+ const IntervalSchema = z.enum(BINANCE_INTERVALS);
385
+ // ─── strategy() header overrides ──────────────────────────────────────────
386
+ //
387
+ // The pineforge-engine runtime accepts a fixed set of strategy(...) header
388
+ // overrides via PINEFORGE_OVERRIDES. Enumerating them here (instead of an
389
+ // opaque ParamMap) gives the agent the exact keys, value types, and enum
390
+ // values it can pass — and lets list_strategy_overrides expose the same
391
+ // catalog at runtime.
392
+ const COMMISSION_TYPES = ["percent", "cash_per_order", "cash_per_contract"];
393
+ const QTY_TYPES = ["fixed", "percent_of_equity", "cash"];
394
+ const CLOSE_ENTRIES_RULES = ["ANY", "FIFO"];
395
+ const STRATEGY_OVERRIDES_CATALOG = {
396
+ initial_capital: {
397
+ type: "number",
398
+ description: "Starting equity in account currency. Mirrors `strategy(initial_capital=...)`.",
399
+ },
400
+ pyramiding: {
401
+ type: "integer",
402
+ description: "Maximum same-direction entries before further entries are blocked. " +
403
+ "0 = single position. Mirrors `strategy(pyramiding=...)`.",
404
+ },
405
+ slippage: {
406
+ type: "integer",
407
+ description: "Per-fill slippage in ticks (mintick units). Mirrors `strategy(slippage=...)`.",
408
+ },
409
+ commission_value: {
410
+ type: "number",
411
+ description: "Commission magnitude. Units depend on commission_type. " +
412
+ "Mirrors `strategy(commission_value=...)`.",
413
+ },
414
+ commission_type: {
415
+ type: "enum",
416
+ enum: COMMISSION_TYPES,
417
+ description: "Commission units. 'percent' = percent of trade value, " +
418
+ "'cash_per_order' = fixed cash per order, " +
419
+ "'cash_per_contract' = fixed cash per contract. " +
420
+ "Mirrors `strategy(commission_type=...)`.",
421
+ },
422
+ default_qty_value: {
423
+ type: "number",
424
+ description: "Default order size, interpreted per default_qty_type. " +
425
+ "Mirrors `strategy(default_qty_value=...)`.",
426
+ },
427
+ default_qty_type: {
428
+ type: "enum",
429
+ enum: QTY_TYPES,
430
+ description: "Default order sizing mode. 'fixed' = contracts/shares, " +
431
+ "'percent_of_equity' = percent of current equity, " +
432
+ "'cash' = fixed cash amount per order. " +
433
+ "Mirrors `strategy(default_qty_type=...)`.",
434
+ },
435
+ process_orders_on_close: {
436
+ type: "boolean",
437
+ description: "When true, market orders submitted on a bar fill at that bar's close " +
438
+ "instead of waiting for the next bar's open. " +
439
+ "Mirrors `strategy(process_orders_on_close=...)`.",
440
+ },
441
+ close_entries_rule: {
442
+ type: "enum",
443
+ enum: CLOSE_ENTRIES_RULES,
444
+ description: "How `strategy.close(id)` resolves which entries to close. " +
445
+ "'FIFO' = first-in-first-out (default). " +
446
+ "'ANY' = match all entries with the same id. " +
447
+ "Mirrors `strategy(close_entries_rule=...)`.",
448
+ },
449
+ };
450
+ const StrategyOverridesSchema = z.object({
451
+ initial_capital: z.number().describe(STRATEGY_OVERRIDES_CATALOG.initial_capital.description).optional(),
452
+ pyramiding: z.number().int().min(0).describe(STRATEGY_OVERRIDES_CATALOG.pyramiding.description).optional(),
453
+ slippage: z.number().int().min(0).describe(STRATEGY_OVERRIDES_CATALOG.slippage.description).optional(),
454
+ commission_value: z.number().min(0).describe(STRATEGY_OVERRIDES_CATALOG.commission_value.description).optional(),
455
+ commission_type: z.enum(COMMISSION_TYPES).describe(STRATEGY_OVERRIDES_CATALOG.commission_type.description).optional(),
456
+ default_qty_value: z.number().describe(STRATEGY_OVERRIDES_CATALOG.default_qty_value.description).optional(),
457
+ default_qty_type: z.enum(QTY_TYPES).describe(STRATEGY_OVERRIDES_CATALOG.default_qty_type.description).optional(),
458
+ process_orders_on_close: z.boolean().describe(STRATEGY_OVERRIDES_CATALOG.process_orders_on_close.description).optional(),
459
+ close_entries_rule: z.enum(CLOSE_ENTRIES_RULES).describe(STRATEGY_OVERRIDES_CATALOG.close_entries_rule.description).optional(),
460
+ }).strict();
461
+ const StrategyOverridesGridSchema = z.object({
462
+ initial_capital: z.array(z.number()).min(1).optional(),
463
+ pyramiding: z.array(z.number().int().min(0)).min(1).optional(),
464
+ slippage: z.array(z.number().int().min(0)).min(1).optional(),
465
+ commission_value: z.array(z.number().min(0)).min(1).optional(),
466
+ commission_type: z.array(z.enum(COMMISSION_TYPES)).min(1).optional(),
467
+ default_qty_value: z.array(z.number()).min(1).optional(),
468
+ default_qty_type: z.array(z.enum(QTY_TYPES)).min(1).optional(),
469
+ process_orders_on_close: z.array(z.boolean()).min(1).optional(),
470
+ close_entries_rule: z.array(z.enum(CLOSE_ENTRIES_RULES)).min(1).optional(),
471
+ }).strict();
472
+ // ─── Runtime args (run_backtest_full args, NOT strategy() header) ─────────
473
+ //
474
+ // These are passed to pineforge-engine's run_backtest_full() rather than the
475
+ // per-strategy override table. They control how the engine consumes input
476
+ // bars (timeframe semantics, bar magnifier sub-bar sampling) and never
477
+ // appear in the Pine source. The engine validates them and surfaces any
478
+ // mismatch (e.g. script_tf finer than input_tf) through
479
+ // strategy_get_last_error() — agents see that as
480
+ // {"engine":"pineforge","error":"..."} on stdout, exit code 1.
481
+ const MAGNIFIER_DISTS = [
482
+ "uniform", "cosine", "triangle", "endpoints", "front_loaded", "back_loaded",
483
+ ];
484
+ const RUNTIME_ARGS_CATALOG = {
485
+ input_tf: {
486
+ type: "enum",
487
+ description: "Chart bar timeframe — the resolution of the OHLCV CSV being fed in. " +
488
+ "Use Pine timeframe strings: '1', '5', '15', '60', '240' (minutes), " +
489
+ "'D', '1D', 'W', '1W', 'M', '1M'. Empty / omitted = the engine " +
490
+ "auto-detects the timeframe from the gap between the first two bars. " +
491
+ "Set explicitly when the CSV's resolution is ambiguous, when fewer " +
492
+ "than 2 bars are present, or when registering request.security() " +
493
+ "evaluators that need the chart TF stated up front.",
494
+ },
495
+ script_tf: {
496
+ type: "enum",
497
+ description: "Strategy / script timeframe — the resolution at which the strategy's " +
498
+ "on_bar() runs. Empty / omitted = same as input_tf (no aggregation). " +
499
+ "Set to a coarser timeframe (e.g. input_tf='5', script_tf='60') to " +
500
+ "have the engine aggregate the input bars into higher-TF script bars " +
501
+ "before invoking the strategy. MUST be coarser than or equal to " +
502
+ "input_tf — finer values are rejected with the error " +
503
+ "'script timeframe must be coarser than or equal to input timeframe'. " +
504
+ "When the Pine source uses request.security() to pull a higher TF, " +
505
+ "leave script_tf empty (the security TF is encoded inside the Pine " +
506
+ "source, not here).",
507
+ },
508
+ bar_magnifier: {
509
+ type: "boolean",
510
+ description: "When true, the engine samples each input bar's price path at " +
511
+ "magnifier_samples sub-points and walks stop / limit / trailing " +
512
+ "orders against the sub-bars instead of the bar's OHLC corners. " +
513
+ "Improves intra-bar fill realism for strategies that hinge on " +
514
+ "wick fills, but multiplies engine work by ~magnifier_samples. " +
515
+ "Default false. Most useful when input_tf is coarse (15m+) and " +
516
+ "the strategy uses tight intra-bar exits.",
517
+ },
518
+ magnifier_samples: {
519
+ type: "integer",
520
+ description: "Sub-bar sample count when bar_magnifier=true. Minimum 2 " +
521
+ "(open + close); typical range 4–16. Higher values give smoother " +
522
+ "intra-bar fill simulation at linear cost. Ignored when " +
523
+ "bar_magnifier=false. Default 4.",
524
+ },
525
+ magnifier_dist: {
526
+ type: "enum",
527
+ enum: MAGNIFIER_DISTS,
528
+ description: "Distribution of sub-bar samples along the OHLC path when " +
529
+ "bar_magnifier=true. 'endpoints' (default) always includes the " +
530
+ "exact O/H/L/C corners and uniformly fills between them — best for " +
531
+ "TradingView parity. 'uniform' = equal spacing. 'cosine' = denser " +
532
+ "near segment endpoints. 'triangle' = denser near segment midpoints. " +
533
+ "'front_loaded' / 'back_loaded' = denser near open / close, " +
534
+ "simulating opening- or closing-impulse price action.",
535
+ },
536
+ };
537
+ const RuntimeArgsSchema = z.object({
538
+ input_tf: z.string().describe(RUNTIME_ARGS_CATALOG.input_tf.description).optional(),
539
+ script_tf: z.string().describe(RUNTIME_ARGS_CATALOG.script_tf.description).optional(),
540
+ bar_magnifier: z.boolean().describe(RUNTIME_ARGS_CATALOG.bar_magnifier.description).optional(),
541
+ magnifier_samples: z.number().int().min(2).describe(RUNTIME_ARGS_CATALOG.magnifier_samples.description).optional(),
542
+ magnifier_dist: z.enum(MAGNIFIER_DISTS).describe(RUNTIME_ARGS_CATALOG.magnifier_dist.description).optional(),
543
+ }).strict();
544
+ // ─── Server factory ────────────────────────────────────────────────────────
545
+ /**
546
+ * Build the MCP server with the common tool surface registered against
547
+ * `runner`. When `opts.imageTools` is true the Docker image-management tools
548
+ * (`pull_engine_image`, `check_engine_image`) are added; otherwise a read-only
549
+ * `engine_info` tool is registered instead (the in-container/local product,
550
+ * where the engine is baked in and there is nothing to pull/check).
551
+ *
552
+ * The caller is responsible for connecting a transport.
553
+ */
554
+ export function createServer(runner, opts) {
555
+ const server = new McpServer({ name: "pineforge-codegen-mcp", version: VERSION }, { capabilities: { tools: {} } });
556
+ // Mode-aware run-location clauses so tool copy stays accurate per backend
557
+ // without duplicating tool logic.
558
+ const transpileWhere = runner.mode === "docker"
559
+ ? "using the pineforge-codegen transpiler bundled in the pineforge-engine Docker image"
560
+ : "using the pineforge-codegen transpiler bundled in this image (in-process, no host Docker)";
561
+ const backtestWhere = runner.mode === "docker"
562
+ ? "via the pineforge-engine Docker image on the user's local machine. Fully local — " +
563
+ "transpile + backtest run in-container; nothing leaves the box, no API key."
564
+ : "via the bundled pineforge-engine (in-process, no host Docker daemon). Fully local — " +
565
+ "transpile + backtest run in this image; nothing leaves the box, no API key.";
566
+ const gridWhere = runner.mode === "docker"
567
+ ? "transpile the Pine source ONCE (locally, in-container)"
568
+ : "transpile the Pine source ONCE (in-process, no host Docker)";
569
+ const concurrencyHelp = runner.mode === "docker"
570
+ ? "Set concurrency > 1 to run backtests in parallel — each docker container " +
571
+ "has its own startup overhead, so 2-4 is usually plenty."
572
+ : "Set concurrency > 1 to run backtests in parallel — 2-4 is usually plenty.";
573
+ server.registerTool("transpile_pine", {
574
+ description: "Transpile PineScript v6 source to a C++ translation unit locally, " +
575
+ transpileWhere + ". " +
576
+ "No API key, no network — source never leaves the machine. Returns the " +
577
+ "generated C++ as text. Use backtest_pine if you also want to run the strategy.",
578
+ inputSchema: {
579
+ source: z.string().describe("PineScript v6 source (must include //@version=6)."),
580
+ image: z.string().optional().describe(`Docker image override. Defaults to ${DEFAULT_IMAGE}.`),
581
+ },
582
+ }, async ({ source, image }) => ({
583
+ content: [{
584
+ type: "text",
585
+ text: await runner.transpile(source, image),
586
+ }],
587
+ }));
588
+ server.registerTool("backtest_pine", {
589
+ description: "Transpile a PineScript v6 strategy and run it against an OHLCV CSV " +
590
+ backtestWhere + " " +
591
+ "Optional `inputs` overrides input.*() named values from the Pine source " +
592
+ "(keys = the second arg of input.*(...) calls, e.g. 'Fast Length'). " +
593
+ "Optional `overrides` overrides strategy(...) header fields " +
594
+ "(initial_capital, commission_value, default_qty_value, pyramiding, " +
595
+ "slippage, default_qty_type, commission_type, process_orders_on_close). " +
596
+ "Returns the parsed JSON report (summary, trades, applied_inputs, " +
597
+ "applied_overrides, elapsed_seconds). Use backtest_pine_grid for sweeps.",
598
+ inputSchema: {
599
+ source: z.string().describe("PineScript v6 source."),
600
+ ohlcv_csv_path: z.string().describe("Absolute or cwd-relative path to OHLCV CSV with header " +
601
+ "'timestamp,open,high,low,close,volume' (timestamp = UNIX ms UTC)."),
602
+ image: z.string().optional().describe(`Docker image override. Defaults to ${DEFAULT_IMAGE}.`),
603
+ inputs: ParamMapSchema.optional().describe("Map of Pine input.*() names → value (string/number/bool). " +
604
+ "Sent as PINEFORGE_INPUTS env var to the runtime."),
605
+ overrides: StrategyOverridesSchema.optional().describe("strategy(...) header overrides. Each key maps to a single argument " +
606
+ "of the Pine `strategy()` call; only the keys you set are applied. " +
607
+ "Sent as PINEFORGE_OVERRIDES env var. Call list_engine_params " +
608
+ "for the full catalog with types and enum values."),
609
+ runtime: RuntimeArgsSchema.optional().describe("Engine runtime args (NOT strategy() header) controlling timeframe " +
610
+ "semantics and intra-bar fill simulation. input_tf / script_tf set " +
611
+ "the chart and strategy timeframes — script_tf must be coarser " +
612
+ "than or equal to input_tf or the engine rejects the run. " +
613
+ "bar_magnifier + magnifier_samples + magnifier_dist enable sub-bar " +
614
+ "price-path sampling for tighter stop / limit fills. Each field is " +
615
+ "optional and only forwarded to the engine when set. Call " +
616
+ "list_engine_params for the full catalog."),
617
+ },
618
+ }, async ({ source, ohlcv_csv_path, image, inputs, overrides, runtime }) => asTextResult(await runBacktest(runner, { source, ohlcv_csv_path, image, inputs, overrides, runtime })));
619
+ server.registerTool("backtest_pine_grid", {
620
+ description: "Run a parameter sweep: " + gridWhere + ", " +
621
+ "then re-run the same compiled strategy against the OHLCV CSV across the " +
622
+ "cartesian product of `inputs` × `overrides` grids. Returns a ranked list " +
623
+ "of {inputs, overrides, summary, elapsed_seconds} entries sorted by `sort_by` " +
624
+ "descending, plus the top entry under `best`. Cap: max_combinations (default " +
625
+ "64). " + concurrencyHelp,
626
+ inputSchema: {
627
+ source: z.string().describe("PineScript v6 source."),
628
+ ohlcv_csv_path: z.string().describe("Path to OHLCV CSV (same format as backtest_pine)."),
629
+ image: z.string().optional().describe(`Docker image override. Defaults to ${DEFAULT_IMAGE}.`),
630
+ inputs: ParamGridSchema.optional().describe("Grid of input.*() names → list of values to sweep. " +
631
+ "Example: {\"Fast Length\": [8, 12, 19], \"Slow Length\": [21, 26, 39]}"),
632
+ overrides: StrategyOverridesGridSchema.optional().describe("Grid of strategy(...) header overrides → list of values, one axis " +
633
+ "per key. Example: {\"default_qty_value\": [1, 5], \"commission_value\": [0.04]}. " +
634
+ "Call list_engine_params for the full catalog with types and enum values."),
635
+ fixed_inputs: ParamMapSchema.optional().describe("Inputs applied to every combo (overridden by per-combo `inputs` keys)."),
636
+ fixed_overrides: StrategyOverridesSchema.optional().describe("Overrides applied to every combo (overridden by per-combo `overrides` keys)."),
637
+ runtime: RuntimeArgsSchema.optional().describe("Engine runtime args applied to every combo in the sweep. Same " +
638
+ "shape as backtest_pine.runtime — input_tf / script_tf / " +
639
+ "bar_magnifier / magnifier_samples / magnifier_dist. Currently " +
640
+ "fixed across the grid (not swept); add to the grid axes through " +
641
+ "future versions if you need to vary them."),
642
+ max_combinations: z.number().int().min(1).max(1024).optional()
643
+ .describe("Hard cap on combinations. Default 64."),
644
+ concurrency: z.number().int().min(1).max(8).optional()
645
+ .describe("Parallel backtests. Default 1."),
646
+ include_trades: z.boolean().optional()
647
+ .describe("Include the per-trade list in each result. Default false (saves tokens)."),
648
+ sort_by: z.enum(["net_pnl", "win_rate_pct", "max_drawdown", "total_trades"])
649
+ .optional().describe("summary.* field to rank by, descending. Default net_pnl."),
650
+ },
651
+ }, async (args) => asTextResult(await runBacktestGrid(runner, args)));
652
+ server.registerTool("fetch_binance_ohlcv", {
653
+ description: "Fetch OHLCV candles from Binance public API and write a backtest-ready " +
654
+ "CSV (header: timestamp,open,high,low,close,volume; timestamp = open time " +
655
+ "in UNIX ms UTC). Supports `spot` and `usdt_perp` (USDT-margined " +
656
+ "perpetual futures). Requests larger than 1000 bars are paginated " +
657
+ "automatically. The output path must " +
658
+ "live inside the MCP cwd unless PINEFORGE_ALLOW_ANYWHERE=1.",
659
+ inputSchema: {
660
+ symbol: z.string().min(2).describe("Binance symbol, e.g. 'BTCUSDT'. Use binance_symbols to validate."),
661
+ interval: IntervalSchema.describe("Kline interval. Spot supports 1s + 1m..1M; usdt_perp supports 1m..1M (no 1s)."),
662
+ market: MarketSchema.optional().describe("'spot' (default) or 'usdt_perp'."),
663
+ limit: z.number().int().min(1).max(100_000).optional()
664
+ .describe("Total bars to fetch. Default 1000. Paginated above 1000."),
665
+ start_time: z.number().int().nonnegative().optional()
666
+ .describe("UNIX ms UTC. If unset, derived from end_time/now and limit."),
667
+ end_time: z.number().int().nonnegative().optional()
668
+ .describe("UNIX ms UTC. Defaults to now."),
669
+ output_path: z.string().describe("Path to write the CSV (will create parent dirs as needed)."),
670
+ },
671
+ }, async ({ symbol, interval, market, limit, start_time, end_time, output_path }) => asTextResult(await fetchBinanceOhlcv({
672
+ symbol,
673
+ interval,
674
+ market: market ?? "spot",
675
+ limit: limit ?? 1000,
676
+ start_time,
677
+ end_time,
678
+ output_path,
679
+ })));
680
+ server.registerTool("binance_symbols", {
681
+ description: "List/validate symbols available on the Binance public API for OHLCV " +
682
+ "fetching. Filters: `query` (substring of the symbol), `quote_asset` " +
683
+ "(e.g. 'USDT'), `base_asset` (e.g. 'BTC'), `status` (e.g. 'TRADING'), " +
684
+ "`contract_type` (futures only, e.g. 'PERPETUAL'). Results are " +
685
+ "cached 5 min in process. Free.",
686
+ inputSchema: {
687
+ market: MarketSchema.describe("'spot' or 'usdt_perp'."),
688
+ query: z.string().optional().describe("Case-insensitive substring of the symbol."),
689
+ quote_asset: z.string().optional().describe("Filter by quote asset (e.g. 'USDT')."),
690
+ base_asset: z.string().optional().describe("Filter by base asset (e.g. 'BTC')."),
691
+ status: z.string().optional().describe("Filter by status. 'TRADING' returns active only."),
692
+ contract_type: z.string().optional().describe("Futures only. 'PERPETUAL' for usdt_perp swaps."),
693
+ limit: z.number().int().min(1).max(2000).optional()
694
+ .describe("Max symbols to return. Default 200."),
695
+ },
696
+ }, async (args) => asTextResult(await binanceSymbols(args)));
697
+ server.registerTool("list_engine_params", {
698
+ description: "Returns the full catalog of engine knobs accepted by backtest_pine / " +
699
+ "backtest_pine_grid in two groups: strategy_overrides (the 9 " +
700
+ "strategy(...) header fields the runtime reads via " +
701
+ "PINEFORGE_OVERRIDES — initial_capital, pyramiding, slippage, " +
702
+ "commission_value, commission_type, default_qty_value, " +
703
+ "default_qty_type, process_orders_on_close, close_entries_rule) and " +
704
+ "runtime_args (input_tf, script_tf, bar_magnifier, " +
705
+ "magnifier_samples, magnifier_dist — args to run_backtest_full, " +
706
+ "NOT part of the strategy() header). Each entry is " +
707
+ "{key, type, enum?, description}. Does not run the engine. " +
708
+ "Use this to discover what knobs the engine exposes " +
709
+ "before issuing a backtest.",
710
+ inputSchema: {},
711
+ }, async () => asTextResult({
712
+ strategy_overrides: Object.entries(STRATEGY_OVERRIDES_CATALOG).map(([key, spec]) => ({
713
+ key,
714
+ type: spec.type,
715
+ ...(spec.enum ? { enum: [...spec.enum] } : {}),
716
+ description: spec.description,
717
+ })),
718
+ runtime_args: Object.entries(RUNTIME_ARGS_CATALOG).map(([key, spec]) => ({
719
+ key,
720
+ type: spec.type,
721
+ ...(spec.enum ? { enum: [...spec.enum] } : {}),
722
+ description: spec.description,
723
+ })),
724
+ }));
725
+ if (opts.imageTools) {
726
+ server.registerTool("pull_engine_image", {
727
+ description: "Run `docker pull` for the pineforge-engine runtime image on the user's " +
728
+ "machine. Useful before the first backtest_pine call.",
729
+ inputSchema: {
730
+ image: z.string().optional().describe(`Image to pull. Defaults to ${DEFAULT_IMAGE}.`),
731
+ },
732
+ }, async ({ image }) => asTextResult(await runner.pullImage(image ?? DEFAULT_IMAGE)));
733
+ server.registerTool("check_engine_image", {
734
+ description: "Check whether the local pineforge-engine Docker image is up to date " +
735
+ "with the registry. Compares per-platform manifest digests via " +
736
+ "`docker manifest inspect --verbose` (no image layers downloaded). " +
737
+ "Returns up_to_date + recommend_pull. With auto_pull=true, runs " +
738
+ "`docker pull` in the same call when the local image is stale or " +
739
+ "missing. Note: this is independent of " +
740
+ "the MCP server's own version (`@pineforge/codegen-mcp`); the MCP " +
741
+ "version and the engine image version evolve separately.",
742
+ inputSchema: {
743
+ image: z.string().optional().describe(`Image to check. Defaults to ${DEFAULT_IMAGE}.`),
744
+ auto_pull: z.boolean().optional().describe("If true and the image is stale or missing, run `docker pull` in " +
745
+ "the same call. Default false (report only)."),
746
+ },
747
+ }, async ({ image, auto_pull }) => asTextResult(await runner.checkImage(image ?? DEFAULT_IMAGE, auto_pull === true)));
748
+ }
749
+ else {
750
+ server.registerTool("engine_info", {
751
+ description: "Report the bundled backtest engine: mode, baked-in flag, and version. " +
752
+ "This image runs the engine in-process (no host Docker daemon needed).",
753
+ inputSchema: {},
754
+ }, async () => asTextResult(await runner.engineInfo()));
755
+ }
756
+ return server;
757
+ }
758
+ //# sourceMappingURL=server.js.map