getExchangeDetails.ts•1.82 kB
import { z } from 'zod';
import type { MarketstackClient, MarketstackApiParams } from '../../marketstackClient.js';
// Define the input schema shape for the Exchange Details tool
const getExchangeDetailsInputSchemaShape = {
mic: z
.string()
.describe(
'Obtain information about a specific stock exchange by attaching its MIC identification to your API request URL, e.g. `XNAS`.'
),
};
type RawSchemaShape = typeof getExchangeDetailsInputSchemaShape;
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 Exchange Details tool
const getExchangeDetailsHandler = async (input: Input, client: MarketstackClient): Promise<Output> => {
try {
const { mic } = input;
// Construct the endpoint path with the MIC
const endpoint = `exchanges/${mic}`;
const apiRequestParams: MarketstackApiParams = {
endpoint,
};
const data = await client.fetchApiData(apiRequestParams);
return data;
} catch (error: unknown) {
console.error('getExchangeDetails tool error:', error);
const message = error instanceof Error ? error.message : 'An unknown error occurred.';
throw new Error(`getExchangeDetails 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 getExchangeDetailsTool: MarketstackToolDefinition = {
name: 'get_exchange_details',
description: 'Obtain information about a specific stock exchange.',
inputSchemaShape: getExchangeDetailsInputSchemaShape,
handler: getExchangeDetailsHandler,
};