get_portfolio
Retrieve a client's investment portfolio from T-Invest, including positions and balances, with optional filtering by specific tickers.
Instructions
Получить портфель клиента в Т-Инвестициях
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| accountId | Yes | Идентификатор счёта (можно получить через get_accounts) | |
| tickers | No | Фильтр по тикерам |
Implementation Reference
- src/tools/get-portfolio.ts:32-83 (handler)The function `registerGetPortfolio` registers the 'get_portfolio' MCP tool and contains the main handler logic, including API interaction and response formatting.
export function registerGetPortfolio(server: McpServer, client: TInvestClient): void { server.tool( 'get_portfolio', 'Получить портфель клиента в Т-Инвестициях', { accountId: z.string().describe('Идентификатор счёта (можно получить через get_accounts)'), tickers: z.array(z.string()).optional().describe('Фильтр по тикерам'), }, READ_ONLY, async ({ accountId, tickers }) => { try { const response = await client.post<GetPortfolioResponse>( API_PATHS.OPERATIONS.GET_PORTFOLIO, { accountId, currency: 'RUB' }, ); if (!response.positions || response.positions.length === 0) { return { content: [{ type: 'text' as const, text: 'Портфель пуст.' }] }; } const settled = await inBatches( response.positions, (pos) => resolveInstrumentByFigi(client, pos.figi), ); const tickerFilter = tickers?.map((t) => t.toUpperCase()); const results: string[] = []; for (let i = 0; i < response.positions.length; i++) { const pos = response.positions[i]; const result = settled[i]; const ticker = (result.status === 'fulfilled' && result.value) ? result.value : pos.figi; if (tickerFilter && tickerFilter.length > 0 && !tickerFilter.includes(ticker.toUpperCase())) { continue; } results.push(formatPosition(ticker, pos)); } if (results.length === 0) { return { content: [{ type: 'text' as const, text: 'Позиции по указанным тикерам не найдены.' }] }; } return { content: [{ type: 'text' as const, text: results.join(SEPARATOR) }] }; } catch (error) { return { content: [{ type: 'text' as const, text: `Ошибка: ${error instanceof Error ? error.message : String(error)}` }], isError: true, }; } }, ); - src/index.ts:69-69 (registration)Registration of the get_portfolio tool in the main server entry point.
registerGetPortfolio(server, client);