@coinrithm/mcp-trading 0.7.2 → 0.7.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +46 -1
  2. package/README.md +9 -7
  3. package/dist/agent/act.d.ts +2 -2
  4. package/dist/agent/act.js +24 -3
  5. package/dist/agent/cli.js +59 -18
  6. package/dist/agent/client.d.ts +33 -0
  7. package/dist/agent/client.js +34 -7
  8. package/dist/agent/decision.d.ts +3 -0
  9. package/dist/agent/decision.js +26 -3
  10. package/dist/agent/decisionValidator.js +2 -1
  11. package/dist/agent/deploymentOverlay.js +25 -5
  12. package/dist/agent/engine.d.ts +2 -1
  13. package/dist/agent/engine.js +4 -1
  14. package/dist/agent/extract.js +3 -1
  15. package/dist/agent/gate.js +25 -5
  16. package/dist/agent/index.js +0 -1
  17. package/dist/agent/indicators.js +4 -2
  18. package/dist/agent/manifest.js +1 -1
  19. package/dist/agent/mechanical.d.ts +36 -0
  20. package/dist/agent/mechanical.js +286 -0
  21. package/dist/agent/observe.js +120 -52
  22. package/dist/agent/prompt.d.ts +3 -1
  23. package/dist/agent/prompt.js +17 -6
  24. package/dist/agent/providers.js +39 -4
  25. package/dist/agent/resolve.js +23 -6
  26. package/dist/agent/resolvePm.js +14 -3
  27. package/dist/agent/runEvidence.js +6 -2
  28. package/dist/agent/runner.d.ts +8 -2
  29. package/dist/agent/runner.js +363 -59
  30. package/dist/agent/scorecard.js +12 -4
  31. package/dist/agent/setups.js +57 -9
  32. package/dist/agent/skill.js +1 -1
  33. package/dist/agent/state.js +9 -4
  34. package/dist/agent/types.d.ts +17 -2
  35. package/dist/agent/types.js +2 -1
  36. package/dist/agent/util.js +11 -4
  37. package/dist/agent/version.d.ts +1 -1
  38. package/dist/agent/version.js +1 -1
  39. package/dist/client.d.ts +33 -0
  40. package/dist/client.js +12 -3
  41. package/dist/executionPolicy.d.ts +2 -0
  42. package/dist/executionPolicy.js +21 -0
  43. package/dist/http.js +10 -2
  44. package/dist/tools.d.ts +1 -0
  45. package/dist/tools.js +214 -29
  46. package/package.json +9 -1
package/dist/tools.js CHANGED
@@ -6,8 +6,12 @@
6
6
  // isError results rather than thrown so the model can react.
7
7
  import { z } from "zod";
8
8
  import { bearerFromHeader } from "./client.js";
9
- const PAPER_NOTE = "Paper trading only — virtual funds (50,000 mUSD). Not financial advice. " +
10
- "Paper fills apply a disclosed execution cost folded into realized PnL: " +
9
+ import { PAPER_EXECUTION_VERSION } from "./executionPolicy.js";
10
+ // Served on tool descriptions. Names the versioned execution policy and never
11
+ // claims costless execution (drift-tested in executionPolicy.test.ts).
12
+ export const PAPER_NOTE = "Paper trading only — virtual funds (50,000 mUSD). Not financial advice. " +
13
+ `Paper fills run under the versioned ${PAPER_EXECUTION_VERSION} policy and ` +
14
+ "apply a disclosed execution cost folded into realized PnL: " +
11
15
  "spot/futures pay a taker fee (spot market orders also pay half-spread + " +
12
16
  "slippage); PM fills at the ask with size-based slippage and a Polymarket-" +
13
17
  "shaped taker fee, with entryProbability kept at the mid for calibration. " +
@@ -64,6 +68,54 @@ const AGENT_TRACE_SCHEMA = z
64
68
  })
65
69
  .optional()
66
70
  .describe("Optional private trace metadata stored in the caller's ledger.");
71
+ // Optional SELF-REPORTED provenance a caller attaches to a PM open/opportunity so the
72
+ // durable artifact records WHAT RAN. Every field carries NO trust: the server always
73
+ // stamps the execution/evaluation policy versions AND providerVerified itself
74
+ // (providerVerified can NEVER be raised by a caller), validates/caps each value, and
75
+ // hex-checks the hashes. Sending ANY block (even {}) makes the artifact schemaVersion 2.
76
+ const PROVENANCE_REPORT_SCHEMA = z
77
+ .object({
78
+ runtimeKind: z
79
+ .enum(["hosted_scheduler", "self_host_runner", "byo_api", "mcp_tool"])
80
+ .optional()
81
+ .describe("The runtime surface you ran on (self-reported; no trust)."),
82
+ packageVersion: z.string().max(40).optional(),
83
+ bundleId: z.string().max(120).optional(),
84
+ bundleVersion: z.string().max(40).optional(),
85
+ skillVersions: z
86
+ .record(z.string(), z.string())
87
+ .optional()
88
+ .describe("{skillId: version}. Capped: 50 keys, key<=120 / value<=40."),
89
+ promptHash: z
90
+ .string()
91
+ .regex(/^[0-9a-fA-F]{64}$/)
92
+ .optional()
93
+ .describe("sha256 hex of your exact prompt strings. HASH ONLY — never raw text."),
94
+ configHash: z
95
+ .string()
96
+ .regex(/^[0-9a-fA-F]{64}$/)
97
+ .optional()
98
+ .describe("sha256 hex of your resolved config/spec. HASH ONLY — never raw text."),
99
+ modelProvider: z.string().max(80).optional(),
100
+ modelName: z.string().max(80).optional(),
101
+ evidenceRef: z
102
+ .object({
103
+ snapshotIds: z
104
+ .array(z.string().max(200))
105
+ .optional()
106
+ .describe("Opaque snapshot ids (capped at 100)."),
107
+ sourceCapturedAt: z
108
+ .string()
109
+ .optional()
110
+ .describe("Source capture time (ISO 8601)."),
111
+ })
112
+ .optional()
113
+ .describe("Pointers to the observation evidence (never the evidence itself)."),
114
+ })
115
+ .optional()
116
+ .describe("Optional self-reported provenance (WHAT RAN). No trust: the server stamps " +
117
+ "policy versions + providerVerified itself. Any block (even {}) makes the " +
118
+ "artifact schemaVersion 2.");
67
119
  function readOnlyAnnotations(title) {
68
120
  return {
69
121
  title,
@@ -358,9 +410,12 @@ export function registerTools(server, client) {
358
410
  description: "Find active-open, quote-ready-first prediction markets on the mock-PM " +
359
411
  "sources (Kalshi + Polymarket by default). Returns source, slug, " +
360
412
  "quoteable outcome externalMarketIds, freshness, volume/liquidity/spread, " +
361
- "and decisionSupport. This is discovery only call pm_quote with one " +
362
- "returned outcomeExternalMarketId before open_pm_position because pm_quote " +
363
- "is the final eligibility source. " +
413
+ "decisionSupport, and quality (the truth engine's persisted verdict: " +
414
+ "decisionEligible plus stable warning/block reason codes; " +
415
+ "decisionEligible=false means opens are blocked and alerts suppressed " +
416
+ "while the market stays visible). This is discovery only — call pm_quote " +
417
+ "with one returned outcomeExternalMarketId before open_pm_position " +
418
+ "because pm_quote is the final eligibility source. " +
364
419
  PAPER_NOTE,
365
420
  inputSchema: {
366
421
  q: z
@@ -422,16 +477,16 @@ export function registerTools(server, client) {
422
477
  PAPER_NOTE,
423
478
  inputSchema: {
424
479
  venue: z.string().optional().describe("Optional venue filter."),
425
- eventType: z.string().optional().describe("Optional event type filter."),
480
+ eventType: z
481
+ .string()
482
+ .optional()
483
+ .describe("Optional event type filter."),
426
484
  runId: z.string().optional().describe("Optional run id filter."),
427
485
  decisionId: z
428
486
  .string()
429
487
  .optional()
430
488
  .describe("Optional decision id filter."),
431
- status: z
432
- .string()
433
- .optional()
434
- .describe("Optional ledgerStatus filter."),
489
+ status: z.string().optional().describe("Optional ledgerStatus filter."),
435
490
  from: z.string().optional().describe("Optional ISO start timestamp."),
436
491
  to: z.string().optional().describe("Optional ISO end timestamp."),
437
492
  limit: z
@@ -451,7 +506,17 @@ export function registerTools(server, client) {
451
506
  },
452
507
  outputSchema: API_RESULT_OUTPUT_SCHEMA,
453
508
  annotations: readOnlyAnnotations("Get private agent ledger"),
454
- }, async ({ venue, eventType, runId, decisionId, status, from, to, limit, offset, agentTrace, }, extra) => present(await client.getLedger({ venue, eventType, runId, decisionId, status, from, to, limit, offset }, requestKey(extra), agentTrace)));
509
+ }, async ({ venue, eventType, runId, decisionId, status, from, to, limit, offset, agentTrace, }, extra) => present(await client.getLedger({
510
+ venue,
511
+ eventType,
512
+ runId,
513
+ decisionId,
514
+ status,
515
+ from,
516
+ to,
517
+ limit,
518
+ offset,
519
+ }, requestKey(extra), agentTrace)));
455
520
  server.registerTool("export_agent_ledger", {
456
521
  title: "Export private agent ledger",
457
522
  description: "Export up to 1,000 private ledger rows for the calling API key as JSON. " +
@@ -460,16 +525,16 @@ export function registerTools(server, client) {
460
525
  PAPER_NOTE,
461
526
  inputSchema: {
462
527
  venue: z.string().optional().describe("Optional venue filter."),
463
- eventType: z.string().optional().describe("Optional event type filter."),
528
+ eventType: z
529
+ .string()
530
+ .optional()
531
+ .describe("Optional event type filter."),
464
532
  runId: z.string().optional().describe("Optional run id filter."),
465
533
  decisionId: z
466
534
  .string()
467
535
  .optional()
468
536
  .describe("Optional decision id filter."),
469
- status: z
470
- .string()
471
- .optional()
472
- .describe("Optional ledgerStatus filter."),
537
+ status: z.string().optional().describe("Optional ledgerStatus filter."),
473
538
  from: z.string().optional().describe("Optional ISO start timestamp."),
474
539
  to: z.string().optional().describe("Optional ISO end timestamp."),
475
540
  agentTrace: AGENT_TRACE_SCHEMA,
@@ -572,9 +637,13 @@ export function registerTools(server, client) {
572
637
  server.registerTool("pm_quote", {
573
638
  title: "Prediction-market quote",
574
639
  description: "Read-only PM quote for a binary outcome: entry probability, share " +
575
- "estimate, max payout, eligibility, freshness, and decisionSupport " +
576
- "(market quality/liquidity/volume/spread tiers + flags) so you can " +
577
- "quote and gauge tradability in one call. Never mutates state. " +
640
+ "estimate, max payout, eligibility, freshness, decisionSupport " +
641
+ "(market quality/liquidity/volume/spread tiers + flags), quality (the " +
642
+ "persisted truth-engine verdict), and openBlocked/openBlockReasons " +
643
+ "a preview of the open-time quality gate: when openBlocked is true, " +
644
+ "open_pm_position would be rejected 422 with those stored reason codes " +
645
+ "(quality_state_missing, quality_state_stale, quote_dead, " +
646
+ "stale_freshness, ...). Never mutates state. " +
578
647
  "stakeMusd must be > 0 (min to open is 10). Pass side: 'no' to quote " +
579
648
  "backing the NO side (omitted = yes); a NO entry fills at 100 minus the " +
580
649
  "outcome probability and pays out if the outcome resolves false. " +
@@ -596,7 +665,14 @@ export function registerTools(server, client) {
596
665
  },
597
666
  outputSchema: API_RESULT_OUTPUT_SCHEMA,
598
667
  annotations: readOnlyAnnotations("Prediction-market quote"),
599
- }, async ({ source, slug, outcomeExternalMarketId, side, stakeMusd, agentTrace }, extra) => present(await client.pmQuote({ source, slug, outcomeExternalMarketId, side, stakeMusd, agentTrace }, requestKey(extra))));
668
+ }, async ({ source, slug, outcomeExternalMarketId, side, stakeMusd, agentTrace }, extra) => present(await client.pmQuote({
669
+ source,
670
+ slug,
671
+ outcomeExternalMarketId,
672
+ side,
673
+ stakeMusd,
674
+ agentTrace,
675
+ }, requestKey(extra))));
600
676
  server.registerTool("spot_quote", {
601
677
  title: "Spot quote",
602
678
  description: "Read-only spot MARKET quote: live execution price, estimated cost " +
@@ -837,19 +913,125 @@ export function registerTools(server, client) {
837
913
  .string()
838
914
  .min(1)
839
915
  .describe("Unique per PM-open intent; reuse replays the original result."),
916
+ forecastProbability: z
917
+ .number()
918
+ .gt(0)
919
+ .lt(100)
920
+ .optional()
921
+ .describe("OPTIONAL. Report your OWN estimated probability (0-100, exclusive) " +
922
+ "that the chosen side wins, decided BEFORE you look at sizing/fill. " +
923
+ "It is stored SEPARATELY from the market price you pay and feeds your " +
924
+ "PUBLIC calibration record (agentBrier), which scores your forecast " +
925
+ "SKILL — not the market's. Omit it if you are not forecasting; never " +
926
+ "echo the market probability back."),
927
+ provenance: PROVENANCE_REPORT_SCHEMA,
840
928
  agentTrace: AGENT_TRACE_SCHEMA,
841
929
  },
842
930
  outputSchema: API_RESULT_OUTPUT_SCHEMA,
843
931
  annotations: mutatingAnnotations("Open prediction-market position", {
844
932
  idempotent: true,
845
933
  }),
846
- }, async ({ source, slug, outcomeExternalMarketId, side, stakeMusd, idempotencyKey, agentTrace, }, extra) => present(await client.openPmPosition({
934
+ }, async ({ source, slug, outcomeExternalMarketId, side, stakeMusd, idempotencyKey, forecastProbability, provenance, agentTrace, }, extra) => present(await client.openPmPosition({
847
935
  source,
848
936
  slug,
849
937
  outcomeExternalMarketId,
850
938
  side,
851
939
  stakeMusd,
852
940
  idempotencyKey,
941
+ forecastProbability,
942
+ provenance,
943
+ agentTrace,
944
+ }, requestKey(extra))));
945
+ server.registerTool("report_pm_opportunity", {
946
+ title: "Report a non-opened PM opportunity",
947
+ description: "Report a prediction-market opportunity you evaluated but did NOT open, so " +
948
+ "your PUBLIC evaluation reflects the FULL opportunity universe — not only " +
949
+ "the trades you took (otherwise an agent can look skilled by exposure " +
950
+ "choice alone). kind is one of: 'abstained' (you looked at markets and " +
951
+ "chose not to bet), 'forecast_only' (you formed your OWN probability but " +
952
+ "did not trade — forecastProbability is REQUIRED, 1-99), or 'quote_expired' " +
953
+ "(a bet you validated was rejected at open because the market moved). This " +
954
+ "is EVIDENCE, not a trade: it needs only the read scope, never moves funds, " +
955
+ "and is recorded as a durable, hashed decision artifact. It is a " +
956
+ "SELF-REPORT — CoinRithm records what you assert about your own reasoning; " +
957
+ "it does not independently verify that you truly evaluated the market. Put " +
958
+ "the breadth of what you weighed in cohort.universeSize (how many markets) " +
959
+ "and report ONCE per decision cycle, not once per market. Reuse decisionId " +
960
+ "to make a retry idempotent. " +
961
+ PAPER_NOTE,
962
+ inputSchema: {
963
+ kind: z
964
+ .enum(["abstained", "forecast_only", "quote_expired"])
965
+ .describe("abstained = evaluated but did not bet; forecast_only = formed your " +
966
+ "own probability without trading (forecastProbability required); " +
967
+ "quote_expired = a validated open the server rejected at act time."),
968
+ source: z
969
+ .string()
970
+ .optional()
971
+ .describe("Optional subject market source slug (e.g. kalshi)."),
972
+ slug: z.string().optional().describe("Optional subject event slug."),
973
+ outcomeExternalMarketId: z
974
+ .string()
975
+ .optional()
976
+ .describe("Optional case-sensitive outcome/market id of the subject."),
977
+ forecastProbability: z
978
+ .number()
979
+ .min(1)
980
+ .max(99)
981
+ .optional()
982
+ .describe("Your OWN probability (1-99) the chosen side wins. REQUIRED for " +
983
+ "forecast_only; omit for the other kinds. Never echo the market price."),
984
+ marketProbability: z
985
+ .number()
986
+ .min(0)
987
+ .max(100)
988
+ .optional()
989
+ .describe("The market price (0-100) you observed at the time."),
990
+ reasonCode: z
991
+ .string()
992
+ .max(500)
993
+ .optional()
994
+ .describe("Short structured reason (e.g. 'no_edge', 'stale_data')."),
995
+ cohort: z
996
+ .object({
997
+ universeSize: z
998
+ .number()
999
+ .int()
1000
+ .min(0)
1001
+ .optional()
1002
+ .describe("How many markets you were choosing from this cycle."),
1003
+ horizon: z
1004
+ .string()
1005
+ .max(64)
1006
+ .optional()
1007
+ .describe("Your forecast/decision horizon label (e.g. '7d')."),
1008
+ })
1009
+ .optional()
1010
+ .describe("Opportunity-cohort breadth (frozen into the artifact)."),
1011
+ decisionId: z
1012
+ .string()
1013
+ .optional()
1014
+ .describe("Your own id for this decision — idempotency key within your API key."),
1015
+ runId: z.string().optional().describe("Your own run id for grouping."),
1016
+ provenance: PROVENANCE_REPORT_SCHEMA,
1017
+ agentTrace: AGENT_TRACE_SCHEMA,
1018
+ },
1019
+ outputSchema: API_RESULT_OUTPUT_SCHEMA,
1020
+ annotations: mutatingAnnotations("Report a non-opened PM opportunity", {
1021
+ idempotent: true,
1022
+ }),
1023
+ }, async ({ kind, source, slug, outcomeExternalMarketId, forecastProbability, marketProbability, reasonCode, cohort, decisionId, runId, provenance, agentTrace, }, extra) => present(await client.reportPmOpportunity({
1024
+ kind,
1025
+ source,
1026
+ slug,
1027
+ outcomeExternalMarketId,
1028
+ forecastProbability,
1029
+ marketProbability,
1030
+ reasonCode,
1031
+ cohort,
1032
+ decisionId,
1033
+ runId,
1034
+ provenance,
853
1035
  agentTrace,
854
1036
  }, requestKey(extra))));
855
1037
  // ---- Public cross-venue PM data (no API key required) ----
@@ -859,8 +1041,8 @@ export function registerTools(server, client) {
859
1041
  title: "Cross-venue prediction-market statistics",
860
1042
  description: "Free public cross-venue prediction-market statistics: total/open/" +
861
1043
  "closed market counts, total volume, 24h volume, and liquidity " +
862
- "aggregated across all ten venues (Polymarket, Kalshi, Rothera, " +
863
- "Limitless, Smarkets, Manifold, Metaculus, PredictIt, Futuur, Myriad), plus market " +
1044
+ "aggregated across all 11 venues (Polymarket, Kalshi, Rothera, " +
1045
+ "Limitless, Smarkets, Manifold, Metaculus, PredictIt, Futuur, Myriad, ForecastEx), plus market " +
864
1046
  "highlights. Freshness is SOURCE-AWARE — each venue ingests " +
865
1047
  "independently; per-venue health (freshness tier, lag, stale reason) " +
866
1048
  "is at /api/prediction-markets/sources/health. Volume is " +
@@ -879,23 +1061,26 @@ export function registerTools(server, client) {
879
1061
  }, async ({ fiat }) => present(await client.getPublicPmOverview({ fiat })));
880
1062
  server.registerTool("pm_data_events", {
881
1063
  title: "Search prediction markets across all venues",
882
- description: "Free public search over prediction-market events across ALL ten " +
1064
+ description: "Free public search over prediction-market events across ALL 11 " +
883
1065
  "venues (Polymarket, Kalshi, Rothera, Limitless, Smarkets, " +
884
- "Manifold, Metaculus, PredictIt, Futuur, Myriad) — broader than discover_pm_markets, which is " +
1066
+ "Manifold, Metaculus, PredictIt, Futuur, Myriad, ForecastEx) — broader than discover_pm_markets, which is " +
885
1067
  "scoped to the paper-tradeable venues. Returns titles, probabilities, " +
886
1068
  "volume/liquidity, status, and source per event, plus " +
887
1069
  "referenceProbability when present (CoinRithm's canonical cross-venue " +
888
1070
  "number for open events matched across venues — probability, " +
889
1071
  "venueCount, spreadPoints, and outcomeName for multi-outcome " +
890
- "leaders). Research/data only: to trade, use discover_pm_markets + " +
891
- "pm_quote instead. No API key required.",
1072
+ "leaders), quality (persisted truth-engine verdict: decisionEligible " +
1073
+ "+ warning/block reason codes blocked markets stay visible but " +
1074
+ "cannot drive paper opens or alerts), and crossPlatform (sibling " +
1075
+ "venues pricing the same question). Research/data only: to trade, " +
1076
+ "use discover_pm_markets + pm_quote instead. No API key required.",
892
1077
  inputSchema: {
893
1078
  q: z.string().optional().describe("Optional search text."),
894
1079
  source: z
895
1080
  .string()
896
1081
  .optional()
897
1082
  .describe("Optional venue filter: polymarket, kalshi, rothera, limitless, " +
898
- "smarkets, manifold, metaculus, predictit, futuur, or myriad."),
1083
+ "smarkets, manifold, metaculus, predictit, futuur, myriad, or forecastex."),
899
1084
  status: z
900
1085
  .string()
901
1086
  .optional()
@@ -949,7 +1134,7 @@ export function registerTools(server, client) {
949
1134
  source: z
950
1135
  .string()
951
1136
  .describe("Venue slug: polymarket, kalshi, rothera, limitless, smarkets, " +
952
- "manifold, metaculus, predictit, futuur, or myriad."),
1137
+ "manifold, metaculus, predictit, futuur, myriad, or forecastex."),
953
1138
  slug: z.string().describe("Event slug on that venue."),
954
1139
  fiat: z
955
1140
  .string()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coinrithm/mcp-trading",
3
- "version": "0.7.2",
3
+ "version": "0.7.3",
4
4
  "mcpName": "io.github.CoinRithm/mcp-trading",
5
5
  "description": "CoinRithm paper-trading toolkit: an MCP server (coinrithm-mcp) AND a self-host agent runner (coinrithm-agent) for spot, futures, and prediction markets with a user-minted API key.",
6
6
  "type": "module",
@@ -31,6 +31,10 @@
31
31
  "build": "tsc -p tsconfig.json",
32
32
  "typecheck": "tsc -p tsconfig.json --noEmit",
33
33
  "test": "vitest run",
34
+ "lint": "eslint src",
35
+ "lint:fix": "eslint src --fix",
36
+ "format": "prettier --write \"src/**/*.ts\"",
37
+ "format:check": "prettier --check \"src/**/*.ts\"",
34
38
  "start": "node dist/index.js",
35
39
  "start:http": "node dist/http.js",
36
40
  "smoke:agent": "npm run build && node scripts/agent-smoke.mjs",
@@ -70,9 +74,13 @@
70
74
  "zod": "^3.23.8"
71
75
  },
72
76
  "devDependencies": {
77
+ "@eslint/js": "^9.13.0",
73
78
  "@types/express": "^4.17.21",
74
79
  "@types/node": "^20.14.0",
80
+ "eslint": "^9.13.0",
81
+ "prettier": "^3.3.3",
75
82
  "typescript": "^5.5.0",
83
+ "typescript-eslint": "^8.11.0",
76
84
  "vitest": "^2.1.0"
77
85
  }
78
86
  }