Files
apk-reverse/skills/apk-reverse/scripts/mt_mcp_probe.py
T
inliver233 a1ada96cc3 Lint clean: adopt the PR's type/style gate, apply it to the current tree, and drop its path filter
PR #3 proposed adding mypy and flake8 to CI, with configurations for both. The idea is right and the
configurations are kept. Three things about the PR itself needed correcting, and all three were found
by running the gate rather than reading the diff:

  1. It was written against an older tree. Its own edits reference code that no longer exists there
     (`unknown, positions, warnings = ...`, a `struct` import at line 857 that is used 600 lines
     later), so merging it as-is would have removed a live import. The fixes were re-derived against
     the current tree instead, and the PR is superseded.
  2. It added a `paths:` filter to the workflow, watching only `*.py` plus the two lint configs. That
     would stop `check_refs.py` and `check_routing.py` -- documentation gates -- from running on a
     documentation-only commit, which is exactly the change they exist to catch. The workflow keeps
     the PR's `actions/*@v7` bump and gains no filter; the reason is written next to the trigger.
  3. `pip install --upgrade pip pytest` was dropped to just the packages, because upgrading pip in CI
     is not a goal and can fail for reasons unrelated to this repository.

What the gate found on the current tree, and what was done about it -- 70 flake8 findings and 3 mypy
errors, none of which were cosmetic-only:

  F401 (23)  unused imports, removed. These were safe to delete because the import statement itself
             cannot have a side effect that matters here.
  F841 (12)  unused bindings, handled three ways rather than one. Dead code was deleted
             (`mt_mcp_probe.print_waiting_help`'s `host`, `repack.py`'s `signer_jar`); a computed
             value that was then forgotten was **wired up** (`rasc_build.py` now names the linker
             target in its build-failure hint instead of discarding it); and a binding that is
             intentional kept its place with `# noqa: F841` and the reason -- `java2c_probe` reads
             dex header fields one by one against the format spec, and deleting three of them to
             please a linter would have made the parser stop lining up with the format it implements.
  E741 (14)  the ambiguous loop name `l`, renamed with an AST pass that resolves each binding to its
             own scope. A regex rename would have been wrong: several scopes bind their own `l`.
             That pass had a defect of its own -- it treated a comprehension target as living in the
             comprehension scope while leaving references to it in the enclosing one -- which
             produced seven `undefined name 'l'`. Found by running flake8, fixed, and the counting
             bug is documented in `check_routing.py`, whose own `tables()` had the same class of
             error in the other direction.
  style      continuation indents, blank lines between top-level definitions, and lines over length.
             The line limit is 160, not 120, and the number is a **measurement**: this repository's
             secret-scanner rule table holds raw regex source of 148 and 156 characters, which cannot
             be split without changing the pattern.
  mypy (3)   two missing annotations on module-level containers, and one optional-dependency rebind
             (`dexutil = None` after `except ImportError`) that is the repository's own idiom and
             now says so with a `type: ignore[assignment]` and the reason. No code was reordered to
             satisfy the checker.

Verified on a venv containing only `mypy` and `flake8` -- the poorest environment the configuration
must work in, and the one CI uses: **flake8 reports 0, mypy reports "Success: no issues found in 55
source files"**. Behaviour is unchanged: 625 passed / 163 skipped / 25 xfailed, the same five gates
green (check_repo 0 problem, check_refs 0 dangling, check_routing coherent, check_budget 0 failure,
check_commands 0 drift), and every key script still prints help.

  .flake8        new, with the line-length and complexity numbers and their reasons
  mypy.ini       new; optional imports declared, nothing excluded from the analysis
  ci.yml         `actions/*@v7`; new `lint` job on one interpreter; no path filter
  30 files       the findings above
2026-09-22 19:56:34 +08:00

225 lines
8.5 KiB
Python

#!/usr/bin/env python3
"""Probe the MT Manager built-in APK MCP server (Streamable HTTP).
MT Manager (bin.mt.plus) ships an "APK MCP" service that exposes its APK
analysis/edit/repack/sign capabilities to MCP clients over Streamable HTTP,
by default on device port 8787 (reachable from the PC via
``adb forward tcp:8787 tcp:8787``).
This script does the MCP handshake by hand (JSON-RPC 2.0 over plain HTTP
POSTs, no mcp SDK dependency):
1. ``initialize`` -> serverInfo + capabilities
2. ``notifications/initialized``
3. ``tools/list`` -> tool inventory
Output is a human-readable capability report grouping the ``mt_apk_*`` tools
by category. When the server is not up yet (it must be started by hand in
the MT UI; adb cannot start it), the script prints waiting instructions and
exits 2, so it can also be used as a "is it up yet" check in a loop.
Verification status of the protocol shape: measured against the MT 2.26.9
implementation on 2026-09 (see docs/tool-verification/EXTENSION-kernel-ondevice.md).
"""
import argparse
import json
import sys
import urllib.error
import urllib.request
DEFAULT_URL = "http://127.0.0.1:8787/mcp"
# Tool categories for the report, keyed by name prefix within mt_apk_*.
CATEGORIES = [
("open", "APK open/selection"),
("list", "Entry listing"),
("search", "Text/string search"),
("read", "Content read"),
("dex", "Dex analysis"),
("resource", "Resources"),
("native", "Native (.so) static analysis"),
("edit", "Edit sessions"),
("patch", "Byte/instruction patching"),
("build", "Repack + sign"),
("close", "Cleanup"),
]
def http_post(url, payload, session_id=None, timeout=10.0):
"""POST one JSON-RPC message; return (status, headers, parsed-body-or-None).
Handles both plain application/json responses and text/event-stream
(SSE-framed) responses, which Streamable HTTP servers may use for
request bodies. Returns the first JSON object found in either shape.
"""
body = json.dumps(payload).encode("utf-8")
headers = {
"Content-Type": "application/json",
"Accept": "application/json, text/event-stream",
}
if session_id:
headers["Mcp-Session-Id"] = session_id
req = urllib.request.Request(url, data=body, headers=headers, method="POST")
try:
with urllib.request.urlopen(req, timeout=timeout) as resp:
raw = resp.read().decode("utf-8", "replace")
sid = resp.headers.get("Mcp-Session-Id")
return resp.status, sid, parse_body(raw, resp.headers.get("Content-Type", ""))
except urllib.error.HTTPError as e:
raw = e.read().decode("utf-8", "replace")
return e.code, None, parse_body(raw, e.headers.get("Content-Type", ""))
def parse_body(raw, ctype):
"""Extract the first JSON message from a JSON or SSE-framed body."""
raw = raw.strip()
if not raw:
return None
if "text/event-stream" in (ctype or ""):
for line in raw.splitlines():
if line.startswith("data:"):
chunk = line[len("data:"):].strip()
try:
return json.loads(chunk)
except ValueError:
continue
return None
try:
return json.loads(raw)
except ValueError:
return None
def print_waiting_help(url):
print("[waiting] MCP server is not answering at %s" % url)
print()
print("The MT APK MCP must be started by hand in the MT UI (adb cannot start it):")
print(" 1. On the phone: MT Manager -> side drawer -> Tools -> APK MCP -> Start")
print(" 2. Note the address shown on that page (host:port, default 8787)")
print(" 3. On the PC, make the port reachable over USB:")
print(" adb forward tcp:8787 tcp:8787")
print(" 4. Re-run this probe: python mt_mcp_probe.py")
print()
print("(LAN alternative: connect the PC and phone to the same network and pass")
print(" --url http://<phone-ip>:8787/mcp)")
def main():
ap = argparse.ArgumentParser(
description="Probe the MT Manager APK MCP (Streamable HTTP) and list its tools.")
ap.add_argument("--url", default=DEFAULT_URL,
help="MCP endpoint (default: %(default)s)")
ap.add_argument("--timeout", type=float, default=10.0,
help="per-request timeout in seconds (default: %(default)s)")
ap.add_argument("--json", action="store_true",
help="print raw JSON-RPC responses instead of the report")
args = ap.parse_args()
# 1) initialize
init = {
"jsonrpc": "2.0", "id": 1, "method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {"name": "mt-mcp-probe", "version": "0.1"},
},
}
try:
status, session_id, body = http_post(args.url, init, timeout=args.timeout)
except (urllib.error.URLError, OSError) as e:
print_waiting_help(args.url)
if args.json:
print("[error] %s" % e)
return 2
if body is None or "result" not in (body or {}):
print("[unexpected] HTTP %d, no MCP initialize result in response" % status)
if args.json:
print(json.dumps(body, indent=2) if body is not None else "(empty body)")
return 1
result = body["result"]
server = result.get("serverInfo", {})
proto = result.get("protocolVersion", "?")
print("[online] %s | server %s %s | protocol %s | HTTP %d%s" % (
args.url, server.get("name", "?"), server.get("version", "?"),
proto, status, " | session %s" % session_id if session_id else ""))
if args.json:
print(json.dumps(result, indent=2))
# 2) initialized notification (no id -> notification; 202 expected, body ignored)
note = {"jsonrpc": "2.0", "method": "notifications/initialized"}
try:
http_post(args.url, note, session_id=session_id, timeout=args.timeout)
except (urllib.error.URLError, OSError):
pass # some servers accept it silently; tools/list will tell us
# 3) tools/list
listing = {"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}
try:
status, _, body = http_post(args.url, listing, session_id=session_id,
timeout=args.timeout)
except (urllib.error.URLError, OSError) as e:
print("[error] tools/list failed after successful initialize: %s" % e)
return 1
if body is None or "result" not in (body or {}):
print("[unexpected] HTTP %d, no tools/list result" % status)
if args.json:
print(json.dumps(body, indent=2) if body is not None else "(empty body)")
return 1
tools = body["result"].get("tools", [])
if args.json:
print(json.dumps(body, indent=2))
# 4) capability report
print()
print("== MCP tool inventory: %d tool(s) ==" % len(tools))
mt_tools = [t for t in tools if t.get("name", "").startswith("mt_apk_")]
other = [t for t in tools if not t.get("name", "").startswith("mt_apk_")]
grouped = {prefix: [] for prefix, _ in CATEGORIES}
for t in sorted(mt_tools, key=lambda x: x.get("name", "")):
name = t.get("name", "")
suffix = name[len("mt_apk_"):]
placed = False
for prefix, label in CATEGORIES:
if suffix == prefix or suffix.startswith(prefix + "_"):
grouped[prefix].append(t)
placed = True
break
if not placed:
grouped.setdefault("misc", []).append(t)
for prefix, label in CATEGORIES:
items = grouped.get(prefix, [])
if not items:
continue
print("\n[%s] %s" % (label, len(items)))
for t in items:
desc = (t.get("description") or "").strip().splitlines()
first = desc[0].strip() if desc else ""
print(" %-42s %s" % (t.get("name"), first[:90]))
if grouped.get("misc"):
print("\n[other mt_apk_*] %d" % len(grouped["misc"]))
for t in grouped["misc"]:
print(" %s" % t.get("name"))
if other:
print("\n[non-mt_apk tools] %d" % len(other))
for t in other:
print(" %s" % t.get("name"))
print()
print("Constraints (from the official MT MCP docs, see on-device-tooling.md):")
print(" - no Java decompilation; read smali directly instead")
print(" - .so support is static analysis only (no dynamic run, no pseudo-C)")
print(" - resources.arsc: existing entries editable, no new locales/entries")
return 0
if __name__ == "__main__":
sys.exit(main())