cancel_order
Cancel pending trading orders on DhanHQ by providing the order ID. This tool requires authentication to execute order cancellations.
Instructions
Cancels a pending order. Requires authentication and a valid order ID.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| orderId | Yes | Order ID to cancel |
Input Schema (JSON Schema)
{
"properties": {
"orderId": {
"description": "Order ID to cancel",
"type": "string"
}
},
"required": [
"orderId"
],
"type": "object"
}
Implementation Reference
- src/authentication.ts:321-344 (handler)Core handler function that executes the cancel order logic by sending a DELETE request to the Dhan API endpoint.export async function cancelOrder(orderId: string): Promise<OrderResponse> { try { log(`Cancelling order: ${orderId}`); const response = await axios.delete<OrderResponse>( `https://api.dhan.co/v2/orders/${orderId}`, { headers: getApiHeaders(), } ); log(`✓ Order cancelled successfully. Order ID: ${response.data.orderId}`); return response.data; } catch (error) { const errorMessage = error instanceof axios.AxiosError ? `API Error: ${error.response?.status} - ${JSON.stringify(error.response?.data)}` : error instanceof Error ? error.message : 'Unknown error'; log(`✗ Failed to cancel order: ${errorMessage}`); throw new Error(`Failed to cancel order: ${errorMessage}`); }
- src/index.ts:558-570 (handler)MCP tool dispatcher handler that extracts the orderId argument and invokes the cancelOrder function, returning the result as MCP content.case 'cancel_order': { console.error('[Tool] Executing: cancel_order'); const { orderId } = args as Record<string, unknown>; const result = await cancelOrder(orderId as string); return { content: [ { type: 'text' as const, text: JSON.stringify(result, null, 2), }, ], }; }
- src/index.ts:188-199 (schema)Tool schema definition including name, description, and input schema requiring a string orderId.{ name: 'cancel_order', description: 'Cancels a pending order. Requires authentication and a valid order ID.', inputSchema: { type: 'object' as const, properties: { orderId: { type: 'string', description: 'Order ID to cancel' }, }, required: ['orderId'], }, },
- src/index.ts:359-361 (registration)Registration of the tools list handler, which includes the cancel_order tool schema for MCP listTools requests.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools, }));