rover-fetch 0.2.0

An MCP server for fetching and prepping web content for LLM agents.
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
use std::path::PathBuf;
use std::process::ExitCode;
use std::sync::Arc;

use clap::{Parser, Subcommand};

/// ASCII banner shown above the auto-generated help on `rover --help` and
/// `rover help`. Subcommand `--help` pages are intentionally banner-free so
/// the user isn't re-greeted every time they look up flags.
const HELP_BANNER: &str = r#"
             .--~~,__       ____
:-....,-------`~~'._.'     / __ \____ _   _____  _____
 `-,,,  ,_      ;'~U'     / /_/ / __ \ | / / _ \/ ___/
  _,-' ,'`-__; '--.      / _, _/ /_/ / |/ /  __/ /
 (_/'~~      ''''(;     /_/ |_|\____/|___/\___/_/
"#;

#[derive(Debug, Parser)]
#[command(
    name = "rover",
    version,
    about = "Web fetch & prep for LLM agents",
    before_help = HELP_BANNER,
)]
struct Cli {
    /// Path to a TOML config file. If absent, defaults are used.
    #[arg(long, global = true)]
    config: Option<PathBuf>,

    /// SECURITY: skip integrity verification of cached local model files
    /// against their `.rover-integrity.toml` manifest before loading. This
    /// disables tamper detection for downloaded weights — only use it if you
    /// understand the risk. Equivalent to setting
    /// `ROVER_UNSAFE_DISABLE_MODEL_INTEGRITY_CHECK=1`.
    #[cfg(feature = "local-inference")]
    #[arg(long, global = true)]
    unsafe_disable_model_integrity_check: bool,

    #[command(subcommand)]
    command: Command,
}

#[derive(Debug, Subcommand)]
enum Command {
    /// Start the MCP server (long-running).
    Mcp(McpArgs),

    /// One-shot fetch, prints markdown to stdout.
    Fetch(FetchArgs),

    /// Inspect or monitor a batch_fetch task (alias for `rover task` with a kind check).
    Batch {
        id: String,
        #[arg(long)]
        monitor: bool,
        #[arg(long)]
        cancel: bool,
        #[arg(long, value_enum, default_value_t = OutputFormat::Human)]
        format: OutputFormat,
        /// Stream events starting after this event id (use with --monitor).
        #[arg(long)]
        from_event: Option<i64>,
    },

    /// Inspect or monitor a long-running task.
    Task {
        id: String,
        #[arg(long)]
        monitor: bool,
        #[arg(long)]
        cancel: bool,
        #[arg(long, value_enum, default_value_t = OutputFormat::Human)]
        format: OutputFormat,
        /// Stream events starting after this event id (use with --monitor).
        #[arg(long)]
        from_event: Option<i64>,
    },

    /// Cache operations.
    #[command(subcommand)]
    Cache(CacheCmd),

    /// Verify the Rover environment.
    Doctor(DoctorArgs),

    /// Inspect or modify config.
    #[command(subcommand)]
    Config(ConfigCmd),

    /// Manage local HuggingFace model cache.
    #[cfg(feature = "local-inference")]
    #[command(subcommand)]
    Model(rover::cli::model::ModelCmd),
}

#[derive(Debug, clap::Args)]
struct FetchArgs {
    /// URL to fetch.
    url: String,

    /// Bypass the cache for this fetch and always go out to the network.
    #[arg(long)]
    force_refresh: bool,

    /// Skip the robots.txt gate for this fetch. CLI-only escape hatch.
    #[arg(long)]
    ignore_robots: bool,

    /// Override [fetch] user_agent for this request.
    #[arg(long)]
    user_agent: Option<String>,

    /// Override [fetch] timeout_secs (per-request timeout) for this request.
    #[arg(long)]
    timeout_secs: Option<u64>,

    /// Override [rate_limit] requests_per_minute_per_domain.
    #[arg(long)]
    rate_limit_rpm: Option<u32>,

    /// Override [rate_limit] per_domain_concurrency.
    #[arg(long)]
    per_host_concurrency: Option<u32>,

    /// Override [rate_limit] global_concurrency.
    #[arg(long)]
    global_concurrency: Option<u32>,

    /// Override [rate_limit] max_retries.
    #[arg(long)]
    max_retries: Option<u8>,

    /// Auto-summarize when the extracted markdown exceeds N tokens. Runs the
    /// configured [summarization] backend (offline extractive by default) and
    /// replaces the body with a summary sized toward the budget (best-effort).
    #[arg(long)]
    max_tokens: Option<usize>,

    /// JSON SummarizeOpts blob (same shape as the MCP `summarize` args
    /// without `url`), e.g. '{"target_tokens":500}'. Applied before
    /// --max-tokens; the body is replaced with the summary.
    #[arg(long, value_name = "JSON")]
    summarize: Option<String>,
}

#[derive(Debug, clap::Args)]
struct McpArgs {
    /// Disable the robots.txt gate for the lifetime of this server. All MCP
    /// fetch tools will skip the robots check.
    #[arg(long)]
    ignore_robots: bool,

    /// Override [rate_limit] requests_per_minute_per_domain.
    #[arg(long)]
    rate_limit_rpm: Option<u32>,

    /// Override [rate_limit] per_domain_concurrency.
    #[arg(long)]
    per_host_concurrency: Option<u32>,

    /// Override [rate_limit] global_concurrency.
    #[arg(long)]
    global_concurrency: Option<u32>,

    /// Override [rate_limit] max_retries.
    #[arg(long)]
    max_retries: Option<u8>,
}

#[derive(Debug, Clone, Copy, clap::ValueEnum)]
enum OutputFormat {
    Human,
    Ndjson,
}

impl From<OutputFormat> for rover::cli::task::OutputFormat {
    fn from(value: OutputFormat) -> Self {
        match value {
            OutputFormat::Human => rover::cli::task::OutputFormat::Human,
            OutputFormat::Ndjson => rover::cli::task::OutputFormat::Ndjson,
        }
    }
}

#[derive(Debug, Subcommand)]
enum CacheCmd {
    /// List cached URLs (most recent first).
    List {
        #[arg(long, default_value_t = 20)]
        limit: u64,
        #[arg(long, default_value_t = 0)]
        offset: u64,
    },
    /// Print the cached Markdown for a URL.
    Get { url: String },
    /// Delete cache entries matching a glob (`*`, `?`).
    Purge {
        pattern: String,
        /// Required to wipe the entire cache (`*` pattern).
        #[arg(long)]
        all: bool,
    },
    /// Show cache size, entry count, expired count.
    Stats,
}

impl CacheCmd {
    fn into_runtime_args(self) -> rover::cli::cache::Args {
        match self {
            CacheCmd::List { limit, offset } => rover::cli::cache::Args::List { limit, offset },
            CacheCmd::Get { url } => rover::cli::cache::Args::Get { url },
            CacheCmd::Purge { pattern, all } => rover::cli::cache::Args::Purge { pattern, all },
            CacheCmd::Stats => rover::cli::cache::Args::Stats,
        }
    }
}

#[derive(Debug, Subcommand)]
enum ConfigCmd {
    Show,
    Set { key: String, value: String },
}

#[derive(Debug, clap::Args)]
struct DoctorArgs {
    /// Output format: `human` (default) prints one line per check;
    /// `ndjson` emits one JSON object per line for scripting.
    #[arg(long, default_value = "human")]
    format: String,
}

/// Build a `SummarizerService` for CLI subcommands that need it.
///
/// The MCP server builds its own service inside `serve_stdio`; this helper
/// exists for CLI paths (e.g. a future `rover fetch --summarize`) so the
/// construction stays in one place. Currently unused outside the MCP path —
/// `#[allow(dead_code)]` keeps `warnings = deny` happy until M7's CLI
/// summarize wiring lands.
#[allow(dead_code)]
async fn build_summarizer_service(
    db: rover::storage::Db,
    config: &rover::config::Config,
) -> anyhow::Result<Arc<rover::summarizer::SummarizerService>> {
    let registry = Arc::new(
        rover::summarizer::registry::build(config, config.tokenizer.default)
            .map_err(anyhow::Error::from)?,
    );
    Ok(Arc::new(
        rover::summarizer::SummarizerService::new(
            db,
            registry,
            config.summarization.fallback_to_extractive,
        )
        .with_guard(std::sync::Arc::new(
            rover::guard::Guard::from_config(&config.prompt_injection)
                .map_err(anyhow::Error::from)?,
        )),
    ))
}

fn main() -> ExitCode {
    rover::fetcher::client::install_ring_provider();
    let cli = Cli::parse();

    // Per-subcommand log defaults. `rover mcp` is a long-running server
    // where observability is the point, so it keeps a chatty default. The
    // one-shot CLI subcommands write to a user's terminal next to their
    // actual output, so they default to `warn` — quiet on success but
    // real diagnostics (stale serves, HAR flush failures, etc.) still
    // surface. `RUST_LOG` overrides either default.
    let default_filter = match &cli.command {
        Command::Mcp(_) => "info,rover=debug",
        _ => "warn",
    };
    rover::telemetry::init(default_filter);

    // Surface the model-integrity bypass loudly at startup. The CLI flag and
    // the env var converge here: setting the flag exports the env var (read by
    // the verification path), and either path triggers the warning. Done before
    // the runtime spawns any threads so the `set_var` is sound.
    #[cfg(feature = "local-inference")]
    {
        if cli.unsafe_disable_model_integrity_check {
            // SAFETY: still single-threaded — the tokio runtime is built below.
            unsafe { std::env::set_var(rover::model_integrity::DISABLE_ENV, "1") };
        }
        if rover::model_integrity::check_disabled() {
            tracing::warn!(
                target: "rover::model_integrity",
                "model integrity verification is DISABLED \
                 (--unsafe-disable-model-integrity-check / {}); cached model files will NOT be \
                 checked for tampering before loading",
                rover::model_integrity::DISABLE_ENV,
            );
        }
    }

    let runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .expect("tokio runtime");
    runtime.block_on(dispatch(cli))
}

async fn dispatch(cli: Cli) -> ExitCode {
    let result = match cli.command {
        Command::Fetch(args) => {
            rover::cli::fetch::run(args.into_runtime_args(), cli.config.as_deref()).await
        }
        Command::Cache(sub) => {
            let args = sub.into_runtime_args();
            rover::cli::cache::run(args, cli.config.as_deref()).await
        }
        Command::Mcp(args) => {
            rover::cli::mcp::run(args.into_runtime_args(), cli.config.as_deref()).await
        }
        Command::Task {
            id,
            monitor,
            cancel,
            format,
            from_event,
        } => {
            rover::cli::task::run(
                rover::cli::task::Args {
                    id,
                    monitor,
                    cancel,
                    format: format.into(),
                    from_event,
                    expect_kind: None,
                },
                cli.config.as_deref(),
            )
            .await
        }
        Command::Batch {
            id,
            monitor,
            cancel,
            format,
            from_event,
        } => {
            rover::cli::batch::run(
                rover::cli::task::Args {
                    id,
                    monitor,
                    cancel,
                    format: format.into(),
                    from_event,
                    expect_kind: Some("batch_fetch"),
                },
                cli.config.as_deref(),
            )
            .await
        }
        Command::Doctor(args) => {
            let cfg = match rover::config::load_resolved(cli.config.as_deref()) {
                Ok(c) => c,
                Err(e) => {
                    eprintln!("rover: loading config: {e}");
                    return ExitCode::from(1);
                }
            };
            return match rover::cli::doctor::run(
                rover::cli::doctor::Args {
                    format: args.format,
                },
                cfg,
            )
            .await
            {
                Ok(code) => ExitCode::from(code as u8),
                Err(e) => {
                    eprintln!("rover: {e}");
                    ExitCode::from(1)
                }
            };
        }
        Command::Config(cmd) => match cmd {
            ConfigCmd::Show => {
                let res = rover::cli::config::show(rover::cli::config::ShowArgs {
                    config_path: cli.config.clone(),
                });
                return match res {
                    Ok(code) => ExitCode::from(code as u8),
                    Err(e) => {
                        eprintln!("rover: {e}");
                        ExitCode::from(1)
                    }
                };
            }
            ConfigCmd::Set { key, value } => {
                let res = rover::cli::config::set(rover::cli::config::SetArgs {
                    config_path: cli.config.clone(),
                    key,
                    value,
                });
                return match res {
                    Ok(code) => ExitCode::from(code as u8),
                    Err(e) => {
                        eprintln!("rover: {e}");
                        ExitCode::from(1)
                    }
                };
            }
        },
        #[cfg(feature = "local-inference")]
        Command::Model(cmd) => rover::cli::model::run(cmd).await,
    };

    match result {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) => {
            eprintln!("rover: {e}");
            ExitCode::from(1)
        }
    }
}

impl FetchArgs {
    fn into_runtime_args(self) -> rover::cli::fetch::Args {
        rover::cli::fetch::Args {
            url: self.url,
            force_refresh: self.force_refresh,
            ignore_robots: self.ignore_robots,
            user_agent: self.user_agent,
            timeout_secs: self.timeout_secs,
            rate_limit_rpm: self.rate_limit_rpm,
            per_host_concurrency: self.per_host_concurrency,
            global_concurrency: self.global_concurrency,
            max_retries: self.max_retries,
            max_tokens: self.max_tokens,
            summarize: self.summarize,
        }
    }
}

impl McpArgs {
    fn into_runtime_args(self) -> rover::cli::mcp::Args {
        rover::cli::mcp::Args {
            ignore_robots: self.ignore_robots,
            rate_limit_rpm: self.rate_limit_rpm,
            per_host_concurrency: self.per_host_concurrency,
            global_concurrency: self.global_concurrency,
            max_retries: self.max_retries,
        }
    }
}