get_market_positions
Retrieve and organize market positions by specifying a unique market key, with options for pagination, ordering, and sorting to streamline data analysis.
Instructions
Get positions overview for specific markets with pagination and ordering.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| first | No | Number of positions to return (default: 30) | |
| marketUniqueKey | Yes | Unique key of the market | |
| orderBy | No | Field to order by | |
| orderDirection | No | Order direction | |
| skip | No | Number of positions to skip |
Implementation Reference
- src/index.ts:1081-1142 (handler)Handler for the 'get_market_positions' tool. Constructs a GraphQL query using the provided marketUniqueKey and pagination/ordering params, fetches data from Morpho API via axios, validates with MarketPositionsResponseSchema, and returns the paginated list of market positions as JSON.if (name === GET_MARKET_POSITIONS_TOOL) { try { const { marketUniqueKey, ...queryParams } = params as MarketPositionsParams; const finalParams = buildQueryParams({ ...queryParams, where: { marketUniqueKey_in: [marketUniqueKey] } }); const query = ` query { marketPositions${finalParams} { pageInfo { count countTotal } items { supplyShares supplyAssets supplyAssetsUsd borrowShares borrowAssets borrowAssetsUsd collateral collateralUsd market { uniqueKey loanAsset { address symbol } collateralAsset { address symbol } } user { address } } } } `; const response = await axios.post(MORPHO_API_BASE, { query }); const validatedData = MarketPositionsResponseSchema.parse(response.data); return { content: [ { type: 'text', text: JSON.stringify(validatedData.data.marketPositions, null, 2), }, ], }; } catch (error: any) { console.error('Error calling Morpho API:', error.message); return { isError: true, content: [{ type: 'text', text: `Error retrieving market positions: ${error.message}` }], }; } }
- src/index.ts:668-699 (registration)Registration of the 'get_market_positions' tool in the ListTools response, including name, description, and input schema definition.{ name: GET_MARKET_POSITIONS_TOOL, description: 'Get positions overview for specific markets with pagination and ordering.', inputSchema: { type: 'object', properties: { marketUniqueKey: { type: 'string', description: 'Unique key of the market' }, first: { type: 'number', description: 'Number of positions to return (default: 30)' }, skip: { type: 'number', description: 'Number of positions to skip' }, orderBy: { type: 'string', enum: ['SupplyShares', 'BorrowShares', 'SupplyAssets', 'BorrowAssets'], description: 'Field to order by' }, orderDirection: { type: 'string', enum: ['Asc', 'Desc'], description: 'Order direction' } }, required: ['marketUniqueKey'] }, },
- src/index.ts:115-125 (schema)Zod schema for validating the Morpho API response for market positions, including pagination info and array of MarketPositionSchema items.const MarketPositionsResponseSchema = z.object({ data: z.object({ marketPositions: z.object({ pageInfo: z.object({ count: z.number(), countTotal: z.number(), }), items: z.array(MarketPositionSchema) }) }) });
- src/index.ts:56-73 (schema)Zod schema defining the structure of a single market position, used in the response validation. Includes supply/borrow amounts, market details, and user address.const MarketPositionSchema = z.object({ supplyShares: z.union([z.string(), z.number()]).transform(stringToNumber).optional(), supplyAssets: z.union([z.string(), z.number()]).transform(stringToNumber), supplyAssetsUsd: z.union([z.string(), z.number()]).transform(stringToNumber), borrowShares: z.union([z.string(), z.number()]).transform(stringToNumber).optional(), borrowAssets: z.union([z.string(), z.number()]).transform(stringToNumber), borrowAssetsUsd: z.union([z.string(), z.number()]).transform(stringToNumber), collateral: z.union([z.string(), z.number()]).transform(stringToNumber).optional(), collateralUsd: z.union([z.string(), z.number()]).transform(stringToNumber).optional(), market: z.object({ uniqueKey: z.string(), loanAsset: AssetSchema, collateralAsset: AssetSchema, }), user: z.object({ address: z.string() }) });
- src/index.ts:894-907 (helper)Helper function to build GraphQL query parameters string from pagination, ordering, and where inputs. Used in the handler to construct the dynamic query.function buildQueryParams(params: PaginationParams & { orderBy?: string, orderDirection?: OrderDirection, where?: Record<string, any> } = {}): string { const queryParts: string[] = []; if (params.first !== undefined) queryParts.push(`first: ${params.first}`); if (params.skip !== undefined) queryParts.push(`skip: ${params.skip}`); if (params.orderBy) queryParts.push(`orderBy: ${params.orderBy}`); if (params.orderDirection) queryParts.push(`orderDirection: ${params.orderDirection}`); if (params.where && Object.keys(params.where).length > 0) { const whereStr = JSON.stringify(params.where).replace(/"([^"]+)":/g, '$1:'); queryParts.push(`where: ${whereStr}`); } return queryParts.length > 0 ? `(${queryParts.join(', ')})` : ''; }