Skip to main content
Glama
xinkuang

China Stock MCP

by xinkuang

get_stock_value

Retrieve stock valuation analysis data for Chinese stocks by providing the stock symbol to assess investment value and performance metrics.

Instructions

获取指定股票的个股估值分析数据

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
symbolYes股票代码 (例如: '000001')
output_formatNo输出数据格式: json, csv, xml, excel, markdown, html。默认: markdownmarkdown

Implementation Reference

  • Registers the 'get_stock_value' tool using the @mcp.tool decorator.
    @mcp.tool(
        name="get_stock_value", description="获取指定股票的个股估值分析数据"
    )
  • The handler function that fetches the stock valuation data using ak.stock_value_em(symbol), handles empty DataFrame, and returns formatted output.
    def get_stock_value(
        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 = ak.stock_value_em(symbol=symbol)
        if df.empty:
            df = pd.DataFrame()
    
        return _format_dataframe_output(df, output_format)
  • Pydantic schema definitions for input parameters: symbol (str) and output_format (Literal with default).
    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"
  • Helper function to format the DataFrame output in various formats: json, csv, etc., used by get_stock_value.
    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)

Latest Blog Posts

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/xinkuang/china-stock-mcp'

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