finance_news
Access real-time financial news from major media sources using keyword searches. Retrieve relevant articles efficiently with single or multiple keywords for market insights.
Instructions
通过真正的搜索API获取主流财经媒体的新闻内容,支持单个或多个关键词智能搜索
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | 搜索关键词,支持单个关键词如'药明康德'、'腾讯',或多个关键词用空格分开如'美联储 加息'、'比特币 监管'等。系统会智能搜索相关历史新闻 |
Implementation Reference
- src/tools/financeNews.ts:26-77 (handler)The main handler function `run` that executes the finance_news tool logic: validates input, searches news via helper, formats and returns results or error.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:16-25 (schema)Input schema defining the required 'query' parameter for the finance_news tool.parameters: { type: "object", properties: { query: { type: "string", description: "搜索关键词,支持单个关键词如'药明康德'、'腾讯',或多个关键词用空格分开如'美联储 加息'、'比特币 监管'等。系统会智能搜索相关历史新闻" } }, required: ["query"] },
- src/tools/financeNews.ts:80-110 (helper)Helper function that performs the actual news search using BaiduNews, handles concurrency, deduplication, and limits to 20 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:133-137 (registration)Registration of the finance_news tool in the ListToolsRequestSchema handler, exposing name, description, and input schema.{ name: financeNews.name, description: financeNews.description, inputSchema: financeNews.parameters },
- src/index.ts:233-236 (registration)Handler dispatch/registration in the CallToolRequestSchema switch case, extracting query and calling financeNews.run.case "finance_news": { const query = String(request.params.arguments?.query); return await financeNews.run({ query }); }