Skip to main content
Glama
bpamiri

SQL Server MCP

by bpamiri

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
NameRequiredDescriptionDefault
schemaNo
include_viewsNo
patternNo

Implementation Reference

  • 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)}
  • The @mcp.tool() decorator registers the list_tables function as an MCP tool, with input schema derived from type hints and docstring.
    @mcp.tool()

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/bpamiri/pymssql-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server