orders.cancel
Cancel all open/pending limit orders on Polymarket with a single call. Use as an emergency stop, before changing strategy, or when unwinding positions. Returns the count of cancelled orders. Not reversible.
Instructions
Cancel ALL open/pending limit orders on Polymarket for this account in a single call. Use as an emergency stop, before changing strategy, after a sudden price move, or when unwinding positions. Not reversible — cancelled orders must be re-placed via orders.buy, wta.bid, or orders.batch. Returns the count of cancelled orders. Call orders.list first if you want to preview what will be cancelled. Only works in live mode (no-op in preview). No parameters. Pro feature.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:274-279 (registration)Registers the 'orders.cancel' MCP tool with the server, linking it to cancelOrdersSchema for input validation and the handleCancelOrders function for execution.
server.tool( "orders.cancel", "Cancel ALL open/pending limit orders on Polymarket for this account in a single call. Use as an emergency stop, before changing strategy, after a sudden price move, or when unwinding positions. Not reversible — cancelled orders must be re-placed via orders.buy, wta.bid, or orders.batch. Returns the count of cancelled orders. Call orders.list first if you want to preview what will be cancelled. Only works in live mode (no-op in preview). No parameters. Pro feature.", cancelOrdersSchema.shape, safe("orders.cancel", async () => ({ content: [{ type: "text" as const, text: await handleCancelOrders(tradeExecutor) }] })) ); - src/tools/cancel-orders.ts:6-6 (schema)Defines the cancelOrdersSchema as an empty Zod object (no parameters required) for the 'orders.cancel' tool.
export const cancelOrdersSchema = z.object({}); - src/tools/cancel-orders.ts:8-30 (handler)The handleCancelOrders function: checks Pro license, validates live mode, calls executor.cancelAllOrders(), and returns the count of cancelled orders or an error message.
export async function handleCancelOrders(executor: TradeExecutor): Promise<string> { const isPro = await checkLicense(); if (!isPro) return requirePro("cancel_orders"); if (executor.getMode() !== "live") { return "Cancel orders only works in live mode. Use `go_live` to switch to live trading first."; } try { const result = await executor.cancelAllOrders(); if (result.cancelled === 0) { return "No open orders to cancel."; } log("trade", `Cancelled ${result.cancelled} open orders`); return `Cancelled ${result.cancelled} open orders.`; } catch (err: any) { log("error", `Cancel orders failed: ${err}`); const hint = err?.message?.includes("credentials") ? " Verify your API credentials are configured correctly." : ""; return `Failed to cancel orders. The Polymarket API returned an error.${hint} Check the event log for details.`; } } - The cancelAllOrders() method on TradeExecutor: fetches open orders via the CLOB client, cancels them all, and returns the count of cancelled orders.
async cancelAllOrders(): Promise<{ cancelled: number }> { const client = await this.getClobClient(); const openOrders = await client.getOpenOrders(); if (!openOrders || openOrders.length === 0) return { cancelled: 0 }; await client.cancelAll(); return { cancelled: openOrders.length }; } - src/utils/tool-wrapper.ts:6-19 (helper)The 'safe' wrapper used by the tool registration to catch errors and return user-friendly error messages.
export function safe( toolName: string, handler: (...args: any[]) => McpResult | Promise<McpResult> ): (...args: any[]) => Promise<McpResult> { return async (...args: any[]) => { try { return await handler(...args); } catch (err: any) { const message = err?.message ?? String(err); log("error", `Tool "${toolName}" failed: ${message}`); return { content: [{ type: "text" as const, text: `An error occurred while running "${toolName}". Please try again or check the event log for details.` }] }; } }; }