use std::io::Write;
use anyhow::Result;
use clap::Subcommand;
use crate::mcp::BasemindServer;
#[derive(Subcommand, Debug)]
pub enum WebCmd {
Scrape {
url: String,
#[arg(long)]
no_index: bool,
#[arg(long)]
scope: Option<String>,
},
Crawl {
url: String,
#[arg(long)]
max_pages: Option<u32>,
#[arg(long)]
max_depth: Option<u32>,
#[arg(long)]
scope: Option<String>,
},
Map { url: String },
}
#[cfg(feature = "crawl")]
pub async fn run(server: &BasemindServer, cmd: WebCmd, json: bool, out: &mut impl Write) -> Result<()> {
use crate::mcp::params::*;
use super::render::emit;
use super::run_tool;
fn parse_url(s: &str) -> Result<crate::url::Url> {
s.parse::<crate::url::Url>()
.map_err(|e| anyhow::anyhow!("invalid url {s:?}: {e}"))
}
match cmd {
WebCmd::Scrape { url, no_index, scope } => {
let p = WebScrapeParams {
url: parse_url(&url)?,
index: !no_index,
scope,
};
let r = run_tool("web_scrape", server.web_scrape(Parameters(p)).await)?;
emit("web_scrape", &r, json, out)
}
WebCmd::Crawl {
url,
max_pages,
max_depth,
scope,
} => {
let p = WebCrawlParams {
url: parse_url(&url)?,
max_pages,
max_depth,
scope,
};
let r = run_tool("web_crawl", server.web_crawl(Parameters(p)).await)?;
emit("web_crawl", &r, json, out)
}
WebCmd::Map { url } => {
let p = WebMapParams { url: parse_url(&url)? };
let r = run_tool("web_map", server.web_map(Parameters(p)).await)?;
emit("web_map", &r, json, out)
}
}
}
#[cfg(not(feature = "crawl"))]
pub async fn run(_server: &BasemindServer, _cmd: WebCmd, _json: bool, _out: &mut impl Write) -> Result<()> {
anyhow::bail!("this `basemind` was built without the `crawl` feature; rebuild with --features crawl")
}