get_operation_data
Fetches quarterly operation capability data (e.g., turnover ratios) for a specific stock in China's A-share market using stock code, year, and quarter inputs. Outputs data in a markdown table format.
Instructions
Fetches quarterly operation capability data (e.g., turnover ratios) 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 operation capability 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:45-64 (handler)The primary handler function for the "get_operation_data" MCP tool. Decorated with @app.tool() for automatic registration. Validates inputs via helper, fetches data from data source, formats as markdown table.@app.tool() def get_operation_data(code: str, year: str, quarter: int, limit: int = 250, format: str = "markdown") -> str: """ Get quarterly operation capability data (e.g., turnover ratios) 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: Operation capability metrics table. """ return call_financial_data_tool( "get_operation_data", active_data_source.get_operation_data, "Operation Capability", code, year, quarter, limit=limit, format=format )
- mcp_server.py:52-52 (registration)Invocation of register_financial_report_tools which defines and registers the get_operation_data tool (and other financial report tools) with the MCP app instance.register_financial_report_tools(app, active_data_source)
- src/tools/base.py:15-74 (helper)Shared helper invoked by the handler to perform input validation, call the data source method, handle exceptions, and format the pandas DataFrame output as a markdown table.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:417-419 (helper)Data source-specific implementation that invokes Baostock's query_operation_data API via a shared fetching helper to retrieve raw operation data as DataFrame.def get_operation_data(self, code: str, year: str, quarter: int) -> pd.DataFrame: """Fetches quarterly operation capability data using Baostock.""" return _fetch_financial_data(bs.query_operation_data, "Operation Capability", code, year, quarter)