Skip to main content
Glama
sam2332

SQLite MCP Server

by sam2332

query_data

Execute SELECT queries on SQLite databases to retrieve and analyze data, enabling AI assistants to interact with database content through structured queries.

Instructions

Execute a SELECT query on the database

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesSQL SELECT query to execute
limitNoMaximum number of rows to return

Implementation Reference

  • The handler function that executes SELECT queries on the SQLite database, enforces SELECT-only, applies LIMIT if missing, formats results as a markdown-like table, and returns as CallToolResult.
    private async queryData(args: { query: string; limit?: number }): Promise<CallToolResult> {
      if (!this.db) {
        throw new Error("No database connected. Use connect_database first.");
      }
    
      try {
        // Ensure it's a SELECT query
        const trimmedQuery = args.query.trim().toLowerCase();
        if (!trimmedQuery.startsWith("select")) {
          throw new Error("Only SELECT queries are allowed with query_data. Use execute_query for other operations.");
        }
    
        const limit = args.limit || 100;
        const queryWithLimit = args.query.toLowerCase().includes("limit") 
          ? args.query 
          : `${args.query} LIMIT ${limit}`;
    
        const results = this.db.prepare(queryWithLimit).all();
        
        if (results.length === 0) {
          return {
            content: [
              {
                type: "text",
                text: "Query executed successfully. No rows returned.",
              } satisfies TextContent,
            ],
          };
        }
    
        // Format results as a table
        const headers = Object.keys(results[0] as Record<string, unknown>);
        const rows = results.map((row) => 
          headers.map(header => String((row as Record<string, unknown>)[header] ?? "NULL")).join(" | ")
        );
        
        const headerRow = headers.join(" | ");
        const separator = headers.map(h => "-".repeat(h.length)).join("-|-");
        const table = [headerRow, separator, ...rows].join("\n");
    
        return {
          content: [
            {
              type: "text",
              text: `Query results (${results.length} rows):\n\n${table}`,
            } satisfies TextContent,
          ],
        };
      } catch (error) {
        throw new Error(`Query failed: ${error instanceof Error ? error.message : String(error)}`);
      }
    }
  • Input schema definition for the query_data tool, specifying 'query' as required string and optional 'limit' number.
    inputSchema: {
      type: "object",
      properties: {
        query: {
          type: "string",
          description: "SQL SELECT query to execute",
        },
        limit: {
          type: "number",
          description: "Maximum number of rows to return",
          default: 100,
        },
      },
      required: ["query"],
    },
  • src/index.ts:101-118 (registration)
    Tool registration in the listTools response, including name, description, and input schema.
      name: "query_data",
      description: "Execute a SELECT query on the database",
      inputSchema: {
        type: "object",
        properties: {
          query: {
            type: "string",
            description: "SQL SELECT query to execute",
          },
          limit: {
            type: "number",
            description: "Maximum number of rows to return",
            default: 100,
          },
        },
        required: ["query"],
      },
    },
  • src/index.ts:171-172 (registration)
    Dispatcher in callToolRequestHandler switch statement that routes query_data calls to the queryData handler method.
    case "query_data":
      return await this.queryData(args as { query: string; limit?: number });

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/sam2332/mcp-quick-sqlite3'

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