Get Top Gainers
get_top_gainersRetrieve the top 10 gaining stocks on the Colombo Stock Exchange for today's trading session. Provides real-time data on price increases to identify market leaders.
Instructions
Get the top 10 gaining stocks in the CSE for the current trading day.
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": "get_top_gainers"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:201-230 (handler)Core handler that fetches top gainers from the CSE API (https://www.cse.lk/api/topGainers) and maps the response to symbol, price, change, and changePercentage fields.
async function getTopGainers() { try { const response = await axios.post( 'https://www.cse.lk/api/topGainers', {}, { headers: { 'Content-Type': 'application/json' }, timeout: 10000 } ); const data = response.data || []; return data.map((item: any) => ({ symbol: item.symbol, price: item.price, change: item.change, changePercentage: item.changePercentage })); } catch (error: any) { if (error.code === 'ECONNABORTED') { throw new Error('Request timed out. Please try again.'); } else if (error.response) { throw new Error(`API error: ${error.response.status} - ${error.response.statusText}`); } else { throw new Error(`Network error: ${error.message}`); } } - src/index.ts:464-507 (registration)Registration of the 'get_top_gainers' tool with the MCP server, including its title, description, empty inputSchema, and the handler that calls getTopGainers() and formats results.
server.registerTool( "get_top_gainers", { title: "Get Top Gainers", description: "Get the top 10 gaining stocks in the CSE for the current trading day.", inputSchema: {} }, async () => { try { const topGainers = await getTopGainers(); const formattedGainers = topGainers.map((stock: any, index: number) => { const priceChangeSymbol = stock.change >= 0 ? '+' : ''; return { rank: index + 1, symbol: stock.symbol, price: `Rs. ${stock.price.toFixed(2)}`, change: `${priceChangeSymbol}${stock.change.toFixed(2)}`, changePercentage: `${priceChangeSymbol}${stock.changePercentage.toFixed(2)}%` }; }); return { content: [{ type: "text", text: JSON.stringify({ title: "Top 10 Gainers", count: formattedGainers.length, gainers: formattedGainers, lastUpdated: new Date().toISOString() }, null, 2) }] }; } catch (error: any) { return { content: [{ type: "text", text: `Error fetching top gainers: ${error.message}` }], isError: true }; } } ); - src/index.ts:466-469 (schema)Schema definition for the get_top_gainers tool: title, description, and an empty inputSchema (no parameters required).
{ title: "Get Top Gainers", description: "Get the top 10 gaining stocks in the CSE for the current trading day.", inputSchema: {}