Skip to main content
Glama
t3rmed

Hyperliquid MCP Server

by t3rmed

place_trigger_order

Execute stop-loss or take-profit orders on Hyperliquid DEX by setting trigger prices for automated position management and risk control.

Instructions

Place a trigger order (stop-loss or take-profit) on Hyperliquid

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
assetIndexYesAsset index for the coin (0 for BTC, 1 for ETH, etc.)
clientOrderIdNoClient order ID (optional)
isBuyYesTrue for buy order, false for sell order
isMarketYesWhether to execute as market order when triggered
reduceOnlyNoWhether this is a reduce-only order (optional, default false)
sizeYesOrder size as string
triggerPriceYesTrigger price as string
triggerTypeYesTrigger type

Implementation Reference

  • Tool schema definition for place_trigger_order, including input parameters like assetIndex, isBuy, size, triggerPrice, etc.
    place_trigger_order_tool = Tool(
        name="place_trigger_order",
        description="Place a trigger order (stop-loss or take-profit) on Hyperliquid",
        inputSchema={
            "type": "object",
            "properties": {
                "assetIndex": {
                    "type": "number",
                    "description": "Asset index for the coin (0 for BTC, 1 for ETH, etc.)",
                },
                "isBuy": {
                    "type": "boolean",
                    "description": "True for buy order, false for sell order",
                },
                "size": {
                    "type": "string",
                    "description": "Order size as string",
                },
                "triggerPrice": {
                    "type": "string",
                    "description": "Trigger price as string",
                },
                "isMarket": {
                    "type": "boolean",
                    "description": "Whether to execute as market order when triggered",
                },
                "triggerType": {
                    "type": "string",
                    "description": "Trigger type",
                    "enum": ["tp", "sl"],
                },
                "reduceOnly": {
                    "type": "boolean",
                    "description": "Whether this is a reduce-only order (optional, default false)",
                },
                "clientOrderId": {
                    "type": "string",
                    "description": "Client order ID (optional)",
                },
            },
            "required": ["assetIndex", "isBuy", "size", "triggerPrice", "isMarket", "triggerType"],
        },
    )
  • The main handler function that processes the place_trigger_order tool call, constructs the OrderRequest with trigger type, and calls the HyperliquidClient to place the order.
    async def handle_place_trigger_order(client: HyperliquidClient, args: Dict[str, Any]) -> Dict[str, Any]:
        """Handle place trigger order request."""
        asset_index = args["assetIndex"]
        is_buy = args["isBuy"]
        size = args["size"]
        trigger_price = args["triggerPrice"]
        is_market = args["isMarket"]
        trigger_type = args["triggerType"]
        reduce_only = args.get("reduceOnly", False)
        client_order_id = args.get("clientOrderId")
    
        order = OrderRequest(
            a=asset_index,
            b=is_buy,
            p="0",  # Not used for trigger orders
            s=size,
            r=reduce_only,
            t={
                "trigger": TriggerOrderType(
                    triggerPx=trigger_price,
                    isMarket=is_market,
                    tpsl=trigger_type,
                )
            },
        )
    
        if client_order_id:
            order.c = client_order_id
    
        action = PlaceOrderAction(orders=[order])
        result = await client.place_order(action)
    
        if not result.success:
            raise ValueError(f"Failed to place trigger order: {result.error}")
    
        return {
            "content": [
                TextContent(
                    type="text",
                    text=f"Trigger order placed successfully!\n\n{json.dumps(result.data, indent=2)}",
                )
            ]
        }
  • Registration of the place_trigger_order_tool in the MCP server's list_tools handler.
    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,
        ]
  • Dispatch/registration of the handle_place_trigger_order in the MCP call_tool handler for the name 'place_trigger_order'.
    async def call_tool(name: str, arguments: dict[str, Any] | None) -> list[TextContent]:
        """Handle tool calls."""
        args = arguments or {}
    
        try:
            if name == "get_all_mids":
                result = await handle_get_all_mids(client, args)
            elif name == "get_l2_book":
                result = await handle_get_l2_book(client, args)
            elif name == "get_candle_snapshot":
                result = await handle_get_candle_snapshot(client, args)
            elif name == "get_open_orders":
                result = await handle_get_open_orders(client, args)
            elif name == "get_user_fills":
                result = await handle_get_user_fills(client, args)
            elif name == "get_user_fills_by_time":
                result = await handle_get_user_fills_by_time(client, args)
            elif name == "get_portfolio":
                result = await handle_get_portfolio(client, args)
            elif name == "place_order":
                result = await handle_place_order(client, args)
            elif name == "place_trigger_order":
                result = await handle_place_trigger_order(client, args)
            elif name == "cancel_order":
                result = await handle_cancel_order(client, args)
            elif name == "cancel_all_orders":
                result = await handle_cancel_all_orders(client, args)
            else:
                raise ValueError(f"Unknown tool: {name}")
    
            return result["content"]
    
        except Exception as error:
            error_message = str(error)
            return [
                TextContent(
                    type="text",
                    text=f"Error: {error_message}",
                )
            ]
  • Registration of the handler in the TOOL_HANDLERS dict for the HTTP MCP server.
    TOOL_HANDLERS = {
        "get_all_mids": handle_get_all_mids,
        "get_l2_book": handle_get_l2_book,
        "get_candle_snapshot": handle_get_candle_snapshot,
        "get_open_orders": handle_get_open_orders,
        "get_user_fills": handle_get_user_fills,
        "get_user_fills_by_time": handle_get_user_fills_by_time,
        "get_portfolio": handle_get_portfolio,
        "place_order": handle_place_order,
        "place_trigger_order": handle_place_trigger_order,
        "cancel_order": handle_cancel_order,
        "cancel_all_orders": handle_cancel_all_orders,
    }

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/t3rmed/hyperliquid-mcp'

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