normalize_stock_code
Convert stock codes to Baostock format for consistent data access in China's A-share market.
Instructions
Normalize a stock code to Baostock format.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes |
Implementation Reference
- src/tools/helpers.py:18-25 (handler)The MCP tool handler for 'normalize_stock_code', decorated with @app.tool(). It logs the invocation and executes the core logic through run_tool_with_handling.@app.tool() def normalize_stock_code(code: str) -> str: """Normalize a stock code to Baostock format.""" logger.info("Tool 'normalize_stock_code' called with input=%s", code) return run_tool_with_handling( lambda: normalize_stock_code_logic(code), context="normalize_stock_code", )
- mcp_server.py:58-58 (registration)Top-level call to register_helpers_tools(app), which registers the 'normalize_stock_code' tool as part of the helper tools.register_helpers_tools(app)
- src/use_cases/helpers.py:7-27 (helper)Core helper function implementing the stock code normalization logic using regex to match various input formats and convert to Baostock standard (sh/sz.XXXXXX).def normalize_stock_code_logic(code: str) -> str: validate_non_empty_str(code, "code") raw = code.strip() m = re.fullmatch(r"(?i)(sh|sz)[.]?(\d{6})", raw) if m: ex, num = m.group(1).lower(), m.group(2) return f"{ex}.{num}" m2 = re.fullmatch(r"(\d{6})[.]?(?i:(sh|sz))", raw) if m2: num, ex = m2.group(1), m2.group(2).lower() return f"{ex}.{num}" m3 = re.fullmatch(r"(\d{6})", raw) if m3: num = m3.group(1) ex = "sh" if num.startswith("6") else "sz" return f"{ex}.{num}" raise ValueError("Unsupported code format. Examples: 'sh.600000', '600000', '000001.SZ'.")