instrumentation
Injects source-level taps into JavaScript VMP scripts to log property access, calls, and dispatcher stages, and rewrite sources for debugging obfuscated code.
Instructions
JSVMP source-level instrumentation (v0.9.0 unified).
Replaces instrument_jsvmp_source / get_instrumentation_log /
stop_instrumentation / reload_with_hooks.
Args:
action:
"install" — register route + AST/regex rewrite on matched scripts.
Requires url_pattern. (was: instrument_jsvmp_source)
"log" — fetch accumulated tap events from instrumented code.
(was: get_instrumentation_log)
"stop" — unregister instrumentation route.
(was: stop_instrumentation)
"reload" — reload page so persistent hooks fire before page JS.
(was: reload_with_hooks)
"status" — show active instrumentations, their options and the
files passed through unchanged, with reasons.
(was: get_instrumentation_status)
"rewrite_source" — apply the same rewrite to local JS without a
browser: give `source` (text) or `source_path`, and
`output_path` for anything larger than ~100KB. The
output runs in Node/vm/jsdom too; events collect in
globalThis.__mcp_vmp_log. No size limit applies.
url_pattern: For "install"/"stop" — glob pattern matching VMP script URLs.
mode: For "install"/"rewrite_source" — "ast" (esprima then local
bundled Acorn) or "regex" (conservative whole-program subset;
unsupported input skipped).
tag: For "install"/"rewrite_source"/"log" — group identifier.
rewrite_member_access: tap obj[key] reads (default True). Start here.
rewrite_calls: tap fn(args) and obj.m(args) calls (default False since
v1.10.0). Turn it on only when call events are needed: call taps
add helper frames, so callee.caller reads null and the maximum
recursion depth drops, which fingerprinting code can notice.
tap_stage_calls: rewrite the flattened-VMP dispatcher shape
`return fn.apply(this, [<int>].concat(Array.prototype.slice
.call(arguments)))` into per-state `tap_stage` events (state id `p`,
argc, first args previews, return preview), pushed when the state
returns. Events recorded while a state runs carry it as `stage`.
This is a literal match, so it also works when the rest of the
file is not parseable (status then shows "(stage taps only)"), and
it is independent of `mode`/`filter_*`.
inject: source-level regex insertion, independent of `mode` and usable
together with `tap_stage_calls`/`filter_*`. A list of
`{"find": <regex>, "before": <js>, "after": <js>, "group": <name>}`:
every match of `find` (re.MULTILINE) gets `before` inserted at the
match start and/or `after` at the match end. `$1..$99`/`$0` in the
snippets expand to capture groups, `$$` is a literal `$`.
Four safety layers keep injected code from breaking syntax:
(1) each snippet is wrapped into a complete statement (`;` guard on
both ends, or `;
...
;when it contains newlines or//); (2) insertions landing inside string, template, comment or regex literals are skipped individually (skipped_insertions/ literal_skips); (3) a beforeinsert on aswitch case :/default: label is automatically shifted past that label's colon (label_shift), so it executes when jumping to that state and never lands before the first case; (4) when the original source parses (esprima or local bundled Acorn for modern ES2025/module syntax), the injected output is re-parsed and any group that would break syntax is rolled back individually (inject.skipped_groups, reason "would break syntax"). Note: single-line inserts add zero newlines (lines_added == 0), though mode="ast"prepends the instrumentation runtime header at the top of the file (onlymode="regex"with zero member rewrites keeps exact 1-based line numbers). Per-group inserted counts come back ininject.groups[].insertions (raw regex matches in.matches) and accumulate across newly rewritten scripts in status/logasinject_hits. The injected code runs in the page main world; read the globals it fills with evaluate_js(expression, world="main"). inject_validate: default True — run the parse validation and per-group fallback described above. False skips the AST re-parse check while keeping the ;statement wrapper, literal-region skip guard, and case/defaultlabel shift active. include_source_site: attach a stable site_id to tap events, plus an original-source range map in the log response. Default False. max_rewrites: hard cap on rewrites per file; status lists capped files. fallback_on_error: try conservative regex on AST failure only without property/object filters; otherwise pass through unchanged. ignore_csp: For "install" — skip CSP pre-flight check. clear_log: For "reload" — clear JSVMP logs before reload. wait_until: For "reload" — "load" / "domcontentloaded" / "networkidle". tag_filter: For "log" — filter by tag. type_filter: For "log" — "tap_get", "tap_call", "tap_method", "tap_call_err", "tap_stage", "tap_stage_break". key_filter: For "log" — substring match on property/method name. after_seq: For "log" — only events with seq > after_seq, oldest first; page with the returned next_after_seq. seq_window: For "log" — [start, end], events with start <= seq <= end. stage: For "log" — events recorded while dispatcher statestageran, plus that state's own tap_stage events. limit: For "log" — max entries: the most recent ones, or the first ones when after_seq/seq_window is set. clear: For "log" — clear log after retrieval in the selected main world. frame_url: For log; select a target frame by URL pattern. frame_name: For log; select a target frame by name. frame_index: For log; current frame snapshot index, not a persistent identity. Source scripts run in the main world; logs are always read there. Same-origin child frames log into the top frame's log with frame_url/frame_depth, so a removed iframe keeps its events. filter_property_names: For AST — only rewrite reads/methods of these property names (e.g. ['userAgent', 'platform', 'webdriver']). filter_object_names: For AST — only rewrite when the static base object path matches (e.g. ['navigator', 'this.bytecode']). Dynamic object identity is not inferred. Regex mode rejects nonempty filters. max_file_size: For "install" — files larger than this (bytes) trigger on_oversized behavior. Default 2,000,000. on_oversized: For "install" — "selective" (require filters), "skip", or "force" (full rewrite anyway). Default "selective". source: For "rewrite_source" — JS text to rewrite. source_path: For "rewrite_source" — local JS file to rewrite. output_path: For "rewrite_source" — where to write the rewritten JS (must differ from source_path); with include_source_site the site map goes to<output_path>.sites.json`.
Returns:
dict with action-specific results. "log" also returns seq, dropped
(events lost to the 20000-entry cap) and the options each install used.
IMPORTANT — timing for sync-loaded scripts (e.g. webmssdk):
Route interception only catches requests made AFTER the route is
registered. For scripts loaded via <script src> during page load,
you MUST call instrumentation(action='install') BEFORE navigate().
Pattern:
1. launch_browser()
2. instrumentation(action='install', url_pattern='**/webmssdk*')
3. navigate("https://www.douyin.com/...")
If called after navigate, use instrumentation(action='reload') to
re-trigger page load with routes active.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"action": "<action>"
},
"name": "instrumentation"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| tag | No | vmp | |
| mode | No | ast | |
| clear | No | ||
| limit | No | ||
| stage | No | ||
| action | Yes | ||
| inject | No | ||
| source | No | ||
| after_seq | No | ||
| clear_log | No | ||
| frame_url | No | ||
| frame_name | No | ||
| ignore_csp | No | ||
| key_filter | No | ||
| seq_window | No | ||
| tag_filter | No | ||
| wait_until | No | load | |
| frame_index | No | ||
| output_path | No | ||
| source_path | No | ||
| type_filter | No | ||
| url_pattern | No | ||
| max_rewrites | No | ||
| on_oversized | No | selective | |
| max_file_size | No | ||
| rewrite_calls | No | ||
| inject_validate | No | ||
| tap_stage_calls | No | ||
| fallback_on_error | No | ||
| filter_object_names | No | ||
| include_source_site | No | ||
| filter_property_names | No | ||
| rewrite_member_access | No |