BREP_mcp_core 0.11.12

The BREP MCP server core: the Model Context Protocol surface generated from the CAD app's own registries, its sessions, script runner and transports (stdio and streamable HTTP). Embedded in the app behind `brep-app --mcp`; hosted headlessly by BREP_mcp.
Documentation
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! The MCP front door (`run_script` and its five companions) and the one
//! dispatcher every `cad.*` method goes through — a code-mode program's
//! bridge and the app's own editor alike.
//!
//! The transport admits a request only with the loopback credential (D3,
//! `http::Credential`; stdio is its launching process's own), so every
//! admitted client is the trusted local caller ([`caller_of_transport`]).
use super::ops::Caller;
use super::wire::{self, ApiError};
use super::CadService;
use crate::tools::{Annotations, ToolOutput, ToolSpec};
use serde_json::{json, Value};
use std::sync::Arc;

/// Who an admitted MCP request is: the local user, whose token the HTTP
/// transport checked (`http::Credential`) or whose process owns stdio.
pub fn caller_of_transport() -> Caller {
    Caller::local()
}

fn string<'a>(req: &'a Value, key: &str) -> Result<&'a str, ApiError> {
    req.get(key).and_then(Value::as_str).ok_or_else(|| ApiError::new("invalid_arguments", "validation", format!("{key} is required")))
}

fn only(req: &Value, allowed: &[&str]) -> Result<(), ApiError> {
    let obj = req.as_object().ok_or_else(|| ApiError::new("invalid_arguments", "validation", "the request must be an object"))?;
    if let Some(k) = obj.keys().find(|k| !allowed.contains(&k.as_str())) {
        let takes = if allowed.is_empty() { "it takes no fields".to_string() } else { format!("it takes {{{}}}", allowed.join(", ")) };
        return Err(ApiError::new("invalid_arguments", "validation", format!("unknown field `{k}`; {takes}")));
    }
    Ok(())
}

/// A call whose request has one obvious field, made with that field's value
/// alone (`search("fillet")`, `read("design-part")`): the request it means.
/// The first call an agent writes is the positional one; refusing it bought
/// a failed run and a retry, never a different program.
fn positional(req: &Value, field: &str) -> Value {
    match req {
        Value::String(_) => json!({ field: req }),
        _ => req.clone(),
    }
}

/// `describe`'s paths: `paths` (an array), or one path or an array of them
/// under the names agents reach for first (`path`, `name`, `method`, `tool`),
/// or the path or paths alone (`describe("brep.features.feature_add")`).
fn describe_paths(req: &Value) -> Result<Vec<String>, ApiError> {
    const NAMES: &[&str] = &["paths", "path", "name", "names", "method", "tool"];
    let req = match req {
        Value::String(_) | Value::Array(_) => &json!({ "paths": req }),
        _ => req,
    };
    let obj = req.as_object().ok_or_else(|| ApiError::new("invalid_arguments", "validation", "describe takes {paths: string[]} or a path, e.g. cad.discovery.describe(\"brep.features.feature_add\")"))?;
    if let Some(k) = obj.keys().find(|k| !NAMES.contains(&k.as_str())) {
        return Err(ApiError::new("invalid_arguments", "validation", format!("unknown field `{k}`; describe takes {{paths: string[]}}, e.g. cad.discovery.describe({{paths: [\"brep.features.feature_add\"]}})")));
    }
    let given: Vec<&Value> = NAMES.iter().filter_map(|k| obj.get(*k)).collect();
    let [value] = given[..] else {
        return Err(ApiError::new("invalid_arguments", "validation", "describe takes {paths: string[]}, e.g. cad.discovery.describe({paths: [\"brep.features.feature_add\"]})"));
    };
    let items: Vec<&Value> = match value { Value::Array(a) => a.iter().collect(), v => vec![v] };
    items.into_iter()
        .map(|p| p.as_str().map(str::to_string).ok_or_else(|| ApiError::new("invalid_arguments", "validation", "paths holds strings: exact paths such as brep.features.feature_add or cad.sketch.create")))
        .collect()
}

/// The `brep://` references the guides cite, readable from a script with
/// `cad.docs.read(uri)` (a client that lists no MCP resources has no other
/// way to them); `{type}`, `{group}` and `{path}` take a feature type, a
/// script group and a function path.
const REFERENCES: &[(&str, &str)] = &[
    ("brep://schema/sketch", "the sketch persistentData schema (points, geometries, constraints) with a working example"),
    ("brep://schema/features/{type}", "one feature's inputParams JSON Schema and defaults, e.g. brep://schema/features/E"),
    ("brep://schema/features", "every feature's inputParams schema (large; prefer one type)"),
    ("brep://api/group/{group}", "one script group's functions with argument and result schemas"),
    ("brep://api/function/{path}", "one function's contract, e.g. brep://api/function/brep.features.feature_add"),
    ("brep://api/commands", "every application function (large; prefer one group)"),
    ("brep://cad/sdk.d.ts", "TypeScript declarations of the cad client"),
];

/// The one field among `names` a request gives, as a string.
fn one_of<'a>(req: &'a Value, names: &[&str], usage: &str) -> Result<&'a str, ApiError> {
    let obj = req.as_object().ok_or_else(|| ApiError::new("invalid_arguments", "validation", usage))?;
    if let Some(k) = obj.keys().find(|k| !names.contains(&k.as_str())) {
        return Err(ApiError::new("invalid_arguments", "validation", format!("unknown field `{k}`; {usage}")));
    }
    let given: Vec<&Value> = names.iter().filter_map(|k| obj.get(*k)).collect();
    match given[..] {
        [Value::String(s)] => Ok(s),
        _ => Err(ApiError::new("invalid_arguments", "validation", usage)),
    }
}

/// A guide name as agents write it: `Design Part`, `design_part.md`,
/// `skill://design-part/SKILL.md` all name `design-part`.
fn guide_key(name: &str) -> String {
    let name = name.trim();
    let name = name.strip_prefix("skill://").unwrap_or(name);
    let name = name.strip_suffix("/SKILL.md").or_else(|| name.strip_suffix(".md")).unwrap_or(name);
    name.to_ascii_lowercase().replace([' ', '_'], "-")
}

/// `cad.docs.read`: a guide by name, or one of the [`REFERENCES`] (with or
/// without its `brep://`).
async fn read_doc(service: &CadService, name: &str) -> Result<Value, ApiError> {
    let key = guide_key(name);
    if let Some(skill) = crate::skills::SKILLS.iter().find(|s| s.name == key) {
        return Ok(json!({ "name": skill.name, "text": skill.text }));
    }
    let uri = if name.starts_with("brep://") { name.to_string() } else if name.contains('/') { format!("brep://{}", name.trim_start_matches('/')) } else { String::new() };
    let readable = REFERENCES.iter().any(|(r, _)| match r.split_once('{') { Some((prefix, _)) => uri.starts_with(prefix) && uri.len() > prefix.len(), None => uri == *r });
    if readable {
        if let Some(text) = service.reference_text(&uri).await? {
            return Ok(json!({ "name": uri, "text": text }));
        }
    }
    let guides: Vec<&str> = crate::skills::SKILLS.iter().map(|s| s.name).collect();
    let references: Vec<&str> = REFERENCES.iter().map(|(r, _)| *r).collect();
    Err(ApiError::new("invalid_arguments", "lookup", format!("no guide or reference `{name}`; the guides are {}; the references are {}", guides.join(", "), references.join(", "))))
}

/// Every logical service method, by its exact name.
pub async fn call_service(service: &CadService, caller: &Caller, method: &str, req: &Value) -> Result<Value, ApiError> {
    if req.to_string().len() > 1 << 20 {
        return Err(ApiError::new("resource_limit", "validation", "the request is larger than 1 MiB"));
    }
    match method {
        "cad.inputs.exportOptions" => service.code.export_options(caller, req).await,
        "cad.discovery.search" => {
            caller.require(&["catalogue.read"])?;
            let req = &positional(req, "query");
            only(req, &["query", "limit", "cursor"])?;
            service.search(req).await
        }
        "cad.discovery.describe" => {
            caller.require(&["catalogue.read"])?;
            let paths = describe_paths(req)?;
            service.describe(&paths).await
        }
        // The guides and declarations, for a client that cannot read MCP
        // resources: tools-only runs never saw skill://design-part, and
        // guessed the API one failed call at a time.
        "cad.docs.list" => {
            only(req, &[])?;
            let guides = crate::skills::SKILLS.iter().map(|s| json!({ "name": s.name, "description": s.description }));
            let references = REFERENCES.iter().map(|(uri, what)| json!({ "name": uri, "description": what }));
            Ok(json!({ "guides": guides.chain(references).collect::<Vec<_>>() }))
        }
        "cad.docs.read" => {
            let req = &positional(req, "name");
            let name = one_of(req, &["name", "uri", "guide", "path"], "read takes a guide name or a brep:// reference, e.g. cad.docs.read(\"design-part\") or cad.docs.read(\"brep://schema/sketch\")")?;
            read_doc(service, name).await
        }
        "cad.docs.sdk" => {
            let req = &positional(req, "group");
            only(req, &["group"])?;
            let group = req.get("group").and_then(Value::as_str).map(|g| g.trim_start_matches("brep.").trim_end_matches(".*").trim_end_matches('.'));
            service.sdk_declarations(group).await
        }
        "cad.documents.list" => {
            only(req, &[])?;
            service.ops.documents(caller, None).await
        }
        "cad.documents.get" => {
            only(req, &["documentId"])?;
            service.ops.documents(caller, Some(string(req, "documentId")?)).await
        }
        "cad.operations.issueKey" => service.ops.issue_key(caller, req),
        "cad.operations.submit" => service.ops.submit(caller, req),
        "cad.operations.status" => service.ops.status(caller, req),
        "cad.operations.result" => service.ops.result(caller, req),
        "cad.operations.cancel" => service.ops.cancel(caller, req),
        "cad.commands.query" => service.ops.query(caller, req).await,
        "cad.code.submit" => service.code.submit(caller, req),
        "cad.code.status" => service.code.status(caller, req),
        "cad.code.result" => service.code.result(caller, req),
        "cad.code.cancel" => service.code.cancel(caller, req),
        "cad.artifacts.read" => {
            caller.require(&["artifact.read"])?;
            only(req, &["artifactId", "offset", "limit"])?;
            let offset = req.get("offset").map_or(Some(0), Value::as_u64).ok_or_else(|| ApiError::new("invalid_arguments", "validation", "offset must be a non-negative integer"))?;
            let limit = match req.get("limit") {
                None => None,
                Some(v) => Some(v.as_u64().ok_or_else(|| ApiError::new("invalid_arguments", "validation", "limit must be a positive integer"))? as usize),
            };
            service.artifacts.read(&caller.owner(), string(req, "artifactId")?, offset as usize, limit)
        }
        "cad.artifacts.create" => {
            caller.require(&["artifact.write"])?;
            only(req, &["mediaType", "bytesBase64", "path"])?;
            if string(req, "mediaType")? != "model/step" {
                return Err(ApiError::new("invalid_arguments", "validation", "mediaType must be model/step"));
            }
            // Either the bytes, or (for the local user's run_script) a file
            // on the host: the way a large input reaches a program.
            let bytes = match (req.get("bytesBase64"), req.get("path").and_then(Value::as_str)) {
                (None, Some(path)) => {
                    caller.require(&["host.files.read"])?;
                    let len = std::fs::metadata(path).map_err(|e| ApiError::new("invalid_arguments", "external_io", format!("{path}: {e}")))?.len();
                    if len > super::artifacts::MAX_ARTIFACT as u64 {
                        return Err(ApiError::new("resource_limit", "external_io", format!("{path} is larger than {} bytes", super::artifacts::MAX_ARTIFACT)));
                    }
                    std::fs::read(path).map_err(|e| ApiError::new("invalid_arguments", "external_io", format!("{path}: {e}")))?
                }
                (Some(_), None) => {
                    use base64::Engine as _;
                    base64::engine::general_purpose::STANDARD
                        .decode(string(req, "bytesBase64")?)
                        .map_err(|e| ApiError::new("invalid_arguments", "validation", format!("bytesBase64: {e}")))?
                }
                _ => return Err(ApiError::new("invalid_arguments", "validation", "give exactly one of bytesBase64 and path")),
            };
            service.artifacts.create(caller.owner(), "model/step", bytes)
        }
        // The streaming route for an input above the 1 MiB transport limit
        // (contract §12): begin with the length and digest, append chunks
        // in order, commit — the artifact exists only once it all matches.
        "cad.artifacts.begin" => {
            caller.require(&["artifact.write"])?;
            only(req, &["mediaType", "byteLength", "sha256"])?;
            if string(req, "mediaType")? != "model/step" {
                return Err(ApiError::new("invalid_arguments", "validation", "mediaType must be model/step"));
            }
            let length = req.get("byteLength").and_then(Value::as_u64).ok_or_else(|| ApiError::new("invalid_arguments", "validation", "byteLength is required"))?;
            service.artifacts.begin(caller.owner(), "model/step", length as usize, string(req, "sha256")?)
        }
        "cad.artifacts.append" => {
            caller.require(&["artifact.write"])?;
            only(req, &["uploadId", "offset", "bytesBase64"])?;
            let offset = req.get("offset").and_then(Value::as_u64).ok_or_else(|| ApiError::new("invalid_arguments", "validation", "offset is required"))?;
            use base64::Engine as _;
            let bytes = base64::engine::general_purpose::STANDARD
                .decode(string(req, "bytesBase64")?)
                .map_err(|e| ApiError::new("invalid_arguments", "validation", format!("bytesBase64: {e}")))?;
            service.artifacts.append(&caller.owner(), string(req, "uploadId")?, offset as usize, bytes)
        }
        "cad.artifacts.commit" => {
            caller.require(&["artifact.write"])?;
            only(req, &["uploadId"])?;
            service.artifacts.commit(&caller.owner(), string(req, "uploadId")?)
        }
        other => Err(ApiError::new("method_unknown", "lookup", format!("no method `{other}`"))),
    }
}

/// The MCP's front door: the only tools a client lists and calls.
/// Everything else the app can do is a script function,
/// `brep.<group>.<tool>(args)`, and the `cad.*` service is the script's
/// `cad` client.
pub const FRONT_DOOR: &[&str] = &["run_script", "script_result", "script_halt", "screenshot", "input", "files"];

/// Retry keys `run_script` remembers; past this many the map starts again
/// (an issued key expires within minutes anyway).
const RETRY_KEYS: usize = 4096;

fn retry_key(keys: &mut std::collections::HashMap<String, String>, caller: &Caller, key: &str) -> Result<String, String> {
    if let Some(issued) = keys.get(key) { return Ok(issued.clone()); }
    if keys.len() >= RETRY_KEYS { return Err("retry key capacity reached; existing keys remain valid; reconcile work before restarting".into()); }
    let issued = super::keys::issue(&caller.scope, super::keys::Purpose::Execution).0;
    keys.insert(key.into(), issued.clone());
    Ok(issued)
}

/// Await the result when no budget is supplied; an explicit budget returns
/// status if the execution is still running at its deadline.
async fn settle_execution(service: &CadService, caller: &Caller, id: &str, wait_ms: Option<u64>) -> Value {
    let deadline = wait_ms.map(|ms| std::time::Instant::now() + std::time::Duration::from_millis(ms));
    loop {
        match service.code.result(caller, &json!({ "executionId": id })) {
            Ok(result) => return wire::ok(result),
            Err(e) if e.code == "not_ready" && deadline.is_none_or(|d| std::time::Instant::now() < d) => {
                tokio::time::sleep(std::time::Duration::from_millis(25)).await;
            }
            Err(e) if e.code == "not_ready" => {
                return wire::respond(service.code.status(caller, &json!({ "executionId": id })).map(|s| json!({ "running": true, "status": s })));
            }
            Err(e) => return wire::fail(&e),
        }
    }
}

/// Start the headless session if none is live, and wait for the app's tools
/// to register (the session's registry is read asynchronously).
pub(super) async fn ensure_session(service: &CadService, slot: &crate::tools::app::SessionSlot) -> Result<(), String> {
    let Some(state) = service.server.get().and_then(std::sync::Weak::upgrade) else { return Ok(()) };
    let _starting = state.session_start.lock().await;
    if slot.read().await.is_some() { return Ok(()); }
    let start = state.tools.read().await.get("session_start").cloned();
    if let Some(start) = start {
        (start.handler)(json!({ "backend": "headless", "seed": true, "record": false })).await?;
        crate::server::BrepServer { state: state.clone() }.rebuild_tools().await;
    }
    Ok(())
}

/// The front door (group `script`): `run_script`, `script_result`,
/// `script_halt`, `screenshot` (the capture tool, a session started first)
/// `input` and `files`.
pub fn front_door_tools(service: Arc<CadService>, slot: crate::tools::app::SessionSlot) -> Vec<ToolSpec> {
    let screenshot = crate::tools::compose::capture_tools(slot.clone()).into_iter().find(|t| t.name == "screenshot").expect("capture registry includes screenshot");
    let wait = json!({ "type": "integer", "minimum": 0, "maximum": 600000, "description": "Optional wait budget in ms. run_script awaits completion when omitted; script_result defaults to 0. An explicit budget returns status if work is still running." });
    let run = service.clone();
    let result = service.clone();
    let halt = service.clone();
    let input_slot = slot.clone();
    let shot = service.clone();
    let shot_slot = slot.clone();
    let files_service = service.clone();
    let mut tools = vec![
        ToolSpec::new(
            "files", "script", "Manage files under named roots: list/stat/read/write/mkdir/copy/move/delete. Paths are relative. Writes/copies/moves refuse replacement unless overwrite:true; directory copy/delete requires recursive:true. documents/workspace use the app store; artifacts uses the execution area (direct calls can address execution-id subdirectories); BREP_MCP_FILE_ROOTS configures extra native roots. Read/write transfer limit256KiB, page limit256, directory/tree limit1024 entries. This aggregate tool can perform destructive operations; list/stat/read do not modify files.",
            crate::files::input_schema(), Annotations::DESTRUCTIVE,
            move |args| {
                let service = files_service.clone();
                Box::pin(async move { service.files.call(&service, &Caller::local(), args).await })
            },
        ),
        ToolSpec::new(
            "screenshot",
            "script",
            "A PNG of the app as it is now (region: full | viewport | {x,y,w,h}, egui points). Starts a headless session if none is live.",
            screenshot.input_schema,
            Annotations { read_only: false, destructive: true, idempotent: false, waits: false },
            move |args| {
                let service = shot.clone();
                let slot = shot_slot.clone();
                Box::pin(async move {
                    ensure_session(&service, &slot).await?;
                    let state = service.server.get().and_then(std::sync::Weak::upgrade).ok_or("the server is stopping")?;
                    let capture = state.tools.read().await.specs.iter().find(|t| t.name == "screenshot" && t.group == "capture").cloned();
                    let capture = capture.ok_or("no capture tool: the session has no app")?;
                    (capture.handler)(args).await
                })
            },
        ),
        ToolSpec::new(
            "run_script",
            "script",
            "Run a JavaScript (or TypeScript) program on the CAD app: `export default async function main()` returning JSON. Every app tool is a script function, `await brep.<group>.<tool>(args)`, beside the `cad` client of `@brep/cad`. Before modelling, return `await cad.docs.read('design-part')` once: it is the modelling guide; `cad.docs.sdk(group)` gives a group's typed signatures. Each function takes one object argument; pass `context` from docs_list to check the document revision. Put multi-line text in template literals (`...`): a quoted string cannot hold the line break a JSON \\n becomes. Awaits the completed result on this same tool call by default. An explicit wait_ms opts into a bounded wait/status response; retrying the same source and key recovers that same execution. script_result remains an optional recovery tool; script_halt cancels work. Starts a headless session if none is live.",
            json!({ "type": "object", "properties": { "source": { "type": "string", "minLength": 1, "maxLength": 262144, "description": "UTF-8 source, at most 256 KiB" }, "language": { "enum": ["javascript", "typescript"], "default": "javascript", "description": "TypeScript requires explicitly provisioned Node and TypeScript 7.0.2; tools/list reports this host's compiler availability. Strict checking precedes execution." }, "wait_ms": wait, "key": { "type": "string", "minLength": 1, "maxLength": 128, "description": "a retry key of your choosing: the same key with the same program answers the SAME execution (submitted once), so a run whose reply was lost is found again; the same key with another program is refused" } }, "required": ["source"], "additionalProperties": false }),
            Annotations { read_only: false, destructive: true, idempotent: false, waits: false },
            move |args| {
                let service = run.clone();
                let slot = slot.clone();
                Box::pin(async move {
                    let caller = caller_of_transport();
                    // A client's retry key stands for one issued key, so a
                    // resubmission binds to the execution it already started.
                    let key = match args.get("key").and_then(Value::as_str) {
                        Some(k) if k.is_empty() || k.len() > 128 => return Err("key must be 1 to 128 characters".into()),
                        Some(_) if service.code.persistent() => super::keys::issue(&caller.scope, super::keys::Purpose::Execution).0,
                        Some(k) => {
                            let mut keys = service.retry_keys.lock().unwrap();
                            retry_key(&mut keys, &caller, k)?
                        }
                        None => super::keys::issue(&caller.scope, super::keys::Purpose::Execution).0,
                    };
                    let req = json!({
                        "manifest": service.catalogue.identity(),
                        "language": args.get("language").and_then(Value::as_str).unwrap_or("javascript"),
                        "source": args.get("source").and_then(Value::as_str).unwrap_or_default(),
                        "initialDocuments": [],
                        "requestedCapabilities": [],
                        "idempotencyKey": key,
                    });
                    let retry = args.get("key").and_then(Value::as_str);
                    let replay = match service.code.replay(&caller, &req, true, retry) {
                        Ok(status) => status,
                        Err(e) => return Ok(ToolOutput::json(wire::fail(&e))),
                    };
                    let submitted = if let Some(status) = replay { Ok(status) } else {
                        ensure_session(&service, &slot).await?;
                        service.code.submit_with_retry(&caller, &req, true, retry)
                    };
                    let status = match submitted {
                        Ok(s) => s,
                        Err(e) => return Ok(ToolOutput::json(wire::fail(&e))),
                    };
                    let id = status["executionId"].as_str().unwrap_or_default().to_string();
                    let wait_ms = args.get("wait_ms").and_then(Value::as_u64).map(|ms| ms.min(600_000));
                    Ok(ToolOutput::json(settle_execution(&service, &caller, &id, wait_ms).await))
                })
            },
        ),
        ToolSpec::new(
            "script_result",
            "script",
            "A script's result once it has finished (waiting up to wait_ms, default 0), else its status.",
            json!({ "type": "object", "properties": { "executionId": { "type": "string" }, "wait_ms": wait }, "required": ["executionId"], "additionalProperties": false }),
            Annotations { read_only: true, destructive: false, idempotent: true, waits: false },
            move |args| {
                let service = result.clone();
                Box::pin(async move {
                    let caller = caller_of_transport();
                    let id = args.get("executionId").and_then(Value::as_str).unwrap_or_default().to_string();
                    let wait_ms = args.get("wait_ms").and_then(Value::as_u64).unwrap_or(0).min(600_000);
                    Ok(ToolOutput::json(settle_execution(&service, &caller, &id, Some(wait_ms)).await))
                })
            },
        ),
        ToolSpec::new(
            "script_halt",
            "script",
            "Halt a running script now: its worker is killed wherever it is. CAD operations it had already started run on (cancel one with its own cancel).",
            json!({ "type": "object", "properties": { "executionId": { "type": "string" } }, "required": ["executionId"], "additionalProperties": false }),
            Annotations { read_only: false, destructive: false, idempotent: true, waits: false },
            move |args| {
                let service = halt.clone();
                Box::pin(async move {
                    let caller = caller_of_transport();
                    Ok(ToolOutput::json(wire::respond(service.code.cancel(&caller, &args))))
                })
            },
        ),
        ToolSpec::new(
            "input",
            "script",
            "Inject mouse and keyboard input into the app: `steps`, each `{tool, args}` with tool one of the pointer/keyboard tools (click, drag, hotkey, type_text, key, text, pointer_move, pointer_down, pointer_up, wheel, click_entity, drag_entity, modifiers_set, paste, click_gap, pointer_gone), run in order. Answers each step's result; stops at the first that fails.",
            json!({ "type": "object", "properties": { "steps": { "type": "array", "maxItems": 256, "items": { "type": "object", "properties": { "tool": { "type": "string" }, "args": { "type": "object" } }, "required": ["tool"], "additionalProperties": false } } }, "required": ["steps"], "additionalProperties": false }),
            Annotations { read_only: false, destructive: true, idempotent: false, waits: false },
            move |args| {
                let service = service.clone();
                let _slot = input_slot.clone();
                Box::pin(async move {
                    let state = service.server.get().and_then(std::sync::Weak::upgrade).ok_or("the server is stopping")?;
                    let mut results = Vec::new();
                    for step in args.get("steps").and_then(Value::as_array).cloned().unwrap_or_default() {
                        let name = step.get("tool").and_then(Value::as_str).unwrap_or_default().to_string();
                        let spec = state.tools.read().await.get(&name).cloned();
                        let Some(spec) = spec.filter(|s| matches!(s.group, "pointer" | "keyboard")) else {
                            return Err(format!("`{name}` is not a pointer or keyboard tool"));
                        };
                        let out = (spec.handler)(step.get("args").cloned().unwrap_or(json!({}))).await.map_err(|e| format!("{name}: {e}"))?;
                        results.push(json!({ "tool": name, "result": out.json }));
                    }
                    Ok(ToolOutput::json(json!({ "steps": results })))
                })
            },
        ),
    ];
    for tool in &mut tools {
        match tool.name.as_str() {
            "run_script" | "script_result" => tool.output_schema = super::output_schema::execution(false),
            "script_halt" => tool.output_schema = super::output_schema::execution(true),
            "screenshot" => tool.output_schema = screenshot.output_schema.clone(),
            "files" => tool.output_schema = crate::files::output_schema(),
            "input" => tool.output_schema = crate::tools::object_schema(json!({
                "steps":{"type":"array","items":crate::tools::object_schema(json!({
                    "tool":{"type":"string"},
                    "result":{"type":"object","additionalProperties":true,"description":"The selected pointer/keyboard tool's JSON result; native envelope fields and composed results differ by tool."}
                }), &["tool","result"])}
            }), &["steps"]),
            _ => {},
        }
    }
    tools
}