1use std::sync::Arc;
8
9use rmcp::{
10 ErrorData,
11 handler::server::{
12 ServerHandler,
13 router::tool::ToolRouter,
14 wrapper::{Json, Parameters},
15 },
16 model::{CallToolResult, Implementation, ProtocolVersion, ServerCapabilities, ServerInfo},
17 tool, tool_handler, tool_router,
18};
19
20use crate::{
21 errors::map_err,
22 state::AppState,
23 tools,
24 tools::{
25 actions::{
26 AxTreeArgs, AxTreeResult, CaptureCaptchaResult, ClickArgs, ClickByRoleArgs,
27 ClickVisualCoordsArgs, DetectCaptchaResult, EvalJsArgs, EvalJsResult, ExtractArgs,
28 ExtractResult, InjectCaptchaTokenArgs, NetworkCaptureResult, OkResult,
29 SessionIdArgs as ActionSessionIdArgs, SolveCaptchaArgs, SolveCaptchaResult,
30 TeleportArgs, TitleResult, TypeTextArgs, WaitIdleArgs,
31 },
32 download::{
33 DownloadArgs, DownloadArmArgs, DownloadArmResult, DownloadResult, DownloadWaitArgs,
34 },
35 fetch::{FetchArgs, FetchManyArgs, FetchManyResult, FetchResult},
36 introspect::PoolStatus,
37 screenshot::ScreenshotArgs,
38 session::{
39 SessionCloseResult, SessionContentResult, SessionIdArgs, SessionNavigateArgs,
40 SessionNavigateResult, SessionOpenArgs, SessionOpenResult,
41 },
42 },
43};
44
45#[derive(Debug)]
47pub struct VoidCrawlServer {
48 state: Arc<AppState>,
49 #[allow(dead_code, reason = "read by the `#[tool_handler]` macro expansion")]
50 tool_router: ToolRouter<Self>,
51}
52
53impl VoidCrawlServer {
54 pub fn new(state: Arc<AppState>) -> Self {
55 Self { state, tool_router: Self::tool_router() }
56 }
57
58 pub fn state(&self) -> &AppState {
59 &self.state
60 }
61}
62
63#[tool_router]
64impl VoidCrawlServer {
65 #[tool(
66 name = "fetch",
67 description = "Fetch a URL with stealth headless Chrome and return HTML + metadata. \
68Use for single-shot scrapes; for bulk use fetch_many."
69 )]
70 pub async fn fetch(
71 &self,
72 Parameters(args): Parameters<FetchArgs>,
73 ) -> Result<Json<FetchResult>, ErrorData> {
74 tools::fetch::run(self, args).await.map(Json).map_err(map_err)
75 }
76
77 #[tool(
78 name = "fetch_many",
79 description = "Fetch many URLs in parallel over the shared browser pool. Returns \
80one entry per request in input order; per-request errors do not abort the batch. \
81Each result carries `waited_ms` (time queued for a tab), and the batch carries a \
82`pool` summary {max_tabs, submitted, queued, max_waited_ms, note} — if `queued > 0` \
83you oversubscribed the pool; cap batches at `max_tabs` (see pool_status) for full parallelism."
84 )]
85 pub async fn fetch_many(
86 &self,
87 Parameters(args): Parameters<FetchManyArgs>,
88 ) -> Result<Json<FetchManyResult>, ErrorData> {
89 Ok(Json(tools::fetch::run_many(self, args).await))
90 }
91
92 #[tool(
93 name = "download",
94 description = "Download a file (PDF, archive, image, …) through stealth Chrome and scan \
95it with a built-in Rust antivirus gate (magic-byte type check + yara-x signatures) BEFORE it is \
96trusted. The file is fetched into a quarantine dir and only moved into `output_dir` if it passes \
97every check; a flagged file is deleted and the result has `ok=false` with a `reason`. Returns \
98{ok, verdict, path?, reason?, detected_mime, size}. Use this instead of `fetch` when you need the \
99actual bytes of a downloadable resource rather than rendered HTML. OPT-IN: disabled unless the \
100server is run with VOIDCRAWL_ALLOW_DOWNLOADS=1. NOTE: a `clean` verdict means it passed the \
101size + content-type + bundled-signature checks, not that it is guaranteed malware-free."
102 )]
103 pub async fn download(
104 &self,
105 Parameters(args): Parameters<DownloadArgs>,
106 ) -> Result<Json<DownloadResult>, ErrorData> {
107 tools::download::run(self, args).await.map(Json).map_err(map_err)
108 }
109
110 #[tool(
111 name = "download_arm",
112 description = "Arm an open session to capture the file produced by the NEXT \
113download-triggering action — for downloads started by clicking a button (e.g. Google Drive's \
114'Download'), where there's no stable URL to pass to `download`. Flow: session_open → \
115session_navigate → download_arm → click_by_role(\"button\",\"Download\") (+ \"Download anyway\" if \
116an interstitial appears) → download_wait. OPT-IN: needs VOIDCRAWL_ALLOW_DOWNLOADS=1."
117 )]
118 pub async fn download_arm(
119 &self,
120 Parameters(args): Parameters<DownloadArmArgs>,
121 ) -> Result<Json<DownloadArmResult>, ErrorData> {
122 tools::download::arm(self, args).await.map(Json).map_err(map_err)
123 }
124
125 #[tool(
126 name = "download_wait",
127 description = "Wait for the download armed by `download_arm` to land, scan it with the \
128antivirus gate, and (if clean) move it into the output dir. Returns {ok, verdict, path?, reason?, \
129detected_mime, size}. Call after the click(s) that trigger the download. NOTE: a `clean` verdict \
130means it passed the size + bundled-signature checks; the content-type disguise check does NOT run \
131on action downloads (no Content-Type is observed), so `clean` is not a malware-free guarantee."
132 )]
133 pub async fn download_wait(
134 &self,
135 Parameters(args): Parameters<DownloadWaitArgs>,
136 ) -> Result<Json<DownloadResult>, ErrorData> {
137 tools::download::wait(self, args).await.map(Json).map_err(map_err)
138 }
139
140 #[tool(
141 name = "screenshot",
142 description = "Load a URL in stealth headless Chrome and return a full-page PNG."
143 )]
144 pub async fn screenshot(
145 &self,
146 Parameters(args): Parameters<ScreenshotArgs>,
147 ) -> Result<CallToolResult, ErrorData> {
148 tools::screenshot::run(self, args).await
149 }
150
151 #[tool(
152 name = "session_open",
153 description = "Open a new stateful browser session with a dedicated Chrome instance. \
154Returns a session_id used by session_navigate / session_content / session_close. \
155Pass `user_data_dir` to mount a persistent profile (e.g. one already logged into LinkedIn); \
156omit it for an ephemeral cookieless profile. Set `headful=true` to bring up a visible window \
157(useful for a one-time manual login into the persistent profile)."
158 )]
159 pub async fn session_open(
160 &self,
161 Parameters(args): Parameters<SessionOpenArgs>,
162 ) -> Result<Json<SessionOpenResult>, ErrorData> {
163 tools::session::open(self, args).await.map(Json)
164 }
165
166 #[tool(
167 name = "session_navigate",
168 description = "Navigate the given session to a URL and wait for it to settle. \
169wait_for accepts 'networkidle' (default) or 'selector:<css>' (event-driven, no polling)."
170 )]
171 pub async fn session_navigate(
172 &self,
173 Parameters(args): Parameters<SessionNavigateArgs>,
174 ) -> Result<Json<SessionNavigateResult>, ErrorData> {
175 tools::session::navigate(self, args).await.map(Json)
176 }
177
178 #[tool(
179 name = "session_content",
180 description = "Return the current HTML, title, and URL of the given session's page."
181 )]
182 pub async fn session_content(
183 &self,
184 Parameters(args): Parameters<SessionIdArgs>,
185 ) -> Result<Json<SessionContentResult>, ErrorData> {
186 tools::session::content(self, args).await.map(Json)
187 }
188
189 #[tool(
190 name = "session_close",
191 description = "Close the given session: shut down its Chrome instance and free resources. \
192Always call this when you're done — otherwise the browser stays alive until the server exits."
193 )]
194 pub async fn session_close(
195 &self,
196 Parameters(args): Parameters<SessionIdArgs>,
197 ) -> Result<Json<SessionCloseResult>, ErrorData> {
198 tools::session::close(self, args).await.map(Json)
199 }
200
201 #[tool(
202 name = "pool_status",
203 description = "Report the browser pool configuration plus a live snapshot of \
204concurrency: `max_tabs`, `available` (free slots right now), `in_flight`, and \
205`sessions_open`. Read `available` before a big fan-out to size the batch."
206 )]
207 pub async fn pool_status(&self) -> Result<Json<PoolStatus>, ErrorData> {
208 tools::introspect::pool_status(self).await.map(Json).map_err(map_err)
209 }
210
211 #[tool(
212 name = "click",
213 description = "Click the first element matching a CSS selector in an open session."
214 )]
215 pub async fn click(
216 &self,
217 Parameters(args): Parameters<ClickArgs>,
218 ) -> Result<Json<OkResult>, ErrorData> {
219 tools::actions::click(self, args).await.map(Json)
220 }
221
222 #[tool(
223 name = "teleport",
224 description = "Override the session's geolocation (and optionally timezone + locale) so \
225navigator.geolocation and location-aware sites resolve to the given lat/lon — 'teleport' the \
226browser. The geolocation permission is granted automatically. Call after session_open and \
227BEFORE navigating; the override persists across navigations. For Google Maps 'near me' queries: \
228use a FRESH session per location, and navigate to the search twice (prime + read) — Maps resolves \
229location on first load and applies it on the next request."
230 )]
231 pub async fn teleport(
232 &self,
233 Parameters(args): Parameters<TeleportArgs>,
234 ) -> Result<Json<OkResult>, ErrorData> {
235 tools::actions::teleport(self, args).await.map(Json)
236 }
237
238 #[tool(
239 name = "click_visual_coords",
240 description = "Click at pixel coordinates (x, y) in CSS pixels. Use when selector-based \
241clicks fail silently (React forms that ignore dispatchEvent clicks). Coords are pre-DPR: \
242divide screenshot pixels by devicePixelRatio on HiDPI."
243 )]
244 pub async fn click_visual_coords(
245 &self,
246 Parameters(args): Parameters<ClickVisualCoordsArgs>,
247 ) -> Result<Json<OkResult>, ErrorData> {
248 tools::actions::click_visual_coords(self, args).await.map(Json)
249 }
250
251 #[tool(
252 name = "type_text",
253 description = "Type text into an input. With `selector`, focuses + types. Without, \
254dispatches keys to whatever currently has focus (pair with click_visual_coords first)."
255 )]
256 pub async fn type_text(
257 &self,
258 Parameters(args): Parameters<TypeTextArgs>,
259 ) -> Result<Json<OkResult>, ErrorData> {
260 tools::actions::type_text(self, args).await.map(Json)
261 }
262
263 #[tool(
264 name = "eval_js",
265 description = "Evaluate a JS expression in the session's page. Returns the value as JSON."
266 )]
267 pub async fn eval_js(
268 &self,
269 Parameters(args): Parameters<EvalJsArgs>,
270 ) -> Result<Json<EvalJsResult>, ErrorData> {
271 tools::actions::eval_js(self, args).await.map(Json)
272 }
273
274 #[tool(name = "title", description = "Return the current document title of the session.")]
275 pub async fn title(
276 &self,
277 Parameters(args): Parameters<ActionSessionIdArgs>,
278 ) -> Result<Json<TitleResult>, ErrorData> {
279 tools::actions::title(self, args).await.map(Json)
280 }
281
282 #[tool(
283 name = "extract",
284 description = "Run document.querySelectorAll(selector) and return each element's text content."
285 )]
286 pub async fn extract(
287 &self,
288 Parameters(args): Parameters<ExtractArgs>,
289 ) -> Result<Json<ExtractResult>, ErrorData> {
290 tools::actions::extract(self, args).await.map(Json)
291 }
292
293 #[tool(
294 name = "session_ax_tree",
295 description = "Return the page's accessibility (AX) tree — the semantic view assistive \
296tech sees, with implicit roles resolved, accessible names computed, and hidden nodes pruned. \
297Default `mode=compact` gives a pruned, indented role/name outline for reading; `mode=raw` gives \
298full CDP nodes. `named_count` vs `node_count` signals AX richness: when low, fall back to HTML, \
299screenshot, or CSS selectors. Complements (does not replace) the DOM/visual tools."
300 )]
301 pub async fn session_ax_tree(
302 &self,
303 Parameters(args): Parameters<AxTreeArgs>,
304 ) -> Result<Json<AxTreeResult>, ErrorData> {
305 tools::actions::ax_tree(self, args).await.map(Json)
306 }
307
308 #[tool(
309 name = "click_by_role",
310 description = "Click an element by its accessibility role + accessible name (e.g. \
311role=\"button\", name=\"Load more\") instead of a CSS selector. More durable across redesigns, \
312but flakier when names are ambiguous, localized, or duplicated — pair with session_ax_tree to \
313see available roles/names, and fall back to `click` (CSS) or `click_visual_coords` when it fails."
314 )]
315 pub async fn click_by_role(
316 &self,
317 Parameters(args): Parameters<ClickByRoleArgs>,
318 ) -> Result<Json<OkResult>, ErrorData> {
319 tools::actions::click_by_role(self, args).await.map(Json)
320 }
321
322 #[tool(
323 name = "wait_for_network_idle",
324 description = "Wait for Chrome's network-idle lifecycle event. Event-driven, no polling."
325 )]
326 pub async fn wait_for_network_idle(
327 &self,
328 Parameters(args): Parameters<WaitIdleArgs>,
329 ) -> Result<Json<OkResult>, ErrorData> {
330 tools::actions::wait_for_network_idle(self, args).await.map(Json)
331 }
332
333 #[tool(
334 name = "network_capture",
335 description = "Return the Resource Timing entries (URL, initiator type, transfer size, duration) \
336observed since the session's most recent navigation. Backed by performance.getEntriesByType('resource')."
337 )]
338 pub async fn network_capture(
339 &self,
340 Parameters(args): Parameters<ActionSessionIdArgs>,
341 ) -> Result<Json<NetworkCaptureResult>, ErrorData> {
342 tools::actions::network_capture(self, args).await.map(Json)
343 }
344
345 #[tool(
346 name = "solve_captcha",
347 description = "Click the Turnstile / reCAPTCHA-v2 / hCaptcha checkbox in an open session \
348using real CDP mouse events (not JS click — widgets detect that) and wait for the response \
349token to appear. Returns the kind detected, the coordinates clicked, the token value (once \
350the widget writes it into its hidden input), and a `solved` flag. No-op (solved=true) when \
351the page has no captcha. Only handles widgets whose anchor frame is already visible — if \
352detect_captcha reports `turnstile` because the runtime loaded but no widget mounted, trigger \
353the form submit that mounts the widget first."
354 )]
355 pub async fn solve_captcha(
356 &self,
357 Parameters(args): Parameters<SolveCaptchaArgs>,
358 ) -> Result<Json<SolveCaptchaResult>, ErrorData> {
359 tools::actions::solve_captcha(self, args).await.map(Json)
360 }
361
362 #[tool(
363 name = "detect_captcha",
364 description = "Probe the DOM for captcha / bot-wall markers. Returns the kind tag \
365(recaptcha, hcaptcha, turnstile, cloudflare_challenge, datadome) or null."
366 )]
367 pub async fn detect_captcha(
368 &self,
369 Parameters(args): Parameters<ActionSessionIdArgs>,
370 ) -> Result<Json<DetectCaptchaResult>, ErrorData> {
371 tools::actions::detect_captcha_tool(self, args).await.map(Json)
372 }
373
374 #[tool(
375 name = "capture_captcha",
376 description = "Deep structured probe of a captcha challenge. Returns kind, sitekey, \
377widget rect + selector, response-field selector, existing token (if already solved), page URL, \
378and Turnstile action/cdata attrs. Use this to hand off to a third-party solver API \
379(2Captcha / CapSolver / Anti-Captcha) or a human-in-the-loop flow, then call \
380`inject_captcha_token` with the resulting token."
381 )]
382 pub async fn capture_captcha(
383 &self,
384 Parameters(args): Parameters<ActionSessionIdArgs>,
385 ) -> Result<Json<CaptureCaptchaResult>, ErrorData> {
386 tools::actions::capture_captcha_tool(self, args).await.map(Json)
387 }
388
389 #[tool(
390 name = "inject_captcha_token",
391 description = "Write a solved captcha token into the page's hidden response field and \
392fire input/change events so React-controlled forms pick it up. For Turnstile, invokes any \
393registered `data-callback` function. `kind` defaults to whatever is currently detected; pass \
394explicitly ('turnstile'/'recaptcha'/'hcaptcha') to skip re-detection."
395 )]
396 pub async fn inject_captcha_token(
397 &self,
398 Parameters(args): Parameters<InjectCaptchaTokenArgs>,
399 ) -> Result<Json<OkResult>, ErrorData> {
400 tools::actions::inject_captcha_token_tool(self, args).await.map(Json)
401 }
402}
403
404#[tool_handler]
405impl ServerHandler for VoidCrawlServer {
406 fn get_info(&self) -> ServerInfo {
407 let mut info = ServerInfo::default();
408 info.protocol_version = ProtocolVersion::default();
409 info.capabilities = ServerCapabilities::builder().enable_tools().build();
410 info.server_info = {
411 let mut imp = Implementation::default();
412 imp.name = "voidcrawl-mcp".into();
413 imp.version = env!("CARGO_PKG_VERSION").into();
414 imp
415 };
416 info.instructions = Some(
421 "Stealthy headless Chrome over a shared, fingerprint-patched tab pool — a drop-in \
422replacement for Playwright / Chromium MCP.\n\n\
423WORKFLOW. Stateless scrape: `fetch` (one URL) or `fetch_many` (parallel; returns \
424{results:[{ok,result,error}]} in input order — per-item errors don't abort the batch, and \
425status_code is nested under each item's `result`). Stateful flows (login, pagination, clicking): \
426`session_open` → `session_navigate` → … → `session_close`. ALWAYS session_close; sessions are \
427cookie-isolated.\n\n\
428PERCEIVE → ACT → EXTRACT. To see a page, call `session_ax_tree` — a compact role/name outline, \
429far cheaper than HTML (don't dump raw HTML to reason over a page). If `named_count` is low vs \
430`node_count` the accessibility tree is thin; fall back to `screenshot`. To click: `click` (CSS \
431selector) or `click_by_role` (accessibility role + accessible name — durable across redesigns); \
432last resort `click_visual_coords` for React forms that ignore synthetic clicks. To extract data, \
433run `extract` / `eval_js` with a JS expression and return data, not markup.\n\n\
434GOTCHAS. `click_by_role` name matching is EXACT (case + whitespace) — read the exact name from \
435`session_ax_tree` first; use `nth` for duplicates. After an in-page (SPA) click, \
436`wait_for_network_idle` may run to its full timeout — pass a short `timeout_secs` or use \
437`wait_for:\"selector:<css>\"`. On a captcha error, surface it and rotate proxy/profile; don't \
438retry the same URL and don't try to solve."
439 .into(),
440 );
441 info
442 }
443}