File size: 16,534 Bytes
a4fd077
 
7361f85
a4fd077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7361f85
 
 
a4fd077
7361f85
a4fd077
 
7361f85
a4fd077
 
 
 
 
 
96cb792
a4fd077
 
 
96cb792
 
 
a4fd077
 
 
 
7361f85
a4fd077
 
 
 
 
 
 
 
 
 
 
 
96cb792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4fd077
 
 
 
 
 
96cb792
 
 
 
a4fd077
 
 
 
 
 
 
 
96cb792
 
a4fd077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7361f85
a4fd077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96cb792
a4fd077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7361f85
a4fd077
 
 
 
96cb792
a4fd077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96cb792
 
 
 
 
 
 
 
a4fd077
 
 
 
 
 
 
 
 
96cb792
 
a4fd077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7361f85
a4fd077
7361f85
a4fd077
 
7361f85
a4fd077
 
 
 
 
 
7361f85
a4fd077
 
 
 
 
7361f85
a4fd077
7361f85
a4fd077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7361f85
a4fd077
 
 
7361f85
a4fd077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7361f85
a4fd077
 
 
7361f85
a4fd077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96cb792
a4fd077
 
96cb792
a4fd077
 
 
 
96cb792
a4fd077
 
 
 
 
 
 
7361f85
a4fd077
 
 
7361f85
a4fd077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7361f85
a4fd077
7361f85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4fd077
7361f85
 
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
"""High-Performance Stealth Web Scraper & Browser Automation MCP Server

Directly serves SSE & HTTP endpoints via Uvicorn on 0.0.0.0:7860.
"""

import asyncio
import base64
import json
import logging
import os
import sys
from contextlib import asynccontextmanager
from typing import Any, AsyncGenerator

from bs4 import BeautifulSoup
from duckduckgo_search import DDGS
import httpx
from lxml_html_clean import Cleaner
import markdownify
from mcp.server.fastmcp import FastMCP
from patchright.async_api import Browser, BrowserContext, Page, async_playwright
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Mount, Route
import trafilatura
import uvicorn

# ==========================================
# 1. Logging & Configuration
# ==========================================
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
    handlers=[logging.StreamHandler(sys.stdout)],
)
logger = logging.getLogger("mcp_server")

PORT = int(os.getenv("PORT", 7860))
HOST = "0.0.0.0"
MAX_CONCURRENT_BROWSERS = 4
MAX_RECYCLE_REQUESTS = 100
MAX_CONTENT_CHARS = 100_000
HTTP_TIMEOUT_SEC = 12.0
BROWSER_TIMEOUT_MS = 30000

# ==========================================
# 2. Browser Pool
# ==========================================


class StealthBrowserPool:

    def __init__(self):
        self._playwright = None
        self._browser: Browser | None = None
        self._semaphore = asyncio.Semaphore(MAX_CONCURRENT_BROWSERS)
        self._request_counter = 0
        self._lock = asyncio.Lock()

    async def get_browser(self) -> Browser:
        async with self._lock:
            if not self._playwright:
                self._playwright = await async_playwright().start()
            if not self._browser or not self._browser.is_connected():
                logger.info("Initializing Chromium Stealth instance...")
                self._browser = await self._playwright.chromium.launch(
                    headless=True,
                    args=[
                        "--no-sandbox",
                        "--disable-setuid-sandbox",
                        "--disable-dev-shm-usage",
                        "--disable-gpu",
                        "--disable-blink-features=AutomationControlled",
                        "--no-first-run",
                        "--window-size=1920,1080",
                    ],
                )
                self._request_counter = 0
            return self._browser

    async def _check_recycle(self):
        async with self._lock:
            self._request_counter += 1
            if self._request_counter >= MAX_RECYCLE_REQUESTS:
                if self._browser:
                    try:
                        await self._browser.close()
                    except Exception:
                        pass
                self._browser = None

    @asynccontextmanager
    async def get_page(self) -> AsyncGenerator[Page, None]:
        await self._semaphore.acquire()
        context: BrowserContext | None = None
        page: Page | None = None
        try:
            browser = await self.get_browser()
            context = await browser.new_context(
                viewport={"width": 1920, "height": 1080},
                user_agent=(
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                    "AppleWebKit/537.36 (KHTML, like Gecko) "
                    "Chrome/131.0.0.0 Safari/537.36"
                ),
                locale="en-US",
                timezone_id="America/New_York",
            )
            await context.add_init_script("""
                Object.defineProperty(navigator, 'webdriver', { get: () => undefined });
                Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5] });
                Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });
                window.chrome = { runtime: {} };
            """)
            page = await context.new_page()
            page.set_default_navigation_timeout(BROWSER_TIMEOUT_MS)
            page.set_default_timeout(BROWSER_TIMEOUT_MS)
            yield page
        finally:
            if page:
                try:
                    await page.close()
                except Exception:
                    pass
            if context:
                try:
                    await context.close()
                except Exception:
                    pass
            self._semaphore.release()
            await self._check_recycle()


browser_pool = StealthBrowserPool()

# ==========================================
# 3. Content Extraction Pipeline
# ==========================================


def clean_html(raw_html: str) -> str:
    cleaner = Cleaner(
        scripts=True,
        javascript=True,
        comments=True,
        style=True,
        links=False,
        meta=False,
        page_structure=False,
        safe_attrs_only=False,
    )
    return cleaner.clean_html(raw_html)


def extract_content(html: str, url: str = "") -> dict[str, Any]:
    extracted = trafilatura.extract(
        html,
        url=url,
        include_links=True,
        include_images=True,
        output_format="markdown",
    )
    metadata = trafilatura.extract_metadata(html) or {}
    meta_dict = {
        "title": getattr(metadata, "title", "") or "",
        "author": getattr(metadata, "author", "") or "",
        "date": getattr(metadata, "date", "") or "",
        "description": getattr(metadata, "description", "") or "",
    }

    if not extracted or len(extracted.strip()) < 100:
        cleaned = clean_html(html)
        soup = BeautifulSoup(cleaned, "html.parser")
        for tag in soup(
            ["nav", "footer", "aside", "header", "script", "style", "noscript"]
        ):
            tag.decompose()
        if not meta_dict["title"] and soup.title:
            meta_dict["title"] = soup.title.string or ""
        extracted = markdownify.markdownify(
            str(soup), heading_style="ATX", strip=["svg"]
        )

    if len(extracted) > MAX_CONTENT_CHARS:
        extracted = (
            extracted[:MAX_CONTENT_CHARS]
            + f"\n\n... [Truncated: reached {MAX_CONTENT_CHARS} limit]"
        )

    return {"content": extracted.strip(), "metadata": meta_dict}


def extract_json_ld(html: str) -> list[dict]:
    soup = BeautifulSoup(html, "html.parser")
    schemas = []
    for script in soup.find_all("script", type="application/ld+json"):
        try:
            if script.string:
                data = json.loads(script.string.strip())
                schemas.append(data)
        except Exception:
            continue
    return schemas


# ==========================================
# 4. Hybrid Scraping Core
# ==========================================

FAST_HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.9",
}


async def dismiss_popups(page: Page):
    selectors = [
        'button[id*="cookie" i]',
        'button[class*="cookie" i]',
        'button[aria-label*="accept" i]',
        'button:has-text("Accept all")',
        'button:has-text("Accept")',
        'button:has-text("I agree")',
        'button:has-text("Got it")',
    ]
    for sel in selectors:
        try:
            elem = await page.query_selector(sel)
            if elem and await elem.is_visible():
                await elem.click(timeout=800)
                await asyncio.sleep(0.2)
                break
        except Exception:
            continue


async def scrape_pipeline(
    url: str,
    force_browser: bool = False,
    auto_scroll: bool = True,
    wait_for_selector: str | None = None,
) -> dict:
    if not force_browser:
        try:
            async with httpx.AsyncClient(
                http2=True,
                timeout=HTTP_TIMEOUT_SEC,
                follow_redirects=True,
                headers=FAST_HEADERS,
            ) as client:
                resp = await client.get(url)
                if resp.status_code == 200:
                    html = resp.text
                    if not any(
                        s in html.lower()
                        for s in [
                            "cf-challenge",
                            "ray-id",
                            "just a moment...",
                        ]
                    ):
                        parsed = extract_content(html, url=url)
                        if len(parsed["content"]) >= 150:
                            return {
                                "url": str(resp.url),
                                "status": 200,
                                "engine": "fast-http",
                                "content": parsed["content"],
                                "metadata": parsed["metadata"],
                            }
        except Exception:
            pass

    async with browser_pool.get_page() as page:
        await page.goto(url, wait_until="domcontentloaded")
        try:
            cf_frame = await page.query_selector(
                "iframe[src*='challenges.cloudflare.com']"
            )
            if cf_frame:
                box = await cf_frame.bounding_box()
                if box:
                    await page.mouse.click(
                        box["x"] + box["width"] / 2,
                        box["y"] + box["height"] / 2,
                    )
                    await asyncio.sleep(2.0)
        except Exception:
            pass

        await dismiss_popups(page)

        if wait_for_selector:
            try:
                await page.wait_for_selector(
                    wait_for_selector, state="visible", timeout=8000
                )
            except Exception:
                pass

        if auto_scroll:
            for _ in range(4):
                await page.mouse.wheel(0, 1000)
                await asyncio.sleep(0.3)

        await asyncio.sleep(0.5)
        html = await page.content()
        final_url = page.url
        title = await page.title()

    parsed = extract_content(html, url=final_url)
    if not parsed["metadata"].get("title"):
        parsed["metadata"]["title"] = title

    return {
        "url": final_url,
        "status": 200,
        "engine": "patchright-stealth",
        "content": parsed["content"],
        "metadata": parsed["metadata"],
    }


# ==========================================
# 5. MCP Server & Tool Definitions
# ==========================================
mcp = FastMCP("WebScraper")


@mcp.tool()
async def scrape_url(
    url: str,
    force_browser: bool = False,
    auto_scroll: bool = True,
    wait_for_selector: str | None = None,
) -> dict:
    """Scrapes cleaned Markdown content and metadata from any URL."""
    return await scrape_pipeline(
        url, force_browser, auto_scroll, wait_for_selector
    )


@mcp.tool()
async def search_and_scrape(query: str, max_results: int = 3) -> dict:
    """Searches DuckDuckGo and concurrently extracts content from top results."""
    max_results = min(max(1, max_results), 5)
    loop = asyncio.get_running_loop()

    def _search():
        with DDGS() as ddgs:
            return list(ddgs.text(query, max_results=max_results))

    results = await loop.run_in_executor(None, _search)
    if not results:
        return {"query": query, "results": []}

    tasks = [
        scrape_pipeline(res["href"], force_browser=False, auto_scroll=False)
        for res in results
    ]
    scraped_payloads = await asyncio.gather(*tasks, return_exceptions=True)

    enriched = []
    for meta, payload in zip(results, scraped_payloads):
        if isinstance(payload, dict):
            enriched.append(
                {
                    "title": meta.get("title", ""),
                    "url": meta.get("href", ""),
                    "snippet": meta.get("body", ""),
                    "content": payload.get("content", ""),
                }
            )
        else:
            enriched.append(
                {
                    "title": meta.get("title", ""),
                    "url": meta.get("href", ""),
                    "snippet": meta.get("body", ""),
                    "content": f"[Error: {str(payload)}]",
                }
            )
    return {"query": query, "results": enriched}


@mcp.tool()
async def take_screenshot(
    url: str, full_page: bool = True, wait_seconds: float = 1.0
) -> dict:
    """Takes a full-page or viewport screenshot of a webpage."""
    async with browser_pool.get_page() as page:
        await page.goto(url, wait_until="networkidle")
        if wait_seconds > 0:
            await asyncio.sleep(min(wait_seconds, 10.0))
        screenshot_bytes = await page.screenshot(
            full_page=full_page, type="png"
        )
        b64 = base64.b64encode(screenshot_bytes).decode("utf-8")
        return {
            "url": page.url,
            "title": await page.title(),
            "format": "image/png;base64",
            "base64_image": b64,
        }


@mcp.tool()
async def interact_page(
    url: str, actions: list[dict[str, Any]], extract_markdown: bool = True
) -> dict:
    """Executes a list of browser actions (click, type, press, wait, evaluate)."""
    async with browser_pool.get_page() as page:
        await page.goto(url, wait_until="domcontentloaded")
        logs = []

        for idx, act in enumerate(actions):
            act_type = act.get("type", "").lower()
            try:
                if act_type == "click":
                    sel = act["selector"]
                    await page.click(sel, timeout=6000)
                    logs.append(f"[{idx}] Clicked '{sel}'")
                elif act_type == "type":
                    sel = act["selector"]
                    txt = act["text"]
                    await page.fill(sel, txt, timeout=6000)
                    logs.append(f"[{idx}] Typed into '{sel}'")
                elif act_type == "press":
                    k = act["key"]
                    await page.keyboard.press(k)
                    logs.append(f"[{idx}] Pressed '{k}'")
                elif act_type == "wait":
                    s = min(float(act.get("seconds", 1.0)), 15.0)
                    await asyncio.sleep(s)
                    logs.append(f"[{idx}] Waited {s}s")
                elif act_type == "evaluate":
                    res = await page.evaluate(act["script"])
                    logs.append(f"[{idx}] Evaluated script -> {res}")
            except Exception as e:
                logs.append(f"[{idx}] Failed: {str(e)}")

        html = await page.content()
        final_url = page.url

    res = {"final_url": final_url, "logs": logs}
    if extract_markdown:
        parsed = extract_content(html, url=final_url)
        res["content"] = parsed["content"]
        res["metadata"] = parsed["metadata"]
    return res


@mcp.tool()
async def extract_structured_data(
    url: str, css_selectors: dict[str, str] | None = None
) -> dict:
    """Parses structured JSON-LD schemas and arbitrary CSS selector targets."""
    async with browser_pool.get_page() as page:
        await page.goto(url, wait_until="domcontentloaded")
        html = await page.content()

    soup = BeautifulSoup(html, "html.parser")
    custom_data = {}
    if css_selectors:
        for k, sel in css_selectors.items():
            elements = soup.select(sel)
            custom_data[k] = [el.get_text(strip=True) for el in elements]

    return {
        "url": url,
        "custom_fields": custom_data,
        "json_ld_schemas": extract_json_ld(html),
    }


# ==========================================
# 6. Starlette SSE Router & Server Runner
# ==========================================


async def health_check(request):
    return JSONResponse(
        {
            "status": "healthy",
            "mcp_sse_endpoint": "/sse",
            "transport": "Server-Sent Events (SSE)",
        }
    )


# Mount the MCP SSE application with health endpoints
starlette_app = Starlette(
    routes=[
        Route("/", endpoint=health_check),
        Route("/healthz", endpoint=health_check),
        Mount("/", app=mcp.sse_app()),
    ]
)

if __name__ == "__main__":
    logger.info(f"Starting MCP Server on http://{HOST}:{PORT} ...")
    uvicorn.run(starlette_app, host=HOST, port=PORT, log_level="info")