get_all_stock
Fetch A-share stocks and indices with trading status for a specific date. Returns a table showing which stocks are trading or suspended.
Instructions
Fetch a list of all stocks (A-shares and indices) and their trading status for a date.
Args:
date: Optional. The date in 'YYYY-MM-DD' format. If None, uses the current date.
Returns:
Markdown table listing stock codes and trading status (1=trading, 0=suspended).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date | No | ||
| limit | No | ||
| format | No | markdown |
Implementation Reference
- src/tools/market_overview.py:48-63 (handler)Primary handler and registration for the MCP 'get_all_stock' tool. Decorated with @app.tool(), it processes inputs, logs the call, and delegates execution to the fetch_all_stock use case via run_tool_with_handling.@app.tool() def get_all_stock(date: Optional[str] = None, limit: int = 250, format: str = "markdown") -> str: """ Fetch a list of all stocks (A-shares and indices) and their trading status for a date. Args: date: Optional. The date in 'YYYY-MM-DD' format. If None, uses the current date. Returns: Markdown table listing stock codes and trading status (1=trading, 0=suspended). """ logger.info(f"Tool 'get_all_stock' called for date={date or 'default'}") return run_tool_with_handling( lambda: fetch_all_stock(active_data_source, date=date, limit=limit, format=format), context=f"get_all_stock:{date or 'default'}", )
- Helper function that invokes the data source's get_all_stock method, validates format, and formats the resulting DataFrame as markdown/json/csv table.def fetch_all_stock(data_source: FinancialDataSource, *, date: Optional[str], limit: int, format: str) -> str: validate_output_format(format) df = data_source.get_all_stock(date=date) meta = {"as_of": date or "default"} return format_table_output(df, format=format, max_rows=limit, meta=meta)
- src/baostock_data_source.py:622-662 (helper)Core data fetching implementation in BaostockDataSource class. Calls Baostock's query_all_stock API, handles errors, and constructs pandas DataFrame from results.def get_all_stock(self, date: Optional[str] = None) -> pd.DataFrame: """Fetches all stock list for a given date using Baostock.""" logger.info(f"Fetching all stock list for date={date or 'default'}") try: with baostock_login_context(): rs = bs.query_all_stock(day=date) if rs.error_code != '0': logger.error( f"Baostock API error (All Stock) for date {date}: {rs.error_msg} (code: {rs.error_code})") if "no record found" in rs.error_msg.lower() or rs.error_code == '10002': # Check if this applies raise NoDataFoundError( f"No stock data found for date {date}. Baostock msg: {rs.error_msg}") else: raise DataSourceError( f"Baostock API error fetching all stock list: {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 stock list returned for date {date} (empty result set).") raise NoDataFoundError( f"No stock list found for date {date} (empty result set).") result_df = pd.DataFrame(data_list, columns=rs.fields) logger.info( f"Retrieved {len(result_df)} stock records for date {date or 'default'}.") return result_df except (LoginError, NoDataFoundError, DataSourceError, ValueError) as e: logger.warning( f"Caught known error fetching all stock list for date {date}: {type(e).__name__}") raise e except Exception as e: logger.exception( f"Unexpected error fetching all stock list for date {date}: {e}") raise DataSourceError( f"Unexpected error fetching all stock list for date {date}: {e}")
- src/data_source_interface.py:93-96 (schema)Abstract method definition in FinancialDataSource interface, specifying the expected signature and purpose for concrete implementations.@abstractmethod def get_all_stock(self, date: Optional[str] = None) -> pd.DataFrame: """Fetches list of all stocks and their trading status on a given date.""" pass
- mcp_server.py:54-54 (registration)Invocation of the registration function for market overview tools, including get_all_stock, in the main MCP server setup.register_market_overview_tools(app, active_data_source)