feat(mcp-hyperliquid): leverage_cap + testnet resolver + environment_info
This commit is contained in:
@@ -5,11 +5,9 @@ import os
|
||||
from fastapi import Depends, FastAPI, HTTPException
|
||||
from mcp_common.auth import Principal, TokenStore, require_principal
|
||||
from mcp_common.mcp_bridge import mount_mcp_endpoint
|
||||
from mcp_common.risk_guard import (
|
||||
enforce_aggregate,
|
||||
enforce_leverage,
|
||||
enforce_single_notional,
|
||||
)
|
||||
from mcp_common.environment import EnvironmentInfo
|
||||
from mcp_hyperliquid.leverage_cap import enforce_leverage as _enforce_leverage
|
||||
from mcp_hyperliquid.leverage_cap import get_max_leverage
|
||||
from mcp_common.server import build_app
|
||||
from pydantic import BaseModel, field_validator, model_validator
|
||||
|
||||
@@ -167,35 +165,6 @@ class ClosePositionReq(BaseModel):
|
||||
instrument: str
|
||||
|
||||
|
||||
# --- CER-016 notional helpers ---
|
||||
|
||||
async def _compute_notional_hl(client: HyperliquidClient, body: PlaceOrderReq) -> float:
|
||||
"""HL perp: amount è in base asset → notional = amount * price."""
|
||||
ref_price: float | None = body.price
|
||||
if ref_price is None:
|
||||
try:
|
||||
tk = await client.get_ticker(body.instrument)
|
||||
ref_price = tk.get("mark_price") or tk.get("last_price") or tk.get("price")
|
||||
except Exception:
|
||||
ref_price = None
|
||||
if not ref_price:
|
||||
return 0.0
|
||||
return float(body.amount) * float(ref_price)
|
||||
|
||||
|
||||
async def _current_aggregate_hl(client: HyperliquidClient) -> float:
|
||||
try:
|
||||
positions = await client.get_positions()
|
||||
except Exception:
|
||||
return 0.0
|
||||
total = 0.0
|
||||
for p in positions or []:
|
||||
size = abs(float(p.get("size") or p.get("amount") or 0))
|
||||
mark = float(p.get("mark_price") or p.get("price") or 0)
|
||||
total += size * mark
|
||||
return total
|
||||
|
||||
|
||||
# --- ACL helper ---
|
||||
|
||||
def _check(principal: Principal, *, core: bool = False, observer: bool = False) -> None:
|
||||
@@ -210,11 +179,39 @@ def _check(principal: Principal, *, core: bool = False, observer: bool = False)
|
||||
|
||||
# --- App factory ---
|
||||
|
||||
def create_app(*, client: HyperliquidClient, token_store: TokenStore) -> FastAPI:
|
||||
def create_app(
|
||||
*,
|
||||
client: HyperliquidClient,
|
||||
token_store: TokenStore,
|
||||
creds: dict | None = None,
|
||||
env_info: EnvironmentInfo | None = None,
|
||||
) -> FastAPI:
|
||||
creds = creds or {}
|
||||
app = build_app(name="mcp-hyperliquid", version="0.1.0", token_store=token_store)
|
||||
|
||||
# --- Read tools: core + observer ---
|
||||
|
||||
@app.post("/tools/environment_info", tags=["reads"])
|
||||
async def t_environment_info(principal: Principal = Depends(require_principal)):
|
||||
_check(principal, core=True, observer=True)
|
||||
if env_info is None:
|
||||
return {
|
||||
"exchange": "hyperliquid",
|
||||
"environment": "testnet" if getattr(client, "testnet", True) else "mainnet",
|
||||
"source": "credentials",
|
||||
"env_value": None,
|
||||
"base_url": getattr(client, "base_url", None),
|
||||
"max_leverage": get_max_leverage(creds),
|
||||
}
|
||||
return {
|
||||
"exchange": env_info.exchange,
|
||||
"environment": env_info.environment,
|
||||
"source": env_info.source,
|
||||
"env_value": env_info.env_value,
|
||||
"base_url": env_info.base_url,
|
||||
"max_leverage": get_max_leverage(creds),
|
||||
}
|
||||
|
||||
@app.post("/tools/get_markets", tags=["reads"])
|
||||
async def t_get_markets(
|
||||
body: GetMarketsReq, principal: Principal = Depends(require_principal)
|
||||
@@ -307,14 +304,7 @@ def create_app(*, client: HyperliquidClient, token_store: TokenStore) -> FastAPI
|
||||
body: PlaceOrderReq, principal: Principal = Depends(require_principal)
|
||||
):
|
||||
_check(principal, core=True)
|
||||
enforce_leverage(body.leverage)
|
||||
if not body.reduce_only:
|
||||
notional = await _compute_notional_hl(client, body)
|
||||
enforce_single_notional(
|
||||
notional, exchange="hyperliquid", instrument=body.instrument
|
||||
)
|
||||
agg = await _current_aggregate_hl(client)
|
||||
enforce_aggregate(agg, notional)
|
||||
_enforce_leverage(body.leverage, creds=creds, exchange="hyperliquid")
|
||||
return await client.place_order(
|
||||
instrument=body.instrument,
|
||||
side=body.side,
|
||||
@@ -361,6 +351,7 @@ def create_app(*, client: HyperliquidClient, token_store: TokenStore) -> FastAPI
|
||||
token_store=token_store,
|
||||
internal_base_url=f"http://localhost:{port}",
|
||||
tools=[
|
||||
{"name": "environment_info", "description": "Ambiente operativo (testnet/mainnet), source, base_url, max_leverage cap."},
|
||||
{"name": "get_markets", "description": "Lista mercati perp disponibili."},
|
||||
{"name": "get_ticker", "description": "Ticker di un perp."},
|
||||
{"name": "get_orderbook", "description": "Orderbook L2."},
|
||||
|
||||
Reference in New Issue
Block a user