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
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | ||
| quarter | Yes | ||
| year | Yes |
Implementation Reference
- src/tools/financial_reports.py:25-43 (handler)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 )
- src/tools/base.py:15-74 (helper)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}"
- src/baostock_data_source.py:413-415 (helper)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)
- src/tools/financial_reports.py:15-23 (registration)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 """