getAccounts
Retrieve a list of brokerage accounts to manage trading positions, track portfolios, and access account details for trading operations.
Instructions
Get list of brokerage accounts
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "getAccounts"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:445-468 (handler)The handler function that executes the getAccounts tool logic: makes an authenticated GET request to the TradeStation /brokerage/accounts endpoint and returns the JSON-formatted accounts data or error.
async (args) => { try { const accounts = await makeAuthenticatedRequest('/brokerage/accounts'); return { content: [ { type: "text", text: JSON.stringify(accounts, null, 2) } ] }; } catch (error: unknown) { return { content: [ { type: "text", text: `Failed to fetch accounts: ${error instanceof Error ? error.message : 'Unknown error'}` } ], isError: true }; } } - src/index.ts:102-102 (schema)The input schema for the getAccounts tool, which is an empty object indicating no parameters are required.
const accountsSchema = {}; - src/index.ts:441-469 (registration)Registration of the getAccounts tool on the MCP server using server.tool, specifying name, description, schema, and inline handler function.
server.tool( "getAccounts", "Get list of brokerage accounts", accountsSchema, async (args) => { try { const accounts = await makeAuthenticatedRequest('/brokerage/accounts'); return { content: [ { type: "text", text: JSON.stringify(accounts, null, 2) } ] }; } catch (error: unknown) { return { content: [ { type: "text", text: `Failed to fetch accounts: ${error instanceof Error ? error.message : 'Unknown error'}` } ], isError: true }; } } ); - src/index.ts:179-229 (helper)Shared helper function that handles authentication token management (including refresh) and makes Axios requests to TradeStation API endpoints, used by the getAccounts handler.
async function makeAuthenticatedRequest( endpoint: string, method: AxiosRequestConfig['method'] = 'GET', data: any = null ): Promise<any> { const userTokens = tokenStore.get(DEFAULT_USER); if (!userTokens) { throw new Error('User not authenticated. Please set TRADESTATION_REFRESH_TOKEN in .env file.'); } // Check if token is expired or about to expire (within 60 seconds) if (userTokens.expiresAt < Date.now() + 60000) { // Refresh the token const newTokens = await refreshToken(userTokens.refreshToken); tokenStore.set(DEFAULT_USER, newTokens); } try { const options: AxiosRequestConfig = { method, url: `${TS_API_BASE}${endpoint}`, headers: { 'Authorization': `Bearer ${tokenStore.get(DEFAULT_USER)?.accessToken}`, 'Content-Type': 'application/json', 'Accept': 'application/json' }, timeout: 60000 }; if (data && (method === 'POST' || method === 'PUT' || method === 'PATCH')) { options.data = data; } const response = await axios(options); return response.data; } catch (error: unknown) { if (error instanceof AxiosError) { const errorMessage = error.response?.data?.Message || error.response?.data?.message || error.message; const statusCode = error.response?.status; console.error(`API request error [${statusCode}]: ${errorMessage}`); console.error('Endpoint:', endpoint); throw new Error(`API Error (${statusCode}): ${errorMessage}`); } else if (error instanceof Error) { console.error('API request error:', error.message); throw error; } else { console.error('Unknown API request error:', error); throw new Error('Unknown API request error'); } }