get_news_data
Retrieve stock-related news data for Chinese securities by providing a stock symbol, with output available in multiple formats including JSON, CSV, and markdown.
Instructions
获取股票相关的新闻数据
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| symbol | Yes | 股票代码 (例如: '000001') | |
| output_format | No | 输出数据格式: json, csv, xml, excel, markdown, html。默认: markdown | markdown |
Implementation Reference
- src/china_stock_mcp/server.py:315-326 (handler)The main handler function decorated with @mcp.tool for the 'get_news_data' tool. It takes symbol and output_format, fetches news data from 'eastmoney' via ako.get_news_data, and returns formatted output.@mcp.tool(name="get_news_data", description="获取股票相关的新闻数据") def get_news_data( symbol: Annotated[str, Field(description="股票代码 (例如: '000001')")], output_format: Annotated[ Literal["json", "csv", "xml", "excel", "markdown", "html"], Field(description="输出数据格式: json, csv, xml, excel, markdown, html。默认: markdown"), ] = "markdown" ) -> str: """获取股票相关新闻数据.""" df = ako.get_news_data(symbol=symbol, source="eastmoney") return _format_dataframe_output(df, output_format)
- Input schema using Pydantic Annotated types and Field for validation and descriptions.symbol: Annotated[str, Field(description="股票代码 (例如: '000001')")], output_format: Annotated[ Literal["json", "csv", "xml", "excel", "markdown", "html"], Field(description="输出数据格式: json, csv, xml, excel, markdown, html。默认: markdown"), ] = "markdown" ) -> str:
- src/china_stock_mcp/server.py:315-315 (registration)The @mcp.tool decorator registers the function as an MCP tool with name 'get_news_data'.@mcp.tool(name="get_news_data", description="获取股票相关的新闻数据")
- src/china_stock_mcp/server.py:89-117 (helper)Supporting utility to format the DataFrame result into various output formats like json, csv, etc., called by the handler.def _format_dataframe_output( df: pd.DataFrame, output_format: Literal["json", "csv", "xml", "excel", "markdown", "html"], ) -> str: """ 根据指定的格式格式化 DataFrame 输出。 """ if df.empty: return json.dumps([]) if output_format == "json": return df.to_json(orient="records", force_ascii=False) elif output_format == "csv": return df.to_csv(index=False) elif output_format == "xml": return df.to_xml(index=False) elif output_format == "excel": # 使用 BytesIO 将 Excel 写入内存 output = io.BytesIO() df.to_excel(output, index=False, engine='openpyxl') # 返回 base64 编码的二进制数据,或者直接返回字节流 # 为了兼容性,这里尝试返回 utf-8 编码的字符串,但对于二进制文件,通常直接传输字节流更合适 return output.getvalue().decode("utf-8", errors="ignore") elif output_format == "markdown": return df.to_markdown(index=False) elif output_format == "html": return df.to_html(index=False) else: return df.to_json(orient="records", force_ascii=False)