get_all_stock
Retrieve a comprehensive list of A-shares and indices with their trading status for a specific or current date. Returns a markdown table detailing stock codes, names, and trading activity (1 for trading, 0 for suspended).
Instructions
Fetches a list of all stocks (A-shares and indices) and their trading status for a given date.
Args:
date: Optional. The date in 'YYYY-MM-DD' format. If None, uses the current date.
Returns:
Markdown table listing stock codes, names, and their trading status (1=trading, 0=suspended).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date | No |
Implementation Reference
- src/tools/market_overview.py:63-99 (handler)Primary MCP tool handler for 'get_all_stock'. Decorated with @app.tool(), fetches raw stock list from active_data_source.get_all_stock(), handles various exceptions, formats output as markdown/JSON/CSV table with optional row limit.@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'}") try: # Add date validation if desired df = active_data_source.get_all_stock(date=date) logger.info( f"Successfully retrieved stock list for {date or 'default'}.") meta = {"as_of": date or "default"} return format_table_output(df, format=format, max_rows=limit, meta=meta) except NoDataFoundError as e: logger.warning(f"NoDataFoundError: {e}") return f"Error: {e}" except LoginError as e: logger.error(f"LoginError: {e}") return f"Error: Could not connect to data source. {e}" except DataSourceError as e: logger.error(f"DataSourceError: {e}") return f"Error: An error occurred while fetching data. {e}" except ValueError as e: logger.warning(f"ValueError: {e}") return f"Error: Invalid input parameter. {e}" except Exception as e: logger.exception( f"Unexpected Exception processing get_all_stock: {e}") return f"Error: An unexpected error occurred: {e}"
- mcp_server.py:54-54 (registration)Registration call for market_overview tools, including 'get_all_stock', invoked after FastMCP app initialization.register_market_overview_tools(app, active_data_source)
- src/baostock_data_source.py:622-663 (helper)Concrete implementation of get_all_stock in BaostockDataSource, queries baostock.query_all_stock API, handles errors, returns pandas DataFrame with stock list and status.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:94-96 (helper)Abstract method definition in FinancialDataSource interface, specifying the contract for get_all_stock used by tool handlers.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