trading_agent_v2 / hype_accounts_server.py
samsonleegh's picture
Update hype_accounts_server.py
2771f03 verified
from mcp.server.fastmcp import FastMCP
from hype_accounts import setup, long_short_shares_mkt_price
mcp = FastMCP("hype_accounts_server")
@mcp.tool()
async def get_account_details() -> dict:
"""Get cash balance, cryptocurrency holdings and PNL of hyperliquid account.
"""
account_details_dict = {
"holdings": None,
"cash_balance": None,
"profit_and_loss": None
}
address, info, exchange = setup()
user_state = info.user_state(address)
account_details_dict["cash_balance"] = user_state['marginSummary']['accountValue']
if user_state['assetPositions'] == []:
account_details_dict["holdings"] = "No holdings"
else:
account_details_dict["holdings"] = user_state['assetPositions']
account_details_dict["profit_and_loss"] = exchange.info.portfolio(address)[3][1]['pnlHistory'][-1][1]
return account_details_dict
@mcp.tool()
async def long_perps_mkt_price(symbol: str, value:float, leverage:int, stop_loss_price:float, take_profit_price:float) -> str:
"""Create a long position for a cryptocurrency perpetual contract at market price.
Args:
symbol: The symbol of the cryptocurrency (e.g., "HYPE","ETC","BTC","XRP").
value: The USD value to long.
leverage: The leverage to use for the position.
stop_loss_price: The stop loss price for the position.
take_profit_price: The take profit price for the position.
"""
return str(long_short_shares_mkt_price(symbol, value, leverage, is_buy=True, stop_loss_price=stop_loss_price, take_profit_price=take_profit_price))
@mcp.tool()
async def short_perps_mkt_price(symbol: str, value:float, leverage:int, stop_loss_price:float, take_profit_price:float) -> str:
"""Create a short position for a cryptocurrency perpetual contract at market price.
Args:
symbol: The symbol of the cryptocurrency (e.g., "HYPE","ETC","BTC").
value: The USD value to short.
leverage: The leverage to use for the position.
stop_loss_price: The stop loss price for the position.
take_profit_price: The take profit price for the position.
"""
return str(long_short_shares_mkt_price(symbol, value, leverage, is_buy=False, stop_loss_price=stop_loss_price, take_profit_price=take_profit_price))
@mcp.tool()
async def close_perps_mkt_price(symbol: str, close_size: float = None) -> str:
"""Close the position for a cryptocurrency perpetual contract at market price.
Args:
symbol: The symbol of the cryptocurrency (e.g., "HYPE","ETC","BTC").
close_size: The size of the position to close. If None, closes the entire position.
"""
_, _, exchange = setup()
return str(exchange.market_close(symbol, sz=close_size))
if __name__ == "__main__":
mcp.run(transport='stdio')