Skip to main content
Glama

✓ SAFE: Execute read-only SQL queries (SELECT, PRAGMA, EXPLAIN). Automatically rejects write operations.

execute_read_only_query

Execute read-only SQL queries like SELECT, PRAGMA, and EXPLAIN while automatically blocking write operations for safety.

Instructions

✓ SAFE: Execute read-only SQL queries (SELECT, PRAGMA, EXPLAIN). Automatically rejects write operations.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesRead-only SQL query to execute (SELECT, PRAGMA, EXPLAIN only)
paramsNoQuery parameters (optional) - Use parameterized queries for security
databaseNoDatabase name (optional, uses context if not provided) - Specify target database

Implementation Reference

  • Handler for the 'execute_read_only_query' tool. Validates the query is SELECT/PRAGMA-only, delegates to database_client.execute_query, and formats the result.
    server.tool(
    	{
    		name: 'execute_read_only_query',
    		description: `✓ SAFE: Execute read-only SQL queries (SELECT, PRAGMA, EXPLAIN). Automatically rejects write operations.`,
    		schema: ReadOnlyQuerySchema,
    	},
    	async ({ query, params = {}, database }) => {
    		try {
    			// Validate that this is a read-only query
    			const normalized_query = query.trim().toLowerCase();
    			if (
    				!normalized_query.startsWith('select') &&
    				!normalized_query.startsWith('pragma')
    			) {
    				throw new Error(
    					'Only SELECT and PRAGMA queries are allowed with execute_read_only_query',
    				);
    			}
    
    			const database_name = resolve_database_name(database);
    			if (database) set_current_database(database);
    
    			const result = await database_client.execute_query(
    				database_name,
    				query,
    				params,
    			);
    
    			const formatted_result = format_query_result(result);
    			return create_tool_response({
    				database: database_name,
    				query,
    				result: formatted_result,
    			});
    		} catch (error) {
    			return create_tool_error_response(error);
    		}
    	},
    );
  • Zod schema for the execute_read_only_query tool inputs: query (required), params (optional), database (optional).
    const ReadOnlyQuerySchema = z.object({
    	query: z.string().describe('Read-only SQL query to execute (SELECT, PRAGMA, EXPLAIN only)'),
    	params: z.record(z.string(), z.any()).optional().describe('Query parameters (optional) - Use parameterized queries for security'),
    	database: z.string().optional().describe('Database name (optional, uses context if not provided) - Specify target database'),
    });
  • The register_tools function registers all tools (including execute_read_only_query) with the MCP server via server.tool().
    export function register_tools(server: McpServer<any>): void {
  • Helper function execute_query that runs the SQL query against the Turso database client. Uses read-only or full-access permission based on query type.
    export async function execute_query(
    	database_name: string,
    	query: string,
    	params: Record<string, any> = {},
    ): Promise<ResultSet> {
    	try {
    		// Determine if this is a read-only query
    		const is_read_only = query
    			.trim()
    			.toLowerCase()
    			.startsWith('select');
    		const permission = is_read_only ? 'read-only' : 'full-access';
    
    		const client = await get_database_client(
    			database_name,
    			permission,
    		);
    
    		// Execute the query
    		return await client.execute({
    			sql: query,
    			args: convert_parameters(params),
    		});
    	} catch (error) {
    		throw new TursoApiError(
    			`Failed to execute query for database ${database_name}: ${
    				(error as Error).message
    			}`,
    			500,
    		);
    	}
    }
  • Helper function that formats query results for display, handling BigInt serialization.
    function format_query_result(result: ResultSet): any {
    	// Convert BigInt to string to avoid serialization issues
    	const lastInsertRowid =
    		result.lastInsertRowid !== null &&
    		typeof result.lastInsertRowid === 'bigint'
    			? result.lastInsertRowid.toString()
    			: result.lastInsertRowid;
    
    	return {
    		rows: result.rows,
    		rowsAffected: result.rowsAffected,
    		lastInsertRowid: lastInsertRowid,
    		columns: result.columns,
    	};
    }
Behavior4/5

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

The description discloses the key behavioral trait of rejecting write operations, which is crucial for safety. No annotations are present, so the description carries the full burden. It does not cover error handling or performance.

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

Conciseness4/5

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

The description is a single sentence, identical to the title, which is very concise. However, the repetition could be better utilized to add extra detail.

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

Completeness4/5

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

For a simple read-only query tool, the description adequately covers purpose and safety. Absence of output schema or annotation is mitigated by the clear statement of behavior, though error handling could be mentioned.

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

Parameters3/5

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

Schema coverage is 100% with detailed parameter descriptions. The tool description adds no additional meaning beyond the schema, so baseline 3 is appropriate.

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

Purpose2/5

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

Tautological: description restates name/title.

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

Usage Guidelines4/5

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

The description provides clear guidance to use this tool for read-only queries and highlights its automatic rejection of writes, but does not explicitly mention alternative tools or when not to use it.

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

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/spences10/mcp-turso-cloud'

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