finance_news
Search financial news from mainstream media using keywords to track market developments and company updates.
Instructions
通过真正的搜索API获取主流财经媒体的新闻内容,支持单个或多个关键词智能搜索
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | 搜索关键词,支持单个关键词如'药明康德'、'腾讯',或多个关键词用空格分开如'美联储 加息'、'比特币 监管'等。系统会智能搜索相关历史新闻 |
Implementation Reference
- src/tools/financeNews.ts:13-78 (handler)The main finance_news tool object, defining name, description, input parameters schema, and the async run handler function that orchestrates news search via internal helpers.export const financeNews = { name: "finance_news", description: "通过真正的搜索API获取主流财经媒体的新闻内容,支持单个或多个关键词智能搜索", parameters: { type: "object", properties: { query: { type: "string", description: "搜索关键词,支持单个关键词如'药明康德'、'腾讯',或多个关键词用空格分开如'美联储 加息'、'比特币 监管'等。系统会智能搜索相关历史新闻" } }, required: ["query"] }, async run(args: { query: string; }) { try { if (!args.query || args.query.trim().length === 0) { throw new Error("搜索关键词不能为空"); } const query = args.query.trim(); console.log(`开始搜索财经新闻,关键词: ${query},使用有效的新闻接口`); const newsResults = await searchFinanceNews(query); if (newsResults.length === 0) { return { content: [ { type: "text", text: `# ${query} 财经新闻搜索结果\n\n未找到相关财经新闻` } ] }; } console.log(`搜索完成,共找到 ${newsResults.length} 条新闻`); // 简化返回格式,参考stock_data的格式 const formattedNews = newsResults.map((news) => { return `${news.title}\n来源: ${news.source} 时间: ${news.publishTime}\n摘要: ${news.summary}${news.url ? `\n链接: ${news.url}` : ''}\n`; }).join('\n---\n\n'); return { content: [ { type: "text", text: `# ${query} 财经新闻搜索结果\n\n${formattedNews}` } ] }; } catch (error) { console.error('搜索财经新闻时发生错误:', error); return { content: [ { type: "text", text: `# ${args.query || '财经新闻'} 搜索失败\n\n错误信息: ${error instanceof Error ? error.message : '未知错误'}` } ] }; } } };
- src/tools/financeNews.ts:4-11 (schema)TypeScript interface defining the structure of a NewsItem used in the finance_news tool response.export interface NewsItem { title: string; summary: string; url: string; source: string; publishTime: string; keywords: string[]; }
- src/tools/financeNews.ts:80-110 (helper)Helper function that performs the actual news search using Baidu News and deduplicates results.async function searchFinanceNews(query: string): Promise<NewsItem[]> { const news: NewsItem[] = []; const keywords = query.split(' ').filter(k => k.trim().length > 0); // 并发搜索多个有效的媒体源(当前仅百度) const searchPromises = [ searchBaiduNews(keywords) ]; try { const results = await Promise.allSettled(searchPromises); results.forEach((result, index) => { const sourceNames = ['百度新闻']; if (result.status === 'fulfilled') { news.push(...result.value); console.log(`${sourceNames[index]} 搜索成功,获得 ${result.value.length} 条新闻`); } else { console.error(`${sourceNames[index]} 搜索失败:`, result.reason); } }); // 去重 const uniqueNews = removeDuplicates(news); return uniqueNews.slice(0, 20); // 最多返回20条 } catch (error) { console.error('并发搜索时发生错误:', error); return []; } }
- src/index.ts:274-277 (registration)Registration and dispatch handler in the stdio MCP server for calling the finance_news tool.case "finance_news": { const query = String(request.params.arguments?.query); return normalizeResult(await financeNews.run({ query })); }
- src/httpServer.ts:302-305 (registration)Registration and dispatch handler in the HTTP MCP server for calling the finance_news tool.case 'finance_news': return await financeNews.run({ query: String(args?.query) });