import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import * as stockDataService from '../services/stockData.js';
export function registerStockDataTools(server: McpServer): void {
// Tool 4: get_stock_quote
server.tool(
'get_stock_quote',
'Get the current stock quote for a given symbol',
{
symbol: z.string().min(1).max(5).describe('Stock ticker symbol (e.g., AAPL, MSFT, GOOGL)')
},
async ({ symbol }) => {
// Check if API key is configured
if (!stockDataService.isConfigured()) {
const instructions = stockDataService.getApiKeyInstructions();
return {
content: [{ type: 'text', text: JSON.stringify(instructions, null, 2) }]
};
}
try {
const quote = await stockDataService.getQuote(symbol);
const sign = quote.change >= 0 ? '+' : '';
const result = {
...quote,
summary: `${quote.symbol}: $${quote.price.toFixed(2)} (${sign}${quote.changePercent.toFixed(2)}%)`
};
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }]
};
} catch (error) {
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
return {
content: [{ type: 'text', text: JSON.stringify({ error: errorMsg }, null, 2) }],
isError: true
};
}
}
);
// Tool 5: compare_stocks
server.tool(
'compare_stocks',
'Compare stock quotes for multiple symbols (note: may take time due to API rate limits)',
{
symbols: z.array(z.string().min(1).max(5)).min(2).max(5).describe('Array of stock ticker symbols to compare')
},
async ({ symbols }) => {
// Check if API key is configured
if (!stockDataService.isConfigured()) {
const instructions = stockDataService.getApiKeyInstructions();
return {
content: [{ type: 'text', text: JSON.stringify(instructions, null, 2) }]
};
}
try {
const result = await stockDataService.compareStocks(symbols);
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }]
};
} catch (error) {
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
return {
content: [{ type: 'text', text: JSON.stringify({ error: errorMsg }, null, 2) }],
isError: true
};
}
}
);
}