-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
393 lines (322 loc) · 14.6 KB
/
Copy pathserver.py
File metadata and controls
393 lines (322 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import os
import sys
from typing import Dict, Any, Literal
from eth_account import Account
from dotenv import load_dotenv
from pydantic import BaseModel, Field
import json
# FastMCP imports
from fastmcp import FastMCP
# HyperLiquid SDK imports
from hyperliquid.info import Info
from hyperliquid.exchange import Exchange
from hyperliquid.utils import constants # Provides TESTNET_API_URL and MAINNET_API_URL
# -------------------- 1. INITIALIZATION AND AUTHENTICATION --------------------
load_dotenv()
# --- FIX: DETERMINE THE API ENDPOINT (Defaults to TESTNET) ---
hl_env = os.environ.get("HYPERLIQUID_ENV", "TESTNET").upper()
HL_API_URL = constants.TESTNET_API_URL if hl_env == "TESTNET" else constants.MAINNET_API_URL
# Get the private key from your .env file
private_key = os.environ.get("HYPERLIQUID_PRIVATE_KEY")
is_key_valid = True
hl_account = None
user_address = "0x000000000000000000000000000000000000DEAD" # Default address
if not private_key:
is_key_valid = False
print("WARNING: HYPERLIQUID_PRIVATE_KEY not set. Trading tools disabled.", file=sys.stderr)
else:
try:
# Step 1: Use eth_account to validate and create the account object
key_bytes = private_key.lower().replace('0x', '')
hl_account = Account.from_key(key_bytes)
user_address = hl_account.address # This is the address that holds the funds
except ValueError as e:
is_key_valid = False
print(f"ERROR: Invalid Private Key format in .env: {e}", file=sys.stderr)
except Exception as e:
is_key_valid = False
print(f"An unexpected error occurred during key setup: {e}", file=sys.stderr)
# Client Setup (Info client)
# FIX APPLIED: Pass the determined API URL (Testnet or Mainnet)
hl_info = Info(HL_API_URL)
# Client Setup (Exchange client - only initialize if key is valid)
if is_key_valid:
try:
# FIX APPLIED: Pass the hl_account object AND the determined API URL
hl_exchange = Exchange(hl_account, HL_API_URL)
except TypeError as e:
hl_exchange = None
print(f"CRITICAL ERROR: Failed to initialize Exchange client: {e}", file=sys.stderr)
is_key_valid = False
else:
hl_exchange = None
# -------------------- 2. PYDANTIC MODELS FOR TOOL INPUTS (Documentation only) --------------------
class MarketOrderInput(BaseModel):
"""Defines input parameters for placing a market order."""
coin: str
is_buy: bool
size: float
reduce_only: bool
class LimitOrderInput(BaseModel):
"""Defines input parameters for placing a limit order."""
coin: str
is_buy: bool
size: float
limit_price: float
time_in_force: Literal["Gtc", "Ioc", "Alo"]
reduce_only: bool
class CancelOrderInput(BaseModel):
"""Defines input parameters for canceling a specific order."""
coin: str
order_id: int
class CandlestickInput(BaseModel):
"""Defines input parameters for retrieving candlestick data."""
coin: str
interval: Literal["1m", "5m", "15m", "30m", "1h", "4h", "1d"]
limit: int
# -------------------- 3. FASTMCP SERVER DEFINITION --------------------
mcp = FastMCP(
name="HyperLiquid DEX Tester",
instructions="Provides read-only market data and authorized trading tools for HyperLiquid DEX."
)
# -------------------- READ-ONLY TOOLS (INFO CLIENT) --------------------
@mcp.tool()
async def get_user_state() -> Dict[str, Any]:
"""
Retrieves the user's current account state (balances, margin, and positions)
for the configured wallet address on the Hyperliquid trading layer.
"""
state = hl_info.user_state(user_address)
return state
@mcp.tool()
async def get_mid_price(coin: str) -> float:
"""
Retrieves the current mid-price (midpoint between best bid and best ask) for a perpetual market.
Args: coin (str): The asset symbol (e.g., 'BTC', 'ETH', 'SOL').
"""
mids_dict = hl_info.all_mids()
if not isinstance(mids_dict, dict):
print(f"ERROR: HyperLiquid API returned unexpected type: {type(mids_dict)}. Response: {mids_dict}", file=sys.stderr)
return -1.0
coin_symbol = coin.upper()
price_str = mids_dict.get(coin_symbol)
if price_str is None:
return 0.0
try:
return float(price_str)
except (ValueError, TypeError) as e:
print(f"WARNING: Could not convert price '{price_str}' to float for {coin_symbol}. Error: {e}", file=sys.stderr)
return -2.0
@mcp.tool()
async def get_order_book(coin: str) -> Dict[str, Any]:
"""
Retrieves the current price from the all_mids endpoint as a simplified order book representation.
Args: coin (str): The asset symbol (e.g., 'BTC', 'ETH').
"""
try:
mids_dict = hl_info.all_mids()
coin_symbol = coin.upper()
price_str = mids_dict.get(coin_symbol)
if price_str is None:
return {"error": f"Coin {coin_symbol} not found in current market data."}
price = float(price_str)
# Create a simplified 'L2' response using the mid-price
return {
"coin": coin_symbol,
"mid_price": price,
"bids": [{"price": price * 0.9999, "size": 1.0}],
"asks": [{"price": price * 1.0001, "size": 1.0}],
"note": "⚠️ Data is simplified. Full L2 depth is unavailable in this SDK version or client.",
}
except Exception as e:
print(f"ERROR in fallback order book: {e}", file=sys.stderr)
return {"error": f"Failed to retrieve price data for {coin}: {str(e)}"}
@mcp.tool()
async def get_open_orders() -> Dict[str, Any]:
"""
Retrieves all currently open limit and trigger orders for the configured wallet address.
"""
try:
orders = hl_info.open_orders(user_address)
if not orders:
return {"status": "success", "message": "No open orders found.", "orders": []}
# Simplify the response structure for the tool user
clean_orders = []
for order in orders:
clean_orders.append({
"coin": order.get('coin'),
"order_id": order.get('oid'),
"side": "BUY" if order.get('side') == 'B' else "SELL",
"limit_price": float(order.get('limitPx')),
"size": float(order.get('sz')),
"timestamp_ms": order.get('timestamp')
})
return {
"status": "success",
"message": f"Found {len(clean_orders)} open orders.",
"orders": clean_orders
}
except Exception as e:
print(f"CRITICAL ERROR during get_open_orders: {e}", file=sys.stderr)
return {"error": f"Failed to retrieve open orders: {str(e)}"}
@mcp.tool()
async def get_all_perpetual_markets() -> Dict[str, Any]:
"""
Retrieves a list of all perpetual contracts available for trading on HyperLiquid.
"""
try:
metadata = hl_info.meta()
perpetuals = [
asset['name']
for asset in metadata.get('universe', [])
if asset.get('type') == 'perp'
]
return {
"status": "success",
"message": f"Retrieved {len(perpetuals)} perpetual contracts.",
"perpetual_contracts": perpetuals
}
except Exception as e:
print(f"CRITICAL ERROR during get_all_perpetual_markets: {e}", file=sys.stderr)
return {"error": f"Failed to retrieve market list: {str(e)}"}
# -------------------- TRADING TOOLS (EXCHANGE CLIENT) --------------------
@mcp.tool()
# FIX APPLIED: RENAMED to execute_market_order to bust the tool cache
async def execute_market_order(
coin: str = Field(..., description="The asset symbol to trade (e.g., 'BTC', 'ETH')."),
is_buy: bool = Field(..., description="True for a BUY order (go long/reduce short), False for a SELL order (go short/reduce long)."),
size: float = Field(..., gt=0, description="The size of the order, must be greater than zero."),
reduce_only: bool = Field(False, description="Set to True to ensure the order only reduces an existing position.")
) -> Dict[str, Any]:
"""
Executes an immediate market order to buy or sell a specified size of an asset.
This tool requires a valid private key for transaction signing.
"""
if not is_key_valid or not hl_exchange:
return {"error": "Trading is disabled. Private key is invalid or Exchange client failed to initialize."}
try:
# Get the market price for the order type
mid_price = await get_mid_price(coin)
if mid_price <= 0:
return {"error": "Could not retrieve valid market price to use for the order."}
# For market orders, limit_px is set far from the mid price to guarantee a fill
limit_px = mid_price * (1.05 if is_buy else 0.95)
result = hl_exchange.order(
coin=coin.upper(),
is_buy=is_buy,
sz=size,
limit_px=limit_px,
order_type={"market": True},
reduce_only=reduce_only
)
status_data = result.get('response', {}).get('data', {}).get('statuses', [{}])[0]
if 'error' in status_data:
return {"status": "failed", "exchange_error": status_data['error']}
return {
"status": "success",
"message": f"Market order placed on {coin}.",
"side": "BUY" if is_buy else "SELL",
"size": size,
"tx_hash": result.get('response', {}).get('hash')
}
except Exception as e:
print(f"CRITICAL ERROR during market order placement: {e}", file=sys.stderr)
return {"error": f"Failed to place order: {str(e)}"}
@mcp.tool()
# FIX APPLIED: Using scalar arguments to resolve the "not callable" error
async def place_limit_order(
coin: str = Field(..., description="The asset symbol to trade (e.g., 'BTC', 'ETH')."),
is_buy: bool = Field(..., description="True for a BUY order, False for a SELL order."),
size: float = Field(..., gt=0, description="The size of the order, must be greater than zero."),
limit_price: float = Field(..., gt=0, description="The specific price at which the order should be filled."),
time_in_force: Literal["Gtc", "Ioc", "Alo"] = Field("Gtc", description="Time-in-Force. Gtc: Good-Til-Canceled (default). Ioc: Immediate-Or-Cancel. Alo: Add-Liquidity-Only (Post-Only)."),
reduce_only: bool = Field(False, description="Set to True to ensure the order only reduces an existing position.")
) -> Dict[str, Any]:
"""
Places a limit order to buy or sell a specified size at a specific price.
This tool requires a valid private key for transaction signing.
"""
if not is_key_valid or not hl_exchange:
return {"error": "Trading is disabled. Private key is invalid or Exchange client failed to initialize."}
try:
result = hl_exchange.order(
coin=coin.upper(),
is_buy=is_buy,
sz=size,
limit_px=limit_price,
order_type={"limit": {"tif": time_in_force}},
reduce_only=reduce_only
)
status_data = result.get('response', {}).get('data', {}).get('statuses', [{}])[0]
if 'error' in status_data:
return {"status": "failed", "exchange_error": status_data['error']}
order_id = status_data.get('resting', {}).get('oid')
return {
"status": "success",
"message": f"Limit order placed for {size} {coin} at {limit_price} with TIF: {time_in_force}.",
"side": "BUY" if is_buy else "SELL",
"order_id": order_id,
"tx_hash": result.get('response', {}).get('hash')
}
except Exception as e:
print(f"CRITICAL ERROR during limit order placement: {e}", file=sys.stderr)
return {"error": f"Failed to place limit order: {str(e)}"}
@mcp.tool()
async def cancel_all_orders() -> Dict[str, Any]:
"""
Cancels all open limit and trigger orders on the user's account across all assets.
This tool is used for risk management and requires authorization.
"""
if not is_key_valid or not hl_exchange:
return {"error": "Trading is disabled. Private key is invalid or Exchange client failed to initialize."}
try:
result = hl_exchange.cancel_all()
tx_hash = result.get('response', {}).get('hash')
if tx_hash:
return {
"status": "success",
"message": "All open orders successfully submitted for cancellation.",
"tx_hash": tx_hash
}
else:
return {
"status": "warning",
"message": "Cancellation request submitted, but no immediate transaction hash was returned (may mean no open orders found)."
}
except Exception as e:
print(f"CRITICAL ERROR during cancel_all: {e}", file=sys.stderr)
return {"error": f"Failed to execute cancel_all: {str(e)}"}
@mcp.tool()
# FIX APPLIED: Using scalar arguments to resolve the "not callable" error
async def cancel_order_by_id(
coin: str = Field(..., description="The asset symbol (e.g., 'BTC', 'ETH') of the order to cancel."),
order_id: int = Field(..., gt=0, description="The unique numerical ID of the open order to cancel.")
) -> Dict[str, Any]:
"""
Cancels a single, specific open order using its unique Order ID (oid).
This tool requires a valid private key for transaction signing.
"""
if not is_key_valid or not hl_exchange:
return {"error": "Trading is disabled. Private key is invalid or Exchange client failed to initialize."}
try:
result = hl_exchange.cancel(
coin=coin.upper(),
oid=order_id
)
tx_hash = result.get('response', {}).get('hash')
status_data = result.get('response', {}).get('data', {}).get('statuses', [{}])[0]
if 'error' in status_data:
return {"status": "failed", "exchange_error": status_data['error']}
return {
"status": "success",
"message": f"Cancellation request submitted for Order ID {order_id} on {coin}.",
"order_id": order_id,
"tx_hash": tx_hash
}
except Exception as e:
print(f"CRITICAL ERROR during cancel_order_by_id: {e}", file=sys.stderr)
return {"error": f"Failed to cancel order {order_id}: {str(e)}"}
# -------------------- 4. RUN THE SERVER --------------------
if __name__ == "__main__":
print(f"Starting HyperLiquid MCP Server for address: {user_address} on {hl_env} environment. Waiting for client connection (STDIO)...", file=sys.stderr)
mcp.run()