-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathask.rs
More file actions
201 lines (187 loc) · 7.69 KB
/
Copy pathask.rs
File metadata and controls
201 lines (187 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! `ask` — natural-language → SQL via `sqlrite::ask::ask_with_database`.
//!
//! This is Phase 7g.8 living inside Phase 7h. Gated behind the crate's
//! `ask` cargo feature: the `tools/list` registry omits it when the
//! feature is off, and the dispatcher's `match` arm is `#[cfg]`-out.
//!
//! Configuration follows the same three-layer precedence the other
//! SDK adapters use: per-call args > `AskConfig::from_env()` > defaults.
//! Per-call overrides are read from the tool's `arguments` (`model`,
//! `max_tokens`, `cache_ttl`); env vars supply the rest (most importantly
//! `SQLRITE_LLM_API_KEY`, which the MCP client must export to the spawned
//! `sqlrite-mcp` process).
//!
//! Optional `execute: true` argument: after generating SQL, runs it
//! through `query` (if SELECT) or `execute` (if DML/DDL — and only
//! when not `--read-only`) and inlines the rows or status. Defaults
//! to `false` because the MCP client typically has its own loop and
//! prefers to call `query`/`execute` itself.
use serde::Deserialize;
use serde_json::{Value, json};
use sqlrite::ask::{AskConfig, AskResponse, CacheTtl, ask_with_database};
use crate::error::ToolError;
use crate::protocol::ServerState;
use crate::tools::{TOOL_OUTPUT_CAP_BYTES, value_to_json};
pub fn metadata() -> Value {
json!({
"name": "ask",
"description": "Generate SQL from a natural-language question, grounded in this \
database's schema. Returns the generated SQL plus the model's \
one-sentence explanation. Optionally executes the SQL in the \
same call (`execute: true`); otherwise the caller decides what \
to do with the SQL — typically reviewing it before passing it \
to the `query` or `execute` tool. Requires `SQLRITE_LLM_API_KEY` \
in the server process's environment.",
"inputSchema": {
"type": "object",
"properties": {
"question": {
"type": "string",
"description": "Natural-language question (e.g. \"How many users are over 30?\").",
},
"execute": {
"type": "boolean",
"description": "If true, the generated SQL is also executed against this \
database and the rows (for SELECT) or status string (for \
DML/DDL) is included in the response. Default: false.",
},
"model": {
"type": "string",
"description": "Override the LLM model (default: claude-sonnet-4-6).",
},
"max_tokens": {
"type": "integer",
"description": "Override max output tokens (default: 1024).",
"minimum": 1,
},
"cache_ttl": {
"type": "string",
"enum": ["5m", "1h", "off"],
"description": "Override Anthropic prompt-cache TTL on the schema block (default: 5m).",
},
},
"required": ["question"],
"additionalProperties": false,
}
})
}
#[derive(Deserialize)]
struct Args {
question: String,
#[serde(default)]
execute: bool,
#[serde(default)]
model: Option<String>,
#[serde(default)]
max_tokens: Option<u32>,
#[serde(default)]
cache_ttl: Option<String>,
}
pub fn handle(args: Value, state: &mut ServerState) -> Result<String, ToolError> {
let args: Args = serde_json::from_value(args)
.map_err(|e| ToolError::new(format!("invalid arguments: {e}")))?;
if args.question.trim().is_empty() {
return Err(ToolError::new("question must not be empty".to_string()));
}
// Build the config: env-derived base, then per-call overrides.
let mut cfg = AskConfig::from_env().map_err(|e| {
ToolError::new(format!(
"ask config: {e} (set SQLRITE_LLM_API_KEY in the environment of the \
spawned `sqlrite-mcp` process — typically via the MCP client's server \
config)"
))
})?;
if let Some(m) = args.model {
cfg.model = m;
}
if let Some(mt) = args.max_tokens {
cfg.max_tokens = mt;
}
if let Some(ttl) = args.cache_ttl {
cfg.cache_ttl = match ttl.as_str() {
"5m" => CacheTtl::FiveMinutes,
"1h" => CacheTtl::OneHour,
"off" => CacheTtl::Off,
other => {
return Err(ToolError::new(format!(
"invalid cache_ttl `{other}`. Use `5m`, `1h`, or `off`."
)));
}
};
}
// Run the LLM call. Hold the database guard across the call but
// drop it before any later `state.conn.execute` / `prepare` —
// those re-acquire the same lock.
let resp: AskResponse = {
let db = state.conn.database();
ask_with_database(&db, &args.question, &cfg)
.map_err(|e| ToolError::new(format!("ask failed: {e}")))?
};
let mut result = json!({
"sql": resp.sql,
"explanation": resp.explanation,
"usage": {
"input_tokens": resp.usage.input_tokens,
"output_tokens": resp.usage.output_tokens,
"cache_creation_input_tokens": resp.usage.cache_creation_input_tokens,
"cache_read_input_tokens": resp.usage.cache_read_input_tokens,
},
"executed": false,
});
// Optional inline execution. Only kicks in if (a) caller asked
// for it, (b) we got non-empty SQL back, (c) for DML/DDL, the
// server isn't read-only.
if args.execute && !resp.sql.trim().is_empty() {
let trimmed = resp.sql.trim_start().to_ascii_lowercase();
let is_select = trimmed.starts_with("select");
if is_select {
let exec_result = run_inline_select(&resp.sql, state);
match exec_result {
Ok(rows) => {
result["executed"] = json!(true);
result["rows"] = rows;
}
Err(e) => {
result["execute_error"] = json!(e.0);
}
}
} else if state.read_only {
result["execute_error"] = json!(
"generated SQL is non-SELECT; not executed because server is in --read-only mode"
);
} else {
match state.conn.execute(&resp.sql) {
Ok(status) => {
result["executed"] = json!(true);
result["status"] = json!(status);
}
Err(e) => {
result["execute_error"] = json!(e.to_string());
}
}
}
}
serde_json::to_string_pretty(&result)
.map_err(|e| ToolError::new(format!("internal: failed to serialize ask response: {e}")))
}
fn run_inline_select(sql: &str, state: &mut ServerState) -> Result<Value, ToolError> {
let stmt = state.conn.prepare(sql)?;
let mut rows = stmt.query()?;
let columns = rows.columns().to_vec();
let mut out: Vec<Value> = Vec::new();
let mut size_estimate = 0;
while let Some(row) = rows.next()? {
let mut obj = serde_json::Map::with_capacity(columns.len());
for (i, col) in columns.iter().enumerate() {
let v: sqlrite::Value = row.get(i)?;
let json_val = value_to_json(&v);
size_estimate += col.len() + 8 + json_val.to_string().len();
obj.insert(col.clone(), json_val);
}
if size_estimate > TOOL_OUTPUT_CAP_BYTES {
break;
}
out.push(Value::Object(obj));
}
Ok(Value::Array(out))
}