Translate NL to SQL
translate_nl_to_sqlTranslates natural language questions into safe, read-only PostgreSQL queries, with optional execution against your database.
Instructions
Translate a natural-language question into a read-only PostgreSQL query against schema. The LLM provider (anthropic / openai / gemini / deepseek / qwen / openrouter / perplexity, plus any operator-declared custom OpenAI-compatible provider) sees a compact brief of the schema (tables, columns, foreign keys) and is instructed to return JSON with sql and explanation. When execute=true, the generated SQL goes through the SAME safety allowlist as run_select before running — writes / DDL / multi-statement input are rejected even if the model produced them. Returns the SQL, model rationale, and (when executed) rows / columns / row_count. table_filter narrows the brief to a known subset when the question is clearly scoped. provider, when supplied, selects which configured LLM provider to call (use this to route between the configured vendors per-call when multiple are configured); when omitted, MCPg uses the default (MCPG_NL2SQL_PROVIDER, otherwise the first available in preference order anthropic → openai → gemini → deepseek → qwen → openrouter → perplexity). Call get_server_info to see which providers are configured.
Example: translate_nl_to_sql(question='top 10 customers by revenue last month', schema='public', execute=true)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| schema | Yes | ||
| execute | No | ||
| database | No | Optional: target a configured secondary (read-only) database by name; omit for the primary. Call list_databases to see the configured ids. | |
| max_rows | No | ||
| provider | No | ||
| question | Yes | ||
| table_filter | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sql | Yes | ||
| rows | Yes | ||
| error | Yes | ||
| model | Yes | ||
| columns | Yes | ||
| executed | Yes | ||
| provider | Yes | ||
| row_count | Yes | ||
| explanation | Yes |