clodds 1.1.0 → 1.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.
Files changed (115) hide show
  1. package/README.md +79 -47
  2. package/dist/agents/handlers/kalshi.d.ts +1 -2
  3. package/dist/agents/handlers/kalshi.js +369 -616
  4. package/dist/agents/handlers/kalshi.js.map +1 -1
  5. package/dist/agents/handlers/types.d.ts +1 -1
  6. package/dist/agents/index.d.ts +1 -1
  7. package/dist/agents/index.js +435 -710
  8. package/dist/agents/index.js.map +1 -1
  9. package/dist/bittensor/index.d.ts +3 -2
  10. package/dist/bittensor/index.js +44 -5
  11. package/dist/bittensor/index.js.map +1 -1
  12. package/dist/cli/commands/gateway.js +37 -2
  13. package/dist/cli/commands/gateway.js.map +1 -1
  14. package/dist/cli/commands/index.js +3 -2
  15. package/dist/cli/commands/index.js.map +1 -1
  16. package/dist/cli/index.js +6 -4
  17. package/dist/cli/index.js.map +1 -1
  18. package/dist/cron/index.d.ts +1 -1
  19. package/dist/cron/index.js +26 -34
  20. package/dist/cron/index.js.map +1 -1
  21. package/dist/db/index.d.ts +12 -0
  22. package/dist/db/index.js +51 -23
  23. package/dist/db/index.js.map +1 -1
  24. package/dist/db/migrations.js +36 -0
  25. package/dist/db/migrations.js.map +1 -1
  26. package/dist/evm/index.d.ts +1 -0
  27. package/dist/evm/index.js +2 -0
  28. package/dist/evm/index.js.map +1 -1
  29. package/dist/evm/pancakeswap.d.ts +55 -0
  30. package/dist/evm/pancakeswap.js +299 -0
  31. package/dist/evm/pancakeswap.js.map +1 -0
  32. package/dist/exchanges/lighter/index.d.ts +95 -0
  33. package/dist/exchanges/lighter/index.js +154 -0
  34. package/dist/exchanges/lighter/index.js.map +1 -0
  35. package/dist/gateway/server.d.ts +9 -0
  36. package/dist/gateway/server.js +208 -1
  37. package/dist/gateway/server.js.map +1 -1
  38. package/dist/gateway/signal-bus.d.ts +2 -43
  39. package/dist/gateway/signal-bus.js.map +1 -1
  40. package/dist/index.js +40 -3
  41. package/dist/index.js.map +1 -1
  42. package/dist/mcp/security.d.ts +44 -0
  43. package/dist/mcp/security.js +143 -0
  44. package/dist/mcp/security.js.map +1 -0
  45. package/dist/mcp/server.js +27 -2
  46. package/dist/mcp/server.js.map +1 -1
  47. package/dist/services/alt-data/index.d.ts +1 -1
  48. package/dist/sessions/index.js +66 -7
  49. package/dist/sessions/index.js.map +1 -1
  50. package/dist/signal-router/router.d.ts +1 -1
  51. package/dist/signal-router/types.d.ts +1 -1
  52. package/dist/skills/bundled/arbitrage/index.js +96 -63
  53. package/dist/skills/bundled/arbitrage/index.js.map +1 -1
  54. package/dist/skills/bundled/betfair/index.js +121 -83
  55. package/dist/skills/bundled/betfair/index.js.map +1 -1
  56. package/dist/skills/bundled/bridge/index.js +42 -13
  57. package/dist/skills/bundled/bridge/index.js.map +1 -1
  58. package/dist/skills/bundled/crypto-hft/index.js +267 -226
  59. package/dist/skills/bundled/crypto-hft/index.js.map +1 -1
  60. package/dist/skills/bundled/drift/index.js +83 -52
  61. package/dist/skills/bundled/drift/index.js.map +1 -1
  62. package/dist/skills/bundled/embeddings/index.js +87 -49
  63. package/dist/skills/bundled/embeddings/index.js.map +1 -1
  64. package/dist/skills/bundled/execution/index.js +47 -25
  65. package/dist/skills/bundled/execution/index.js.map +1 -1
  66. package/dist/skills/bundled/feeds/index.js +45 -25
  67. package/dist/skills/bundled/feeds/index.js.map +1 -1
  68. package/dist/skills/bundled/hyperliquid/index.js +91 -45
  69. package/dist/skills/bundled/hyperliquid/index.js.map +1 -1
  70. package/dist/skills/bundled/integrations/index.js +2 -1
  71. package/dist/skills/bundled/integrations/index.js.map +1 -1
  72. package/dist/skills/bundled/kamino/index.js +71 -43
  73. package/dist/skills/bundled/kamino/index.js.map +1 -1
  74. package/dist/skills/bundled/lighter/index.d.ts +19 -0
  75. package/dist/skills/bundled/lighter/index.js +385 -0
  76. package/dist/skills/bundled/lighter/index.js.map +1 -0
  77. package/dist/skills/bundled/marginfi/index.d.ts +21 -0
  78. package/dist/skills/bundled/marginfi/index.js +387 -0
  79. package/dist/skills/bundled/marginfi/index.js.map +1 -0
  80. package/dist/skills/bundled/opinion/index.js +44 -25
  81. package/dist/skills/bundled/opinion/index.js.map +1 -1
  82. package/dist/skills/bundled/pancakeswap/index.d.ts +19 -0
  83. package/dist/skills/bundled/pancakeswap/index.js +260 -0
  84. package/dist/skills/bundled/pancakeswap/index.js.map +1 -0
  85. package/dist/skills/bundled/setup/index.d.ts +16 -0
  86. package/dist/skills/bundled/setup/index.js +427 -0
  87. package/dist/skills/bundled/setup/index.js.map +1 -0
  88. package/dist/skills/bundled/solend/index.d.ts +22 -0
  89. package/dist/skills/bundled/solend/index.js +401 -0
  90. package/dist/skills/bundled/solend/index.js.map +1 -0
  91. package/dist/skills/bundled/trading-evm/index.js +42 -11
  92. package/dist/skills/bundled/trading-evm/index.js.map +1 -1
  93. package/dist/skills/errors.d.ts +20 -0
  94. package/dist/skills/errors.js +84 -0
  95. package/dist/skills/errors.js.map +1 -0
  96. package/dist/skills/executor.d.ts +1 -1
  97. package/dist/skills/executor.js +515 -41
  98. package/dist/skills/executor.js.map +1 -1
  99. package/dist/skills/help.d.ts +33 -0
  100. package/dist/skills/help.js +73 -0
  101. package/dist/skills/help.js.map +1 -0
  102. package/dist/solana/marginfi.d.ts +112 -0
  103. package/dist/solana/marginfi.js +340 -0
  104. package/dist/solana/marginfi.js.map +1 -0
  105. package/dist/solana/solend.d.ts +120 -0
  106. package/dist/solana/solend.js +261 -0
  107. package/dist/solana/solend.js.map +1 -0
  108. package/dist/trading/bridge.d.ts +1 -1
  109. package/dist/trading/position-bridge.d.ts +1 -1
  110. package/dist/types/signal-bus.d.ts +48 -0
  111. package/dist/types/signal-bus.js +9 -0
  112. package/dist/types/signal-bus.js.map +1 -0
  113. package/dist/types.d.ts +17 -0
  114. package/dist/utils/json-utils.d.ts +8 -8
  115. package/package.json +8 -6
@@ -3,33 +3,17 @@
3
3
  * Kalshi Handlers
4
4
  *
5
5
  * All 78 Kalshi platform handlers migrated from agents/index.ts switch cases.
6
- * Includes execution-service-based trading, no-auth exchange info,
7
- * and credential-based Python CLI handlers.
6
+ * Uses direct HTTP calls to the Kalshi REST API with RSA-PSS authentication.
8
7
  */
9
8
  Object.defineProperty(exports, "__esModule", { value: true });
10
9
  exports.kalshiHandlers = void 0;
11
- const child_process_1 = require("child_process");
12
- const path_1 = require("path");
13
10
  const types_1 = require("./types");
11
+ const kalshi_auth_1 = require("../../utils/kalshi-auth");
14
12
  const risk_1 = require("../../trading/risk");
15
13
  // =============================================================================
16
- // HELPERS
14
+ // CONSTANTS & HELPERS
17
15
  // =============================================================================
18
- /**
19
- * Build environment variables for Kalshi Python CLI
20
- */
21
- function buildKalshiEnv(creds) {
22
- const env = { ...process.env };
23
- if (creds.apiKeyId && creds.privateKeyPem) {
24
- env.KALSHI_API_KEY_ID = creds.apiKeyId;
25
- env.KALSHI_PRIVATE_KEY = creds.privateKeyPem;
26
- }
27
- if (creds.email && creds.password) {
28
- env.KALSHI_EMAIL = creds.email;
29
- env.KALSHI_PASSWORD = creds.password;
30
- }
31
- return env;
32
- }
16
+ const KALSHI_API_BASE = 'https://trading-api.kalshi.com/trade-api/v2';
33
17
  /**
34
18
  * Get Kalshi credentials from handler context
35
19
  */
@@ -40,23 +24,63 @@ function getKalshiCreds(context) {
40
24
  return kalshiCreds;
41
25
  }
42
26
  /**
43
- * Get the trading directory path (relative to this file's location)
27
+ * Build KalshiApiKeyAuth from KalshiCredentials
44
28
  */
45
- function getTradingDir() {
46
- return (0, path_1.join)(__dirname, '..', '..', '..', 'trading');
29
+ function toApiKeyAuth(creds) {
30
+ if (!creds.apiKeyId || !creds.privateKeyPem)
31
+ return null;
32
+ return { apiKeyId: creds.apiKeyId, privateKeyPem: creds.privateKeyPem };
47
33
  }
48
34
  /**
49
- * Execute a Kalshi Python CLI command with credentials
35
+ * Make an unauthenticated GET request to the Kalshi API
50
36
  */
51
- function execKalshiPython(cmd, userEnv) {
37
+ async function kalshiGet(path) {
38
+ const url = `${KALSHI_API_BASE}${path}`;
52
39
  try {
53
- const output = (0, child_process_1.execSync)(cmd, { timeout: 30000, encoding: 'utf-8', env: userEnv });
54
- return output.trim();
40
+ const response = await fetch(url, {
41
+ headers: { 'Content-Type': 'application/json' },
42
+ });
43
+ const data = await response.json();
44
+ return JSON.stringify(data);
55
45
  }
56
46
  catch (err) {
57
- return JSON.stringify({ error: err.stderr || err.message });
47
+ return JSON.stringify({ error: err.message });
58
48
  }
59
49
  }
50
+ /**
51
+ * Make an authenticated request to the Kalshi API
52
+ */
53
+ async function kalshiAuthFetch(auth, method, path, body) {
54
+ const url = `${KALSHI_API_BASE}${path}`;
55
+ const headers = (0, kalshi_auth_1.buildKalshiHeadersForUrl)(auth, method, url);
56
+ const fetchOptions = {
57
+ method,
58
+ headers: { ...headers, 'Content-Type': 'application/json' },
59
+ };
60
+ if (body !== undefined) {
61
+ fetchOptions.body = JSON.stringify(body);
62
+ }
63
+ try {
64
+ const response = await fetch(url, fetchOptions);
65
+ const data = await response.json();
66
+ return JSON.stringify(data);
67
+ }
68
+ catch (err) {
69
+ return JSON.stringify({ error: err.message });
70
+ }
71
+ }
72
+ /**
73
+ * Require auth credentials from context, returning auth object or error string
74
+ */
75
+ function requireAuth(context) {
76
+ const kalshiCreds = getKalshiCreds(context);
77
+ if (!kalshiCreds)
78
+ return (0, types_1.errorResult)('No Kalshi credentials set up. Use setup_kalshi_credentials first.');
79
+ const auth = toApiKeyAuth(kalshiCreds.data);
80
+ if (!auth)
81
+ return (0, types_1.errorResult)('Kalshi API key credentials required (apiKeyId + privateKeyPem).');
82
+ return auth;
83
+ }
60
84
  // =============================================================================
61
85
  // EXECUTION-SERVICE-BASED HANDLERS
62
86
  // =============================================================================
@@ -164,7 +188,7 @@ async function sellHandler(toolInput, context) {
164
188
  /**
165
189
  * kalshi_orders - Get open orders via execution service
166
190
  */
167
- async function ordersHandler(toolInput, context) {
191
+ async function ordersHandler(_toolInput, context) {
168
192
  const execSvc = context.tradingContext?.executionService;
169
193
  if (execSvc) {
170
194
  try {
@@ -227,220 +251,143 @@ async function cancelHandler(toolInput, context) {
227
251
  * kalshi_exchange_status - Get exchange status (no auth required)
228
252
  */
229
253
  async function exchangeStatusHandler(_toolInput, _context) {
230
- const tradingDir = getTradingDir();
231
- const cmd = `cd ${tradingDir} && python3 kalshi.py exchange_status`;
232
- try {
233
- const output = (0, child_process_1.execSync)(cmd, { timeout: 30000, encoding: 'utf-8' });
234
- return output.trim();
235
- }
236
- catch (err) {
237
- return JSON.stringify({ error: err.stderr || err.message });
238
- }
254
+ return kalshiGet('/exchange/status');
239
255
  }
240
256
  /**
241
257
  * kalshi_exchange_schedule - Get exchange schedule (no auth required)
242
258
  */
243
259
  async function exchangeScheduleHandler(_toolInput, _context) {
244
- const tradingDir = getTradingDir();
245
- const cmd = `cd ${tradingDir} && python3 kalshi.py exchange_schedule`;
246
- try {
247
- const output = (0, child_process_1.execSync)(cmd, { timeout: 30000, encoding: 'utf-8' });
248
- return output.trim();
249
- }
250
- catch (err) {
251
- return JSON.stringify({ error: err.stderr || err.message });
252
- }
260
+ return kalshiGet('/exchange/schedule');
253
261
  }
254
262
  /**
255
263
  * kalshi_announcements - Get exchange announcements (no auth required)
256
264
  */
257
265
  async function announcementsHandler(_toolInput, _context) {
258
- const tradingDir = getTradingDir();
259
- const cmd = `cd ${tradingDir} && python3 kalshi.py announcements`;
260
- try {
261
- const output = (0, child_process_1.execSync)(cmd, { timeout: 30000, encoding: 'utf-8' });
262
- return output.trim();
263
- }
264
- catch (err) {
265
- return JSON.stringify({ error: err.stderr || err.message });
266
- }
266
+ return kalshiGet('/exchange/announcements');
267
267
  }
268
268
  // =============================================================================
269
- // CREDENTIAL-BASED PYTHON HANDLERS
269
+ // AUTHENTICATED REST API HANDLERS
270
270
  // =============================================================================
271
271
  /**
272
272
  * kalshi_positions - Get current positions
273
273
  */
274
274
  async function positionsHandler(_toolInput, context) {
275
- const kalshiCreds = getKalshiCreds(context);
276
- if (!kalshiCreds)
277
- return (0, types_1.errorResult)('No Kalshi credentials set up. Use setup_kalshi_credentials first.');
278
- const tradingDir = getTradingDir();
279
- const cmd = `cd ${tradingDir} && python3 kalshi.py positions`;
280
- const userEnv = buildKalshiEnv(kalshiCreds.data);
281
- try {
282
- const output = (0, child_process_1.execSync)(cmd, { timeout: 30000, encoding: 'utf-8', env: userEnv });
283
- return JSON.stringify({ result: output.trim() });
284
- }
285
- catch (error) {
286
- return JSON.stringify({ error: 'Failed to get positions', details: error.stderr || error.message });
287
- }
275
+ const auth = requireAuth(context);
276
+ if (typeof auth === 'string')
277
+ return auth;
278
+ return kalshiAuthFetch(auth, 'GET', '/portfolio/positions');
288
279
  }
289
280
  /**
290
281
  * kalshi_search - Search for markets
291
282
  */
292
- async function searchHandler(toolInput, context) {
293
- const kalshiCreds = getKalshiCreds(context);
294
- if (!kalshiCreds)
295
- return (0, types_1.errorResult)('No Kalshi credentials set up. Use setup_kalshi_credentials first.');
283
+ async function searchHandler(toolInput, _context) {
296
284
  const query = toolInput.query;
297
- const tradingDir = getTradingDir();
298
- const sanitizedQuery = query ? query.replace(/[^a-zA-Z0-9\s_\-.,]/g, '') : '';
299
- const cmd = sanitizedQuery
300
- ? `cd ${tradingDir} && python3 kalshi.py search "${sanitizedQuery}"`
301
- : `cd ${tradingDir} && python3 kalshi.py search`;
302
- const userEnv = buildKalshiEnv(kalshiCreds.data);
303
- return execKalshiPython(cmd, userEnv);
285
+ const params = new URLSearchParams({ status: 'open' });
286
+ if (query)
287
+ params.set('query', query);
288
+ return kalshiGet(`/markets?${params.toString()}`);
304
289
  }
305
290
  /**
306
291
  * kalshi_market - Get market details
307
292
  */
308
- async function marketHandler(toolInput, context) {
309
- const kalshiCreds = getKalshiCreds(context);
310
- if (!kalshiCreds)
311
- return (0, types_1.errorResult)('No Kalshi credentials set up. Use setup_kalshi_credentials first.');
293
+ async function marketHandler(toolInput, _context) {
312
294
  const ticker = toolInput.ticker;
313
- const tradingDir = getTradingDir();
314
- const cmd = `cd ${tradingDir} && python3 kalshi.py market ${ticker}`;
315
- const userEnv = buildKalshiEnv(kalshiCreds.data);
316
- return execKalshiPython(cmd, userEnv);
295
+ return kalshiGet(`/markets/${encodeURIComponent(ticker)}`);
317
296
  }
318
297
  /**
319
298
  * kalshi_balance - Get account balance
320
299
  */
321
300
  async function balanceHandler(_toolInput, context) {
322
- const kalshiCreds = getKalshiCreds(context);
323
- if (!kalshiCreds)
324
- return (0, types_1.errorResult)('No Kalshi credentials set up. Use setup_kalshi_credentials first.');
325
- const tradingDir = getTradingDir();
326
- const cmd = `cd ${tradingDir} && python3 kalshi.py balance`;
327
- const userEnv = buildKalshiEnv(kalshiCreds.data);
328
- return execKalshiPython(cmd, userEnv);
301
+ const auth = requireAuth(context);
302
+ if (typeof auth === 'string')
303
+ return auth;
304
+ return kalshiAuthFetch(auth, 'GET', '/portfolio/balance');
329
305
  }
330
306
  /**
331
307
  * kalshi_orderbook - Get orderbook for a market
332
308
  */
333
- async function orderbookHandler(toolInput, context) {
334
- const kalshiCreds = getKalshiCreds(context);
335
- if (!kalshiCreds)
336
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
309
+ async function orderbookHandler(toolInput, _context) {
337
310
  const ticker = toolInput.ticker;
338
- const tradingDir = getTradingDir();
339
- const cmd = `cd ${tradingDir} && python3 kalshi.py orderbook ${ticker}`;
340
- const userEnv = buildKalshiEnv(kalshiCreds.data);
341
- return execKalshiPython(cmd, userEnv);
311
+ return kalshiGet(`/markets/${encodeURIComponent(ticker)}/orderbook`);
342
312
  }
343
313
  /**
344
314
  * kalshi_market_trades - Get recent trades for a market
345
315
  */
346
- async function marketTradesHandler(toolInput, context) {
347
- const kalshiCreds = getKalshiCreds(context);
348
- if (!kalshiCreds)
349
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
316
+ async function marketTradesHandler(toolInput, _context) {
350
317
  const ticker = toolInput.ticker;
351
318
  const limit = toolInput.limit;
352
- const tradingDir = getTradingDir();
353
- let cmd = `cd ${tradingDir} && python3 kalshi.py market_trades`;
354
- if (ticker)
355
- cmd += ` --ticker ${ticker}`;
319
+ if (ticker) {
320
+ const params = new URLSearchParams();
321
+ if (limit)
322
+ params.set('limit', String(limit));
323
+ const qs = params.toString();
324
+ return kalshiGet(`/markets/${encodeURIComponent(ticker)}/trades${qs ? `?${qs}` : ''}`);
325
+ }
326
+ // No ticker: list all trades
327
+ const params = new URLSearchParams();
356
328
  if (limit)
357
- cmd += ` --limit ${limit}`;
358
- const userEnv = buildKalshiEnv(kalshiCreds.data);
359
- return execKalshiPython(cmd, userEnv);
329
+ params.set('limit', String(limit));
330
+ const qs = params.toString();
331
+ return kalshiGet(`/markets/trades${qs ? `?${qs}` : ''}`);
360
332
  }
361
333
  /**
362
334
  * kalshi_candlesticks - Get candlestick data
363
335
  */
364
- async function candlesticksHandler(toolInput, context) {
365
- const kalshiCreds = getKalshiCreds(context);
366
- if (!kalshiCreds)
367
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
336
+ async function candlesticksHandler(toolInput, _context) {
368
337
  const seriesTicker = toolInput.series_ticker;
369
338
  const ticker = toolInput.ticker;
370
339
  const interval = toolInput.interval;
371
- const tradingDir = getTradingDir();
372
- let cmd = `cd ${tradingDir} && python3 kalshi.py candlesticks ${seriesTicker} ${ticker}`;
340
+ const params = new URLSearchParams({ series_ticker: seriesTicker });
373
341
  if (interval)
374
- cmd += ` --interval ${interval}`;
375
- const userEnv = buildKalshiEnv(kalshiCreds.data);
376
- return execKalshiPython(cmd, userEnv);
342
+ params.set('period_interval', String(interval));
343
+ return kalshiGet(`/markets/${encodeURIComponent(ticker)}/candlesticks?${params.toString()}`);
377
344
  }
378
345
  /**
379
346
  * kalshi_events - List events
380
347
  */
381
- async function eventsHandler(toolInput, context) {
382
- const kalshiCreds = getKalshiCreds(context);
383
- if (!kalshiCreds)
384
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
348
+ async function eventsHandler(toolInput, _context) {
385
349
  const status = toolInput.status;
386
350
  const seriesTicker = toolInput.series_ticker;
387
- const tradingDir = getTradingDir();
388
- let cmd = `cd ${tradingDir} && python3 kalshi.py events`;
351
+ const params = new URLSearchParams();
389
352
  if (status)
390
- cmd += ` --status ${status}`;
353
+ params.set('status', status);
391
354
  if (seriesTicker)
392
- cmd += ` --series ${seriesTicker}`;
393
- const userEnv = buildKalshiEnv(kalshiCreds.data);
394
- return execKalshiPython(cmd, userEnv);
355
+ params.set('series_ticker', seriesTicker);
356
+ const qs = params.toString();
357
+ return kalshiGet(`/events${qs ? `?${qs}` : ''}`);
395
358
  }
396
359
  /**
397
360
  * kalshi_event - Get event details
398
361
  */
399
- async function eventHandler(toolInput, context) {
400
- const kalshiCreds = getKalshiCreds(context);
401
- if (!kalshiCreds)
402
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
362
+ async function eventHandler(toolInput, _context) {
403
363
  const eventTicker = toolInput.event_ticker;
404
- const tradingDir = getTradingDir();
405
- const cmd = `cd ${tradingDir} && python3 kalshi.py event ${eventTicker}`;
406
- const userEnv = buildKalshiEnv(kalshiCreds.data);
407
- return execKalshiPython(cmd, userEnv);
364
+ return kalshiGet(`/events/${encodeURIComponent(eventTicker)}`);
408
365
  }
409
366
  /**
410
367
  * kalshi_series - List series
411
368
  */
412
- async function seriesHandler(toolInput, context) {
413
- const kalshiCreds = getKalshiCreds(context);
414
- if (!kalshiCreds)
415
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
369
+ async function seriesHandler(toolInput, _context) {
416
370
  const category = toolInput.category;
417
- const tradingDir = getTradingDir();
418
- let cmd = `cd ${tradingDir} && python3 kalshi.py series`;
371
+ const params = new URLSearchParams();
419
372
  if (category)
420
- cmd += ` --category ${category}`;
421
- const userEnv = buildKalshiEnv(kalshiCreds.data);
422
- return execKalshiPython(cmd, userEnv);
373
+ params.set('category', category);
374
+ const qs = params.toString();
375
+ return kalshiGet(`/series${qs ? `?${qs}` : ''}`);
423
376
  }
424
377
  /**
425
378
  * kalshi_series_info - Get series info
426
379
  */
427
- async function seriesInfoHandler(toolInput, context) {
428
- const kalshiCreds = getKalshiCreds(context);
429
- if (!kalshiCreds)
430
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
380
+ async function seriesInfoHandler(toolInput, _context) {
431
381
  const seriesTicker = toolInput.series_ticker;
432
- const tradingDir = getTradingDir();
433
- const cmd = `cd ${tradingDir} && python3 kalshi.py series_info ${seriesTicker}`;
434
- const userEnv = buildKalshiEnv(kalshiCreds.data);
435
- return execKalshiPython(cmd, userEnv);
382
+ return kalshiGet(`/series/${encodeURIComponent(seriesTicker)}`);
436
383
  }
437
384
  /**
438
385
  * kalshi_market_order - Place a market order with risk checks
439
386
  */
440
387
  async function marketOrderHandler(toolInput, context) {
441
- const kalshiCreds = getKalshiCreds(context);
442
- if (!kalshiCreds)
443
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
388
+ const auth = requireAuth(context);
389
+ if (typeof auth === 'string')
390
+ return auth;
444
391
  const ticker = toolInput.ticker;
445
392
  const side = toolInput.side;
446
393
  const action = toolInput.action;
@@ -460,18 +407,21 @@ async function marketOrderHandler(toolInput, context) {
460
407
  if (exposureError)
461
408
  return exposureError;
462
409
  }
463
- const tradingDir = getTradingDir();
464
- const cmd = `cd ${tradingDir} && python3 kalshi.py market_order ${ticker} ${side} ${action} ${count}`;
465
- const userEnv = buildKalshiEnv(kalshiCreds.data);
466
- return execKalshiPython(cmd, userEnv);
410
+ return kalshiAuthFetch(auth, 'POST', '/portfolio/orders', {
411
+ ticker,
412
+ side,
413
+ action,
414
+ count,
415
+ type: 'market',
416
+ });
467
417
  }
468
418
  /**
469
419
  * kalshi_batch_create_orders - Batch create orders with risk checks
470
420
  */
471
421
  async function batchCreateOrdersHandler(toolInput, context) {
472
- const kalshiCreds = getKalshiCreds(context);
473
- if (!kalshiCreds)
474
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
422
+ const auth = requireAuth(context);
423
+ if (typeof auth === 'string')
424
+ return auth;
475
425
  const orders = toolInput.orders;
476
426
  const userId = context.userId || '';
477
427
  if (Array.isArray(orders) && orders.length > 0) {
@@ -515,777 +465,580 @@ async function batchCreateOrdersHandler(toolInput, context) {
515
465
  return exposureError;
516
466
  }
517
467
  }
518
- const tradingDir = getTradingDir();
519
- const ordersJson = JSON.stringify(orders).replace(/"/g, '\\"');
520
- const cmd = `cd ${tradingDir} && python3 kalshi.py batch_create_orders "${ordersJson}"`;
521
- const userEnv = buildKalshiEnv(kalshiCreds.data);
522
- return execKalshiPython(cmd, userEnv);
468
+ return kalshiAuthFetch(auth, 'POST', '/portfolio/orders/batched', { orders });
523
469
  }
524
470
  /**
525
471
  * kalshi_batch_cancel_orders - Batch cancel orders
526
472
  */
527
473
  async function batchCancelOrdersHandler(toolInput, context) {
528
- const kalshiCreds = getKalshiCreds(context);
529
- if (!kalshiCreds)
530
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
474
+ const auth = requireAuth(context);
475
+ if (typeof auth === 'string')
476
+ return auth;
531
477
  const orderIds = toolInput.order_ids;
532
- const tradingDir = getTradingDir();
533
- const cmd = `cd ${tradingDir} && python3 kalshi.py batch_cancel_orders ${orderIds.join(',')}`;
534
- const userEnv = buildKalshiEnv(kalshiCreds.data);
535
- return execKalshiPython(cmd, userEnv);
478
+ return kalshiAuthFetch(auth, 'DELETE', '/portfolio/orders/batched', { order_ids: orderIds });
536
479
  }
537
480
  /**
538
481
  * kalshi_cancel_all - Cancel all open orders
539
482
  */
540
483
  async function cancelAllHandler(_toolInput, context) {
541
- const kalshiCreds = getKalshiCreds(context);
542
- if (!kalshiCreds)
543
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
544
- const tradingDir = getTradingDir();
545
- const cmd = `cd ${tradingDir} && python3 kalshi.py cancel_all`;
546
- const userEnv = buildKalshiEnv(kalshiCreds.data);
547
- return execKalshiPython(cmd, userEnv);
484
+ const auth = requireAuth(context);
485
+ if (typeof auth === 'string')
486
+ return auth;
487
+ return kalshiAuthFetch(auth, 'DELETE', '/portfolio/orders');
548
488
  }
549
489
  /**
550
490
  * kalshi_get_order - Get order details
551
491
  */
552
492
  async function getOrderHandler(toolInput, context) {
553
- const kalshiCreds = getKalshiCreds(context);
554
- if (!kalshiCreds)
555
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
493
+ const auth = requireAuth(context);
494
+ if (typeof auth === 'string')
495
+ return auth;
556
496
  const orderId = toolInput.order_id;
557
- const tradingDir = getTradingDir();
558
- const cmd = `cd ${tradingDir} && python3 kalshi.py get_order ${orderId}`;
559
- const userEnv = buildKalshiEnv(kalshiCreds.data);
560
- return execKalshiPython(cmd, userEnv);
497
+ return kalshiAuthFetch(auth, 'GET', `/portfolio/orders/${encodeURIComponent(orderId)}`);
561
498
  }
562
499
  /**
563
500
  * kalshi_amend_order - Amend an existing order
564
501
  */
565
502
  async function amendOrderHandler(toolInput, context) {
566
- const kalshiCreds = getKalshiCreds(context);
567
- if (!kalshiCreds)
568
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
503
+ const auth = requireAuth(context);
504
+ if (typeof auth === 'string')
505
+ return auth;
569
506
  const orderId = toolInput.order_id;
570
507
  const price = toolInput.price;
571
508
  const count = toolInput.count;
572
- const tradingDir = getTradingDir();
573
- let cmd = `cd ${tradingDir} && python3 kalshi.py amend_order ${orderId}`;
574
- if (price)
575
- cmd += ` --price ${price}`;
576
- if (count)
577
- cmd += ` --count ${count}`;
578
- const userEnv = buildKalshiEnv(kalshiCreds.data);
579
- return execKalshiPython(cmd, userEnv);
509
+ const body = {};
510
+ if (price !== undefined)
511
+ body.price = price;
512
+ if (count !== undefined)
513
+ body.count = count;
514
+ return kalshiAuthFetch(auth, 'PATCH', `/portfolio/orders/${encodeURIComponent(orderId)}`, body);
580
515
  }
581
516
  /**
582
517
  * kalshi_decrease_order - Decrease order size
583
518
  */
584
519
  async function decreaseOrderHandler(toolInput, context) {
585
- const kalshiCreds = getKalshiCreds(context);
586
- if (!kalshiCreds)
587
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
520
+ const auth = requireAuth(context);
521
+ if (typeof auth === 'string')
522
+ return auth;
588
523
  const orderId = toolInput.order_id;
589
524
  const reduceBy = toolInput.reduce_by;
590
- const tradingDir = getTradingDir();
591
- const cmd = `cd ${tradingDir} && python3 kalshi.py decrease_order ${orderId} ${reduceBy}`;
592
- const userEnv = buildKalshiEnv(kalshiCreds.data);
593
- return execKalshiPython(cmd, userEnv);
525
+ return kalshiAuthFetch(auth, 'POST', `/portfolio/orders/${encodeURIComponent(orderId)}/decrease`, { reduce_by: reduceBy });
594
526
  }
595
527
  /**
596
528
  * kalshi_queue_position - Get queue position for an order
597
529
  */
598
530
  async function queuePositionHandler(toolInput, context) {
599
- const kalshiCreds = getKalshiCreds(context);
600
- if (!kalshiCreds)
601
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
531
+ const auth = requireAuth(context);
532
+ if (typeof auth === 'string')
533
+ return auth;
602
534
  const orderId = toolInput.order_id;
603
- const tradingDir = getTradingDir();
604
- const cmd = `cd ${tradingDir} && python3 kalshi.py queue_position ${orderId}`;
605
- const userEnv = buildKalshiEnv(kalshiCreds.data);
606
- return execKalshiPython(cmd, userEnv);
535
+ return kalshiAuthFetch(auth, 'GET', `/portfolio/orders/${encodeURIComponent(orderId)}/position`);
607
536
  }
608
537
  /**
609
538
  * kalshi_queue_positions - Get all queue positions
610
539
  */
611
540
  async function queuePositionsHandler(_toolInput, context) {
612
- const kalshiCreds = getKalshiCreds(context);
613
- if (!kalshiCreds)
614
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
615
- const tradingDir = getTradingDir();
616
- const cmd = `cd ${tradingDir} && python3 kalshi.py queue_positions`;
617
- const userEnv = buildKalshiEnv(kalshiCreds.data);
618
- return execKalshiPython(cmd, userEnv);
541
+ const auth = requireAuth(context);
542
+ if (typeof auth === 'string')
543
+ return auth;
544
+ return kalshiAuthFetch(auth, 'GET', '/portfolio/orders/queue-positions');
619
545
  }
620
546
  /**
621
547
  * kalshi_fills - Get fill history
622
548
  */
623
549
  async function fillsHandler(toolInput, context) {
624
- const kalshiCreds = getKalshiCreds(context);
625
- if (!kalshiCreds)
626
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
550
+ const auth = requireAuth(context);
551
+ if (typeof auth === 'string')
552
+ return auth;
627
553
  const ticker = toolInput.ticker;
628
554
  const limit = toolInput.limit;
629
- const tradingDir = getTradingDir();
630
- let cmd = `cd ${tradingDir} && python3 kalshi.py fills`;
555
+ const params = new URLSearchParams();
631
556
  if (ticker)
632
- cmd += ` --ticker ${ticker}`;
557
+ params.set('ticker', ticker);
633
558
  if (limit)
634
- cmd += ` --limit ${limit}`;
635
- const userEnv = buildKalshiEnv(kalshiCreds.data);
636
- return execKalshiPython(cmd, userEnv);
559
+ params.set('limit', String(limit));
560
+ const qs = params.toString();
561
+ return kalshiAuthFetch(auth, 'GET', `/portfolio/fills${qs ? `?${qs}` : ''}`);
637
562
  }
638
563
  /**
639
564
  * kalshi_settlements - Get settlement history
640
565
  */
641
566
  async function settlementsHandler(toolInput, context) {
642
- const kalshiCreds = getKalshiCreds(context);
643
- if (!kalshiCreds)
644
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
567
+ const auth = requireAuth(context);
568
+ if (typeof auth === 'string')
569
+ return auth;
645
570
  const limit = toolInput.limit;
646
- const tradingDir = getTradingDir();
647
- let cmd = `cd ${tradingDir} && python3 kalshi.py settlements`;
571
+ const params = new URLSearchParams();
648
572
  if (limit)
649
- cmd += ` --limit ${limit}`;
650
- const userEnv = buildKalshiEnv(kalshiCreds.data);
651
- return execKalshiPython(cmd, userEnv);
573
+ params.set('limit', String(limit));
574
+ const qs = params.toString();
575
+ return kalshiAuthFetch(auth, 'GET', `/portfolio/settlements${qs ? `?${qs}` : ''}`);
652
576
  }
653
577
  /**
654
578
  * kalshi_account_limits - Get account limits
655
579
  */
656
580
  async function accountLimitsHandler(_toolInput, context) {
657
- const kalshiCreds = getKalshiCreds(context);
658
- if (!kalshiCreds)
659
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
660
- const tradingDir = getTradingDir();
661
- const cmd = `cd ${tradingDir} && python3 kalshi.py account_limits`;
662
- const userEnv = buildKalshiEnv(kalshiCreds.data);
663
- return execKalshiPython(cmd, userEnv);
581
+ const auth = requireAuth(context);
582
+ if (typeof auth === 'string')
583
+ return auth;
584
+ return kalshiAuthFetch(auth, 'GET', '/portfolio/account/limits');
664
585
  }
665
586
  /**
666
587
  * kalshi_api_keys - List API keys
667
588
  */
668
589
  async function apiKeysHandler(_toolInput, context) {
669
- const kalshiCreds = getKalshiCreds(context);
670
- if (!kalshiCreds)
671
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
672
- const tradingDir = getTradingDir();
673
- const cmd = `cd ${tradingDir} && python3 kalshi.py api_keys`;
674
- const userEnv = buildKalshiEnv(kalshiCreds.data);
675
- return execKalshiPython(cmd, userEnv);
590
+ const auth = requireAuth(context);
591
+ if (typeof auth === 'string')
592
+ return auth;
593
+ return kalshiAuthFetch(auth, 'GET', '/portfolio/api-keys');
676
594
  }
677
595
  /**
678
596
  * kalshi_create_api_key - Create a new API key
679
597
  */
680
598
  async function createApiKeyHandler(_toolInput, context) {
681
- const kalshiCreds = getKalshiCreds(context);
682
- if (!kalshiCreds)
683
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
684
- const tradingDir = getTradingDir();
685
- const cmd = `cd ${tradingDir} && python3 kalshi.py create_api_key`;
686
- const userEnv = buildKalshiEnv(kalshiCreds.data);
687
- return execKalshiPython(cmd, userEnv);
599
+ const auth = requireAuth(context);
600
+ if (typeof auth === 'string')
601
+ return auth;
602
+ return kalshiAuthFetch(auth, 'POST', '/portfolio/api-keys', {});
688
603
  }
689
604
  /**
690
605
  * kalshi_delete_api_key - Delete an API key
691
606
  */
692
607
  async function deleteApiKeyHandler(toolInput, context) {
693
- const kalshiCreds = getKalshiCreds(context);
694
- if (!kalshiCreds)
695
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
608
+ const auth = requireAuth(context);
609
+ if (typeof auth === 'string')
610
+ return auth;
696
611
  const apiKey = toolInput.api_key;
697
- const tradingDir = getTradingDir();
698
- const cmd = `cd ${tradingDir} && python3 kalshi.py delete_api_key ${apiKey}`;
699
- const userEnv = buildKalshiEnv(kalshiCreds.data);
700
- return execKalshiPython(cmd, userEnv);
612
+ return kalshiAuthFetch(auth, 'DELETE', `/portfolio/api-keys/${encodeURIComponent(apiKey)}`);
701
613
  }
702
614
  /**
703
615
  * kalshi_fee_changes - Get fee changes
704
616
  */
705
- async function feeChangesHandler(_toolInput, context) {
706
- const kalshiCreds = getKalshiCreds(context);
707
- if (!kalshiCreds)
708
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
709
- const tradingDir = getTradingDir();
710
- const cmd = `cd ${tradingDir} && python3 kalshi.py fee_changes`;
711
- const userEnv = buildKalshiEnv(kalshiCreds.data);
712
- return execKalshiPython(cmd, userEnv);
617
+ async function feeChangesHandler(_toolInput, _context) {
618
+ return kalshiGet('/exchange/fee-changes');
713
619
  }
714
620
  /**
715
621
  * kalshi_user_data_timestamp - Get user data timestamp
716
622
  */
717
623
  async function userDataTimestampHandler(_toolInput, context) {
718
- const kalshiCreds = getKalshiCreds(context);
719
- if (!kalshiCreds)
720
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
721
- const tradingDir = getTradingDir();
722
- const cmd = `cd ${tradingDir} && python3 kalshi.py user_data_timestamp`;
723
- const userEnv = buildKalshiEnv(kalshiCreds.data);
724
- return execKalshiPython(cmd, userEnv);
624
+ const auth = requireAuth(context);
625
+ if (typeof auth === 'string')
626
+ return auth;
627
+ return kalshiAuthFetch(auth, 'GET', '/portfolio/user-data/timestamp');
725
628
  }
726
629
  /**
727
- * kalshi_batch_candlesticks - Get batch candlestick data
630
+ * kalshi_batch_candlesticks - Get batch candlestick data for multiple tickers
728
631
  */
729
- async function batchCandlesticksHandler(toolInput, context) {
730
- const kalshiCreds = getKalshiCreds(context);
731
- if (!kalshiCreds)
732
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
632
+ async function batchCandlesticksHandler(toolInput, _context) {
733
633
  const tickers = toolInput.tickers;
734
- const tradingDir = getTradingDir();
735
- const cmd = `cd ${tradingDir} && python3 kalshi.py batch_candlesticks '${JSON.stringify(tickers)}'`;
736
- const userEnv = buildKalshiEnv(kalshiCreds.data);
737
- return execKalshiPython(cmd, userEnv);
634
+ // Fetch candlesticks for each ticker in parallel
635
+ try {
636
+ const results = await Promise.all(tickers.map(async (t) => {
637
+ const params = new URLSearchParams({ series_ticker: t.series_ticker });
638
+ if (t.interval)
639
+ params.set('period_interval', String(t.interval));
640
+ const url = `${KALSHI_API_BASE}/markets/${encodeURIComponent(t.ticker)}/candlesticks?${params.toString()}`;
641
+ const response = await fetch(url, { headers: { 'Content-Type': 'application/json' } });
642
+ return { ticker: t.ticker, data: await response.json() };
643
+ }));
644
+ return JSON.stringify(results);
645
+ }
646
+ catch (err) {
647
+ return JSON.stringify({ error: err.message });
648
+ }
738
649
  }
739
650
  /**
740
651
  * kalshi_event_metadata - Get event metadata
741
652
  */
742
- async function eventMetadataHandler(toolInput, context) {
743
- const kalshiCreds = getKalshiCreds(context);
744
- if (!kalshiCreds)
745
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
653
+ async function eventMetadataHandler(toolInput, _context) {
746
654
  const eventTicker = toolInput.event_ticker;
747
- const tradingDir = getTradingDir();
748
- const cmd = `cd ${tradingDir} && python3 kalshi.py event_metadata ${eventTicker}`;
749
- const userEnv = buildKalshiEnv(kalshiCreds.data);
750
- return execKalshiPython(cmd, userEnv);
655
+ return kalshiGet(`/events/${encodeURIComponent(eventTicker)}/metadata`);
751
656
  }
752
657
  /**
753
658
  * kalshi_event_candlesticks - Get event candlestick data
754
659
  */
755
- async function eventCandlesticksHandler(toolInput, context) {
756
- const kalshiCreds = getKalshiCreds(context);
757
- if (!kalshiCreds)
758
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
759
- const seriesTicker = toolInput.series_ticker;
660
+ async function eventCandlesticksHandler(toolInput, _context) {
760
661
  const eventTicker = toolInput.event_ticker;
761
662
  const interval = toolInput.interval;
762
- const tradingDir = getTradingDir();
763
- let cmd = `cd ${tradingDir} && python3 kalshi.py event_candlesticks ${seriesTicker} ${eventTicker}`;
663
+ const params = new URLSearchParams();
764
664
  if (interval)
765
- cmd += ` --interval ${interval}`;
766
- const userEnv = buildKalshiEnv(kalshiCreds.data);
767
- return execKalshiPython(cmd, userEnv);
665
+ params.set('period_interval', String(interval));
666
+ const qs = params.toString();
667
+ return kalshiGet(`/events/${encodeURIComponent(eventTicker)}/candlesticks${qs ? `?${qs}` : ''}`);
768
668
  }
769
669
  /**
770
670
  * kalshi_forecast_history - Get forecast history
771
671
  */
772
- async function forecastHistoryHandler(toolInput, context) {
773
- const kalshiCreds = getKalshiCreds(context);
774
- if (!kalshiCreds)
775
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
672
+ async function forecastHistoryHandler(toolInput, _context) {
776
673
  const seriesTicker = toolInput.series_ticker;
777
674
  const eventTicker = toolInput.event_ticker;
778
- const tradingDir = getTradingDir();
779
- const cmd = `cd ${tradingDir} && python3 kalshi.py forecast_history ${seriesTicker} ${eventTicker}`;
780
- const userEnv = buildKalshiEnv(kalshiCreds.data);
781
- return execKalshiPython(cmd, userEnv);
675
+ return kalshiGet(`/series/${encodeURIComponent(seriesTicker)}/events/${encodeURIComponent(eventTicker)}/forecast-history`);
782
676
  }
783
677
  /**
784
678
  * kalshi_multivariate_events - Get multivariate events
785
679
  */
786
- async function multivariateEventsHandler(_toolInput, context) {
787
- const kalshiCreds = getKalshiCreds(context);
788
- if (!kalshiCreds)
789
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
790
- const tradingDir = getTradingDir();
791
- const cmd = `cd ${tradingDir} && python3 kalshi.py multivariate_events`;
792
- const userEnv = buildKalshiEnv(kalshiCreds.data);
793
- return execKalshiPython(cmd, userEnv);
680
+ async function multivariateEventsHandler(_toolInput, _context) {
681
+ return kalshiGet('/events?with_nested_markets=true&status=open');
794
682
  }
795
683
  /**
796
684
  * kalshi_create_order_group - Create an order group
797
685
  */
798
686
  async function createOrderGroupHandler(toolInput, context) {
799
- const kalshiCreds = getKalshiCreds(context);
800
- if (!kalshiCreds)
801
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
687
+ const auth = requireAuth(context);
688
+ if (typeof auth === 'string')
689
+ return auth;
802
690
  const orders = toolInput.orders;
803
691
  const maxLoss = toolInput.max_loss;
804
- const tradingDir = getTradingDir();
805
- let cmd = `cd ${tradingDir} && python3 kalshi.py create_order_group '${JSON.stringify(orders)}'`;
806
- if (maxLoss)
807
- cmd += ` --max_loss ${maxLoss}`;
808
- const userEnv = buildKalshiEnv(kalshiCreds.data);
809
- return execKalshiPython(cmd, userEnv);
692
+ const body = { orders };
693
+ if (maxLoss !== undefined)
694
+ body.max_loss = maxLoss;
695
+ return kalshiAuthFetch(auth, 'POST', '/portfolio/order-groups', body);
810
696
  }
811
697
  /**
812
698
  * kalshi_order_groups - List order groups
813
699
  */
814
700
  async function orderGroupsHandler(_toolInput, context) {
815
- const kalshiCreds = getKalshiCreds(context);
816
- if (!kalshiCreds)
817
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
818
- const tradingDir = getTradingDir();
819
- const cmd = `cd ${tradingDir} && python3 kalshi.py order_groups`;
820
- const userEnv = buildKalshiEnv(kalshiCreds.data);
821
- return execKalshiPython(cmd, userEnv);
701
+ const auth = requireAuth(context);
702
+ if (typeof auth === 'string')
703
+ return auth;
704
+ return kalshiAuthFetch(auth, 'GET', '/portfolio/order-groups');
822
705
  }
823
706
  /**
824
707
  * kalshi_order_group - Get order group details
825
708
  */
826
709
  async function orderGroupHandler(toolInput, context) {
827
- const kalshiCreds = getKalshiCreds(context);
828
- if (!kalshiCreds)
829
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
710
+ const auth = requireAuth(context);
711
+ if (typeof auth === 'string')
712
+ return auth;
830
713
  const groupId = toolInput.group_id;
831
- const tradingDir = getTradingDir();
832
- const cmd = `cd ${tradingDir} && python3 kalshi.py order_group ${groupId}`;
833
- const userEnv = buildKalshiEnv(kalshiCreds.data);
834
- return execKalshiPython(cmd, userEnv);
714
+ return kalshiAuthFetch(auth, 'GET', `/portfolio/order-groups/${encodeURIComponent(groupId)}`);
835
715
  }
836
716
  /**
837
717
  * kalshi_order_group_limit - Set order group limit
838
718
  */
839
719
  async function orderGroupLimitHandler(toolInput, context) {
840
- const kalshiCreds = getKalshiCreds(context);
841
- if (!kalshiCreds)
842
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
720
+ const auth = requireAuth(context);
721
+ if (typeof auth === 'string')
722
+ return auth;
843
723
  const groupId = toolInput.group_id;
844
724
  const maxLoss = toolInput.max_loss;
845
- const tradingDir = getTradingDir();
846
- const cmd = `cd ${tradingDir} && python3 kalshi.py order_group_limit ${groupId} ${maxLoss}`;
847
- const userEnv = buildKalshiEnv(kalshiCreds.data);
848
- return execKalshiPython(cmd, userEnv);
725
+ return kalshiAuthFetch(auth, 'PUT', `/portfolio/order-groups/${encodeURIComponent(groupId)}/max-loss`, { max_loss: maxLoss });
849
726
  }
850
727
  /**
851
728
  * kalshi_order_group_trigger - Trigger order group
852
729
  */
853
730
  async function orderGroupTriggerHandler(toolInput, context) {
854
- const kalshiCreds = getKalshiCreds(context);
855
- if (!kalshiCreds)
856
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
731
+ const auth = requireAuth(context);
732
+ if (typeof auth === 'string')
733
+ return auth;
857
734
  const groupId = toolInput.group_id;
858
- const tradingDir = getTradingDir();
859
- const cmd = `cd ${tradingDir} && python3 kalshi.py order_group_trigger ${groupId}`;
860
- const userEnv = buildKalshiEnv(kalshiCreds.data);
861
- return execKalshiPython(cmd, userEnv);
735
+ return kalshiAuthFetch(auth, 'POST', `/portfolio/order-groups/${encodeURIComponent(groupId)}/trigger`, {});
862
736
  }
863
737
  /**
864
738
  * kalshi_order_group_reset - Reset order group
865
739
  */
866
740
  async function orderGroupResetHandler(toolInput, context) {
867
- const kalshiCreds = getKalshiCreds(context);
868
- if (!kalshiCreds)
869
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
741
+ const auth = requireAuth(context);
742
+ if (typeof auth === 'string')
743
+ return auth;
870
744
  const groupId = toolInput.group_id;
871
- const tradingDir = getTradingDir();
872
- const cmd = `cd ${tradingDir} && python3 kalshi.py order_group_reset ${groupId}`;
873
- const userEnv = buildKalshiEnv(kalshiCreds.data);
874
- return execKalshiPython(cmd, userEnv);
745
+ return kalshiAuthFetch(auth, 'POST', `/portfolio/order-groups/${encodeURIComponent(groupId)}/reset`, {});
875
746
  }
876
747
  /**
877
748
  * kalshi_delete_order_group - Delete order group
878
749
  */
879
750
  async function deleteOrderGroupHandler(toolInput, context) {
880
- const kalshiCreds = getKalshiCreds(context);
881
- if (!kalshiCreds)
882
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
751
+ const auth = requireAuth(context);
752
+ if (typeof auth === 'string')
753
+ return auth;
883
754
  const groupId = toolInput.group_id;
884
- const tradingDir = getTradingDir();
885
- const cmd = `cd ${tradingDir} && python3 kalshi.py delete_order_group ${groupId}`;
886
- const userEnv = buildKalshiEnv(kalshiCreds.data);
887
- return execKalshiPython(cmd, userEnv);
755
+ return kalshiAuthFetch(auth, 'DELETE', `/portfolio/order-groups/${encodeURIComponent(groupId)}`);
888
756
  }
889
757
  /**
890
758
  * kalshi_resting_order_value - Get resting order value
891
759
  */
892
760
  async function restingOrderValueHandler(_toolInput, context) {
893
- const kalshiCreds = getKalshiCreds(context);
894
- if (!kalshiCreds)
895
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
896
- const tradingDir = getTradingDir();
897
- const cmd = `cd ${tradingDir} && python3 kalshi.py resting_order_value`;
898
- const userEnv = buildKalshiEnv(kalshiCreds.data);
899
- return execKalshiPython(cmd, userEnv);
761
+ const auth = requireAuth(context);
762
+ if (typeof auth === 'string')
763
+ return auth;
764
+ return kalshiAuthFetch(auth, 'GET', '/portfolio/resting-order-value');
900
765
  }
901
766
  /**
902
767
  * kalshi_create_subaccount - Create a subaccount
903
768
  */
904
769
  async function createSubaccountHandler(toolInput, context) {
905
- const kalshiCreds = getKalshiCreds(context);
906
- if (!kalshiCreds)
907
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
770
+ const auth = requireAuth(context);
771
+ if (typeof auth === 'string')
772
+ return auth;
908
773
  const name = toolInput.name;
909
- const tradingDir = getTradingDir();
910
- const cmd = `cd ${tradingDir} && python3 kalshi.py create_subaccount "${name}"`;
911
- const userEnv = buildKalshiEnv(kalshiCreds.data);
912
- return execKalshiPython(cmd, userEnv);
774
+ return kalshiAuthFetch(auth, 'POST', '/portfolio/subaccounts', { name });
913
775
  }
914
776
  /**
915
777
  * kalshi_subaccount_balances - Get subaccount balances
916
778
  */
917
779
  async function subaccountBalancesHandler(_toolInput, context) {
918
- const kalshiCreds = getKalshiCreds(context);
919
- if (!kalshiCreds)
920
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
921
- const tradingDir = getTradingDir();
922
- const cmd = `cd ${tradingDir} && python3 kalshi.py subaccount_balances`;
923
- const userEnv = buildKalshiEnv(kalshiCreds.data);
924
- return execKalshiPython(cmd, userEnv);
780
+ const auth = requireAuth(context);
781
+ if (typeof auth === 'string')
782
+ return auth;
783
+ return kalshiAuthFetch(auth, 'GET', '/portfolio/subaccounts/balance');
925
784
  }
926
785
  /**
927
786
  * kalshi_subaccount_transfer - Transfer between subaccounts
928
787
  */
929
788
  async function subaccountTransferHandler(toolInput, context) {
930
- const kalshiCreds = getKalshiCreds(context);
931
- if (!kalshiCreds)
932
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
789
+ const auth = requireAuth(context);
790
+ if (typeof auth === 'string')
791
+ return auth;
933
792
  const fromId = toolInput.from_id;
934
793
  const toId = toolInput.to_id;
935
794
  const amount = toolInput.amount;
936
- const tradingDir = getTradingDir();
937
- const cmd = `cd ${tradingDir} && python3 kalshi.py subaccount_transfer ${fromId} ${toId} ${amount}`;
938
- const userEnv = buildKalshiEnv(kalshiCreds.data);
939
- return execKalshiPython(cmd, userEnv);
795
+ return kalshiAuthFetch(auth, 'POST', '/portfolio/subaccounts/transfer', {
796
+ from_id: fromId,
797
+ to_id: toId,
798
+ amount,
799
+ });
940
800
  }
941
801
  /**
942
802
  * kalshi_subaccount_transfers - List subaccount transfers
943
803
  */
944
804
  async function subaccountTransfersHandler(_toolInput, context) {
945
- const kalshiCreds = getKalshiCreds(context);
946
- if (!kalshiCreds)
947
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
948
- const tradingDir = getTradingDir();
949
- const cmd = `cd ${tradingDir} && python3 kalshi.py subaccount_transfers`;
950
- const userEnv = buildKalshiEnv(kalshiCreds.data);
951
- return execKalshiPython(cmd, userEnv);
805
+ const auth = requireAuth(context);
806
+ if (typeof auth === 'string')
807
+ return auth;
808
+ return kalshiAuthFetch(auth, 'GET', '/portfolio/subaccounts/transfers');
952
809
  }
953
810
  /**
954
811
  * kalshi_comms_id - Get communications ID
955
812
  */
956
813
  async function commsIdHandler(_toolInput, context) {
957
- const kalshiCreds = getKalshiCreds(context);
958
- if (!kalshiCreds)
959
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
960
- const tradingDir = getTradingDir();
961
- const cmd = `cd ${tradingDir} && python3 kalshi.py comms_id`;
962
- const userEnv = buildKalshiEnv(kalshiCreds.data);
963
- return execKalshiPython(cmd, userEnv);
814
+ const auth = requireAuth(context);
815
+ if (typeof auth === 'string')
816
+ return auth;
817
+ return kalshiAuthFetch(auth, 'GET', '/portfolio/communications-id');
964
818
  }
965
819
  /**
966
820
  * kalshi_create_rfq - Create a request for quote
967
821
  */
968
822
  async function createRfqHandler(toolInput, context) {
969
- const kalshiCreds = getKalshiCreds(context);
970
- if (!kalshiCreds)
971
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
823
+ const auth = requireAuth(context);
824
+ if (typeof auth === 'string')
825
+ return auth;
972
826
  const ticker = toolInput.ticker;
973
827
  const side = toolInput.side;
974
828
  const count = toolInput.count;
975
829
  const minPrice = toolInput.min_price;
976
830
  const maxPrice = toolInput.max_price;
977
- const tradingDir = getTradingDir();
978
- let cmd = `cd ${tradingDir} && python3 kalshi.py create_rfq ${ticker} ${side} ${count}`;
979
- if (minPrice)
980
- cmd += ` --min_price ${minPrice}`;
981
- if (maxPrice)
982
- cmd += ` --max_price ${maxPrice}`;
983
- const userEnv = buildKalshiEnv(kalshiCreds.data);
984
- return execKalshiPython(cmd, userEnv);
831
+ const body = { ticker, side, count };
832
+ if (minPrice !== undefined)
833
+ body.min_price = minPrice;
834
+ if (maxPrice !== undefined)
835
+ body.max_price = maxPrice;
836
+ return kalshiAuthFetch(auth, 'POST', '/rfqs', body);
985
837
  }
986
838
  /**
987
839
  * kalshi_rfqs - List RFQs
988
840
  */
989
841
  async function rfqsHandler(_toolInput, context) {
990
- const kalshiCreds = getKalshiCreds(context);
991
- if (!kalshiCreds)
992
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
993
- const tradingDir = getTradingDir();
994
- const cmd = `cd ${tradingDir} && python3 kalshi.py rfqs`;
995
- const userEnv = buildKalshiEnv(kalshiCreds.data);
996
- return execKalshiPython(cmd, userEnv);
842
+ const auth = requireAuth(context);
843
+ if (typeof auth === 'string')
844
+ return auth;
845
+ return kalshiAuthFetch(auth, 'GET', '/rfqs');
997
846
  }
998
847
  /**
999
848
  * kalshi_rfq - Get RFQ details
1000
849
  */
1001
850
  async function rfqHandler(toolInput, context) {
1002
- const kalshiCreds = getKalshiCreds(context);
1003
- if (!kalshiCreds)
1004
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
851
+ const auth = requireAuth(context);
852
+ if (typeof auth === 'string')
853
+ return auth;
1005
854
  const rfqId = toolInput.rfq_id;
1006
- const tradingDir = getTradingDir();
1007
- const cmd = `cd ${tradingDir} && python3 kalshi.py rfq ${rfqId}`;
1008
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1009
- return execKalshiPython(cmd, userEnv);
855
+ return kalshiAuthFetch(auth, 'GET', `/rfqs/${encodeURIComponent(rfqId)}`);
1010
856
  }
1011
857
  /**
1012
858
  * kalshi_cancel_rfq - Cancel an RFQ
1013
859
  */
1014
860
  async function cancelRfqHandler(toolInput, context) {
1015
- const kalshiCreds = getKalshiCreds(context);
1016
- if (!kalshiCreds)
1017
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
861
+ const auth = requireAuth(context);
862
+ if (typeof auth === 'string')
863
+ return auth;
1018
864
  const rfqId = toolInput.rfq_id;
1019
- const tradingDir = getTradingDir();
1020
- const cmd = `cd ${tradingDir} && python3 kalshi.py cancel_rfq ${rfqId}`;
1021
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1022
- return execKalshiPython(cmd, userEnv);
865
+ return kalshiAuthFetch(auth, 'DELETE', `/rfqs/${encodeURIComponent(rfqId)}`);
1023
866
  }
1024
867
  /**
1025
868
  * kalshi_create_quote - Create a quote for an RFQ
1026
869
  */
1027
870
  async function createQuoteHandler(toolInput, context) {
1028
- const kalshiCreds = getKalshiCreds(context);
1029
- if (!kalshiCreds)
1030
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
871
+ const auth = requireAuth(context);
872
+ if (typeof auth === 'string')
873
+ return auth;
1031
874
  const rfqId = toolInput.rfq_id;
1032
875
  const price = toolInput.price;
1033
- const tradingDir = getTradingDir();
1034
- const cmd = `cd ${tradingDir} && python3 kalshi.py create_quote ${rfqId} ${price}`;
1035
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1036
- return execKalshiPython(cmd, userEnv);
876
+ return kalshiAuthFetch(auth, 'POST', `/rfqs/${encodeURIComponent(rfqId)}/quotes`, { price });
1037
877
  }
1038
878
  /**
1039
879
  * kalshi_quotes - List quotes
1040
880
  */
1041
881
  async function quotesHandler(_toolInput, context) {
1042
- const kalshiCreds = getKalshiCreds(context);
1043
- if (!kalshiCreds)
1044
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
1045
- const tradingDir = getTradingDir();
1046
- const cmd = `cd ${tradingDir} && python3 kalshi.py quotes`;
1047
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1048
- return execKalshiPython(cmd, userEnv);
882
+ const auth = requireAuth(context);
883
+ if (typeof auth === 'string')
884
+ return auth;
885
+ return kalshiAuthFetch(auth, 'GET', '/rfqs/quotes');
1049
886
  }
1050
887
  /**
1051
888
  * kalshi_quote - Get quote details
1052
889
  */
1053
890
  async function quoteHandler(toolInput, context) {
1054
- const kalshiCreds = getKalshiCreds(context);
1055
- if (!kalshiCreds)
1056
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
891
+ const auth = requireAuth(context);
892
+ if (typeof auth === 'string')
893
+ return auth;
1057
894
  const quoteId = toolInput.quote_id;
1058
- const tradingDir = getTradingDir();
1059
- const cmd = `cd ${tradingDir} && python3 kalshi.py quote ${quoteId}`;
1060
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1061
- return execKalshiPython(cmd, userEnv);
895
+ return kalshiAuthFetch(auth, 'GET', `/rfqs/quotes/${encodeURIComponent(quoteId)}`);
1062
896
  }
1063
897
  /**
1064
898
  * kalshi_cancel_quote - Cancel a quote
1065
899
  */
1066
900
  async function cancelQuoteHandler(toolInput, context) {
1067
- const kalshiCreds = getKalshiCreds(context);
1068
- if (!kalshiCreds)
1069
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
901
+ const auth = requireAuth(context);
902
+ if (typeof auth === 'string')
903
+ return auth;
1070
904
  const quoteId = toolInput.quote_id;
1071
- const tradingDir = getTradingDir();
1072
- const cmd = `cd ${tradingDir} && python3 kalshi.py cancel_quote ${quoteId}`;
1073
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1074
- return execKalshiPython(cmd, userEnv);
905
+ return kalshiAuthFetch(auth, 'DELETE', `/rfqs/quotes/${encodeURIComponent(quoteId)}`);
1075
906
  }
1076
907
  /**
1077
908
  * kalshi_accept_quote - Accept a quote
1078
909
  */
1079
910
  async function acceptQuoteHandler(toolInput, context) {
1080
- const kalshiCreds = getKalshiCreds(context);
1081
- if (!kalshiCreds)
1082
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
911
+ const auth = requireAuth(context);
912
+ if (typeof auth === 'string')
913
+ return auth;
1083
914
  const quoteId = toolInput.quote_id;
1084
- const tradingDir = getTradingDir();
1085
- const cmd = `cd ${tradingDir} && python3 kalshi.py accept_quote ${quoteId}`;
1086
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1087
- return execKalshiPython(cmd, userEnv);
915
+ return kalshiAuthFetch(auth, 'POST', `/rfqs/quotes/${encodeURIComponent(quoteId)}/accept`, {});
1088
916
  }
1089
917
  /**
1090
918
  * kalshi_confirm_quote - Confirm a quote
1091
919
  */
1092
920
  async function confirmQuoteHandler(toolInput, context) {
1093
- const kalshiCreds = getKalshiCreds(context);
1094
- if (!kalshiCreds)
1095
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
921
+ const auth = requireAuth(context);
922
+ if (typeof auth === 'string')
923
+ return auth;
1096
924
  const quoteId = toolInput.quote_id;
1097
- const tradingDir = getTradingDir();
1098
- const cmd = `cd ${tradingDir} && python3 kalshi.py confirm_quote ${quoteId}`;
1099
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1100
- return execKalshiPython(cmd, userEnv);
925
+ return kalshiAuthFetch(auth, 'POST', `/rfqs/quotes/${encodeURIComponent(quoteId)}/confirm`, {});
1101
926
  }
1102
927
  /**
1103
928
  * kalshi_collections - List collections
1104
929
  */
1105
- async function collectionsHandler(_toolInput, context) {
1106
- const kalshiCreds = getKalshiCreds(context);
1107
- if (!kalshiCreds)
1108
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
1109
- const tradingDir = getTradingDir();
1110
- const cmd = `cd ${tradingDir} && python3 kalshi.py collections`;
1111
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1112
- return execKalshiPython(cmd, userEnv);
930
+ async function collectionsHandler(_toolInput, _context) {
931
+ return kalshiGet('/collections');
1113
932
  }
1114
933
  /**
1115
934
  * kalshi_collection - Get collection details
1116
935
  */
1117
- async function collectionHandler(toolInput, context) {
1118
- const kalshiCreds = getKalshiCreds(context);
1119
- if (!kalshiCreds)
1120
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
936
+ async function collectionHandler(toolInput, _context) {
1121
937
  const collectionTicker = toolInput.collection_ticker;
1122
- const tradingDir = getTradingDir();
1123
- const cmd = `cd ${tradingDir} && python3 kalshi.py collection ${collectionTicker}`;
1124
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1125
- return execKalshiPython(cmd, userEnv);
938
+ return kalshiGet(`/collections/${encodeURIComponent(collectionTicker)}`);
1126
939
  }
1127
940
  /**
1128
941
  * kalshi_collection_lookup - Lookup collection
1129
942
  */
1130
- async function collectionLookupHandler(toolInput, context) {
1131
- const kalshiCreds = getKalshiCreds(context);
1132
- if (!kalshiCreds)
1133
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
943
+ async function collectionLookupHandler(toolInput, _context) {
1134
944
  const collectionTicker = toolInput.collection_ticker;
1135
- const tradingDir = getTradingDir();
1136
- const cmd = `cd ${tradingDir} && python3 kalshi.py collection_lookup ${collectionTicker}`;
1137
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1138
- return execKalshiPython(cmd, userEnv);
945
+ return kalshiGet(`/collections/${encodeURIComponent(collectionTicker)}/lookup`);
1139
946
  }
1140
947
  /**
1141
948
  * kalshi_collection_lookup_history - Get collection lookup history
1142
949
  */
1143
- async function collectionLookupHistoryHandler(toolInput, context) {
1144
- const kalshiCreds = getKalshiCreds(context);
1145
- if (!kalshiCreds)
1146
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
950
+ async function collectionLookupHistoryHandler(toolInput, _context) {
1147
951
  const collectionTicker = toolInput.collection_ticker;
1148
- const tradingDir = getTradingDir();
1149
- const cmd = `cd ${tradingDir} && python3 kalshi.py collection_lookup_history ${collectionTicker}`;
1150
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1151
- return execKalshiPython(cmd, userEnv);
952
+ return kalshiGet(`/collections/${encodeURIComponent(collectionTicker)}/lookup/history`);
1152
953
  }
1153
954
  /**
1154
955
  * kalshi_live_data - Get live data
1155
956
  */
1156
- async function liveDataHandler(toolInput, context) {
1157
- const kalshiCreds = getKalshiCreds(context);
1158
- if (!kalshiCreds)
1159
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
957
+ async function liveDataHandler(toolInput, _context) {
1160
958
  const dataType = toolInput.data_type;
1161
959
  const milestoneId = toolInput.milestone_id;
1162
- const tradingDir = getTradingDir();
1163
- const cmd = `cd ${tradingDir} && python3 kalshi.py live_data ${dataType} ${milestoneId}`;
1164
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1165
- return execKalshiPython(cmd, userEnv);
960
+ return kalshiGet(`/live-data/${encodeURIComponent(dataType)}/${encodeURIComponent(milestoneId)}`);
1166
961
  }
1167
962
  /**
1168
963
  * kalshi_live_data_batch - Get batch live data
1169
964
  */
1170
- async function liveDataBatchHandler(toolInput, context) {
1171
- const kalshiCreds = getKalshiCreds(context);
1172
- if (!kalshiCreds)
1173
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
965
+ async function liveDataBatchHandler(toolInput, _context) {
1174
966
  const requests = toolInput.requests;
1175
- const tradingDir = getTradingDir();
1176
- const cmd = `cd ${tradingDir} && python3 kalshi.py live_data_batch '${JSON.stringify(requests)}'`;
1177
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1178
- return execKalshiPython(cmd, userEnv);
967
+ const url = `${KALSHI_API_BASE}/live-data/batch`;
968
+ try {
969
+ const response = await fetch(url, {
970
+ method: 'POST',
971
+ headers: { 'Content-Type': 'application/json' },
972
+ body: JSON.stringify({ requests }),
973
+ });
974
+ const data = await response.json();
975
+ return JSON.stringify(data);
976
+ }
977
+ catch (err) {
978
+ return JSON.stringify({ error: err.message });
979
+ }
1179
980
  }
1180
981
  /**
1181
982
  * kalshi_milestones - List milestones
1182
983
  */
1183
- async function milestonesHandler(_toolInput, context) {
1184
- const kalshiCreds = getKalshiCreds(context);
1185
- if (!kalshiCreds)
1186
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
1187
- const tradingDir = getTradingDir();
1188
- const cmd = `cd ${tradingDir} && python3 kalshi.py milestones`;
1189
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1190
- return execKalshiPython(cmd, userEnv);
984
+ async function milestonesHandler(_toolInput, _context) {
985
+ return kalshiGet('/milestones');
1191
986
  }
1192
987
  /**
1193
988
  * kalshi_milestone - Get milestone details
1194
989
  */
1195
- async function milestoneHandler(toolInput, context) {
1196
- const kalshiCreds = getKalshiCreds(context);
1197
- if (!kalshiCreds)
1198
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
990
+ async function milestoneHandler(toolInput, _context) {
1199
991
  const milestoneId = toolInput.milestone_id;
1200
- const tradingDir = getTradingDir();
1201
- const cmd = `cd ${tradingDir} && python3 kalshi.py milestone ${milestoneId}`;
1202
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1203
- return execKalshiPython(cmd, userEnv);
992
+ return kalshiGet(`/milestones/${encodeURIComponent(milestoneId)}`);
1204
993
  }
1205
994
  /**
1206
995
  * kalshi_structured_targets - List structured targets
1207
996
  */
1208
- async function structuredTargetsHandler(_toolInput, context) {
1209
- const kalshiCreds = getKalshiCreds(context);
1210
- if (!kalshiCreds)
1211
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
1212
- const tradingDir = getTradingDir();
1213
- const cmd = `cd ${tradingDir} && python3 kalshi.py structured_targets`;
1214
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1215
- return execKalshiPython(cmd, userEnv);
997
+ async function structuredTargetsHandler(_toolInput, _context) {
998
+ return kalshiGet('/structured-targets');
1216
999
  }
1217
1000
  /**
1218
1001
  * kalshi_structured_target - Get structured target details
1219
1002
  */
1220
- async function structuredTargetHandler(toolInput, context) {
1221
- const kalshiCreds = getKalshiCreds(context);
1222
- if (!kalshiCreds)
1223
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
1003
+ async function structuredTargetHandler(toolInput, _context) {
1224
1004
  const targetId = toolInput.target_id;
1225
- const tradingDir = getTradingDir();
1226
- const cmd = `cd ${tradingDir} && python3 kalshi.py structured_target ${targetId}`;
1227
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1228
- return execKalshiPython(cmd, userEnv);
1005
+ return kalshiGet(`/structured-targets/${encodeURIComponent(targetId)}`);
1229
1006
  }
1230
1007
  /**
1231
1008
  * kalshi_incentives - Get incentives
1232
1009
  */
1233
- async function incentivesHandler(_toolInput, context) {
1234
- const kalshiCreds = getKalshiCreds(context);
1235
- if (!kalshiCreds)
1236
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
1237
- const tradingDir = getTradingDir();
1238
- const cmd = `cd ${tradingDir} && python3 kalshi.py incentives`;
1239
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1240
- return execKalshiPython(cmd, userEnv);
1010
+ async function incentivesHandler(_toolInput, _context) {
1011
+ return kalshiGet('/incentives');
1241
1012
  }
1242
1013
  /**
1243
1014
  * kalshi_fcm_orders - Get FCM orders
1244
1015
  */
1245
1016
  async function fcmOrdersHandler(_toolInput, context) {
1246
- const kalshiCreds = getKalshiCreds(context);
1247
- if (!kalshiCreds)
1248
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
1249
- const tradingDir = getTradingDir();
1250
- const cmd = `cd ${tradingDir} && python3 kalshi.py fcm_orders`;
1251
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1252
- return execKalshiPython(cmd, userEnv);
1017
+ const auth = requireAuth(context);
1018
+ if (typeof auth === 'string')
1019
+ return auth;
1020
+ return kalshiAuthFetch(auth, 'GET', '/portfolio/fcm/orders');
1253
1021
  }
1254
1022
  /**
1255
1023
  * kalshi_fcm_positions - Get FCM positions
1256
1024
  */
1257
1025
  async function fcmPositionsHandler(_toolInput, context) {
1258
- const kalshiCreds = getKalshiCreds(context);
1259
- if (!kalshiCreds)
1260
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
1261
- const tradingDir = getTradingDir();
1262
- const cmd = `cd ${tradingDir} && python3 kalshi.py fcm_positions`;
1263
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1264
- return execKalshiPython(cmd, userEnv);
1026
+ const auth = requireAuth(context);
1027
+ if (typeof auth === 'string')
1028
+ return auth;
1029
+ return kalshiAuthFetch(auth, 'GET', '/portfolio/fcm/positions');
1265
1030
  }
1266
1031
  /**
1267
1032
  * kalshi_search_tags - Search by tags
1268
1033
  */
1269
- async function searchTagsHandler(_toolInput, context) {
1270
- const kalshiCreds = getKalshiCreds(context);
1271
- if (!kalshiCreds)
1272
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
1273
- const tradingDir = getTradingDir();
1274
- const cmd = `cd ${tradingDir} && python3 kalshi.py search_tags`;
1275
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1276
- return execKalshiPython(cmd, userEnv);
1034
+ async function searchTagsHandler(_toolInput, _context) {
1035
+ return kalshiGet('/markets/search/tags');
1277
1036
  }
1278
1037
  /**
1279
1038
  * kalshi_search_sports - Search sports markets
1280
1039
  */
1281
- async function searchSportsHandler(_toolInput, context) {
1282
- const kalshiCreds = getKalshiCreds(context);
1283
- if (!kalshiCreds)
1284
- return (0, types_1.errorResult)('No Kalshi credentials set up.');
1285
- const tradingDir = getTradingDir();
1286
- const cmd = `cd ${tradingDir} && python3 kalshi.py search_sports`;
1287
- const userEnv = buildKalshiEnv(kalshiCreds.data);
1288
- return execKalshiPython(cmd, userEnv);
1040
+ async function searchSportsHandler(_toolInput, _context) {
1041
+ return kalshiGet('/markets/search/sports');
1289
1042
  }
1290
1043
  // =============================================================================
1291
1044
  // EXPORT MAP
@@ -1300,7 +1053,7 @@ exports.kalshiHandlers = {
1300
1053
  kalshi_exchange_status: exchangeStatusHandler,
1301
1054
  kalshi_exchange_schedule: exchangeScheduleHandler,
1302
1055
  kalshi_announcements: announcementsHandler,
1303
- // Credential-based Python handlers
1056
+ // Authenticated REST API handlers
1304
1057
  kalshi_positions: positionsHandler,
1305
1058
  kalshi_search: searchHandler,
1306
1059
  kalshi_market: marketHandler,