search_stocks
Search for A-share stocks by code substring on a specific date, returning matching codes with trading status in your preferred format.
Instructions
Search stocks by code substring on a date.
Args:
keyword: Substring to match in the stock code (e.g., '600', '000001').
date: Optional 'YYYY-MM-DD'. If None, uses current date.
limit: Max rows to return. Defaults to 50.
format: Output format: 'markdown' | 'json' | 'csv'. Defaults to 'markdown'.
Returns:
Matching stock codes with their trading status.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| keyword | Yes | ||
| date | No | ||
| limit | No | ||
| format | No | markdown |
Implementation Reference
- src/tools/market_overview.py:65-83 (handler)The handler function for the 'search_stocks' tool. It is registered via the @app.tool() decorator, logs the call, and delegates execution to the fetch_search_stocks helper via run_tool_with_handling.@app.tool() def search_stocks(keyword: str, date: Optional[str] = None, limit: int = 50, format: str = "markdown") -> str: """ Search stocks by code substring on a date. Args: keyword: Substring to match in the stock code (e.g., '600', '000001'). date: Optional 'YYYY-MM-DD'. If None, uses current date. limit: Max rows to return. Defaults to 50. format: Output format: 'markdown' | 'json' | 'csv'. Defaults to 'markdown'. Returns: Matching stock codes with their trading status. """ logger.info("Tool 'search_stocks' called keyword=%s, date=%s, limit=%s, format=%s", keyword, date or "default", limit, format) return run_tool_with_handling( lambda: fetch_search_stocks(active_data_source, keyword=keyword, date=date, limit=limit, format=format), context=f"search_stocks:{keyword}", )
- Core helper function that implements the search logic: validates inputs, fetches all stocks, filters by keyword substring in stock code, and formats the output table.def fetch_search_stocks(data_source: FinancialDataSource, *, keyword: str, date: Optional[str], limit: int, format: str) -> str: validate_output_format(format) validate_non_empty_str(keyword, "keyword") df = data_source.get_all_stock(date=date) if df is None or df.empty: return "(No data available to display)" kw = keyword.strip().lower() filtered = df[df["code"].str.lower().str.contains(kw, na=False)] meta = {"keyword": keyword, "as_of": date or "current"} return format_table_output(filtered, format=format, max_rows=limit, meta=meta)
- mcp_server.py:54-54 (registration)The call to register_market_overview_tools which includes the registration of the search_stocks tool via its @app.tool() decorator.register_market_overview_tools(app, active_data_source)