list_tables
Discover database tables and views in SQL Server with optional filtering by schema, name pattern, or view inclusion to analyze database structure.
Instructions
List all tables and views in the database.
Args:
schema: Filter by schema name (e.g., 'dbo'). If not specified, returns all schemas.
include_views: Include views in results (default: True)
pattern: Filter by name pattern using SQL LIKE syntax (e.g., 'Cust%', '%Order%')
Returns:
Dictionary with:
- tables: List of table/view info (schema, name, type)
- count: Number of results
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| schema | No | ||
| include_views | No | ||
| pattern | No |
Implementation Reference
- src/mssql_mcp/tools/tables.py:13-78 (handler)The handler function for the 'list_tables' tool, decorated with @mcp.tool(). It queries INFORMATION_SCHEMA.TABLES to list tables and views, applying optional filters for schema, inclusion of views, and name pattern. Returns a dictionary with the list of tables and count, or an error message.@mcp.tool() def list_tables( schema: str | None = None, include_views: bool = True, pattern: str | None = None, ) -> dict[str, Any]: """List all tables and views in the database. Args: schema: Filter by schema name (e.g., 'dbo'). If not specified, returns all schemas. include_views: Include views in results (default: True) pattern: Filter by name pattern using SQL LIKE syntax (e.g., 'Cust%', '%Order%') Returns: Dictionary with: - tables: List of table/view info (schema, name, type) - count: Number of results """ try: manager = get_connection_manager() # Build query with optional filters query = """ SELECT TABLE_SCHEMA as [schema], TABLE_NAME as [name], TABLE_TYPE as [type] FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_CATALOG = DB_NAME() """ params: list[Any] = [] if schema: query += " AND TABLE_SCHEMA = %s" params.append(schema) if not include_views: query += " AND TABLE_TYPE = 'BASE TABLE'" if pattern: query += " AND TABLE_NAME LIKE %s" params.append(pattern) query += " ORDER BY TABLE_SCHEMA, TABLE_NAME" rows = manager.execute_query(query, tuple(params) if params else None) # Convert rows to list of dicts tables = [ { "schema": row["schema"], "name": row["name"], "type": "TABLE" if row["type"] == "BASE TABLE" else "VIEW", } for row in rows ] return { "tables": tables, "count": len(tables), } except Exception as e: logger.error(f"Error listing tables: {e}") return {"error": str(e)}