Skip to main content
Glama
samklein952-hub

Hyperliquid MCP Server

close_position

Close open trading positions on Hyperliquid exchange using market or limit orders to manage risk and exit trades.

Instructions

Close an open position (market close or limit close)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
symbolYesMarket symbol
priceNoLimit price to close at (omit for market close)

Implementation Reference

  • Main handler function that executes the close_position logic. Gets open positions, validates the symbol exists, determines position side (long/short), calculates appropriate price if market close, and submits a reduce-only order to close the position.
    def close_position(self, symbol: str, price: Optional[float] = None) -> dict[str, Any]:
        """Close an open position."""
        exchange = self._require_exchange()
        positions = self.get_positions()
        pos = next((p for p in positions if p["symbol"].upper() == symbol.upper()), None)
        if not pos:
            raise ValueError(f"No open position for {symbol}.")
    
        size = abs(float(pos["size"]))
        is_long = float(pos["size"]) > 0
        # To close: sell if long, buy if short
        is_buy = not is_long
    
        if price is None:
            # Market close
            info = self.get_market_info(symbol)
            mid = float(info["mid_price"])
            price = round(mid * (1.01 if is_buy else 0.99), 6)
    
        result = exchange.order(symbol.upper(), is_buy, size, price, {"limit": {"tif": "Gtc"}}, reduce_only=True)
        return {"status": "close_submitted", "symbol": symbol, "size": size, "result": result}
  • Input schema definition for the close_position tool. Defines the expected parameters: symbol (required string) and price (optional number for limit closes).
    Tool(
        name="close_position",
        description="Close an open position (market close or limit close)",
        inputSchema={
            "type": "object",
            "properties": {
                "symbol": {"type": "string", "description": "Market symbol"},
                "price": {"type": "number", "description": "Limit price to close at (omit for market close)"},
            },
            "required": ["symbol"],
        },
    ),
  • Dispatch case in the _dispatch function that routes close_position tool calls to the client.close_position method with symbol and optional price arguments.
    case "close_position":
        return client.close_position(args["symbol"], args.get("price"))
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Discloses support for both market and limit close behaviors, but lacks information on error cases (e.g., no open position) or side effects.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Single, efficient sentence that front-loads the action with zero redundancy.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Adequate for the simple 2-parameter input, though missing error handling details given no output schema exists.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100% so baseline applies; description adds minimal context beyond schema but clarifies the market/limit dichotomy.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

Clearly states the tool closes open positions and distinguishes between market and limit close modes.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Provides no guidance on when to select this tool versus siblings like place_order, or when to prefer market vs limit close.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other 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/samklein952-hub/hyperliquid-mcp'

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