jev-seo 0.3.0

Free Rust SEO and GEO toolkit powered by TypeSafe Jev: 58-rule audits, site crawls, AI citation checks, rank drift, CI gates, 15-tool MCP. Zero cost.
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::io::{BufRead, Write};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct RpcRequest {
    pub(crate) jsonrpc: String,
    pub(crate) id: Option<serde_json::Value>,
    pub(crate) method: String,
    pub(crate) params: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct RpcResponse {
    pub(crate) jsonrpc: String,
    pub(crate) id: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) result: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) error: Option<serde_json::Value>,
}

/// How `seo_cite_check` borrows a model. Live servers use [`StdioSampler`]
/// (MCP `sampling/createMessage` against the connected client); tests and the
/// pure [`handle_request`] path use [`NoSampler`], which always declines.
pub(crate) trait Sampler {
    fn sample(&mut self, prompt: &str) -> std::result::Result<String, String>;
}

/// Sampler for contexts with no client attached: every sampling call fails
/// clearly instead of hanging.
pub(crate) struct NoSampler;

impl Sampler for NoSampler {
    fn sample(&mut self, _prompt: &str) -> std::result::Result<String, String> {
        Err("no sampling client attached; run jev-seo as an MCP server inside an agent that supports sampling".into())
    }
}

/// Live sampler: sends `sampling/createMessage` to the connected client over
/// the same stdio pair and waits for the matching response id. Notifications
/// are skipped; responses for unknown ids keep waiting instead of dropping.
pub(crate) struct StdioSampler<'a> {
    lines: std::io::Lines<std::io::StdinLock<'a>>,
    stdout: &'a mut std::io::Stdout,
    next_id: u64,
    /// Set from the client's `initialize` params: no declared capability,
    /// no request sent (some clients silently drop it, which would hang).
    sampling_allowed: bool,
}

impl<'a> StdioSampler<'a> {
    fn new(lines: std::io::Lines<std::io::StdinLock<'a>>, stdout: &'a mut std::io::Stdout) -> Self {
        Self {
            lines,
            stdout,
            next_id: 0,
            sampling_allowed: false,
        }
    }

    /// Next non-empty client line, or None on EOF/error.
    fn next_line(&mut self) -> Option<String> {
        loop {
            match self.lines.next()? {
                Ok(t) if t.trim().is_empty() => continue,
                Ok(t) => return Some(t),
                Err(_) => return None,
            }
        }
    }

    fn reply(&mut self, text: &str) -> Result<()> {
        writeln!(self.stdout, "{}", text)?;
        self.stdout.flush()?;
        Ok(())
    }
}

impl Sampler for StdioSampler<'_> {
    fn sample(&mut self, prompt: &str) -> std::result::Result<String, String> {
        if !self.sampling_allowed {
            return Err("client did not declare the sampling capability".into());
        }
        let id = format!("jev-seo-sample-{}", self.next_id);
        self.next_id += 1;
        let req = json!({
            "jsonrpc": "2.0",
            "id": id,
            "method": "sampling/createMessage",
            "params": {
                "messages": [{ "role": "user", "content": { "type": "text", "text": prompt } }],
                "maxTokens": 500,
            }
        });
        self.reply(&serde_json::to_string(&req).map_err(|e| e.to_string())?)
            .map_err(|e| e.to_string())?;

        loop {
            let text = match self.next_line() {
                Some(t) => t,
                None => return Err("client closed the stream during sampling".into()),
            };
            let msg: serde_json::Value = serde_json::from_str(&text)
                .map_err(|_| "client sent a non-JSON sampling reply".to_string())?;
            if msg.get("id") != Some(&json!(id)) {
                continue; // notification or unrelated response: keep waiting
            }
            if let Some(err) = msg.get("error") {
                let detail = err
                    .get("message")
                    .and_then(|m| m.as_str())
                    .unwrap_or("unknown error");
                return Err(format!("client declined sampling: {}", detail));
            }
            return sample_text(msg.get("result"))
                .ok_or_else(|| "client sent an unexpected sampling result shape".to_string());
        }
    }
}

/// Pull model text out of a `sampling/createMessage` result, accepting the
/// single-object and array content shapes.
fn sample_text(result: Option<&serde_json::Value>) -> Option<String> {
    let content = result?.get("content")?;
    let first = if let Some(arr) = content.as_array() {
        arr.first()?
    } else {
        content
    };
    if first.get("type")?.as_str()? != "text" {
        return None;
    }
    first.get("text")?.as_str().map(str::to_string)
}

pub fn run_stdio_server() -> Result<()> {
    let stdin = std::io::stdin();
    let mut stdout = std::io::stdout();
    let mut sampler = StdioSampler::new(stdin.lock().lines(), &mut stdout);

    while let Some(text) = sampler.next_line() {
        let req: RpcRequest = match serde_json::from_str(&text) {
            Ok(r) => r,
            Err(_) => {
                let err = json!({ "jsonrpc": "2.0", "id": null,
                    "error": { "code": -32700, "message": "Parse error" } });
                sampler.reply(&err.to_string())?;
                continue;
            }
        };

        // Notifications carry no id and get no reply.
        if req.id.is_none() {
            continue;
        }

        // Capability gate: only clients that declare `sampling` ever get a
        // `sampling/createMessage` request; the rest take the manual loop.
        if req.method == "initialize" {
            sampler.sampling_allowed = req
                .params
                .as_ref()
                .and_then(|p| p.get("capabilities"))
                .and_then(|c| c.get("sampling"))
                .is_some();
        }

        let resp = handle_request_with(&req, &mut sampler);
        let resp_str = serde_json::to_string(&resp)?;
        sampler.reply(&resp_str)?;
    }

    Ok(())
}

/// Pure entry point (no sampling client): used by tests and embedders.
#[allow(dead_code)]
pub(crate) fn handle_request(req: &RpcRequest) -> RpcResponse {
    handle_request_with(req, &mut NoSampler)
}

pub(crate) fn handle_request_with(req: &RpcRequest, sampler: &mut dyn Sampler) -> RpcResponse {
    match req.method.as_str() {
        "initialize" => RpcResponse {
            jsonrpc: "2.0".into(),
            id: req.id.clone(),
            result: Some(json!({
                "protocolVersion": "2024-11-05",
                "capabilities": { "tools": {} },
                "serverInfo": { "name": "jev-seo", "version": env!("CARGO_PKG_VERSION") }
            })),
            error: None,
        },
        "notifications/initialized" | "notifications/cancelled" => RpcResponse {
            jsonrpc: "2.0".into(),
            id: req.id.clone(),
            result: Some(json!({})),
            error: None,
        },
        "tools/list" => RpcResponse {
            jsonrpc: "2.0".into(),
            id: req.id.clone(),
            result: Some(json!({
                "tools": [
                    {
                        "name": "seo_cite_check",
                        "description": "Check whether an AI answer names the target domain for a buyer query. Pass \"answer\" to score directly (works everywhere); omit it to borrow the client's model via sampling, or get the prompt back to answer yourself",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "target": { "type": "string", "description": "Domain or URL to look for, e.g. example.com" },
                                "query": { "type": "string", "description": "Buyer query to check" },
                                "answer": { "type": "string", "description": "AI answer text to score; omit to use sampling or receive the prompt" }
                            },
                            "required": ["target", "query"]
                        }
                    },
                    {
                        "name": "seo_gap",
                        "description": "Ranked gap queue for a verified Search Console site: high-impression queries in weak positions joined with citation history",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "site": { "type": "string", "description": "Verified site URL, e.g. https://example.com/" },
                                "limit": { "type": "integer", "description": "Top queries to rank (default 10)" }
                            },
                            "required": ["site"]
                        }
                    },
                    {
                        "name": "seo_keywords",
                        "description": "Fetch autocomplete keyword suggestions for zero cost",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "query": { "type": "string" }
                            },
                            "required": ["query"]
                        }
                    },
                    {
                        "name": "seo_serp_inspect",
                        "description": "Search live SERP competitors (search API, DuckDuckGo fallback) and analyze content gaps",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "query": { "type": "string" },
                                "limit": { "type": "integer" }
                            },
                            "required": ["query"]
                        }
                    },
                    {
                        "name": "seo_audit",
                        "description": "Audit a local file or an entire directory for on-page SEO issues, duplicate titles, and thin pages",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "path": { "type": "string" }
                            },
                            "required": ["path"]
                        }
                    },
                    {
                        "name": "seo_geo",
                        "description": "Evaluate Generative Engine Optimization (GEO) citation likelihood (1-10) and direct answer presence using TypeSafe Jev",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "target": { "type": "string", "description": "File path or content snippet to score" },
                                "query": { "type": "string", "description": "Target search query" }
                            },
                            "required": ["target", "query"]
                        }
                    },
                    {
                        "name": "seo_schema",
                        "description": "Validate Schema.org JSON-LD markup against active 2026 search specifications",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "target": { "type": "string", "description": "File path, HTML snippet, or JSON string" }
                            },
                            "required": ["target"]
                        }
                    },
                    {
                        "name": "seo_robots",
                        "description": "Inspect robots.txt on a live domain for AI crawler permissions (GPTBot, ClaudeBot, PerplexityBot, Google-Extended) and sitemaps",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "domain": { "type": "string", "description": "Domain or URL to inspect" }
                            },
                            "required": ["domain"]
                        }
                    },
                    {
                        "name": "seo_brief",
                        "description": "Synthesize live SERP competitor results into a ready-to-write content brief with H2 outlines and 150-word direct answer guidance",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "topic": { "type": "string" },
                                "limit": { "type": "integer", "description": "Number of SERP competitors to scrape (default: 5)" }
                            },
                            "required": ["topic"]
                        }
                    },
                    {
                        "name": "seo_sitemap",
                        "description": "Inspect and validate XML sitemaps for protocols, 50k URL limits, and international hreflang tags",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "target": { "type": "string", "description": "URL or local file path to sitemap.xml" }
                            },
                            "required": ["target"]
                        }
                    },
                    {
                        "name": "seo_crawl",
                        "description": "Crawl a live site for broken links, redirect chains, and orphan pages",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "url": { "type": "string", "description": "Start URL" },
                                "max_pages": { "type": "integer", "description": "Maximum pages to fetch (default: 50)" },
                                "sitemap": { "type": "string", "description": "Sitemap URL override (else robots.txt Sitemap: + /sitemap.xml)" }
                            },
                            "required": ["url"]
                        }
                    },
                    {
                        "name": "seo_llms",
                        "description": "Check llms.txt presence and AI crawler permissions for answer-engine readiness",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "domain": { "type": "string", "description": "Domain or URL to inspect" }
                            },
                            "required": ["domain"]
                        }
                    },
                    {
                        "name": "seo_extract",
                        "description": "Extract clean markdown from URLs via paid API (needs TAVILY_API_KEY), else error",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "urls": { "type": "array", "items": { "type": "string" } },
                                "query": { "type": "string", "description": "Rerank intent" }
                            },
                            "required": ["urls", "query"]
                        }
                    },
                    {
                        "name": "seo_explain",
                        "description": "Explain a stable rule id (R19 or RULE-R19): area, severity, effort, title, fix",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "id": { "type": "string", "description": "Rule id, e.g. R19 or RULE-R19" }
                            },
                            "required": ["id"]
                        }
                    },
                    {
                        "name": "seo_report",
                        "description": "Diff two saved audit JSON reports: score delta, rules cleared/new, ranked actions",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "path": { "type": "string", "description": "Current audit JSON path" },
                                "baseline": { "type": "string", "description": "Baseline audit JSON path" },
                                "baseline_label": { "type": "string", "description": "Stored baseline label instead of a path (see drift baseline)" }
                            },
                            "required": ["path"]
                        }
                    }
                ]
            })),
            error: None,
        },
        "tools/call" => {
            let params = req.params.as_ref();
            let tool_name = params
                .and_then(|p| p.get("name"))
                .and_then(|n| n.as_str())
                .unwrap_or("");
            let args = params
                .and_then(|p| p.get("arguments"))
                .cloned()
                .unwrap_or(json!({}));

            let result_content = execute_tool_with(tool_name, &args, sampler);
            let is_error = result_content.starts_with("Error:");
            RpcResponse {
                jsonrpc: "2.0".into(),
                id: req.id.clone(),
                result: Some(json!({
                    "content": [
                        {
                            "type": "text",
                            "text": result_content,
                            "isError": is_error
                        }
                    ],
                    "isError": is_error
                })),
                error: None,
            }
        }
        _ => RpcResponse {
            jsonrpc: "2.0".into(),
            id: req.id.clone(),
            result: None,
            error: Some(json!({ "code": -32601, "message": "Method not found" })),
        },
    }
}

/// Second-layer guard for agent-chosen targets. Static path rules already ran;
/// this asks Jev whether the target smells like a secret. No key configured
/// means this layer is off (static guards still apply); a failed check while
/// configured blocks, because an unreachable guard is not a clean bill.
fn safety_gate(tool: &str, target: &str) -> Option<String> {
    match crate::engine::JevClient::new() {
        None => None,
        Some(client) => match client.safety_block(tool, target) {
            Some(true) => Some(format!(
                "Error: blocked unsafe target for {}: {}",
                tool, target
            )),
            Some(false) => None,
            None => Some(format!(
                "Error: safety check unreachable for {}: {}",
                tool, target
            )),
        },
    }
}

/// Normalize a target domain or URL to the bare host the model's answer is
/// scanned for: lowercase, no scheme, no leading www., no path.
/// The exact buyer-query prompt the agent answers, whether over sampling or
/// with its own model in the manual loop.
fn cite_prompt(query: &str) -> String {
    format!(
        "Answer this search query briefly, naming the specific websites or brands you recommend: {query}"
    )
}

/// Score one answer for a target citation and log the drift verdict.
/// Best-effort ledger: a stuck database never fails the check.
fn cite_verdict(
    target: &str,
    query: &str,
    answer: &str,
    source: &str,
    citations: &[String],
) -> String {
    let host = cite_host(target);
    let lower = answer.to_lowercase();
    let cited = lower.contains(&host) || citations.iter().any(|c| c.to_lowercase().contains(&host));
    let prev = crate::rank::DbStore::open()
        .ok()
        .and_then(|db| db.record_cite(target, query, cited).ok())
        .flatten();
    let excerpt: String = answer.chars().take(300).collect();
    serde_json::to_string_pretty(&json!({
        "target": target,
        "query": query,
        "cited": cited,
        "since_last": prev,
        "source": source,
        "engine_citations": citations,
        "excerpt": excerpt,
    }))
    .unwrap_or_default()
}

fn cite_host(target: &str) -> String {
    let t = target.trim().to_lowercase();
    let t = t.split("://").last().unwrap_or(&t);
    let t = t.strip_prefix("www.").unwrap_or(t);
    t.split(['/', '?', '#']).next().unwrap_or(t).to_string()
}

fn execute_tool_with(name: &str, args: &serde_json::Value, sampler: &mut dyn Sampler) -> String {
    match name {
        "seo_gap" => {
            let site = args.get("site").and_then(|v| v.as_str()).unwrap_or("");
            if site.trim().is_empty() {
                return "Error: seo_gap needs \"site\" (verified Search Console URL).".into();
            }
            let limit = args.get("limit").and_then(|v| v.as_u64()).unwrap_or(10) as usize;
            match crate::gsc::gap(site, limit) {
                Ok(rows) => serde_json::to_string_pretty(&rows).unwrap_or_default(),
                Err(e) => format!("Error: {e:#}"),
            }
        }
        "seo_cite_check" => {
            let target = args
                .get("target")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .trim();
            let query = args
                .get("query")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .trim();
            if target.is_empty() || query.is_empty() {
                return "Error: seo_cite_check needs both \"target\" (domain or URL) and \"query\".".into();
            }
            if let Some(err) = safety_gate("seo_cite_check", target) {
                return err;
            }
            // Universal path: the agent answers with its own model, no client
            // capability needed. Works on every MCP client ever made.
            if let Some(answer) = args.get("answer").and_then(|v| v.as_str()) {
                if !answer.trim().is_empty() {
                    return cite_verdict(target, query, answer, "answer", &[]);
                }
            }
            // Fast path: borrow the client's model over sampling.
            let prompt = cite_prompt(query);
            match sampler.sample(&prompt) {
                Ok(answer) => cite_verdict(target, query, &answer, "sampling", &[]),
                Err(se) => match crate::llm::ask(&prompt) {
                    // Paid path: configured engine answers server-side.
                    Ok((answer, cites)) => cite_verdict(target, query, &answer, "engine", &cites),
                    // Manual loop fallback: tell the agent exactly how to finish.
                    Err(le) => serde_json::to_string_pretty(&json!({
                        "target": target,
                        "query": query,
                        "needs_answer": true,
                        "prompt": prompt,
                        "note": format!("sampling unavailable ({se}); engine unavailable ({le}); answer the prompt with your own model, then re-call seo_cite_check with \"answer\""),
                    }))
                    .unwrap_or_default(),
                },
            }
        }
        "seo_serp_inspect" => {
            let q = args.get("query").and_then(|v| v.as_str()).unwrap_or("");
            let limit = args.get("limit").and_then(|v| v.as_u64()).unwrap_or(5) as usize;
            match crate::serp::scrape_serp(q, limit) {
                Ok((items, _)) => serde_json::to_string_pretty(&items).unwrap_or_default(),
                Err(e) => format!("Error: {}", e),
            }
        }
        "seo_audit" => {
            let path = args.get("path").and_then(|v| v.as_str()).unwrap_or("");
            if let Some(err) = safety_gate("seo_audit", path) {
                return err;
            }
            let checked = match crate::paths::check_audit_path(path) {
                Ok(p) => p,
                Err(e) => return format!("Error: {e:#}"),
            };
            match crate::audit::audit_path(&checked) {
                Ok(rep) => serde_json::to_string_pretty(&rep).unwrap_or_default(),
                Err(e) => format!("Error: {}", e),
            }
        }
        "seo_geo" => {
            let target = args.get("target").and_then(|v| v.as_str()).unwrap_or("");
            let query = args.get("query").and_then(|v| v.as_str()).unwrap_or("");
            if let Some(err) = safety_gate("seo_geo", target) {
                return err;
            }
            let content = match crate::paths::read_user_file(
                target,
                &["md", "mdx", "markdown", "html", "htm", "txt"],
            ) {
                Ok(c) => c,
                Err(e) => return format!("Error: {}", e),
            };
            if let Some(client) = crate::engine::JevClient::new() {
                let lower = target.to_ascii_lowercase();
                let is_html = lower.ends_with(".html") || lower.ends_with(".htm");
                let text = if is_html {
                    crate::fetch::readable_text(&content, 6000)
                } else {
                    content.chars().take(6000).collect::<String>()
                };
                let opening = is_html.then(|| crate::fetch::opening_after_h1(&content, 500));
                let wc = text.split_whitespace().count();
                let state = crate::engine::page_state(
                    query,
                    Some(target.to_string()),
                    None,
                    text,
                    wc,
                    opening,
                );
                match client.judge_page(state) {
                    Ok(eval) => {
                        if crate::policy::injection_blocked(&eval.extra) {
                            return "Error: blocked: injection risk in content (Jev pre-screen)."
                                .into();
                        }
                        if crate::policy::gate("geo", eval.geo_confidence)
                            == crate::policy::Verdict::Drop
                        {
                            return "Error: Jev unsure (low confidence), no score.".into();
                        }
                        serde_json::to_string_pretty(&eval).unwrap_or_default()
                    }
                    Err(e) => format!("Error evaluating GEO via Jev: {}", e),
                }
            } else {
                // Free tier: deterministic keyless score, no model involved.
                let out = json!({
                    "target": target,
                    "query": query,
                    "keyless": crate::geo_keyless::score(&content, query),
                    "note": "keyless deterministic score; set TYPESAFE_API_KEY for Jev semantic judgment",
                });
                serde_json::to_string_pretty(&out).unwrap_or_default()
            }
        }
        "seo_keywords" => {
            let q = args.get("query").and_then(|v| v.as_str()).unwrap_or("");
            match crate::serp::get_autocomplete(q) {
                Ok(items) => {
                    if let Some(client) = crate::engine::JevClient::new() {
                        let state = json!({ "root_query": q, "suggestions": items });
                        let extras = crate::policy::keyword_value_extras(&items, 10);
                        if let Ok(eval) = client.fanout_eval_with(state, extras) {
                            if crate::policy::gate("keywords", eval.confidence())
                                != crate::policy::Verdict::Drop
                            {
                                let out = json!({
                                    "suggestions": items,
                                    "intent": eval.intent,
                                    "intent_confidence": eval.intent_confidence,
                                    "content_gap": eval.content_gap,
                                    "keyword_values": eval.extra
                                });
                                return serde_json::to_string_pretty(&out).unwrap_or_default();
                            }
                        }
                    }
                    serde_json::to_string_pretty(&items).unwrap_or_default()
                }
                Err(e) => format!("Error: {}", e),
            }
        }
        "seo_schema" => {
            let target = args.get("target").and_then(|v| v.as_str()).unwrap_or("");
            if let Some(err) = safety_gate("seo_schema", target) {
                return err;
            }
            match crate::schema::validate_target(target) {
                Ok(rep) => serde_json::to_string_pretty(&rep).unwrap_or_default(),
                Err(e) => format!("Error: {}", e),
            }
        }
        "seo_robots" => {
            let domain = args.get("domain").and_then(|v| v.as_str()).unwrap_or("");
            match crate::robots::inspect_robots(domain) {
                Ok(rep) => serde_json::to_string_pretty(&rep).unwrap_or_default(),
                Err(e) => format!("Error: {}", e),
            }
        }
        "seo_brief" => {
            let topic = args.get("topic").and_then(|v| v.as_str()).unwrap_or("");
            let limit = args.get("limit").and_then(|v| v.as_u64()).unwrap_or(5) as usize;
            match crate::brief::generate_brief(topic, limit) {
                Ok(brief) => serde_json::to_string_pretty(&brief).unwrap_or_default(),
                Err(e) => format!("Error: {}", e),
            }
        }
        "seo_sitemap" => {
            let target = args.get("target").and_then(|v| v.as_str()).unwrap_or("");
            if let Some(err) = safety_gate("seo_sitemap", target) {
                return err;
            }
            match crate::sitemap::audit_sitemap(target) {
                Ok(rep) => serde_json::to_string_pretty(&rep).unwrap_or_default(),
                Err(e) => format!("Error: {}", e),
            }
        }
        "seo_crawl" => {
            let url = args.get("url").and_then(|v| v.as_str()).unwrap_or("");
            if let Some(err) = safety_gate("seo_crawl", url) {
                return err;
            }
            let max_pages =
                args.get("max_pages")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(crate::crawl::DEFAULT_MAX_PAGES as u64) as usize;
            let sitemap = args
                .get("sitemap")
                .and_then(|v| v.as_str())
                .filter(|s| !s.trim().is_empty());
            if let Some(s) = sitemap {
                if let Some(err) = safety_gate("seo_crawl", s) {
                    return err;
                }
            }
            let mut budget = crate::fetch::Budget::default();
            match crate::crawl::crawl_site_with_sitemap(
                url,
                max_pages,
                crate::fetch::FetchMode::Auto,
                &mut budget,
                sitemap,
            ) {
                Ok(rep) => serde_json::to_string_pretty(&rep).unwrap_or_default(),
                Err(e) => format!("Error: {}", e),
            }
        }
        "seo_llms" => {
            let domain = args.get("domain").and_then(|v| v.as_str()).unwrap_or("");
            match crate::llms::check_llms(domain) {
                Ok(rep) => serde_json::to_string_pretty(&rep).unwrap_or_default(),
                Err(e) => format!("Error: {}", e),
            }
        }
        "seo_extract" => {
            let urls: Vec<String> = args
                .get("urls")
                .and_then(|v| v.as_array())
                .map(|a| {
                    a.iter()
                        .filter_map(|v| v.as_str().map(str::to_string))
                        .collect()
                })
                .unwrap_or_default();
            let query = args.get("query").and_then(|v| v.as_str()).unwrap_or("");
            match crate::serp::tavily_extract(&urls, query) {
                Ok(md) => md,
                Err(e) => format!("Error: {}", e),
            }
        }
        "seo_explain" => {
            let id = args.get("id").and_then(|v| v.as_str()).unwrap_or("");
            match crate::rules::explain(id) {
                Some(text) => text,
                None => format!("Error: unknown rule id: {}", id),
            }
        }
        "seo_report" => {
            let path = args.get("path").and_then(|v| v.as_str()).unwrap_or("");
            let baseline = args.get("baseline").and_then(|v| v.as_str()).unwrap_or("");
            let baseline_label = args
                .get("baseline_label")
                .and_then(|v| v.as_str())
                .unwrap_or("");
            if let Some(err) = safety_gate("seo_report", path) {
                return err;
            }
            let base_src = if !baseline_label.trim().is_empty() {
                match crate::rank::DbStore::open() {
                    Ok(db) => match db.load_baseline(baseline_label.trim()) {
                        Ok(Some(src)) => src,
                        Ok(None) => {
                            return format!(
                                "Error: no baseline '{}' stored.",
                                baseline_label.trim()
                            )
                        }
                        Err(e) => return format!("Error: read baseline: {e:#}"),
                    },
                    Err(e) => return format!("Error: open drift store: {e:#}"),
                }
            } else {
                if let Some(err) = safety_gate("seo_report", baseline) {
                    return err;
                }
                match crate::paths::read_user_file(baseline, &["json"]) {
                    Ok(src) => src,
                    Err(e) => return format!("Error: read audit JSON: {e}"),
                }
            };
            let base: crate::audit::DirectoryAuditReport = match serde_json::from_str(&base_src) {
                Ok(b) => b,
                Err(e) => return format!("Error: parse baseline JSON: {}", e),
            };
            match crate::paths::read_user_file(path, &["json"]) {
                Ok(c) => match serde_json::from_str::<crate::audit::DirectoryAuditReport>(&c) {
                    Ok(cur) => {
                        let actions = crate::rules::actions_for(&cur.findings);
                        // Drift is best-effort: a missing ledger yields no alerts.
                        let drift = crate::rank::DbStore::open()
                            .ok()
                            .and_then(|db| db.drift_alerts(200).ok())
                            .unwrap_or_default();
                        let out = json!({
                            "diff": crate::main_report_diff(&cur, &base),
                            "actions": actions,
                            "pairs": crate::audit::cannibalization_pairs(&cur),
                            "drift": drift,
                        });
                        serde_json::to_string_pretty(&out).unwrap_or_default()
                    }
                    Err(e) => format!("Error: parse audit JSON: {}", e),
                },
                Err(e) => format!("Error: read audit JSON: {}", e),
            }
        }
        _ => format!("Error: unknown tool: {}", name),
    }
}