Skip to main content
Glama
24mlight

A Share MCP

get_dividend_data

Fetch dividend information for A-share stocks by stock code and year, returning dividend records including announcement and ex-dividend details.

Instructions

    Fetches dividend information for a given stock code and year.

    Args:
        code: The stock code in Baostock format (e.g., 'sh.600000', 'sz.000001').
        year: The year to query (e.g., '2023').
        year_type: Type of year. Valid options (from Baostock):
                     'report': Announcement year (预案公告年份)
                     'operate': Ex-dividend year (除权除息年份)
                   Defaults to 'report'.

    Returns:
        Dividend records table.
    

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYes
yearYes
year_typeNoreport
limitNo
formatNomarkdown

Implementation Reference

  • MCP tool handler for get_dividend_data. Decorated with @app.tool(), logs the call, and delegates to the use case function fetch_dividend_data via run_tool_with_handling for execution and error handling.
    @app.tool()
    def get_dividend_data(code: str, year: str, year_type: str = "report", limit: int = 250, format: str = "markdown") -> str:
        """
        Fetches dividend information for a given stock code and year.
    
        Args:
            code: The stock code in Baostock format (e.g., 'sh.600000', 'sz.000001').
            year: The year to query (e.g., '2023').
            year_type: Type of year. Valid options (from Baostock):
                         'report': Announcement year (预案公告年份)
                         'operate': Ex-dividend year (除权除息年份)
                       Defaults to 'report'.
    
        Returns:
            Dividend records table.
        """
        logger.info(f"Tool 'get_dividend_data' called for {code}, year={year}, year_type={year_type}")
        return run_tool_with_handling(
            lambda: fetch_dividend_data(
                active_data_source,
                code=code,
                year=year,
                year_type=year_type,
                limit=limit,
                format=format,
            ),
            context=f"get_dividend_data:{code}:{year}",
        )
  • mcp_server.py:51-51 (registration)
    Registration call for stock market tools, including get_dividend_data, passed to the FastMCP app instance.
    register_stock_market_tools(app, active_data_source)
  • Use case helper function that performs validation and formats the dividend data fetched from the data source.
    def fetch_dividend_data(
        data_source: FinancialDataSource,
        *,
        code: str,
        year: str,
        year_type: str = "report",
        limit: int = 250,
        format: str = "markdown",
    ) -> str:
        validate_year(year)
        validate_year_type(year_type)
        validate_output_format(format)
    
        df = data_source.get_dividend_data(code=code, year=year, year_type=year_type)
        meta = {"code": code, "year": year, "year_type": year_type}
        return format_table_output(df, format=format, max_rows=limit, meta=meta)
  • Core implementation in BaostockDataSource that queries the Baostock API for dividend data and returns a DataFrame.
    def get_dividend_data(self, code: str, year: str, year_type: str = "report") -> pd.DataFrame:
        """Fetches dividend information using Baostock."""
        logger.info(
            f"Fetching dividend data for {code}, year={year}, year_type={year_type}")
        try:
            with baostock_login_context():
                rs = bs.query_dividend_data(
                    code=code, year=year, yearType=year_type)
    
                if rs.error_code != '0':
                    logger.error(
                        f"Baostock API error (Dividend) for {code}: {rs.error_msg} (code: {rs.error_code})")
                    if "no record found" in rs.error_msg.lower() or rs.error_code == '10002':
                        raise NoDataFoundError(
                            f"No dividend data found for {code} and year {year}. Baostock msg: {rs.error_msg}")
                    else:
                        raise DataSourceError(
                            f"Baostock API error fetching dividend data: {rs.error_msg} (code: {rs.error_code})")
    
                data_list = []
                while rs.next():
                    data_list.append(rs.get_row_data())
    
                if not data_list:
                    logger.warning(
                        f"No dividend data found for {code}, year {year} (empty result set from Baostock).")
                    raise NoDataFoundError(
                        f"No dividend data found for {code}, year {year} (empty result set).")
    
                result_df = pd.DataFrame(data_list, columns=rs.fields)
                logger.info(
                    f"Retrieved {len(result_df)} dividend records for {code}, year {year}.")
                return result_df

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/24mlight/a_share_mcp_is_just_I_need'

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