execute_sql
Execute custom SQL queries to analyze bakery sales data, retrieve transaction records, and access product catalog information from the database.
Instructions
Execute a custom SQL query on the database. Use with caution. Read-only queries recommended.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | SQL query to execute. | |
| params | No | Parameters for the SQL query (for parameterized queries). Optional. |
Implementation Reference
- src/bakery_data_mcp/server.py:517-536 (handler)The handler function for the 'execute_sql' tool. It takes a SQL query and optional parameters from the arguments, executes it on the database cursor, and returns results for SELECT queries or a success message with row count for modifications.elif name == "execute_sql": query = arguments["query"] params = arguments.get("params", []) cursor.execute(query, params) # Check if this is a SELECT query if query.strip().upper().startswith("SELECT"): results = cursor.fetchall() return [TextContent( type="text", text=json.dumps(results, ensure_ascii=False, indent=2) )] else: conn.commit() return [TextContent( type="text", text=f"Query executed successfully. Rows affected: {cursor.rowcount}" )]
- src/bakery_data_mcp/server.py:204-224 (registration)The registration of the 'execute_sql' tool in the list_tools() function, including its name, description, and input schema definition for the query and optional parameters.Tool( name="execute_sql", description="Execute a custom SQL query on the database. Use with caution. Read-only queries recommended.", inputSchema={ "type": "object", "properties": { "query": { "type": "string", "description": "SQL query to execute." }, "params": { "type": "array", "description": "Parameters for the SQL query (for parameterized queries). Optional.", "items": { "type": ["string", "number", "null"] } } }, "required": ["query"] } ),
- The input schema for the 'execute_sql' tool, defining the expected arguments: a required 'query' string and optional 'params' array.inputSchema={ "type": "object", "properties": { "query": { "type": "string", "description": "SQL query to execute." }, "params": { "type": "array", "description": "Parameters for the SQL query (for parameterized queries). Optional.", "items": { "type": ["string", "number", "null"] } } }, "required": ["query"] }