search_tables
Search database tables using keywords to locate relevant data within the SP Database MCP Server. Specify source type (database, API, or auto) to streamline queries for schema or metadata information.
Instructions
根据关键词搜索数据库表
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| keyword | Yes | 搜索关键词 | |
| source | No | 数据源类型 | auto |
Implementation Reference
- sp_database_mcp/server.py:203-223 (handler)MCP tool handler for 'search_tables': extracts parameters, calls internal _search_tables, formats results as markdown list of matching tables.elif name == "search_tables": keyword = arguments.get("keyword") source = arguments.get("source", "auto") if not keyword: return [TextContent(type="text", text="错误:缺少搜索关键词")] tables = await _search_tables(keyword, source) if tables: output = f"找到 {len(tables)} 个匹配的表:\n\n" for table in tables: output += f"## {table.name}\n" if table.comment: output += f"**说明**: {table.comment}\n" output += f"**字段数**: {len(table.columns)}\n\n" return [TextContent(type="text", text=output)] else: return [ TextContent(type="text", text=f"未找到包含关键词 '{keyword}' 的表") ]
- sp_database_mcp/server.py:137-152 (registration)Registration of 'search_tables' tool in @server.list_tools(), including name, description, and input schema.name="search_tables", description="根据关键词搜索数据库表", inputSchema={ "type": "object", "properties": { "keyword": {"type": "string", "description": "搜索关键词"}, "source": { "type": "string", "enum": ["database", "api", "auto"], "description": "数据源类型", "default": "auto", }, }, "required": ["keyword"], }, ),
- sp_database_mcp/server.py:286-302 (helper)Helper function _search_tables that routes search request to appropriate client (db_client or api_client) based on source.async def _search_tables(keyword: str, source: str) -> List[TableInfo]: """搜索表的内部方法""" if source == "database" and db_client: return db_client.search_tables(keyword) elif source == "api" and api_client: return await api_client.search_tables(keyword) elif source == "auto": # 优先使用数据库直连 if db_client: result = db_client.search_tables(keyword) if result: return result if api_client: return await api_client.search_tables(keyword) return []
- sp_database_mcp/database.py:227-241 (handler)Core DatabaseClient.search_tables method: filters table names by keyword, retrieves full TableInfo for matches.def search_tables(self, keyword: str) -> List[TableInfo]: """根据关键词搜索表""" all_tables = self.get_all_tables() matching_tables = [ table for table in all_tables if keyword.lower() in table.lower() ] result = [] for table_name in matching_tables: table_info = self.get_table_info(table_name) if table_info: result.append(table_info) return result