cancel_order
Cancel trading orders on Hyperliquid DEX using order ID or client order ID to manage positions and execute portfolio adjustments.
Instructions
Cancel a specific order by order ID or client order ID
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| assetIndex | Yes | Asset index for the coin | |
| clientOrderId | No | Client order ID to cancel (use either orderId or clientOrderId) | |
| orderId | No | Order ID to cancel (use either orderId or clientOrderId) |
Implementation Reference
- The async handle_cancel_order function implements the core logic for the cancel_order tool: extracts args, validates orderId or clientOrderId, creates CancelOrderItem and CancelOrderAction, calls client.cancel_order, returns formatted success response or raises ValueError on failure.async def handle_cancel_order(client: HyperliquidClient, args: Dict[str, Any]) -> Dict[str, Any]: """Handle cancel order request.""" asset_index = args["assetIndex"] order_id = args.get("orderId") client_order_id = args.get("clientOrderId") if not order_id and not client_order_id: raise ValueError("Either orderId or clientOrderId must be provided") cancel_item = CancelOrderItem(a=asset_index) if order_id: cancel_item.o = order_id else: cancel_item.c = client_order_id action = CancelOrderAction(cancels=[cancel_item]) result = await client.cancel_order(action) if not result.success: raise ValueError(f"Failed to cancel order: {result.error}") return { "content": [ TextContent( type="text", text=f"Order cancelled successfully!\n\n{json.dumps(result.data, indent=2)}", ) ] }
- src/tools/trading.ts:223-257 (handler)The handleCancelOrder function implements the core logic for the cancel_order tool: extracts args, validates orderId or clientOrderId, creates cancel object and CancelOrderAction, calls client.cancelOrder, returns formatted success response or throws Error on failure. Nearly identical to Python version.export async function handleCancelOrder(client: HyperliquidClient, args: any) { const { assetIndex, orderId, clientOrderId } = args; if (!orderId && !clientOrderId) { throw new Error('Either orderId or clientOrderId must be provided'); } const cancel: any = { a: assetIndex }; if (orderId) { cancel.o = orderId; } else { cancel.c = clientOrderId; } const action: CancelOrderAction = { type: 'cancel', cancels: [cancel] }; const result = await client.cancelOrder(action); if (!result.success) { throw new Error(`Failed to cancel order: ${result.error}`); } return { content: [ { type: 'text', text: `Order cancelled successfully!\n\n${JSON.stringify(result.data, null, 2)}` } ] }; }
- Defines the cancel_order_tool Tool object with name, description, and detailed inputSchema specifying parameters like assetIndex (required), orderId, clientOrderId.cancel_order_tool = Tool( name="cancel_order", description="Cancel a specific order by order ID or client order ID", inputSchema={ "type": "object", "properties": { "assetIndex": { "type": "number", "description": "Asset index for the coin", }, "orderId": { "type": "number", "description": "Order ID to cancel (use either orderId or clientOrderId)", }, "clientOrderId": { "type": "string", "description": "Client order ID to cancel (use either orderId or clientOrderId)", }, }, "required": ["assetIndex"], }, )
- src/tools/trading.ts:89-110 (schema)Defines the cancelOrderTool Tool object with name, description, and inputSchema matching the Python version.export const cancelOrderTool: Tool = { name: 'cancel_order', description: 'Cancel a specific order by order ID or client order ID', inputSchema: { type: 'object', properties: { assetIndex: { type: 'number', description: 'Asset index for the coin' }, orderId: { type: 'number', description: 'Order ID to cancel (use either orderId or clientOrderId)' }, clientOrderId: { type: 'string', description: 'Client order ID to cancel (use either orderId or clientOrderId)' } }, required: ['assetIndex'] } };
- hyperliquid_mcp_server/main.py:67-86 (registration)The list_tools handler registers cancel_order_tool among other tools for the MCP server.@app.list_tools() async def list_tools() -> list: """List all available tools.""" return [ # Market data tools get_all_mids_tool, get_l2_book_tool, get_candle_snapshot_tool, # Account info tools get_open_orders_tool, get_user_fills_tool, get_user_fills_by_time_tool, get_portfolio_tool, # Trading tools place_order_tool, place_trigger_order_tool, cancel_order_tool, cancel_all_orders_tool, ]