Skip to main content
Glama
sharansahu

MCP SQL Agent

by sharansahu

list_tables

Retrieve a list of all tables within a database using the MCP SQL Agent. Simplifies schema exploration by providing a clear overview of available tables.

Instructions

List all tables in the database

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • Handler function for the 'list_tables' MCP tool in the SQLite server implementation. Uses @mcp.tool() decorator for registration and executes a SQLite query to list all tables.
    @mcp.tool()
    def list_tables() -> str:
        """List all tables in the database"""
        conn = sqlite3.connect(db_path)
        try:
            tables_query = "SELECT name FROM sqlite_master WHERE type='table';"
            tables = conn.execute(tables_query).fetchall()
            table_list = "Available tables:\n"
            for (table_name,) in tables:
                table_list += f"- {table_name}\n"
            return table_list
        except Exception as e:
            return f"Error: {str(e)}"
        finally:
            conn.close()
  • Handler function for the 'list_tables' MCP tool in the MySQL server implementation. Uses @mcp.tool() decorator for registration and queries information_schema.TABLES.
    @mcp.tool()
    def list_tables() -> str:
        """List all tables in the database"""
        try:
            conn = mysql.connector.connect(**db_config)
            cursor = conn.cursor()
            
            tables_query = """
            SELECT TABLE_NAME 
            FROM information_schema.TABLES 
            WHERE TABLE_SCHEMA = %s
            """
            cursor.execute(tables_query, (db_config['database'],))
            tables = cursor.fetchall()
            
            table_list = "Available tables:\n"
            for (table_name,) in tables:
                table_list += f"- {table_name}\n"
            return table_list
        except Exception as e:
            return f"Error: {str(e)}"
        finally:
            if 'cursor' in locals():
                cursor.close()
            if 'conn' in locals():
                conn.close()
  • Handler function for the 'list_tables' MCP tool in the Oracle server implementation. Uses @mcp.tool() decorator for registration and queries user_tables.
    @mcp.tool()
    def list_tables() -> str:
        """List all tables in the database"""
        try:
            conn = cx_Oracle.connect(**db_config)
            cursor = conn.cursor()
            
            tables_query = """
            SELECT table_name 
            FROM user_tables 
            ORDER BY table_name
            """
            cursor.execute(tables_query)
            tables = cursor.fetchall()
            
            table_list = "Available tables:\n"
            for (table_name,) in tables:
                table_list += f"- {table_name}\n"
            return table_list
        except Exception as e:
            return f"Error: {str(e)}"
        finally:
            if 'cursor' in locals():
                cursor.close()
            if 'conn' in locals():
                conn.close()
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