anvilforge-boost 0.3.4

Boost — Anvilforge's AI-agent toolkit. Embedded MCP server with framework introspection tools (routes, models, components, schema, queries, logs).
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! Browser MCP tools — Playwright-equivalent surface for AI agents.
//!
//! All four tools accept a `url` and drive a shared headless Chromium instance
//! (see `crate::browser`). Pages are torn down after each tool call to avoid
//! cross-call state leaks. The browser itself is reused.

use async_trait::async_trait;
use base64::engine::general_purpose::STANDARD as B64;
use base64::Engine as _;
use chromiumoxide::cdp::browser_protocol::page::CaptureScreenshotFormat;
use chromiumoxide::handler::viewport::Viewport;
use chromiumoxide::page::ScreenshotParams;
use futures::StreamExt;
use serde_json::{json, Value};

use crate::browser::BrowserManager;
use crate::protocol::CallToolResult;
use crate::tool::{Context, Tool};

fn url_schema(extra_props: Value) -> Value {
    let mut props = serde_json::Map::new();
    props.insert(
        "url".into(),
        json!({ "type": "string", "description": "Absolute or relative URL to open." }),
    );
    if let Value::Object(obj) = extra_props {
        for (k, v) in obj {
            props.insert(k, v);
        }
    }
    json!({
        "type": "object",
        "required": ["url"],
        "properties": props,
    })
}

fn require_url(args: &Value) -> Result<String, CallToolResult> {
    match args.get("url").and_then(|v| v.as_str()) {
        Some(u) if !u.is_empty() => Ok(u.to_string()),
        _ => Err(CallToolResult::error("`url` is required")),
    }
}

// ─── browser-screenshot ────────────────────────────────────────────────────

pub struct BrowserScreenshot {
    pub manager: BrowserManager,
}

#[async_trait]
impl Tool for BrowserScreenshot {
    fn name(&self) -> &'static str {
        "browser-screenshot"
    }
    fn description(&self) -> &'static str {
        "Open a URL in a headless Chromium and return a PNG screenshot as base64. Optional `width`/`height` set the viewport; `full_page=true` captures the entire scrollable page."
    }
    fn input_schema(&self) -> Value {
        url_schema(json!({
            "width":     { "type": "integer", "default": 1280 },
            "height":    { "type": "integer", "default": 800 },
            "full_page": { "type": "boolean", "default": false }
        }))
    }

    async fn call(&self, _ctx: &Context, args: Value) -> CallToolResult {
        let url = match require_url(&args) {
            Ok(u) => u,
            Err(r) => return r,
        };
        let width = args.get("width").and_then(|v| v.as_u64()).unwrap_or(1280) as u32;
        let height = args.get("height").and_then(|v| v.as_u64()).unwrap_or(800) as u32;
        let full_page = args
            .get("full_page")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);

        let page = match self.manager.open(&url).await {
            Ok(p) => p,
            Err(e) => return CallToolResult::error(e),
        };
        // Viewport is set via CDP. The builder in chromiumoxide 0.7 returns
        // `Result<SetDeviceMetricsOverrideParams, _>`; unwrap-or-skip.
        use chromiumoxide::cdp::browser_protocol::emulation::SetDeviceMetricsOverrideParams;
        if let Ok(params) = SetDeviceMetricsOverrideParams::builder()
            .width(width as i64)
            .height(height as i64)
            .device_scale_factor(1.0)
            .mobile(false)
            .build()
        {
            let _ = page.execute(params).await;
        }
        let _ = Viewport::default(); // suppress unused import

        let mut params = ScreenshotParams::builder().format(CaptureScreenshotFormat::Png);
        if full_page {
            params = params.full_page(true);
        }
        let png = match page.screenshot(params.build()).await {
            Ok(b) => b,
            Err(e) => return CallToolResult::error(format!("screenshot: {e}")),
        };
        let _ = page.close().await;

        let encoded = B64.encode(&png);
        CallToolResult::json(&json!({
            "url": url,
            "bytes": png.len(),
            "width": width,
            "height": height,
            "full_page": full_page,
            "format": "png",
            "base64": encoded,
        }))
    }
}

// ─── browser-console ────────────────────────────────────────────────────────

pub struct BrowserConsole {
    pub manager: BrowserManager,
}

#[async_trait]
impl Tool for BrowserConsole {
    fn name(&self) -> &'static str {
        "browser-console"
    }
    fn description(&self) -> &'static str {
        "Open a URL and collect console messages emitted by the page. Returns level + text for each entry. Useful for spotting JS errors after a Spark interaction."
    }
    fn input_schema(&self) -> Value {
        url_schema(json!({
            "wait_ms": { "type": "integer", "default": 500, "description": "How long to listen after load before reporting." }
        }))
    }

    async fn call(&self, _ctx: &Context, args: Value) -> CallToolResult {
        let url = match require_url(&args) {
            Ok(u) => u,
            Err(r) => return r,
        };
        let wait_ms = args.get("wait_ms").and_then(|v| v.as_u64()).unwrap_or(500);

        use chromiumoxide::cdp::browser_protocol::log::EventEntryAdded;
        use chromiumoxide::cdp::js_protocol::runtime::EventConsoleApiCalled;

        let page = match self.manager.open(&url).await {
            Ok(p) => p,
            Err(e) => return CallToolResult::error(e),
        };

        let mut console_events = match page.event_listener::<EventConsoleApiCalled>().await {
            Ok(s) => s,
            Err(e) => return CallToolResult::error(format!("event listener: {e}")),
        };
        let mut log_events = match page.event_listener::<EventEntryAdded>().await {
            Ok(s) => s,
            Err(e) => return CallToolResult::error(format!("event listener: {e}")),
        };

        let mut messages = Vec::<Value>::new();
        let deadline = std::time::Instant::now() + std::time::Duration::from_millis(wait_ms);
        loop {
            let timeout = deadline
                .checked_duration_since(std::time::Instant::now())
                .unwrap_or_default();
            if timeout.is_zero() {
                break;
            }
            tokio::select! {
                _ = tokio::time::sleep(timeout) => break,
                evt = console_events.next() => {
                    if let Some(evt) = evt {
                        let text = evt.args.iter().filter_map(|a| a.value.as_ref().map(|v| v.to_string())).collect::<Vec<_>>().join(" ");
                        messages.push(json!({
                            "kind": "console",
                            "level": format!("{:?}", evt.r#type),
                            "text": text,
                        }));
                    }
                }
                evt = log_events.next() => {
                    if let Some(evt) = evt {
                        messages.push(json!({
                            "kind": "log",
                            "level": format!("{:?}", evt.entry.level),
                            "text": evt.entry.text,
                            "source": format!("{:?}", evt.entry.source),
                            "url": evt.entry.url,
                        }));
                    }
                }
            }
        }

        let _ = page.close().await;

        CallToolResult::json(&json!({
            "url": url,
            "count": messages.len(),
            "messages": messages,
        }))
    }
}

// ─── browser-network ────────────────────────────────────────────────────────

pub struct BrowserNetwork {
    pub manager: BrowserManager,
}

#[async_trait]
impl Tool for BrowserNetwork {
    fn name(&self) -> &'static str {
        "browser-network"
    }
    fn description(&self) -> &'static str {
        "Open a URL and return the network requests the page made. Each entry has method, URL, resource type, and status (when available)."
    }
    fn input_schema(&self) -> Value {
        url_schema(json!({
            "wait_ms": { "type": "integer", "default": 1000 }
        }))
    }

    async fn call(&self, _ctx: &Context, args: Value) -> CallToolResult {
        let url = match require_url(&args) {
            Ok(u) => u,
            Err(r) => return r,
        };
        let wait_ms = args.get("wait_ms").and_then(|v| v.as_u64()).unwrap_or(1000);

        use chromiumoxide::cdp::browser_protocol::network::{
            EventRequestWillBeSent, EventResponseReceived,
        };

        let page = match self.manager.open(&url).await {
            Ok(p) => p,
            Err(e) => return CallToolResult::error(e),
        };

        let mut req_stream = match page.event_listener::<EventRequestWillBeSent>().await {
            Ok(s) => s,
            Err(e) => return CallToolResult::error(format!("event listener: {e}")),
        };
        let mut resp_stream = match page.event_listener::<EventResponseReceived>().await {
            Ok(s) => s,
            Err(e) => return CallToolResult::error(format!("event listener: {e}")),
        };

        let mut by_id: indexmap::IndexMap<String, serde_json::Map<String, Value>> =
            indexmap::IndexMap::new();
        let deadline = std::time::Instant::now() + std::time::Duration::from_millis(wait_ms);
        loop {
            let timeout = deadline
                .checked_duration_since(std::time::Instant::now())
                .unwrap_or_default();
            if timeout.is_zero() {
                break;
            }
            tokio::select! {
                _ = tokio::time::sleep(timeout) => break,
                evt = req_stream.next() => {
                    if let Some(evt) = evt {
                        let mut m = serde_json::Map::new();
                        m.insert("method".into(), json!(evt.request.method));
                        m.insert("url".into(), json!(evt.request.url));
                        m.insert("type".into(), json!(format!("{:?}", evt.r#type)));
                        by_id.entry(format!("{:?}", evt.request_id)).or_default().extend(m);
                    }
                }
                evt = resp_stream.next() => {
                    if let Some(evt) = evt {
                        let entry = by_id.entry(format!("{:?}", evt.request_id)).or_default();
                        entry.insert("status".into(), json!(evt.response.status));
                        entry.insert("status_text".into(), json!(evt.response.status_text));
                        entry.insert("mime_type".into(), json!(evt.response.mime_type));
                    }
                }
            }
        }
        let _ = page.close().await;

        let entries: Vec<Value> = by_id.into_iter().map(|(_, v)| Value::Object(v)).collect();
        CallToolResult::json(&json!({
            "url": url,
            "count": entries.len(),
            "requests": entries,
        }))
    }
}

// ─── browser-click ──────────────────────────────────────────────────────────

pub struct BrowserClick {
    pub manager: BrowserManager,
}

#[async_trait]
impl Tool for BrowserClick {
    fn name(&self) -> &'static str {
        "browser-click"
    }
    fn description(&self) -> &'static str {
        "Open a URL and click the first element matching a CSS selector. Returns the URL after the click (which may have navigated)."
    }
    fn input_schema(&self) -> Value {
        url_schema(json!({
            "selector": { "type": "string", "description": "CSS selector for the element to click." },
            "wait_ms":  { "type": "integer", "default": 500, "description": "How long to wait after click before reading the URL." }
        }))
    }

    async fn call(&self, _ctx: &Context, args: Value) -> CallToolResult {
        let url = match require_url(&args) {
            Ok(u) => u,
            Err(r) => return r,
        };
        let selector = match args.get("selector").and_then(|v| v.as_str()) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => return CallToolResult::error("`selector` is required"),
        };
        let wait_ms = args.get("wait_ms").and_then(|v| v.as_u64()).unwrap_or(500);

        let page = match self.manager.open(&url).await {
            Ok(p) => p,
            Err(e) => return CallToolResult::error(e),
        };
        let element = match page.find_element(&selector).await {
            Ok(el) => el,
            Err(e) => {
                let _ = page.close().await;
                return CallToolResult::error(format!("find_element({selector}): {e}"));
            }
        };
        if let Err(e) = element.click().await {
            let _ = page.close().await;
            return CallToolResult::error(format!("click({selector}): {e}"));
        }
        tokio::time::sleep(std::time::Duration::from_millis(wait_ms)).await;

        let new_url = page.url().await.ok().flatten().unwrap_or_default();
        let _ = page.close().await;

        CallToolResult::json(&json!({
            "url": url,
            "selector": selector,
            "current_url": new_url,
        }))
    }
}

// ─── browser-fill ───────────────────────────────────────────────────────────

pub struct BrowserFill {
    pub manager: BrowserManager,
}

#[async_trait]
impl Tool for BrowserFill {
    fn name(&self) -> &'static str {
        "browser-fill"
    }
    fn description(&self) -> &'static str {
        "Open a URL, locate a single input by CSS selector, replace its value, and optionally submit the enclosing form. Returns the final URL after submit."
    }
    fn input_schema(&self) -> Value {
        url_schema(json!({
            "selector": { "type": "string", "description": "CSS selector targeting the input/textarea." },
            "value":    { "type": "string", "description": "New value to set." },
            "submit":   { "type": "boolean", "default": false, "description": "If true, dispatch a form submit after filling." },
            "wait_ms":  { "type": "integer", "default": 500 }
        }))
    }

    async fn call(&self, _ctx: &Context, args: Value) -> CallToolResult {
        let url = match require_url(&args) {
            Ok(u) => u,
            Err(r) => return r,
        };
        let selector = match args.get("selector").and_then(|v| v.as_str()) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => return CallToolResult::error("`selector` is required"),
        };
        let value = args
            .get("value")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let submit = args
            .get("submit")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);
        let wait_ms = args.get("wait_ms").and_then(|v| v.as_u64()).unwrap_or(500);

        let page = match self.manager.open(&url).await {
            Ok(p) => p,
            Err(e) => return CallToolResult::error(e),
        };

        // Use JS evaluate to set value + fire the right events (works for
        // controlled inputs in React/Vue/Spark alike).
        let escaped = serde_json::to_string(&value).unwrap_or_else(|_| "\"\"".to_string());
        let selector_lit = serde_json::to_string(&selector).unwrap_or_default();
        let script = format!(
            "(function() {{ const el = document.querySelector({selector_lit}); if (!el) return 'not_found'; el.value = {escaped}; el.dispatchEvent(new Event('input', {{ bubbles: true }})); el.dispatchEvent(new Event('change', {{ bubbles: true }})); {} return 'ok'; }})()",
            if submit {
                "if (el.form) el.form.requestSubmit ? el.form.requestSubmit() : el.form.submit();"
            } else {
                ""
            }
        );
        let result = page.evaluate(script).await;
        tokio::time::sleep(std::time::Duration::from_millis(wait_ms)).await;
        let final_url = page.url().await.ok().flatten().unwrap_or_default();
        let _ = page.close().await;

        match result {
            Ok(v) => {
                let outcome = v.into_value().unwrap_or(Value::Null);
                if outcome.as_str() == Some("not_found") {
                    return CallToolResult::error(format!("selector `{selector}` not found"));
                }
                CallToolResult::json(&json!({
                    "url": url,
                    "selector": selector,
                    "submitted": submit,
                    "current_url": final_url,
                }))
            }
            Err(e) => CallToolResult::error(format!("fill: {e}")),
        }
    }
}

// ─── browser-type ───────────────────────────────────────────────────────────

pub struct BrowserType {
    pub manager: BrowserManager,
}

#[async_trait]
impl Tool for BrowserType {
    fn name(&self) -> &'static str {
        "browser-type"
    }
    fn description(&self) -> &'static str {
        "Open a URL, focus an element by selector, and type a string (character-by-character keypresses, useful for triggering keydown handlers)."
    }
    fn input_schema(&self) -> Value {
        url_schema(json!({
            "selector": { "type": "string", "description": "CSS selector for the element to focus." },
            "text":     { "type": "string", "description": "Text to type, one keypress per character." }
        }))
    }

    async fn call(&self, _ctx: &Context, args: Value) -> CallToolResult {
        let url = match require_url(&args) {
            Ok(u) => u,
            Err(r) => return r,
        };
        let selector = match args.get("selector").and_then(|v| v.as_str()) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => return CallToolResult::error("`selector` is required"),
        };
        let text = args
            .get("text")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        let page = match self.manager.open(&url).await {
            Ok(p) => p,
            Err(e) => return CallToolResult::error(e),
        };
        let element = match page.find_element(&selector).await {
            Ok(el) => el,
            Err(e) => {
                let _ = page.close().await;
                return CallToolResult::error(format!("find_element({selector}): {e}"));
            }
        };
        if let Err(e) = element.focus().await {
            let _ = page.close().await;
            return CallToolResult::error(format!("focus: {e}"));
        }
        if let Err(e) = element.type_str(&text).await {
            let _ = page.close().await;
            return CallToolResult::error(format!("type: {e}"));
        }
        let _ = page.close().await;

        CallToolResult::json(&json!({
            "url": url,
            "selector": selector,
            "chars_typed": text.chars().count(),
        }))
    }
}

// ─── browser-wait-for ───────────────────────────────────────────────────────

pub struct BrowserWaitFor {
    pub manager: BrowserManager,
}

#[async_trait]
impl Tool for BrowserWaitFor {
    fn name(&self) -> &'static str {
        "browser-wait-for"
    }
    fn description(&self) -> &'static str {
        "Open a URL and wait for an element matching a CSS selector to appear in the DOM (with a timeout). Useful for tests that need to wait out async data loads or Spark interactions."
    }
    fn input_schema(&self) -> Value {
        url_schema(json!({
            "selector":   { "type": "string", "description": "CSS selector to wait for." },
            "timeout_ms": { "type": "integer", "default": 5000, "description": "Max time to wait, in ms." },
            "poll_ms":    { "type": "integer", "default": 100,  "description": "Polling interval, in ms." }
        }))
    }

    async fn call(&self, _ctx: &Context, args: Value) -> CallToolResult {
        let url = match require_url(&args) {
            Ok(u) => u,
            Err(r) => return r,
        };
        let selector = match args.get("selector").and_then(|v| v.as_str()) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => return CallToolResult::error("`selector` is required"),
        };
        let timeout_ms = args
            .get("timeout_ms")
            .and_then(|v| v.as_u64())
            .unwrap_or(5000);
        let poll_ms = args.get("poll_ms").and_then(|v| v.as_u64()).unwrap_or(100);

        let page = match self.manager.open(&url).await {
            Ok(p) => p,
            Err(e) => return CallToolResult::error(e),
        };

        let deadline = std::time::Instant::now() + std::time::Duration::from_millis(timeout_ms);
        let mut found = false;
        let mut elapsed_ms: u128 = 0;
        while std::time::Instant::now() < deadline {
            if page.find_element(&selector).await.is_ok() {
                found = true;
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(poll_ms)).await;
            elapsed_ms += poll_ms as u128;
        }
        let _ = page.close().await;

        CallToolResult::json(&json!({
            "url": url,
            "selector": selector,
            "found": found,
            "elapsed_ms": elapsed_ms,
        }))
    }
}

// ─── browser-eval ───────────────────────────────────────────────────────────

pub struct BrowserEval {
    pub manager: BrowserManager,
}

#[async_trait]
impl Tool for BrowserEval {
    fn name(&self) -> &'static str {
        "browser-eval"
    }
    fn description(&self) -> &'static str {
        "Open a URL and evaluate a JavaScript expression in the page context. Returns the result as JSON (numbers, strings, booleans, null, or objects/arrays via JSON.stringify)."
    }
    fn input_schema(&self) -> Value {
        url_schema(json!({
            "script": { "type": "string", "description": "JS expression or statement(s). The final expression's value is returned." }
        }))
    }

    async fn call(&self, _ctx: &Context, args: Value) -> CallToolResult {
        let url = match require_url(&args) {
            Ok(u) => u,
            Err(r) => return r,
        };
        let script = match args.get("script").and_then(|v| v.as_str()) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => return CallToolResult::error("`script` is required"),
        };

        let page = match self.manager.open(&url).await {
            Ok(p) => p,
            Err(e) => return CallToolResult::error(e),
        };

        // Wrap in IIFE so we get the final expression value, and stringify any
        // object/array so it round-trips through JSON cleanly.
        let wrapped = format!(
            "(function() {{ try {{ const __r = (function(){{ {script} }})(); return typeof __r === 'object' ? JSON.stringify(__r) : __r; }} catch (e) {{ return 'ERROR: ' + e.message; }} }})()"
        );

        let result = page.evaluate(wrapped).await;
        let _ = page.close().await;

        match result {
            Ok(v) => {
                let raw = v.into_value().unwrap_or(serde_json::Value::Null);
                CallToolResult::json(&json!({
                    "url": url,
                    "value": raw,
                }))
            }
            Err(e) => CallToolResult::error(format!("eval: {e}")),
        }
    }
}