get_stock_data
Retrieve stock trading data from CSMAR databases by stock code, date range, and frequency (daily, weekly, monthly). Access historical prices and volumes to support financial analysis.
Instructions
获取 CSMAR 股票交易数据
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| stock_code | Yes | 股票代码 | |
| start_date | Yes | 开始日期 (YYYY-MM-DD) | |
| end_date | Yes | 结束日期 (YYYY-MM-DD) | |
| frequency | No | 数据频率 |
Implementation Reference
- src/index.js:574-614 (registration)Registration of the 'get_stock_data' tool on the MCP server with input schema definition (stock_code, start_date, end_date, frequency) and the handler function that queries CSMAR stock data.
// 9. 获取股票数据 (已实现) server.registerTool( 'get_stock_data', { description: '获取 CSMAR 股票交易数据', inputSchema: { stock_code: z.string().describe('股票代码'), start_date: z.string().describe('开始日期 (YYYY-MM-DD)'), end_date: z.string().describe('结束日期 (YYYY-MM-DD)'), frequency: z.enum(['daily', 'weekly', 'monthly']).optional().describe('数据频率'), }, }, async ({ stock_code, start_date, end_date, frequency = 'daily' }) => { try { const loginResult = await ensureLogin(); if (!loginResult.success) { return { content: [{ type: 'text', text: JSON.stringify(loginResult, null, 2) }], isError: true }; } // 映射频率参数 const freqMap = { daily: 'D', weekly: 'W', monthly: 'M' }; const freq = freqMap[frequency] || 'D'; // 使用通用查询获取股票数据 // 这里假设有一个股票日行情表,实际表名需要根据数据库确定 const client = await initPythonClient(); const result = await client.call('query', { table_name: 'stock_daily', columns: ['Stkcd', 'Trddt', 'Open', 'High', 'Low', 'Close', 'Vol', 'Amount'], condition: `Stkcd='${stock_code}'`, start_time: start_date, end_time: end_date, limit: 1000 }); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; } catch (error) { return { content: [{ type: 'text', text: `获取股票数据错误: ${error.message}` }], isError: true }; } } ); - src/index.js:586-613 (handler)The actual handler function for 'get_stock_data'. It calls ensureLogin(), maps frequency to 'D'/'W'/'M', calls the Python client to query table 'stock_daily' with columns (Stkcd, Trddt, Open, High, Low, Close, Vol, Amount), and returns the result.
async ({ stock_code, start_date, end_date, frequency = 'daily' }) => { try { const loginResult = await ensureLogin(); if (!loginResult.success) { return { content: [{ type: 'text', text: JSON.stringify(loginResult, null, 2) }], isError: true }; } // 映射频率参数 const freqMap = { daily: 'D', weekly: 'W', monthly: 'M' }; const freq = freqMap[frequency] || 'D'; // 使用通用查询获取股票数据 // 这里假设有一个股票日行情表,实际表名需要根据数据库确定 const client = await initPythonClient(); const result = await client.call('query', { table_name: 'stock_daily', columns: ['Stkcd', 'Trddt', 'Open', 'High', 'Low', 'Close', 'Vol', 'Amount'], condition: `Stkcd='${stock_code}'`, start_time: start_date, end_time: end_date, limit: 1000 }); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; } catch (error) { return { content: [{ type: 'text', text: `获取股票数据错误: ${error.message}` }], isError: true }; } } - src/index.js:579-584 (schema)Input schema for get_stock_data: stock_code (string), start_date (string YYYY-MM-DD), end_date (string YYYY-MM-DD), frequency (optional enum: daily/weekly/monthly).
inputSchema: { stock_code: z.string().describe('股票代码'), start_date: z.string().describe('开始日期 (YYYY-MM-DD)'), end_date: z.string().describe('结束日期 (YYYY-MM-DD)'), frequency: z.enum(['daily', 'weekly', 'monthly']).optional().describe('数据频率'), },