Skip to main content
Glama
K02D

MCP Tabular Data Analysis Server

by K02D

query_sqlite

Execute SQL queries on SQLite databases to retrieve and analyze tabular data, supporting SELECT operations with configurable row limits for data exploration.

Instructions

Execute a SQL query on a SQLite database.

Args:
    db_path: Path to SQLite database file
    query: SQL query to execute (SELECT queries only for safety)
    limit: Maximum number of rows to return (default 100)

Returns:
    Dictionary containing:
    - query: The executed query
    - row_count: Number of rows returned
    - columns: List of column names
    - rows: Query results

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
db_pathYes
queryYes
limitNo

Implementation Reference

  • The main handler function for the 'query_sqlite' MCP tool. It executes SELECT queries on a SQLite database, adds a LIMIT if not present, returns results as JSON-friendly dict with columns and rows. Includes safety checks for SELECT only and file existence. Uses pandas for query execution and _resolve_path helper.
    @mcp.tool()
    def query_sqlite(
        db_path: str,
        query: str,
        limit: int = 100,
    ) -> dict[str, Any]:
        """
        Execute a SQL query on a SQLite database.
        
        Args:
            db_path: Path to SQLite database file
            query: SQL query to execute (SELECT queries only for safety)
            limit: Maximum number of rows to return (default 100)
        
        Returns:
            Dictionary containing:
            - query: The executed query
            - row_count: Number of rows returned
            - columns: List of column names
            - rows: Query results
        """
        # Basic safety check - only allow SELECT
        query_upper = query.strip().upper()
        if not query_upper.startswith("SELECT"):
            raise ValueError("Only SELECT queries are allowed for safety")
        
        path = _resolve_path(db_path)
        if not path.exists():
            raise FileNotFoundError(
                f"Database not found: {db_path}\n"
                f"Resolved to: {path}\n"
                f"Project root: {_PROJECT_ROOT}"
            )
        
        conn = sqlite3.connect(str(path))
        try:
            # Add LIMIT if not present
            if "LIMIT" not in query_upper:
                query = f"{query.rstrip(';')} LIMIT {limit}"
            
            df = pd.read_sql_query(query, conn)
            
            return {
                "query": query,
                "row_count": len(df),
                "columns": df.columns.tolist(),
                "rows": df.to_dict(orient="records"),
            }
        finally:
            conn.close()

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/K02D/mcp-tabular'

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