intercept_request
Intercept network requests matching a URL pattern to log, block, modify, mock, or stop them.
Instructions
Intercept network requests matching a pattern.
Args: url_pattern: URL glob pattern (e.g. "**/api/login*"). action: "log", "block", "modify", "mock", or "stop" (unroute). modify_headers: Headers to add/override (action="modify"). modify_body: Request body replacement (action="modify"). mock_response: Dict with "status", "headers", "body" (action="mock").
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url_pattern | Yes | ||
| action | No | log | |
| modify_headers | No | ||
| modify_body | No | ||
| mock_response | No |
Implementation Reference
- The main handler for the intercept_request MCP tool. Registers a Playwright page.route() with a closure that handles four actions: log (records request to console_logs), block (aborts the request), modify (overrides headers/body), and mock (fulfills with a custom response). Also supports 'stop' action to unroute.
@mcp.tool() async def intercept_request( url_pattern: str, action: str = "log", modify_headers: dict | None = None, modify_body: str | None = None, mock_response: dict | None = None, ) -> dict: """Intercept network requests matching a pattern. Args: url_pattern: URL glob pattern (e.g. "**/api/login*"). action: "log", "block", "modify", "mock", or "stop" (unroute). modify_headers: Headers to add/override (action="modify"). modify_body: Request body replacement (action="modify"). mock_response: Dict with "status", "headers", "body" (action="mock"). """ try: page = await browser_manager.get_active_page() if action == "stop": if url_pattern: await page.unroute(url_pattern) return {"status": "stopped", "pattern": url_pattern} else: await page.unroute("**/*") return {"status": "stopped_all"} async def handler(route): if action == "log": browser_manager._console_logs.append({ "level": "info", "text": f"[INTERCEPT:log] {route.request.method} {route.request.url}", "timestamp": time.time() * 1000, "location": None, }) await route.continue_() elif action == "block": await route.abort() elif action == "modify": overrides = {} if modify_headers: overrides["headers"] = {**dict(route.request.headers), **modify_headers} if modify_body: overrides["post_data"] = modify_body await route.continue_(**overrides) elif action == "mock": resp = mock_response or {} await route.fulfill( status=resp.get("status", 200), headers=resp.get("headers", {"content-type": "application/json"}), body=resp.get("body", "{}"), ) await page.route(url_pattern, handler) return {"status": "intercepting", "pattern": url_pattern, "action": action} except Exception as e: return {"error": str(e)} - Input schema for intercept_request: url_pattern (required URL glob), action (log/block/modify/mock/stop, default 'log'), modify_headers (optional dict), modify_body (optional string), mock_response (optional dict with status/headers/body). Returns a dict.
url_pattern: str, action: str = "log", modify_headers: dict | None = None, modify_body: str | None = None, mock_response: dict | None = None, ) -> dict: """Intercept network requests matching a pattern. Args: url_pattern: URL glob pattern (e.g. "**/api/login*"). action: "log", "block", "modify", "mock", or "stop" (unroute). modify_headers: Headers to add/override (action="modify"). modify_body: Request body replacement (action="modify"). mock_response: Dict with "status", "headers", "body" (action="mock"). """ - src/camoufox_reverse_mcp/tools/network.py:274-274 (registration)Registration of intercept_request via the @mcp.tool() decorator, where 'mcp' is a FastMCP instance from the server module.
@mcp.tool()