@catalyst-team/poly-sdk 0.1.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 (244) hide show
  1. package/.env +0 -0
  2. package/README.md +803 -0
  3. package/dist/__tests__/clob-api.test.d.ts +5 -0
  4. package/dist/__tests__/clob-api.test.d.ts.map +1 -0
  5. package/dist/__tests__/clob-api.test.js +240 -0
  6. package/dist/__tests__/clob-api.test.js.map +1 -0
  7. package/dist/__tests__/integration/bridge-client.integration.test.d.ts +11 -0
  8. package/dist/__tests__/integration/bridge-client.integration.test.d.ts.map +1 -0
  9. package/dist/__tests__/integration/bridge-client.integration.test.js +260 -0
  10. package/dist/__tests__/integration/bridge-client.integration.test.js.map +1 -0
  11. package/dist/__tests__/integration/clob-api.integration.test.d.ts +13 -0
  12. package/dist/__tests__/integration/clob-api.integration.test.d.ts.map +1 -0
  13. package/dist/__tests__/integration/clob-api.integration.test.js +170 -0
  14. package/dist/__tests__/integration/clob-api.integration.test.js.map +1 -0
  15. package/dist/__tests__/integration/ctf-client.integration.test.d.ts +17 -0
  16. package/dist/__tests__/integration/ctf-client.integration.test.d.ts.map +1 -0
  17. package/dist/__tests__/integration/ctf-client.integration.test.js +234 -0
  18. package/dist/__tests__/integration/ctf-client.integration.test.js.map +1 -0
  19. package/dist/__tests__/integration/data-api.integration.test.d.ts +9 -0
  20. package/dist/__tests__/integration/data-api.integration.test.d.ts.map +1 -0
  21. package/dist/__tests__/integration/data-api.integration.test.js +161 -0
  22. package/dist/__tests__/integration/data-api.integration.test.js.map +1 -0
  23. package/dist/__tests__/integration/gamma-api.integration.test.d.ts +9 -0
  24. package/dist/__tests__/integration/gamma-api.integration.test.d.ts.map +1 -0
  25. package/dist/__tests__/integration/gamma-api.integration.test.js +170 -0
  26. package/dist/__tests__/integration/gamma-api.integration.test.js.map +1 -0
  27. package/dist/__tests__/test-utils.d.ts +92 -0
  28. package/dist/__tests__/test-utils.d.ts.map +1 -0
  29. package/dist/__tests__/test-utils.js +143 -0
  30. package/dist/__tests__/test-utils.js.map +1 -0
  31. package/dist/clients/bridge-client.d.ts +388 -0
  32. package/dist/clients/bridge-client.d.ts.map +1 -0
  33. package/dist/clients/bridge-client.js +587 -0
  34. package/dist/clients/bridge-client.js.map +1 -0
  35. package/dist/clients/clob-api.d.ts +318 -0
  36. package/dist/clients/clob-api.d.ts.map +1 -0
  37. package/dist/clients/clob-api.js +388 -0
  38. package/dist/clients/clob-api.js.map +1 -0
  39. package/dist/clients/ctf-client.d.ts +473 -0
  40. package/dist/clients/ctf-client.d.ts.map +1 -0
  41. package/dist/clients/ctf-client.js +915 -0
  42. package/dist/clients/ctf-client.js.map +1 -0
  43. package/dist/clients/data-api.d.ts +134 -0
  44. package/dist/clients/data-api.d.ts.map +1 -0
  45. package/dist/clients/data-api.js +265 -0
  46. package/dist/clients/data-api.js.map +1 -0
  47. package/dist/clients/gamma-api.d.ts +401 -0
  48. package/dist/clients/gamma-api.d.ts.map +1 -0
  49. package/dist/clients/gamma-api.js +352 -0
  50. package/dist/clients/gamma-api.js.map +1 -0
  51. package/dist/clients/trading-client.d.ts +252 -0
  52. package/dist/clients/trading-client.d.ts.map +1 -0
  53. package/dist/clients/trading-client.js +543 -0
  54. package/dist/clients/trading-client.js.map +1 -0
  55. package/dist/clients/websocket-manager.d.ts +100 -0
  56. package/dist/clients/websocket-manager.d.ts.map +1 -0
  57. package/dist/clients/websocket-manager.js +193 -0
  58. package/dist/clients/websocket-manager.js.map +1 -0
  59. package/dist/core/cache-adapter-bridge.d.ts +36 -0
  60. package/dist/core/cache-adapter-bridge.d.ts.map +1 -0
  61. package/dist/core/cache-adapter-bridge.js +81 -0
  62. package/dist/core/cache-adapter-bridge.js.map +1 -0
  63. package/dist/core/cache.d.ts +40 -0
  64. package/dist/core/cache.d.ts.map +1 -0
  65. package/dist/core/cache.js +71 -0
  66. package/dist/core/cache.js.map +1 -0
  67. package/dist/core/errors.d.ts +38 -0
  68. package/dist/core/errors.d.ts.map +1 -0
  69. package/dist/core/errors.js +84 -0
  70. package/dist/core/errors.js.map +1 -0
  71. package/dist/core/rate-limiter.d.ts +31 -0
  72. package/dist/core/rate-limiter.d.ts.map +1 -0
  73. package/dist/core/rate-limiter.js +70 -0
  74. package/dist/core/rate-limiter.js.map +1 -0
  75. package/dist/core/types.d.ts +314 -0
  76. package/dist/core/types.d.ts.map +1 -0
  77. package/dist/core/types.js +19 -0
  78. package/dist/core/types.js.map +1 -0
  79. package/dist/core/unified-cache.d.ts +63 -0
  80. package/dist/core/unified-cache.d.ts.map +1 -0
  81. package/dist/core/unified-cache.js +114 -0
  82. package/dist/core/unified-cache.js.map +1 -0
  83. package/dist/index.d.ts +94 -0
  84. package/dist/index.d.ts.map +1 -0
  85. package/dist/index.js +258 -0
  86. package/dist/index.js.map +1 -0
  87. package/dist/mcp/errors.d.ts +33 -0
  88. package/dist/mcp/errors.d.ts.map +1 -0
  89. package/dist/mcp/errors.js +86 -0
  90. package/dist/mcp/errors.js.map +1 -0
  91. package/dist/mcp/index.d.ts +62 -0
  92. package/dist/mcp/index.d.ts.map +1 -0
  93. package/dist/mcp/index.js +173 -0
  94. package/dist/mcp/index.js.map +1 -0
  95. package/dist/mcp/server.d.ts +17 -0
  96. package/dist/mcp/server.d.ts.map +1 -0
  97. package/dist/mcp/server.js +155 -0
  98. package/dist/mcp/server.js.map +1 -0
  99. package/dist/mcp/tools/guide.d.ts +12 -0
  100. package/dist/mcp/tools/guide.d.ts.map +1 -0
  101. package/dist/mcp/tools/guide.js +801 -0
  102. package/dist/mcp/tools/guide.js.map +1 -0
  103. package/dist/mcp/tools/index.d.ts +11 -0
  104. package/dist/mcp/tools/index.d.ts.map +1 -0
  105. package/dist/mcp/tools/index.js +27 -0
  106. package/dist/mcp/tools/index.js.map +1 -0
  107. package/dist/mcp/tools/market.d.ts +11 -0
  108. package/dist/mcp/tools/market.d.ts.map +1 -0
  109. package/dist/mcp/tools/market.js +314 -0
  110. package/dist/mcp/tools/market.js.map +1 -0
  111. package/dist/mcp/tools/order.d.ts +10 -0
  112. package/dist/mcp/tools/order.d.ts.map +1 -0
  113. package/dist/mcp/tools/order.js +258 -0
  114. package/dist/mcp/tools/order.js.map +1 -0
  115. package/dist/mcp/tools/trade.d.ts +38 -0
  116. package/dist/mcp/tools/trade.d.ts.map +1 -0
  117. package/dist/mcp/tools/trade.js +314 -0
  118. package/dist/mcp/tools/trade.js.map +1 -0
  119. package/dist/mcp/tools/trader.d.ts +11 -0
  120. package/dist/mcp/tools/trader.d.ts.map +1 -0
  121. package/dist/mcp/tools/trader.js +277 -0
  122. package/dist/mcp/tools/trader.js.map +1 -0
  123. package/dist/mcp/tools/wallet.d.ts +274 -0
  124. package/dist/mcp/tools/wallet.d.ts.map +1 -0
  125. package/dist/mcp/tools/wallet.js +579 -0
  126. package/dist/mcp/tools/wallet.js.map +1 -0
  127. package/dist/mcp/types.d.ts +413 -0
  128. package/dist/mcp/types.d.ts.map +1 -0
  129. package/dist/mcp/types.js +5 -0
  130. package/dist/mcp/types.js.map +1 -0
  131. package/dist/services/authorization-service.d.ts +97 -0
  132. package/dist/services/authorization-service.d.ts.map +1 -0
  133. package/dist/services/authorization-service.js +279 -0
  134. package/dist/services/authorization-service.js.map +1 -0
  135. package/dist/services/market-service.d.ts +108 -0
  136. package/dist/services/market-service.d.ts.map +1 -0
  137. package/dist/services/market-service.js +458 -0
  138. package/dist/services/market-service.js.map +1 -0
  139. package/dist/services/realtime-service.d.ts +82 -0
  140. package/dist/services/realtime-service.d.ts.map +1 -0
  141. package/dist/services/realtime-service.js +150 -0
  142. package/dist/services/realtime-service.js.map +1 -0
  143. package/dist/services/swap-service.d.ts +217 -0
  144. package/dist/services/swap-service.d.ts.map +1 -0
  145. package/dist/services/swap-service.js +695 -0
  146. package/dist/services/swap-service.js.map +1 -0
  147. package/dist/services/wallet-service.d.ts +94 -0
  148. package/dist/services/wallet-service.d.ts.map +1 -0
  149. package/dist/services/wallet-service.js +173 -0
  150. package/dist/services/wallet-service.js.map +1 -0
  151. package/dist/utils/price-utils.d.ts +153 -0
  152. package/dist/utils/price-utils.d.ts.map +1 -0
  153. package/dist/utils/price-utils.js +236 -0
  154. package/dist/utils/price-utils.js.map +1 -0
  155. package/docs/00-design.md +760 -0
  156. package/docs/01-mcp.md +2041 -0
  157. package/docs/02-API.md +1148 -0
  158. package/docs/e2e/01-trader-tools.md +159 -0
  159. package/docs/e2e/02-market-tools.md +180 -0
  160. package/docs/e2e/03-order-tools.md +166 -0
  161. package/docs/e2e/04-wallet-tools.md +224 -0
  162. package/docs/e2e/05-trading-tools.md +327 -0
  163. package/docs/e2e/06-integration-scenarios.md +481 -0
  164. package/docs/e2e/coordinator.md +376 -0
  165. package/examples/01-basic-usage.ts +68 -0
  166. package/examples/02-smart-money.ts +95 -0
  167. package/examples/03-market-analysis.ts +108 -0
  168. package/examples/04-kline-aggregation.ts +158 -0
  169. package/examples/05-follow-wallet-strategy.ts +156 -0
  170. package/examples/06-services-demo.ts +124 -0
  171. package/examples/07-realtime-websocket.ts +117 -0
  172. package/examples/08-trading-orders.ts +278 -0
  173. package/examples/09-rewards-tracking.ts +187 -0
  174. package/examples/10-ctf-operations.ts +336 -0
  175. package/examples/11-live-arbitrage-scan.ts +221 -0
  176. package/examples/12-trending-arb-monitor.ts +406 -0
  177. package/examples/README.md +179 -0
  178. package/package.json +62 -0
  179. package/scripts/README.md +163 -0
  180. package/scripts/approvals/approve-erc1155.ts +129 -0
  181. package/scripts/approvals/approve-neg-risk-erc1155.ts +149 -0
  182. package/scripts/approvals/approve-neg-risk.ts +102 -0
  183. package/scripts/approvals/check-all-allowances.ts +150 -0
  184. package/scripts/approvals/check-allowance.ts +129 -0
  185. package/scripts/approvals/check-ctf-approval.ts +158 -0
  186. package/scripts/datas/001-report.md +486 -0
  187. package/scripts/datas/clone-modal-screenshot.png +0 -0
  188. package/scripts/deposit/deposit-native-usdc.ts +179 -0
  189. package/scripts/deposit/deposit-usdc.ts +155 -0
  190. package/scripts/deposit/swap-usdc-to-usdce.ts +375 -0
  191. package/scripts/research/research-markets.ts +166 -0
  192. package/scripts/trading/check-orders.ts +50 -0
  193. package/scripts/trading/sell-nvidia-positions.ts +206 -0
  194. package/scripts/trading/test-order.ts +172 -0
  195. package/scripts/truth.md +440 -0
  196. package/scripts/verify/test-approve-trading.ts +98 -0
  197. package/scripts/verify/test-provider-fix.ts +43 -0
  198. package/scripts/verify/test-search-mcp.ts +113 -0
  199. package/scripts/verify/verify-all-apis.ts +160 -0
  200. package/scripts/wallet/check-wallet-balances.ts +75 -0
  201. package/scripts/wallet/test-wallet-operations.ts +191 -0
  202. package/scripts/wallet/verify-wallet-tools.ts +124 -0
  203. package/src/__tests__/clob-api.test.ts +301 -0
  204. package/src/__tests__/integration/bridge-client.integration.test.ts +314 -0
  205. package/src/__tests__/integration/clob-api.integration.test.ts +218 -0
  206. package/src/__tests__/integration/ctf-client.integration.test.ts +331 -0
  207. package/src/__tests__/integration/data-api.integration.test.ts +194 -0
  208. package/src/__tests__/integration/gamma-api.integration.test.ts +206 -0
  209. package/src/__tests__/test-utils.ts +170 -0
  210. package/src/clients/bridge-client.ts +841 -0
  211. package/src/clients/clob-api.ts +629 -0
  212. package/src/clients/ctf-client.ts +1216 -0
  213. package/src/clients/data-api.ts +469 -0
  214. package/src/clients/gamma-api.ts +597 -0
  215. package/src/clients/trading-client.ts +749 -0
  216. package/src/clients/websocket-manager.ts +267 -0
  217. package/src/core/cache-adapter-bridge.ts +94 -0
  218. package/src/core/cache.ts +85 -0
  219. package/src/core/errors.ts +117 -0
  220. package/src/core/rate-limiter.ts +74 -0
  221. package/src/core/types.ts +360 -0
  222. package/src/core/unified-cache.ts +153 -0
  223. package/src/index.ts +455 -0
  224. package/src/mcp/README.md +380 -0
  225. package/src/mcp/errors.ts +124 -0
  226. package/src/mcp/index.ts +309 -0
  227. package/src/mcp/server.ts +183 -0
  228. package/src/mcp/tools/guide.ts +821 -0
  229. package/src/mcp/tools/index.ts +73 -0
  230. package/src/mcp/tools/market.ts +363 -0
  231. package/src/mcp/tools/order.ts +326 -0
  232. package/src/mcp/tools/trade.ts +417 -0
  233. package/src/mcp/tools/trader.ts +322 -0
  234. package/src/mcp/tools/wallet.ts +683 -0
  235. package/src/mcp/types.ts +472 -0
  236. package/src/services/authorization-service.ts +357 -0
  237. package/src/services/market-service.ts +544 -0
  238. package/src/services/realtime-service.ts +196 -0
  239. package/src/services/swap-service.ts +896 -0
  240. package/src/services/wallet-service.ts +259 -0
  241. package/src/utils/price-utils.ts +307 -0
  242. package/tsconfig.json +8 -0
  243. package/vitest.config.ts +19 -0
  244. package/vitest.integration.config.ts +18 -0
@@ -0,0 +1,380 @@
1
+ # Polymarket MCP Server
2
+
3
+ A Model Context Protocol (MCP) server that exposes Polymarket functionality to AI agents.
4
+
5
+ ## Quick Start
6
+
7
+ ### Run with npx (after build)
8
+
9
+ ```bash
10
+ # Build the package
11
+ pnpm -F @prediction-router/poly-sdk build
12
+
13
+ # Run the MCP server
14
+ pnpm -F @prediction-router/poly-sdk mcp
15
+ ```
16
+
17
+ ### Development mode
18
+
19
+ ```bash
20
+ pnpm -F @prediction-router/poly-sdk mcp:dev
21
+ ```
22
+
23
+ ## Claude Desktop Configuration
24
+
25
+ Add to your Claude Desktop config (`~/Library/Application Support/Claude/claude_desktop_config.json`):
26
+
27
+ ### Read-only mode (market data, orderbook, trader analysis)
28
+
29
+ ```json
30
+ {
31
+ "mcpServers": {
32
+ "polymarket": {
33
+ "command": "node",
34
+ "args": ["/path/to/PerdictionRouter/packages/poly-sdk/dist/mcp/server.js"]
35
+ }
36
+ }
37
+ }
38
+ ```
39
+
40
+ ### Trading mode (with private key)
41
+
42
+ ```json
43
+ {
44
+ "mcpServers": {
45
+ "polymarket": {
46
+ "command": "node",
47
+ "args": ["/path/to/PerdictionRouter/packages/poly-sdk/dist/mcp/server.js"],
48
+ "env": {
49
+ "POLY_PRIVATE_KEY": "your-wallet-private-key"
50
+ }
51
+ }
52
+ }
53
+ }
54
+ ```
55
+
56
+ ### Development mode
57
+
58
+ ```json
59
+ {
60
+ "mcpServers": {
61
+ "polymarket": {
62
+ "command": "npx",
63
+ "args": ["tsx", "/path/to/PerdictionRouter/packages/poly-sdk/src/mcp/server.ts"],
64
+ "env": {
65
+ "POLY_PRIVATE_KEY": "your-wallet-private-key"
66
+ }
67
+ }
68
+ }
69
+ }
70
+ ```
71
+
72
+ ## Environment Variables
73
+
74
+ | Variable | Description | Required |
75
+ |----------|-------------|----------|
76
+ | `POLY_PRIVATE_KEY` | Wallet private key (with or without 0x prefix) | For trading |
77
+ | `POLY_CHAIN_ID` | Chain ID (137=mainnet, 80002=testnet) | Optional (default: 137) |
78
+
79
+ **Note**: API credentials are automatically derived from the private key using Polymarket's `createOrDeriveApiKey()`. You only need to provide the wallet private key to enable trading.
80
+
81
+ ## Available Tools
82
+
83
+ ### Trader Tools
84
+
85
+ | Tool | Description |
86
+ |------|-------------|
87
+ | `get_trader_positions` | Get current positions for a wallet address |
88
+ | `get_trader_trades` | Get trade history for a wallet |
89
+ | `get_trader_profile` | Get trader profile with PnL and stats |
90
+ | `get_leaderboard` | Get top traders from leaderboard |
91
+
92
+ ### Market Tools
93
+
94
+ | Tool | Description |
95
+ |------|-------------|
96
+ | `get_market` | Get market details by slug or conditionId |
97
+ | `search_markets` | Search markets by keyword |
98
+ | `get_trending_markets` | Get trending markets by volume/liquidity |
99
+ | `get_market_trades` | Get recent trades for a market |
100
+
101
+ ### Order Tools
102
+
103
+ | Tool | Description |
104
+ |------|-------------|
105
+ | `get_orderbook` | Get orderbook depth for a market outcome |
106
+ | `get_best_prices` | Get best bid/ask prices for both outcomes |
107
+ | `estimate_execution` | Estimate execution price and slippage |
108
+
109
+ ### Trade Tools (requires API credentials)
110
+
111
+ | Tool | Description |
112
+ |------|-------------|
113
+ | `place_limit_order` | Place a limit order |
114
+ | `place_market_order` | Place a market order |
115
+ | `cancel_order` | Cancel an existing order |
116
+ | `get_my_orders` | Get your open orders |
117
+
118
+ ### Wallet Tools (requires private key)
119
+
120
+ | Tool | Description |
121
+ |------|-------------|
122
+ | `get_supported_deposit_assets` | Get all supported assets and chains for deposits |
123
+ | `get_deposit_addresses` | Get deposit addresses (EVM, Solana, Bitcoin) |
124
+ | `check_allowances` | Check ERC20 and ERC1155 allowances for trading |
125
+ | `approve_trading` | Set up all required approvals for trading |
126
+
127
+ ## Tool Examples
128
+
129
+ ### Get Market
130
+
131
+ ```json
132
+ {
133
+ "name": "get_market",
134
+ "arguments": {
135
+ "identifier": "will-bitcoin-hit-100k-in-2024"
136
+ }
137
+ }
138
+ ```
139
+
140
+ Response:
141
+ ```json
142
+ {
143
+ "market": {
144
+ "conditionId": "0x...",
145
+ "question": "Will Bitcoin hit $100k in 2024?",
146
+ "slug": "will-bitcoin-hit-100k-in-2024"
147
+ },
148
+ "prices": {
149
+ "yes": 0.65,
150
+ "no": 0.35
151
+ },
152
+ "stats": {
153
+ "volume": 1500000,
154
+ "liquidity": 250000
155
+ }
156
+ }
157
+ ```
158
+
159
+ ### Get Trader Positions
160
+
161
+ ```json
162
+ {
163
+ "name": "get_trader_positions",
164
+ "arguments": {
165
+ "address": "0x1234..."
166
+ }
167
+ }
168
+ ```
169
+
170
+ Response:
171
+ ```json
172
+ {
173
+ "trader": {
174
+ "address": "0x1234...",
175
+ "name": "TopTrader"
176
+ },
177
+ "positions": [
178
+ {
179
+ "market": "Will Bitcoin hit $100k?",
180
+ "outcome": "Yes",
181
+ "size": 1000,
182
+ "avgPrice": 0.55,
183
+ "currentPrice": 0.65,
184
+ "pnl": 100,
185
+ "pnlPercent": 18.18
186
+ }
187
+ ],
188
+ "summary": {
189
+ "totalPositions": 5,
190
+ "totalValue": 5000,
191
+ "unrealizedPnl": 500
192
+ }
193
+ }
194
+ ```
195
+
196
+ ### Get Orderbook
197
+
198
+ ```json
199
+ {
200
+ "name": "get_orderbook",
201
+ "arguments": {
202
+ "conditionId": "0x...",
203
+ "outcome": "Yes",
204
+ "depth": 10
205
+ }
206
+ }
207
+ ```
208
+
209
+ Response:
210
+ ```json
211
+ {
212
+ "market": {
213
+ "conditionId": "0x...",
214
+ "title": "Will Bitcoin hit $100k?"
215
+ },
216
+ "outcome": "Yes",
217
+ "orderbook": {
218
+ "bids": [
219
+ { "price": 0.64, "size": 1000, "total": 640 },
220
+ { "price": 0.63, "size": 2000, "total": 1900 }
221
+ ],
222
+ "asks": [
223
+ { "price": 0.66, "size": 500, "total": 330 },
224
+ { "price": 0.67, "size": 1500, "total": 1335 }
225
+ ]
226
+ },
227
+ "summary": {
228
+ "bestBid": 0.64,
229
+ "bestAsk": 0.66,
230
+ "spread": 0.02,
231
+ "spreadPercent": 3.12
232
+ }
233
+ }
234
+ ```
235
+
236
+ ### Estimate Execution
237
+
238
+ ```json
239
+ {
240
+ "name": "estimate_execution",
241
+ "arguments": {
242
+ "conditionId": "0x...",
243
+ "outcome": "Yes",
244
+ "side": "BUY",
245
+ "amount": 1000
246
+ }
247
+ }
248
+ ```
249
+
250
+ Response:
251
+ ```json
252
+ {
253
+ "market": "Will Bitcoin hit $100k?",
254
+ "order": {
255
+ "side": "BUY",
256
+ "outcome": "Yes",
257
+ "usdcAmount": 1000
258
+ },
259
+ "estimate": {
260
+ "avgPrice": 0.658,
261
+ "sharesReceived": 1519.76,
262
+ "priceImpact": 0.012,
263
+ "worstPrice": 0.67
264
+ },
265
+ "warning": null
266
+ }
267
+ ```
268
+
269
+ ### Check Allowances
270
+
271
+ ```json
272
+ {
273
+ "name": "check_allowances",
274
+ "arguments": {}
275
+ }
276
+ ```
277
+
278
+ Response:
279
+ ```json
280
+ {
281
+ "wallet": "0x1234...",
282
+ "erc20": {
283
+ "usdc": {
284
+ "balance": "1000.00",
285
+ "allowances": [
286
+ { "contract": "CTF Exchange", "approved": true, "allowance": "unlimited" },
287
+ { "contract": "Neg Risk CTF Exchange", "approved": true, "allowance": "unlimited" }
288
+ ]
289
+ }
290
+ },
291
+ "erc1155": {
292
+ "conditionalTokens": {
293
+ "approvals": [
294
+ { "contract": "CTF Exchange", "approved": true },
295
+ { "contract": "Neg Risk CTF Exchange", "approved": true }
296
+ ]
297
+ }
298
+ },
299
+ "tradingReady": true,
300
+ "issues": []
301
+ }
302
+ ```
303
+
304
+ ### Get Deposit Addresses
305
+
306
+ ```json
307
+ {
308
+ "name": "get_deposit_addresses",
309
+ "arguments": {}
310
+ }
311
+ ```
312
+
313
+ Response:
314
+ ```json
315
+ {
316
+ "wallet": "0x1234...",
317
+ "addresses": {
318
+ "evm": "0xabc...",
319
+ "solana": "ABC123...",
320
+ "bitcoin": "bc1q..."
321
+ },
322
+ "evmChains": ["Ethereum", "Polygon", "Arbitrum", "Base", "Optimism"],
323
+ "instructions": "Send assets to these addresses to deposit to your Polymarket account..."
324
+ }
325
+ ```
326
+
327
+ ## Programmatic Usage
328
+
329
+ You can also use the MCP tools programmatically:
330
+
331
+ ```typescript
332
+ import { PolymarketSDK, createMcpHandler } from '@prediction-router/poly-sdk';
333
+
334
+ const sdk = new PolymarketSDK();
335
+ const handler = createMcpHandler(sdk);
336
+
337
+ // Call any tool
338
+ const result = await handler('get_market', {
339
+ identifier: 'btc-100k',
340
+ });
341
+
342
+ console.log(result);
343
+ ```
344
+
345
+ Or register with an MCP server instance:
346
+
347
+ ```typescript
348
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
349
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
350
+ import { PolymarketSDK, registerMcpTools } from '@prediction-router/poly-sdk';
351
+
352
+ const server = new Server(
353
+ { name: 'my-server', version: '1.0.0' },
354
+ { capabilities: { tools: {} } }
355
+ );
356
+
357
+ const sdk = new PolymarketSDK();
358
+ registerMcpTools(server, sdk);
359
+
360
+ const transport = new StdioServerTransport();
361
+ await server.connect(transport);
362
+ ```
363
+
364
+ ## Architecture
365
+
366
+ ```
367
+ src/mcp/
368
+ ├── index.ts # Main exports and handler factory
369
+ ├── server.ts # Standalone MCP server (CLI entry point)
370
+ ├── types.ts # TypeScript type definitions
371
+ ├── errors.ts # Error handling utilities
372
+ ├── tools/
373
+ │ ├── index.ts # Tool exports
374
+ │ ├── trader.ts # Trader analysis tools
375
+ │ ├── market.ts # Market discovery tools
376
+ │ ├── order.ts # Orderbook analysis tools
377
+ │ ├── trade.ts # Trading execution tools
378
+ │ └── wallet.ts # Deposit and authorization tools
379
+ └── README.md # This file
380
+ ```
@@ -0,0 +1,124 @@
1
+ /**
2
+ * MCP Error Handling for Polymarket Tools
3
+ */
4
+
5
+ export enum ErrorCode {
6
+ INVALID_ADDRESS = 'INVALID_ADDRESS',
7
+ MARKET_NOT_FOUND = 'MARKET_NOT_FOUND',
8
+ INSUFFICIENT_BALANCE = 'INSUFFICIENT_BALANCE',
9
+ ORDER_REJECTED = 'ORDER_REJECTED',
10
+ RATE_LIMITED = 'RATE_LIMITED',
11
+ AUTH_REQUIRED = 'AUTH_REQUIRED',
12
+ INVALID_INPUT = 'INVALID_INPUT',
13
+ INTERNAL_ERROR = 'INTERNAL_ERROR',
14
+ }
15
+
16
+ export class McpToolError extends Error {
17
+ constructor(
18
+ public code: ErrorCode,
19
+ message: string,
20
+ public details?: Record<string, unknown>
21
+ ) {
22
+ super(message);
23
+ this.name = 'McpToolError';
24
+ }
25
+
26
+ toResponse() {
27
+ return {
28
+ error: {
29
+ code: this.code,
30
+ message: this.message,
31
+ details: this.details,
32
+ },
33
+ };
34
+ }
35
+ }
36
+
37
+ export function validateAddress(address: string): void {
38
+ if (!address || typeof address !== 'string') {
39
+ throw new McpToolError(
40
+ ErrorCode.INVALID_ADDRESS,
41
+ 'Address is required'
42
+ );
43
+ }
44
+ if (!address.startsWith('0x') || address.length !== 42) {
45
+ throw new McpToolError(
46
+ ErrorCode.INVALID_ADDRESS,
47
+ 'Address must be a valid Ethereum address (0x...)',
48
+ { address }
49
+ );
50
+ }
51
+ }
52
+
53
+ export function validateConditionId(conditionId: string): void {
54
+ if (!conditionId || typeof conditionId !== 'string') {
55
+ throw new McpToolError(
56
+ ErrorCode.INVALID_INPUT,
57
+ 'Condition ID is required'
58
+ );
59
+ }
60
+ if (!conditionId.startsWith('0x')) {
61
+ throw new McpToolError(
62
+ ErrorCode.INVALID_INPUT,
63
+ 'Condition ID must start with 0x',
64
+ { conditionId }
65
+ );
66
+ }
67
+ }
68
+
69
+ export function validateOutcome(outcome: string): void {
70
+ if (!['Yes', 'No'].includes(outcome)) {
71
+ throw new McpToolError(
72
+ ErrorCode.INVALID_INPUT,
73
+ 'Outcome must be "Yes" or "No"',
74
+ { outcome }
75
+ );
76
+ }
77
+ }
78
+
79
+ export function validateSide(side: string): void {
80
+ if (!['BUY', 'SELL'].includes(side)) {
81
+ throw new McpToolError(
82
+ ErrorCode.INVALID_INPUT,
83
+ 'Side must be "BUY" or "SELL"',
84
+ { side }
85
+ );
86
+ }
87
+ }
88
+
89
+ export function validatePrice(price: number): void {
90
+ if (typeof price !== 'number' || price < 0.001 || price > 0.999) {
91
+ throw new McpToolError(
92
+ ErrorCode.INVALID_INPUT,
93
+ 'Price must be between 0.001 and 0.999',
94
+ { price }
95
+ );
96
+ }
97
+ }
98
+
99
+ export function validatePositiveNumber(value: number, name: string): void {
100
+ if (typeof value !== 'number' || value <= 0) {
101
+ throw new McpToolError(
102
+ ErrorCode.INVALID_INPUT,
103
+ `${name} must be a positive number`,
104
+ { [name]: value }
105
+ );
106
+ }
107
+ }
108
+
109
+ export function wrapError(err: unknown): McpToolError {
110
+ if (err instanceof McpToolError) {
111
+ return err;
112
+ }
113
+ if (err instanceof Error) {
114
+ // Check for known error types
115
+ if (err.message.includes('rate limit')) {
116
+ return new McpToolError(ErrorCode.RATE_LIMITED, 'Rate limit exceeded');
117
+ }
118
+ if (err.message.includes('not found')) {
119
+ return new McpToolError(ErrorCode.MARKET_NOT_FOUND, err.message);
120
+ }
121
+ return new McpToolError(ErrorCode.INTERNAL_ERROR, err.message);
122
+ }
123
+ return new McpToolError(ErrorCode.INTERNAL_ERROR, 'An unexpected error occurred');
124
+ }