list_tables
Discover and filter database tables and views in SQL Server to analyze schema structure and locate specific data objects.
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 core handler function for the 'list_tables' tool. It queries INFORMATION_SCHEMA.TABLES to list tables and views, applying optional filters for schema, views, and name patterns. Decorated with @mcp.tool() for automatic registration and schema inference from signature/docstring.@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)}
- src/mssql_mcp/tools/tables.py:13-13 (registration)The @mcp.tool() decorator registers the list_tables function as an MCP tool, with input schema derived from type hints and docstring.@mcp.tool()