Skip to main content
Glama
mohdhaji87

Nmap MCP Server

by mohdhaji87

nmap_custom_scan

Execute tailored Nmap scans with custom options to identify network hosts, services, and vulnerabilities. Input targets and specific parameters for precise network analysis.

Instructions

Perform custom Nmap scan with user-defined options

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
custom_optionsYes
output_formatNonormal
targetsYes

Implementation Reference

  • The main handler function for the nmap_custom_scan tool. It accepts targets, custom nmap options as a string, and optional output format. It constructs the nmap argument list, calls the run_nmap_command helper, and returns the formatted results or error.
    async def nmap_custom_scan( targets: str, custom_options: str, output_format: str = "normal" ) -> str: """Perform custom Nmap scan with user-defined options.""" # Parse custom options args = custom_options.split() # Add output format if specified if output_format == "xml": args.append("-oX") args.append("-") elif output_format == "grepable": args.append("-oG") args.append("-") args.append(targets) result = run_nmap_command(args) if result["success"]: return f"Custom scan completed:\n\n{result['stdout']}" else: return f"Custom scan failed:\n\n{result['stderr']}"
  • server.py:321-324 (registration)
    Registration of the nmap_custom_scan tool using the FastMCP @app.tool decorator, specifying the name and description.
    @app.tool( name="nmap_custom_scan", description="Perform custom Nmap scan with user-defined options" )
  • Shared utility function that runs the nmap subprocess with given arguments, handles timeout and errors, and returns structured output including stdout, stderr, exit code, and success flag. Used by all nmap tools including nmap_custom_scan.
    def run_nmap_command(args: List[str], timeout: int = 300) -> Dict[str, Any]: """ Execute an nmap command and return the results. Args: args: List of nmap command arguments timeout: Command timeout in seconds Returns: Dictionary containing command output, error, and exit code """ try: # Construct the full nmap command cmd = ["nmap"] + args logger.info(f"Executing nmap command: {' '.join(cmd)}") # Run the command with timeout result = subprocess.run( cmd, capture_output=True, text=True, timeout=timeout, check=False ) return { "stdout": result.stdout, "stderr": result.stderr, "exit_code": result.returncode, "success": result.returncode == 0 } except subprocess.TimeoutExpired: return { "stdout": "", "stderr": f"Command timed out after {timeout} seconds", "exit_code": -1, "success": False } except FileNotFoundError: return { "stdout": "", "stderr": "nmap command not found. Please ensure nmap is installed and in PATH", "exit_code": -1, "success": False } except Exception as e: return { "stdout": "", "stderr": f"Error executing nmap command: {str(e)}", "exit_code": -1, "success": False }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mohdhaji87/Nmap-MCP-Server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server