Skip to main content
Glama
maven81g

TradeStation MCP Server

by maven81g

getBalances

Retrieve account balances and buying power from TradeStation to monitor trading capital and available funds for investment decisions.

Instructions

Get account balances and buying power

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
accountIdNoAccount ID (optional, uses TRADESTATION_ACCOUNT_ID from env if not provided)

Implementation Reference

  • The main handler function for the getBalances tool. It retrieves the account ID from arguments or environment, calls the TradeStation API endpoint `/brokerage/accounts/{accountId}/balances` using the shared makeAuthenticatedRequest helper, formats the response as JSON text block, and handles errors.
    async (args) => { try { const accountId = args.accountId || TS_ACCOUNT_ID; if (!accountId) { throw new Error('Account ID is required. Either provide accountId parameter or set TRADESTATION_ACCOUNT_ID in .env file.'); } const balances = await makeAuthenticatedRequest( `/brokerage/accounts/${encodeURIComponent(accountId)}/balances` ); return { content: [ { type: "text", text: JSON.stringify(balances, null, 2) } ] }; } catch (error: unknown) { return { content: [ { type: "text", text: `Failed to fetch balances: ${error instanceof Error ? error.message : 'Unknown error'}` } ], isError: true }; } }
  • Zod input schema for the getBalances tool, defining an optional accountId parameter.
    const balancesSchema = { accountId: z.string().optional().describe('Account ID (optional, uses TRADESTATION_ACCOUNT_ID from env if not provided)') };
  • src/index.ts:471-507 (registration)
    Registration of the getBalances tool with the MCP server, including name, description, schema, and inline handler function.
    server.tool( "getBalances", "Get account balances and buying power", balancesSchema, async (args) => { try { const accountId = args.accountId || TS_ACCOUNT_ID; if (!accountId) { throw new Error('Account ID is required. Either provide accountId parameter or set TRADESTATION_ACCOUNT_ID in .env file.'); } const balances = await makeAuthenticatedRequest( `/brokerage/accounts/${encodeURIComponent(accountId)}/balances` ); return { content: [ { type: "text", text: JSON.stringify(balances, null, 2) } ] }; } catch (error: unknown) { return { content: [ { type: "text", text: `Failed to fetch balances: ${error instanceof Error ? error.message : 'Unknown error'}` } ], isError: true }; } } );
  • Shared utility function used by getBalances (and other tools) to make authenticated HTTP requests to the TradeStation API base URL.
    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'); } } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/maven81g/tradestation_mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server