getTickerDetails.ts•2.28 kB
import { z } from 'zod';
import type { MarketstackClient, MarketstackApiParams } from '../../marketstackClient.js';
// Define the input schema shape for the Ticker Details tool
const getTickerDetailsInputSchemaShape = {
symbol: z
.string()
.describe('Obtain information about a specific ticker symbol by attach it to your API request URL, e.g. `AAPL`.'),
exchange: z
.string()
.optional()
.describe(
'To filter your results based on a specific stock exchange, use this parameter to specify the MIC identification of a stock exchange. Example: `XNAS`'
),
// Note: The documentation mentions limit and offset for the /tickers endpoint, but it seems to apply
// when listing multiple tickers, not getting details for a single one. We'll omit them for this tool.
};
type RawSchemaShape = typeof getTickerDetailsInputSchemaShape;
type Input = z.infer<z.ZodObject<RawSchemaShape>>;
type Output = any; // TODO: Define a more specific output type based on Marketstack response
// Define the handler function for the Ticker Details tool
const getTickerDetailsHandler = async (input: Input, client: MarketstackClient): Promise<Output> => {
try {
const { symbol, exchange } = input;
// Construct the endpoint path with the symbol
let endpoint = `tickers/${symbol}`;
const apiRequestParams: MarketstackApiParams = {
endpoint,
...(exchange && { exchange }), // Include if exchange is provided
};
const data = await client.fetchApiData(apiRequestParams);
return data;
} catch (error: unknown) {
console.error('getTickerDetails tool error:', error);
const message = error instanceof Error ? error.message : 'An unknown error occurred.';
throw new Error(`getTickerDetails tool failed: ${message}`);
}
};
// Define the tool definition object structure
type MarketstackToolDefinition = {
name: string;
description: string;
inputSchemaShape: RawSchemaShape;
handler: (input: Input, client: MarketstackClient) => Promise<Output>;
};
export const getTickerDetailsTool: MarketstackToolDefinition = {
name: 'get_ticker_details',
description: 'Obtain information about a specific ticker symbol.',
inputSchemaShape: getTickerDetailsInputSchemaShape,
handler: getTickerDetailsHandler,
};