Skip to main content
Glama
sharansahu

MCP SQL Agent

by sharansahu

get_schema

Retrieve full database schema details, including table structures and sample data, to interact with databases using natural language queries via the MCP SQL Agent.

Instructions

Get the complete database schema with table structures and sample data

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • Handler function for 'get_schema' tool registered via @mcp.tool() decorator. Delegates to get_database_schema() helper.
    @mcp.tool()
    def get_schema() -> str:
        """Get the complete database schema with table structures and sample data"""
        return get_database_schema()
  • Handler function for 'get_schema' tool registered via @mcp.tool() decorator. Delegates to get_database_schema() helper.
    @mcp.tool()
    def get_schema() -> str:
        """Get the complete database schema with table structures and sample data"""
        return get_database_schema()
  • Handler function for 'get_schema' tool registered via @mcp.tool() decorator. Delegates to get_database_schema() helper.
    @mcp.tool()
    def get_schema() -> str:
        """Get the complete database schema with table structures and sample data"""
        return get_database_schema()
  • Core logic for fetching MySQL database schema, including tables, columns, and sample data.
    def get_database_schema() -> str:
        """Get the database schema information"""
        try:
            conn = mysql.connector.connect(**db_config)
            cursor = conn.cursor()
            
            # Get all table names
            tables_query = """
            SELECT TABLE_NAME 
            FROM information_schema.TABLES 
            WHERE TABLE_SCHEMA = %s
            """
            cursor.execute(tables_query, (db_config['database'],))
            tables = cursor.fetchall()
            
            schema_info = f"Database Schema for '{db_config['database']}':\n\n"
            
            for (table_name,) in tables:
                schema_info += f"Table: {table_name}\n"
                
                # Get column information for each table
                columns_query = """
                SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_KEY, EXTRA
                FROM information_schema.COLUMNS 
                WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s
                ORDER BY ORDINAL_POSITION
                """
                cursor.execute(columns_query, (db_config['database'], table_name))
                columns = cursor.fetchall()
                
                for column in columns:
                    col_name, data_type, is_nullable, default_value, column_key, extra = column
                    pk_indicator = " (PRIMARY KEY)" if column_key == "PRI" else ""
                    null_indicator = " NOT NULL" if is_nullable == "NO" else ""
                    default_indicator = f" DEFAULT {default_value}" if default_value else ""
                    auto_inc = f" {extra}" if extra else ""
                    schema_info += f"  - {col_name}: {data_type}{pk_indicator}{null_indicator}{default_indicator}{auto_inc}\n"
                
                # Get sample data (first 3 rows)
                sample_query = f"SELECT * FROM `{table_name}` LIMIT 3"
                try:
                    cursor.execute(sample_query)
                    sample_data = cursor.fetchall()
                    if sample_data:
                        schema_info += f"  Sample data:\n"
                        for row in sample_data:
                            schema_info += f"    {row}\n"
                except Exception as e:
                    schema_info += f"  Sample data: Error reading sample data - {e}\n"
                
                schema_info += "\n"
            
            return schema_info
            
        except Exception as e:
            return f"Error getting schema: {str(e)}"
        finally:
            if 'cursor' in locals():
                cursor.close()
            if 'conn' in locals():
                conn.close()
  • Core logic for fetching SQLite database schema using PRAGMA table_info.
    def get_database_schema() -> str:
        """Get the database schema information"""
        conn = sqlite3.connect(db_path)
        try:
            # Get all table names
            tables_query = "SELECT name FROM sqlite_master WHERE type='table';"
            tables = conn.execute(tables_query).fetchall()
            
            schema_info = "Database Schema:\n\n"
            
            for (table_name,) in tables:
                schema_info += f"Table: {table_name}\n"
                
                # Get column information for each table
                pragma_query = f"PRAGMA table_info({table_name});"
                columns = conn.execute(pragma_query).fetchall()
                
                for column in columns:
                    cid, name, data_type, notnull, default_value, pk = column
                    pk_indicator = " (PRIMARY KEY)" if pk else ""
                    null_indicator = " NOT NULL" if notnull else ""
                    default_indicator = f" DEFAULT {default_value}" if default_value else ""
                    schema_info += f"  - {name}: {data_type}{pk_indicator}{null_indicator}{default_indicator}\n"
                
                # Get sample data (first 3 rows)
                sample_query = f"SELECT * FROM {table_name} LIMIT 3;"
                try:
                    sample_data = conn.execute(sample_query).fetchall()
                    if sample_data:
                        schema_info += f"  Sample data:\n"
                        for row in sample_data:
                            schema_info += f"    {row}\n"
                except Exception as e:
                    schema_info += f"  Sample data: Error reading sample data - {e}\n"
                
                schema_info += "\n"
            
            return schema_info
            
        except Exception as e:
            return f"Error getting schema: {str(e)}"
        finally:
            conn.close()
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the tool retrieves schema and sample data, but doesn't cover critical aspects like whether this is a read-only operation, potential performance impacts, data freshness, or error handling. For a tool with zero annotation coverage, this leaves significant gaps in understanding its behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core purpose ('Get the complete database schema') and adds specific details ('with table structures and sample data') without any wasted words. It's appropriately sized for a simple tool with no parameters.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's simplicity (0 parameters, output schema exists), the description is minimally adequate. However, with no annotations and sibling tools present, it lacks context on when to use it versus alternatives and behavioral details. The output schema will cover return values, but the description doesn't fully address the tool's role in the broader context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The tool has 0 parameters with 100% schema description coverage, so the schema fully documents the lack of inputs. The description adds no parameter information, which is appropriate here, but since there are no parameters to explain, it doesn't need to compensate for any gaps. Baseline 4 is assigned as per rules for 0 parameters.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose with a specific verb ('Get') and resource ('complete database schema'), including what it retrieves ('table structures and sample data'). However, it doesn't explicitly differentiate from sibling tools like 'describe_table' or 'list_tables', which likely provide overlapping or related schema information.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided on when to use this tool versus alternatives such as 'describe_table', 'list_tables', or 'search_tables'. The description implies a comprehensive schema retrieval but doesn't specify use cases, prerequisites, or exclusions, leaving the agent to infer usage from context alone.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

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/sharansahu/mcp-sql'

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