Skip to main content
Glama
harshitdynamite

DhanHQ MCP Server

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

TableJSON Schema
NameRequiredDescriptionDefault
orderIdYesOrder ID to cancel

Implementation Reference

  • 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}`);
      }
  • 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),
          },
        ],
      };
    }
  • 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,
    }));

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/harshitdynamite/DhanMCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server