get_portfolio
Retrieve portfolio details including positions, profit/loss, and margin usage for Hyperliquid DEX trading accounts.
Instructions
Get portfolio information including positions, PnL, and margin usage
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| user | No | User wallet address (optional, defaults to configured wallet) |
Implementation Reference
- The main handler function for the 'get_portfolio' tool. Fetches portfolio data from HyperliquidClient, handles errors, formats summary including total notional position, unrealized PnL, margin used, and last updated time as structured text content.async def handle_get_portfolio(client: HyperliquidClient, args: Dict[str, Any]) -> Dict[str, Any]: """Handle get portfolio request.""" user = args.get("user") result = await client.get_portfolio(user) if not result.success: raise ValueError(f"Failed to get portfolio: {result.error}") portfolio = result.data if not portfolio: return { "content": [ TextContent( type="text", text="No portfolio data found.", ) ] } last_updated = ( datetime.fromtimestamp(portfolio["time"] / 1000).isoformat() if portfolio.get("time") else "N/A" ) return { "content": [ TextContent( type="text", text=f"Portfolio Summary:\n\nTotal Notional Position: ${portfolio.get('totalNtlPos', '0')}\nTotal Unrealized PnL: ${portfolio.get('totalUnrealizedPnl', '0')}\nTotal Margin Used: ${portfolio.get('totalMarginUsed', '0')}\nLast Updated: {last_updated}", ) ] }
- The schema definition for the 'get_portfolio' tool, specifying input schema with optional 'user' wallet address.get_portfolio_tool = Tool( name="get_portfolio", description="Get portfolio information including positions, PnL, and margin usage", inputSchema={ "type": "object", "properties": { "user": { "type": "string", "description": "User wallet address (optional, defaults to configured wallet)", } }, "required": [], }, )
- hyperliquid_mcp_server/main.py:106-107 (registration)Registration of the 'get_portfolio' handler in the main MCP server's call_tool dispatcher.elif name == "get_portfolio": result = await handle_get_portfolio(client, args)
- hyperliquid_mcp_server/http_server.py:51-51 (registration)Maps 'get_portfolio' tool name to its handler in the HTTP server's TOOL_HANDLERS dictionary."get_portfolio": handle_get_portfolio,
- src/tools/account-info.ts:171-189 (handler)TypeScript equivalent handler for 'get_portfolio' tool in the Node.js MCP server implementation.export async function handleGetPortfolio(client: HyperliquidClient, args: any) { const { user } = args; const result = await client.getPortfolio(user); if (!result.success) { throw new Error(`Failed to get portfolio: ${result.error}`); } const portfolio = result.data; return { content: [ { type: 'text', text: `Portfolio Summary:\n\nTotal Notional Position: $${portfolio?.totalNtlPos || '0'}\nTotal Unrealized PnL: $${portfolio?.totalUnrealizedPnl || '0'}\nTotal Margin Used: $${portfolio?.totalMarginUsed || '0'}\nLast Updated: ${portfolio?.time ? new Date(portfolio.time).toISOString() : 'N/A'}` } ] }; }