Files
rengine/mcp/tools/explain_finding.py
T
Yogesh OjhaandClaude Opus 5 0bb32328fa feat(mcp): agents can interrogate an attack surface, not just start one
Recon MCP servers expose one verb: go and scan. reconFTW v5 ships seven tools
and not one of them reads a result, because a pipeline that writes files has
nothing to query. reNgine's value is the correlated corpus, so this server is
read-first — fourteen tools for asking questions, of which four can act and
only two reach a target. Wrapping the OpenAPI document instead would have cost
276 tools and 112,600 tokens of definitions, and would still not work, because
every result endpoint needs a scan id an agent has no way to choose.

Everything lives in mcp/, mounted into the api the way stages/ and reports/
are. The protocol is implemented directly rather than through an SDK: a
tools-only server is a small JSON-RPC surface, this keeps the package free of
dependencies, and protocol.py is the only module that knows the wire format.
Tools are plug-and-play like stages and report sections — one file under
mcp/tools/, a Tool subclass whose Input model is both the schema the model
plans against and the schema the server validates with. Nothing registers it.
mcp/dimensions.py is the second axis, one entry per result dimension, so
query_assets, group_assets, surface_brief and describe_query_language each stay
short and a sixth dimension is a single entry.

ToolResult carries the product's contracts structurally. Every count comes back
with the query token that reproduces it, so a number an agent reports equals
the rows a human lands on. A dimension nobody has scanned is reported as not
scanned rather than as zero, which is why resolve_target resolves the scan that
actually covered each dimension instead of taking the newest one. Rows carrying
text the scanned party wrote are flagged untrusted, so the model reports them
rather than following them.

Access is an opaque service token, hashed at rest, bound to a project and a
capability set, revocable by row. Read, plan, write and launch are separate;
launch is the only one that reaches a machine you do not own and is off at the
instance ceiling until an operator raises it. No token may exceed the ceiling,
lowering it narrows the tokens that already exist, and tools/list returns only
what a token may call. Start and stop live on the MCP page rather than in a
config file — the server is mounted in the api, so stopped means refusing
connections rather than a supervised daemon, and the dialog says so: stopping
does not cancel a scan an agent already started.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J6fcWDtvxTer9chPWAwKd2
2026-09-06 18:24:35 +05:30

144 lines
5.1 KiB
Python

"""One finding, with the evidence and the remediation, minus the noise."""
from __future__ import annotations
import re
from pydantic import Field
from mcp import links
from mcp.context import ToolContext
from mcp.dimensions import dimension
from mcp.errors import ToolError
from mcp.result import ToolResult
from mcp.tools._scope import resolve
from mcp.tools.base import Tool, ToolGroup, ToolInput
from shared.definitions.surface import SurfaceDimension
MAX_LOCATIONS = 25
MAX_EVIDENCE_CHARS = 1200
_FINGERPRINT = re.compile(r"^[0-9a-f]{64}$")
_CVE = re.compile(r"^CVE-\d{4}-\d{4,}$", re.IGNORECASE)
class Input(ToolInput):
target: str = Field(description="The target the finding was reported on.")
finding: str = Field(
description=(
"A template id, a check name, a CVE, or a finding fingerprint. "
"Anything query_assets returned in template_id, template_name, "
"cve_ids or fingerprint works."
)
)
class ExplainFinding(Tool):
name = "explain_finding"
title = "Explain finding"
group = ToolGroup.EXPLAIN.value
description = (
"Everything reNgine knows about one finding: what the check tests for, why it "
"matters, how to fix it, its CVE/CWE/CVSS/EPSS/KEV signals, every place it "
"fires on this target, and the current triage decision. "
"Use it after query_assets to turn a row into an explanation."
)
Input = Input
examples = (
"explain_finding target=example.com finding=CVE-2021-44228",
"explain_finding target=example.com finding=apache-detect",
)
async def run(self, ctx: ToolContext, args: Input) -> ToolResult:
from app.services.vulnerability import VulnerabilityService # noqa: PLC0415
dim = dimension(SurfaceDimension.VULNERABILITIES.value)
scope = await resolve(ctx, args.target)
scan_id = scope.require(dim)
needle = args.finding.strip()
query = _query_for(needle)
f = dim.build_filter(query, limit=MAX_LOCATIONS, offset=0)
page = await dim.search(ctx.session, scan_id, f, scope.project_id)
if getattr(page, "error", None) or not page.items:
msg = (
f"No finding on {scope.target.target_value} matches {needle!r}. "
"Use query_assets with dimension=vulnerabilities to list what is there."
)
raise ToolError(msg)
service = VulnerabilityService(ctx.session)
detail = await service.get(scan_id, page.items[0].id)
if detail is None:
msg = "That finding could not be loaded."
raise ToolError(msg)
locations = [
{
"matched_at": row.matched_at,
"host": row.host,
"ip": row.ip,
"port": row.port,
"state": row.state,
"is_new": row.is_new,
}
for row in page.items
]
return ToolResult(
summary=(
f"{detail.severity.upper()} — {detail.template_name} on "
f"{scope.target.target_value}, {page.total} occurrence(s)"
),
data={
"check": {
"template_id": detail.template_id,
"name": detail.template_name,
"severity": detail.severity,
"scanner": detail.scanner,
"description": _trim(detail.description),
"impact": _trim(detail.impact),
"remediation": _trim(detail.remediation),
"references": (detail.references or [])[:8],
"tags": (detail.tags or [])[:12],
},
"risk": {
"cve_ids": detail.cve_ids or [],
"cwe_ids": detail.cwe_ids or [],
"cvss_score": detail.cvss_score,
"epss_score": detail.epss_score,
"epss_percentile": detail.epss_percentile,
"known_exploited": detail.is_kev,
},
"review": {"state": detail.state, "note": detail.note},
"occurrences": page.total,
"locations": locations,
"evidence": {
"matched_at": detail.matched_at,
"extracted": (detail.extracted_results or [])[:10],
"curl": _trim(detail.curl_command, 600),
},
},
pivot=links.scan_tab(
ctx.ui_base_url, scan_id, dim.tab, f"template:{detail.template_id}"
),
caveats=scope.caveat(dim),
untrusted=True,
)
def _query_for(needle: str) -> str:
quoted = needle.replace('"', '\\"')
if _FINGERPRINT.match(needle):
return f'name:"{quoted}" or template:"{quoted}"'
if _CVE.match(needle):
return f'cve:"{quoted}"'
return f'template:"{quoted}" or name:"{quoted}"'
def _trim(value: str | None, limit: int = MAX_EVIDENCE_CHARS) -> str | None:
if not value:
return None
text = value.strip()
return text if len(text) <= limit else text[:limit] + "…"