get_index_constituents
Fetch constituent stocks for major Chinese A-share indices like HS300, SZ50, or ZZ500 to analyze market composition and track index performance.
Instructions
Generic index constituent fetch (hs300/sz50/zz500).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| index | Yes | ||
| date | No | ||
| limit | No | ||
| format | No | markdown |
Implementation Reference
- src/tools/indices.py:57-63 (handler)The main handler function for the 'get_index_constituents' tool, decorated with @app.tool() for registration, which wraps the core logic in run_tool_with_handling and delegates to fetch_index_constituents.@app.tool() def get_index_constituents(index: str, date: Optional[str] = None, limit: int = 250, format: str = "markdown") -> str: """Generic index constituent fetch (hs300/sz50/zz500).""" return run_tool_with_handling( lambda: fetch_index_constituents(active_data_source, index=index, date=date, limit=limit, format=format), context=f"get_index_constituents:{index}", )
- src/use_cases/indices.py:8-15 (schema)Schema-like mapping of supported index names (including Chinese aliases) to canonical keys, used by validate_index_key for input validation.INDEX_MAP = { "hs300": "hs300", "沪深300": "hs300", "zz500": "zz500", "中证500": "zz500", "sz50": "sz50", "上证50": "sz50", }
- mcp_server.py:53-53 (registration)Top-level registration call that invokes register_index_tools, thereby registering the get_index_constituents tool (along with other index tools) to the MCP app.register_index_tools(app, active_data_source)
- src/use_cases/indices.py:25-35 (helper)Core helper function implementing the tool logic: validates inputs, maps index name, fetches data from the data source, and formats the output table.def fetch_index_constituents(data_source: FinancialDataSource, *, index: str, date: Optional[str], limit: int, format: str) -> str: validate_output_format(format) key = validate_index_key(index, INDEX_MAP) if key == "hs300": df = data_source.get_hs300_stocks(date=date) elif key == "sz50": df = data_source.get_sz50_stocks(date=date) else: df = data_source.get_zz500_stocks(date=date) meta = {"index": key, "as_of": date or "latest"} return format_table_output(df, format=format, max_rows=limit, meta=meta)