Skip to main content
Glama
24mlight

A Share MCP

get_profit_data

Retrieve quarterly profitability metrics (ROE, net profit margin) for A-share stocks by entering stock code, year, and quarter. Outputs data in a markdown table or error message for invalid requests.

Instructions

Fetches quarterly profitability data (e.g., ROE, net profit margin) for a stock. Args: code: The stock code (e.g., 'sh.600000'). year: The 4-digit year (e.g., '2023'). quarter: The quarter (1, 2, 3, or 4). Returns: Markdown table with profitability data or an error message.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYes
quarterYes
yearYes

Implementation Reference

  • MCP tool handler for 'get_profit_data'. Decorated with @app.tool(), it defines the input schema via type hints and docstring, calls the shared helper to fetch and format data from the active data source.
    def get_profit_data(code: str, year: str, quarter: int, limit: int = 250, format: str = "markdown") -> str: """ Get quarterly profitability data (e.g., ROE, net profit margin) for a stock. Args: code: The stock code (e.g., 'sh.600000'). year: The 4-digit year (e.g., '2023'). quarter: The quarter (1, 2, 3, or 4). Returns: Profitability metrics table. """ return call_financial_data_tool( "get_profit_data", active_data_source.get_profit_data, "Profitability", code, year, quarter, limit=limit, format=format )
  • Shared helper invoked by the get_profit_data handler. Performs input validation, invokes the data source method, formats the DataFrame output, and handles various exceptions.
    def call_financial_data_tool( tool_name: str, # Pass the bound method like active_data_source.get_profit_data data_source_method: Callable, data_type_name: str, code: str, year: str, quarter: int, *, limit: int = 250, format: str = "markdown", ) -> str: """ Helper function to reduce repetition for financial data tools Args: tool_name: Name of the tool for logging data_source_method: Method to call on the data source data_type_name: Type of financial data (for logging) code: Stock code year: Year to query quarter: Quarter to query Returns: Markdown formatted string with results or error message """ logger.info(f"Tool '{tool_name}' called for {code}, {year}Q{quarter}") try: # Basic validation if not year.isdigit() or len(year) != 4: logger.warning(f"Invalid year format requested: {year}") return f"Error: Invalid year '{year}'. Please provide a 4-digit year." if not 1 <= quarter <= 4: logger.warning(f"Invalid quarter requested: {quarter}") return f"Error: Invalid quarter '{quarter}'. Must be between 1 and 4." # Call the appropriate method on the already instantiated active_data_source df = data_source_method(code=code, year=year, quarter=quarter) logger.info( f"Successfully retrieved {data_type_name} data for {code}, {year}Q{quarter}.") meta = {"code": code, "year": year, "quarter": quarter, "dataset": data_type_name} return format_table_output(df, format=format, max_rows=limit, meta=meta) except NoDataFoundError as e: logger.warning(f"NoDataFoundError for {code}, {year}Q{quarter}: {e}") return f"Error: {e}" except LoginError as e: logger.error(f"LoginError for {code}: {e}") return f"Error: Could not connect to data source. {e}" except DataSourceError as e: logger.error(f"DataSourceError for {code}: {e}") return f"Error: An error occurred while fetching data. {e}" except ValueError as e: logger.warning(f"ValueError processing request for {code}: {e}") return f"Error: Invalid input parameter. {e}" except Exception as e: logger.exception( f"Unexpected Exception processing {tool_name} for {code}: {e}") return f"Error: An unexpected error occurred: {e}"
  • Data source implementation of get_profit_data method called by the tool chain. Delegates to internal _fetch_financial_data helper for Baostock API query.
    def get_profit_data(self, code: str, year: str, quarter: int) -> pd.DataFrame: """Fetches quarterly profitability data using Baostock.""" return _fetch_financial_data(bs.query_profit_data, "Profitability", code, year, quarter)
  • Registration function that contains the @app.tool() decorators for get_profit_data and other financial report tools. Called from main server setup.
    def register_financial_report_tools(app: FastMCP, active_data_source: FinancialDataSource): """ Register financial report related tools with the MCP app. Args: app: The FastMCP app instance active_data_source: The active financial data source """

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