search
Find stock codes by searching with company names or stock keywords across major markets including Shanghai, Shenzhen, Hong Kong, and US exchanges.
Instructions
根据股票名称、公司名称等关键词查找股票代码, 不支持加密货币。该工具比较耗时,当你知道股票代码或用户已指定股票代码时,建议直接通过股票代码使用其他工具
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| keyword | Yes | 搜索关键词,公司名称、股票名称、股票代码、证券简称 | |
| market | No | 股票市场,仅支持: sh(上证), sz(深证), hk(港股), us(美股), 不支持加密货币 | sh |
Implementation Reference
- mcp_aktools/__init__.py:33-41 (handler)The handler function implementing the core logic of the 'search' tool. It invokes the ak_search helper to find stock information based on keyword and market, then formats and returns the result.def search( keyword: str = Field(description="搜索关键词,公司名称、股票名称、股票代码、证券简称"), market: str = field_market, ): info = ak_search(None, keyword, market) if info is not None: suffix = f"交易市场: {market}" return "\n".join([info.to_string(), suffix]) return f"Not Found for {keyword}"
- mcp_aktools/__init__.py:28-32 (registration)The @mcp.tool decorator registers the 'search' function as an MCP tool with title and description.@mcp.tool( title="查找股票代码", description="根据股票名称、公司名称等关键词查找股票代码, 不支持加密货币。" "该工具比较耗时,当你知道股票代码或用户已指定股票代码时,建议直接通过股票代码使用其他工具", )
- mcp_aktools/__init__.py:34-36 (schema)Input schema for the search tool parameters: keyword (search term) and market (stock market). Uses shared field_market definition from line 21.keyword: str = Field(description="搜索关键词,公司名称、股票名称、股票代码、证券简称"), market: str = field_market, ):
- mcp_aktools/__init__.py:527-561 (helper)Supporting helper function ak_search that queries various akshare datasets to find matching stock/fund codes or names based on symbol, keyword, or market.def ak_search(symbol=None, keyword=None, market=None): markets = [ ["sh", ak.stock_info_a_code_name, "code", "name"], ["sh", ak.stock_info_sh_name_code, "证券代码", "证券简称"], ["sz", ak.stock_info_sz_name_code, "A股代码", "A股简称"], ["hk", ak.stock_hk_spot, "代码", "中文名称"], ["hk", ak.stock_hk_spot_em, "代码", "名称"], ["us", ak.get_us_stock_name, "symbol", "cname"], ["us", ak.get_us_stock_name, "symbol", "name"], ["sh", ak.fund_etf_spot_ths, "基金代码", "基金名称"], ["sz", ak.fund_etf_spot_ths, "基金代码", "基金名称"], ["sh", ak.fund_info_index_em, "基金代码", "基金名称"], ["sz", ak.fund_info_index_em, "基金代码", "基金名称"], ["sh", ak.fund_etf_spot_em, "代码", "名称"], ["sz", ak.fund_etf_spot_em, "代码", "名称"], ] for m in markets: if market and market != m[0]: continue all = ak_cache(m[1], ttl=86400, ttl2=86400*7) if all is None or all.empty: continue for _, v in all.iterrows(): code, name = str(v[m[2]]).upper(), str(v[m[3]]).upper() if symbol and symbol.upper() == code: return v if keyword and keyword.upper() in [code, name]: return v for _, v in all.iterrows() if keyword else []: name = str(v[m[3]]) if len(keyword) >= 4 and keyword in name: return v if name.startswith(keyword): return v return None