Skip to main content
Glama

MCP Stock Assistant

1.js2.5 kB
// 新浪财经API基础URL const SINA_HISTORY_API_BASE = "https://finance.sina.com.cn/realstock/company"; // 通用请求头 const USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"; /** * 获取股票历史数据 * @param {string} stockCode - 股票代码 * @param {number} days - 获取的天数 * @returns {Promise<Array|null>} - 返回历史数据数组或null */ async function fetchStockHistory(stockCode, days = 7) { try { // 处理股票代码格式(新浪API需要小写的股票代码) const formattedCode = stockCode.toLowerCase(); // 构建URL,使用新浪财经API const url = `${SINA_HISTORY_API_BASE}/${formattedCode}/qianfuquan.js`; // 发送请求 const response = await fetch(url, { headers: { "User-Agent": USER_AGENT, "Referer": "https://finance.sina.com.cn" } }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } // 获取响应文本 const responseText = await response.text(); // 处理响应文本(新浪API返回的是JavaScript代码,不是标准JSON) // 格式类似于: [{total:4446,data:{_2025_04_03:"2.175242",...}}] const jsonText = responseText.replace(/\s*\/\*[\s\S]*?\*\/\s*$/, ""); // 移除尾部注释 const data = JSON.parse(jsonText); if (!data || !data[0] || !data[0].data) { console.error("API error or no history data:", data); return null; } // 转换数据格式 const historyData = data[0].data; const historyItems = []; // 只获取最近days天的数据 const keys = Object.keys(historyData) .filter(key => key.startsWith("_")) // 过滤掉非日期键 .sort((a, b) => b.localeCompare(a)) // 按日期降序排序 .slice(0, days); // 只取最近days天 // 转换数据格式 for (const key of keys) { // 将_2025_04_03格式转换为2025-04-03 const dateStr = key.substring(1).replace(/_/g, "-"); historyItems.push({ date: dateStr, price: historyData[key] }); } return historyItems; } catch (error) { console.error("Error fetching stock history:", error); return null; } } const getValue =async()=>{ const val = await fetchStockHistory("SH600519", 7); console.log(val); } getValue();

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/qqzhangyanhua/mcp-stock'

If you have feedback or need assistance with the MCP directory API, please join our Discord server