gc-httpx-mcp
Version:
Model Context Protocol (MCP) server for httpx - Fast HTTP toolkit for port scanning and service detection
76 lines (75 loc) • 2.63 kB
JavaScript
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { spawn } from 'node:child_process';
// Get httpx path from environment variable
const httpxPath = process.env.HTTPX_PATH;
if (!httpxPath) {
console.error("HTTPX_PATH environment variable not set");
process.exit(1);
}
// Create server instance
const server = new McpServer({
name: "httpx",
version: "1.0.0",
});
function removeAnsiCodes(str) {
// eslint-disable-next-line no-control-regex
return str.replace(/\x1b\[[0-9;]*m/g, '');
}
server.tool("httpx", "Scans the given target domains and detects active HTTP/HTTPS services on ports like 80 and 443.", {
target: z.array(z.string()).describe("A list of domain names (e.g., example.com) to scan for HTTP and HTTPS services."),
ports: z.array(z.number()).optional().describe("List of ports to scan"),
probes: z.array(z.string()).optional().describe("List of probes to use")
}, async ({ target, ports, probes }) => {
const httpxArgs = ["-u", target.join(","), "-silent"];
if (ports && ports.length > 0) {
httpxArgs.push("-p", ports.join(","));
}
if (probes && probes.length > 0) {
for (const probe of probes) {
httpxArgs.push(`-${probe}`);
}
}
let output = '';
const httpx = spawn(httpxPath, httpxArgs);
// Handle stdout
httpx.stdout.on('data', (data) => {
output += data.toString();
});
// Handle stderr
httpx.stderr.on('data', (data) => {
output += data.toString();
});
// Handle process completion
return new Promise((resolve, reject) => {
httpx.on('close', (code) => {
if (code === 0 || typeof code === "undefined") {
output = removeAnsiCodes(output);
resolve({
content: [{
type: "text",
text: output
}]
});
}
else {
reject(new Error(`httpx exited with code ${code}`));
}
});
httpx.on('error', (error) => {
reject(new Error(`Error to start httpx: ${error.message}`));
});
});
});
// Start the server
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("httpx MCP Server running on stdio");
}
main().catch((error) => {
console.error("Fatal error in main():", error);
process.exit(1);
});