port-scanner-mcp 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/api.js +145 -0
- package/dist/license.js +78 -0
- package/dist/server.js +33 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,3 +19,21 @@ node dist/index.js
|
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
Scans are bounded to 50 ports per call and 2 seconds per port. Scanning systems you do not own may be illegal where you live.
|
|
22
|
+
|
|
23
|
+
<!-- paywall -->
|
|
24
|
+
## Premium tools
|
|
25
|
+
|
|
26
|
+
These tools need a license key:
|
|
27
|
+
|
|
28
|
+
* `summarize_scan_results`
|
|
29
|
+
|
|
30
|
+
Buy a key at https://mcp-marketplace.io/server/io-github-mrfentmen-port-scanner-mcp and set it in your MCP client config:
|
|
31
|
+
|
|
32
|
+
```json
|
|
33
|
+
"env": { "MCP_LICENSE_KEY": "mcp_live_..." }
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The key is checked against MCP Marketplace, cached for 24 hours, and keeps
|
|
37
|
+
working offline once one check has succeeded. Every other tool on this server
|
|
38
|
+
stays free.
|
|
39
|
+
<!-- /paywall -->
|
package/dist/api.js
CHANGED
|
@@ -50,3 +50,148 @@ export async function scanCommon(args) {
|
|
|
50
50
|
? `Open ports on ${host}:\n${results.join("\n")}`
|
|
51
51
|
: `No common ports open on ${host}`;
|
|
52
52
|
}
|
|
53
|
+
// ---- muxB tools:
|
|
54
|
+
const MAX_PORTS = 4096;
|
|
55
|
+
const MAX_SPEC = 20000;
|
|
56
|
+
const MAX_REPORT = 200000;
|
|
57
|
+
const STATES = ["open", "closed", "filtered", "timeout", "refused", "unreachable"];
|
|
58
|
+
function pad(label, width = 20) {
|
|
59
|
+
return label.length >= width ? label : label + " ".repeat(width - label.length);
|
|
60
|
+
}
|
|
61
|
+
function runsOf(sorted) {
|
|
62
|
+
const runs = [];
|
|
63
|
+
for (const port of sorted) {
|
|
64
|
+
const last = runs[runs.length - 1];
|
|
65
|
+
if (last && port === last[1] + 1)
|
|
66
|
+
last[1] = port;
|
|
67
|
+
else
|
|
68
|
+
runs.push([port, port]);
|
|
69
|
+
}
|
|
70
|
+
return runs;
|
|
71
|
+
}
|
|
72
|
+
function formatRuns(runs, limit = 12) {
|
|
73
|
+
if (!runs.length)
|
|
74
|
+
return "none";
|
|
75
|
+
const shown = runs.slice(0, limit).map(([from, to]) => (from === to ? String(from) : `${from}-${to}`));
|
|
76
|
+
return shown.join(", ") + (runs.length > limit ? `, +${runs.length - limit} more` : "");
|
|
77
|
+
}
|
|
78
|
+
export async function parsePortSpec(args) {
|
|
79
|
+
const spec = (args.spec ?? "").trim();
|
|
80
|
+
if (!spec)
|
|
81
|
+
throw new Error("Provide a port specification in spec, for example 22,80,8000-8010");
|
|
82
|
+
if (spec.length > MAX_SPEC)
|
|
83
|
+
throw new Error(`Keep spec under ${MAX_SPEC} characters`);
|
|
84
|
+
const cap = Math.min(MAX_PORTS, Math.max(1, Math.trunc(Number(args.max_ports ?? MAX_PORTS) || MAX_PORTS)));
|
|
85
|
+
const accepted = [];
|
|
86
|
+
const rejected = [];
|
|
87
|
+
let duplicates = 0;
|
|
88
|
+
const seen = new Set();
|
|
89
|
+
for (const chunk of spec.split(/[\s,;]+/).filter(Boolean)) {
|
|
90
|
+
const range = /^(\d{1,5})\s*[-:.]{1,2}\s*(\d{1,5})$/.exec(chunk);
|
|
91
|
+
const single = /^(\d{1,5})$/.exec(chunk);
|
|
92
|
+
let from;
|
|
93
|
+
let to;
|
|
94
|
+
if (range) {
|
|
95
|
+
from = Number(range[1]);
|
|
96
|
+
to = Number(range[2]);
|
|
97
|
+
if (from > to) {
|
|
98
|
+
rejected.push(`${chunk} (range runs backwards)`);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (to - from + 1 > cap) {
|
|
102
|
+
rejected.push(`${chunk} (expands to more than ${cap} ports)`);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else if (single) {
|
|
107
|
+
from = Number(single[1]);
|
|
108
|
+
to = from;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
rejected.push(`${chunk} (not a port or a range)`);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (from < 1 || to > 65535) {
|
|
115
|
+
rejected.push(`${chunk} (outside 1-65535)`);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
for (let port = from; port <= to; port += 1) {
|
|
119
|
+
if (seen.has(port)) {
|
|
120
|
+
duplicates += 1;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
seen.add(port);
|
|
124
|
+
accepted.push(port);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
accepted.sort((a, b) => a - b);
|
|
128
|
+
const acceptedRuns = runsOf(accepted);
|
|
129
|
+
const out = [];
|
|
130
|
+
out.push(`Port specification expansion (${spec.length} characters of input)`);
|
|
131
|
+
out.push(`${pad("Accepted")}${accepted.length} distinct ports, lowest ${accepted[0] ?? "none"}, highest ${accepted[accepted.length - 1] ?? "none"}`);
|
|
132
|
+
out.push(`${pad("Ranges found")}${acceptedRuns.length} contiguous run${acceptedRuns.length === 1 ? "" : "s"}: ${formatRuns(acceptedRuns, 20)}`);
|
|
133
|
+
out.push(`${pad("Duplicates")}${duplicates} repeated port${duplicates === 1 ? "" : "s"} collapsed away`);
|
|
134
|
+
out.push(`${pad("Rejected")}${rejected.length} entr${rejected.length === 1 ? "y" : "ies"}`);
|
|
135
|
+
for (const item of rejected.slice(0, 12))
|
|
136
|
+
out.push(` - ${item}`);
|
|
137
|
+
if (rejected.length > 12)
|
|
138
|
+
out.push(` +${rejected.length - 12} more rejected entries not listed`);
|
|
139
|
+
out.push(`${pad("To scan")}${accepted.length ? `spec = "${accepted.slice(0, 40).join(",")}${accepted.length > 40 ? ",..." : ""}"` : "nothing valid to scan, fix the specification first"}`);
|
|
140
|
+
if (accepted.length > 40)
|
|
141
|
+
out.push(` the scan_host ports field holds the full list; ${accepted.length} ports is ${Math.ceil(accepted.length / 10)} times the 50 port cap in one call`);
|
|
142
|
+
return out.join("\n");
|
|
143
|
+
}
|
|
144
|
+
export async function summarizeScanResults(args) {
|
|
145
|
+
const report = args.report ?? "";
|
|
146
|
+
if (!report.trim())
|
|
147
|
+
throw new Error("Paste a scan report into report to summarise");
|
|
148
|
+
if (report.length > MAX_REPORT)
|
|
149
|
+
throw new Error(`Keep report under ${MAX_REPORT} characters`);
|
|
150
|
+
const counts = Object.fromEntries(STATES.map((state) => [state, 0]));
|
|
151
|
+
const open = [];
|
|
152
|
+
const all = [];
|
|
153
|
+
let parsed = 0;
|
|
154
|
+
let ignored = 0;
|
|
155
|
+
for (const line of report.split(/\r?\n/)) {
|
|
156
|
+
const match = /(?:^|\s)(\d{1,5})\s*[:=\-]?\s*(open|closed|filtered|timeout|refused|unreachable)\b/i.exec(line);
|
|
157
|
+
if (!match) {
|
|
158
|
+
if (line.trim())
|
|
159
|
+
ignored += 1;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
const port = Number(match[1]);
|
|
163
|
+
if (port < 1 || port > 65535) {
|
|
164
|
+
ignored += 1;
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const state = match[2].toLowerCase();
|
|
168
|
+
counts[state] += 1;
|
|
169
|
+
parsed += 1;
|
|
170
|
+
all.push(port);
|
|
171
|
+
if (state === "open")
|
|
172
|
+
open.push(port);
|
|
173
|
+
}
|
|
174
|
+
const out = [];
|
|
175
|
+
out.push(`Scan report summary (${report.length} characters pasted)`);
|
|
176
|
+
if (!parsed) {
|
|
177
|
+
out.push(`${pad("Result")}no "port: state" line was recognised, ${ignored} line${ignored === 1 ? "" : "s"} ignored`);
|
|
178
|
+
out.push("Expected lines look like \"8080: OPEN\" or \"443 closed\".");
|
|
179
|
+
return out.join("\n");
|
|
180
|
+
}
|
|
181
|
+
const total = parsed;
|
|
182
|
+
const openSorted = [...open].sort((a, b) => a - b);
|
|
183
|
+
out.push(`${pad("Parsed")}${parsed} port lines, ${ignored} line${ignored === 1 ? "" : "s"} ignored`);
|
|
184
|
+
out.push(`${pad("States")}${STATES.filter((state) => counts[state] > 0).map((state) => `${state} ${counts[state]}`).join(" ")}`);
|
|
185
|
+
out.push(`${pad("Open ratio")}${((counts.open / total) * 100).toFixed(1)}% of ${total} probed ports answered`);
|
|
186
|
+
const runs = runsOf(openSorted);
|
|
187
|
+
out.push(`${pad("Open ports")}${openSorted.length ? formatRuns(runs, 30) : "none"}`);
|
|
188
|
+
out.push(`${pad("Highest open")}${openSorted.length ? openSorted[openSorted.length - 1] : "not reported - no port answered"}`);
|
|
189
|
+
out.push(`${pad("Contiguous runs")}${runs.length} run${runs.length === 1 ? "" : "s"}: ${formatRuns(runs, 12)}`);
|
|
190
|
+
const longest = runs.reduce((max, run) => Math.max(max, run[1] - run[0] + 1), 0);
|
|
191
|
+
const isolated = runs.filter(([from, to]) => from === to).length;
|
|
192
|
+
out.push(`${pad("Pattern")}longest contiguous open run ${longest} port${longest === 1 ? "" : "s"}, ${isolated} isolated open port${isolated === 1 ? "" : "s"}`);
|
|
193
|
+
const sorted = [...new Set(all)].sort((a, b) => a - b);
|
|
194
|
+
out.push(`${pad("Coverage")}${new Set(all).size} distinct ports probed, spanning ${sorted.length ? `${sorted[0]} to ${sorted[sorted.length - 1]}` : "not reported"}`);
|
|
195
|
+
out.push(`${pad("Reading")}${counts.open === 0 ? "nothing answered, so either the host is down or every probed port is filtered" : longest >= 3 ? "a contiguous block answered, which usually means one service listening on a range" : counts.open <= 2 ? "only one or two ports answered, which is a small attack surface" : "several scattered ports answered, worth reviewing individually"}`);
|
|
196
|
+
return out.join("\n");
|
|
197
|
+
}
|
package/dist/license.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// Premium tools on this server need a license key bought from MCP Marketplace.
|
|
2
|
+
// Buyers put the key in their MCP client config as MCP_LICENSE_KEY. Every tool
|
|
3
|
+
// that is not listed in PREMIUM keeps working without a key.
|
|
4
|
+
const SLUG = "port-scanner-mcp";
|
|
5
|
+
// Tools that need a key. Everything else is free.
|
|
6
|
+
const PREMIUM = new Set([
|
|
7
|
+
"summarize_scan_results",
|
|
8
|
+
]);
|
|
9
|
+
const BUY_URL = `https://mcp-marketplace.io/server/io-github-mrfentmen-${SLUG}`;
|
|
10
|
+
// The license check calls the marketplace's verify endpoint directly instead of
|
|
11
|
+
// using @mcp_marketplace/license. That SDK sends no `apikey` header, so every
|
|
12
|
+
// check it makes is rejected by Supabase before it reaches the function. The
|
|
13
|
+
// publishable key below is public by design - it ships in mcp-marketplace.io's
|
|
14
|
+
// own JavaScript and only ever reaches their licence check.
|
|
15
|
+
const DEFAULT_VERIFY_URL = "https://virupvwhtkpkjsiskckg.supabase.co/functions/v1/verify-key";
|
|
16
|
+
const PUBLISHABLE_KEY = "sb_publishable_BuqlW96Ke8C_zzJG-LQv1Q_YHi_r_4h";
|
|
17
|
+
const OK_CACHE_MS = 60 * 60 * 1000;
|
|
18
|
+
const BAD_CACHE_MS = 60 * 1000;
|
|
19
|
+
const seen = new Map();
|
|
20
|
+
const REASONS = {
|
|
21
|
+
missing_key: "no key is set",
|
|
22
|
+
invalid_format: "that key is not a valid MCP Marketplace key",
|
|
23
|
+
not_found: "that key was not found",
|
|
24
|
+
revoked: "that key has been revoked",
|
|
25
|
+
rotated: "that key was rotated, use your new one",
|
|
26
|
+
expired: "that key has expired, renew it",
|
|
27
|
+
rate_limited: "there were too many checks just now, try again shortly",
|
|
28
|
+
network_error: "the license server could not be reached",
|
|
29
|
+
};
|
|
30
|
+
function blocked(tool, reason) {
|
|
31
|
+
const why = REASONS[reason] ?? `the license server answered "${reason}"`;
|
|
32
|
+
return `"${tool}" needs a license key, but ${why}. ` +
|
|
33
|
+
`Set MCP_LICENSE_KEY in your MCP client config. Get a key: ${BUY_URL}`;
|
|
34
|
+
}
|
|
35
|
+
async function verify(key) {
|
|
36
|
+
const res = await fetch(process.env.MCP_LICENSE_VERIFY_URL || DEFAULT_VERIFY_URL, {
|
|
37
|
+
method: "POST",
|
|
38
|
+
headers: {
|
|
39
|
+
apikey: PUBLISHABLE_KEY,
|
|
40
|
+
Authorization: `Bearer ${PUBLISHABLE_KEY}`,
|
|
41
|
+
"Content-Type": "application/json",
|
|
42
|
+
},
|
|
43
|
+
body: JSON.stringify({ key, slug: SLUG }),
|
|
44
|
+
// an unusable key answers 400 with a JSON body, so read the body either way
|
|
45
|
+
signal: AbortSignal.timeout(15000),
|
|
46
|
+
});
|
|
47
|
+
const data = (await res.json());
|
|
48
|
+
if (typeof data?.valid !== "boolean")
|
|
49
|
+
return { valid: false, reason: "unexpected_response" };
|
|
50
|
+
return data;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Returns null when the caller may run the tool, or a short message telling
|
|
54
|
+
* them how to get a key.
|
|
55
|
+
*
|
|
56
|
+
* A good key is remembered for an hour, so a busy session does not hit the
|
|
57
|
+
* license server on every call and keeps working through a brief outage.
|
|
58
|
+
*/
|
|
59
|
+
export async function premiumRequired(tool) {
|
|
60
|
+
if (!PREMIUM.has(tool))
|
|
61
|
+
return null;
|
|
62
|
+
const key = process.env.MCP_LICENSE_KEY;
|
|
63
|
+
if (!key)
|
|
64
|
+
return blocked(tool, "missing_key");
|
|
65
|
+
const hit = seen.get(key);
|
|
66
|
+
if (hit && Date.now() - hit.at < (hit.valid ? OK_CACHE_MS : BAD_CACHE_MS)) {
|
|
67
|
+
return hit.valid ? null : blocked(tool, hit.reason ?? "invalid");
|
|
68
|
+
}
|
|
69
|
+
let result;
|
|
70
|
+
try {
|
|
71
|
+
result = await verify(key);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
result = { valid: false, reason: "network_error" };
|
|
75
|
+
}
|
|
76
|
+
seen.set(key, { ...result, at: Date.now() });
|
|
77
|
+
return result.valid ? null : blocked(tool, result.reason ?? "invalid");
|
|
78
|
+
}
|
package/dist/server.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { scanCommon } from "./api.js";
|
|
4
|
-
import { scanHost } from "./api.js";
|
|
4
|
+
import { scanHost, summarizeScanResults, parsePortSpec } from "./api.js";
|
|
5
|
+
import { premiumRequired } from "./license.js";
|
|
5
6
|
const text = (value) => ({ content: [{ type: "text", text: value }] });
|
|
6
7
|
const textError = (t) => ({ content: [{ type: "text", text: t }], isError: true });
|
|
7
8
|
const READ_ONLY = { readOnlyHint: true, openWorldHint: true };
|
|
@@ -34,5 +35,36 @@ export function createServer() {
|
|
|
34
35
|
return textError(error(e));
|
|
35
36
|
}
|
|
36
37
|
});
|
|
38
|
+
server.registerTool("summarize_scan_results", {
|
|
39
|
+
title: "Summarize scan results",
|
|
40
|
+
description: "Summarise a scan report you paste back in: state counts, open ratio, open ports as contiguous runs, highest open port, longest run, isolated opens and probed coverage. Computed locally from the text you supply; no connection is attempted.",
|
|
41
|
+
inputSchema: z.object({ report: z.string().min(1).max(200000).describe("Scan output to summarise, for example \"80: OPEN\" lines, up to 200,000 characters.") }),
|
|
42
|
+
annotations: READ_ONLY,
|
|
43
|
+
}, async (args) => {
|
|
44
|
+
// ---- paywall: summarize_scan_results ----
|
|
45
|
+
const paywallMessage = await premiumRequired("summarize_scan_results");
|
|
46
|
+
if (paywallMessage)
|
|
47
|
+
return { content: [{ type: "text", text: paywallMessage }], isError: true };
|
|
48
|
+
// ---- paywall: end ----
|
|
49
|
+
try {
|
|
50
|
+
return text(await summarizeScanResults(args));
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
return textError(error(e));
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
server.registerTool("parse_port_spec", {
|
|
57
|
+
title: "Parse port spec",
|
|
58
|
+
description: "Expand a port specification you supply into a deduplicated sorted list: contiguous runs, duplicates collapsed, backwards and out-of-range entries rejected with a reason, and the port count the specification implies. No connection is attempted.",
|
|
59
|
+
inputSchema: z.object({ spec: z.string().min(1).max(20000).describe("Port specification such as 22,80,8000-8010, up to 20,000 characters."), max_ports: z.number().int().min(1).max(4096).optional().describe("Reject any single range wider than this many ports, 1-4096.") }),
|
|
60
|
+
annotations: READ_ONLY,
|
|
61
|
+
}, async (args) => {
|
|
62
|
+
try {
|
|
63
|
+
return text(await parsePortSpec(args));
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
return textError(error(e));
|
|
67
|
+
}
|
|
68
|
+
});
|
|
37
69
|
return server;
|
|
38
70
|
}
|
package/package.json
CHANGED