get_dupont_data
Retrieve quarterly DuPont analysis data (ROE decomposition) for a specified A-share stock by providing stock code, year, and quarter. Outputs a markdown table for clear financial insights.
Instructions
Fetches quarterly DuPont analysis data (ROE decomposition) 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 DuPont analysis 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:129-148 (handler)MCP tool handler implementation for 'get_dupont_data'. This is the entry point decorated with @app.tool(), handling input parameters, delegating to data source via call_financial_data_tool, and returning formatted output.@app.tool() def get_dupont_data(code: str, year: str, quarter: int, limit: int = 250, format: str = "markdown") -> str: """ Get quarterly DuPont analysis data (ROE decomposition) 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: DuPont analysis metrics table. """ return call_financial_data_tool( "get_dupont_data", active_data_source.get_dupont_data, "DuPont Analysis", code, year, quarter, limit=limit, format=format )
- src/baostock_data_source.py:433-435 (helper)Core data retrieval logic in BaostockDataSource class. Calls Baostock's query_dupont_data via the shared _fetch_financial_data helper to obtain raw DataFrame.def get_dupont_data(self, code: str, year: str, quarter: int) -> pd.DataFrame: """Fetches quarterly DuPont analysis data using Baostock.""" return _fetch_financial_data(bs.query_dupont_data, "DuPont Analysis", code, year, quarter)
- src/tools/base.py:15-73 (helper)Shared helper function used by financial report tools, including get_dupont_data. Performs input validation, calls the data source method, handles errors, and formats the DataFrame output using format_table_output.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/tools/financial_reports.py:15-24 (registration)The register_financial_report_tools function where all financial report MCP tools, including get_dupont_data, are registered via @app.tool() decorators.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 """ @app.tool()