nmap_service_detection
Identify running services and their versions on specified targets using network scanning capabilities provided by the Nmap MCP Server.
Instructions
Perform service and version detection scan
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| intensity | No | ||
| ports | No | common | |
| targets | Yes |
Implementation Reference
- server.py:121-124 (registration)Registration of the nmap_service_detection tool using the @app.tool decorator with name and description.@app.tool( name="nmap_service_detection", description="Perform service and version detection scan" )
- server.py:125-138 (handler)The handler function that constructs Nmap arguments for service/version detection (-sV with version intensity) and executes via run_nmap_command, returning formatted results.async def nmap_service_detection( targets: str, ports: str = "common", intensity: int = 7 ) -> str: """Perform service and version detection scan.""" args = ["-sV", f"--version-intensity={intensity}", "-p", ports, targets] result = run_nmap_command(args) if result["success"]: return f"Service detection scan completed:\n\n{result['stdout']}" else: return f"Service detection scan failed:\n\n{result['stderr']}"
- server.py:38-92 (helper)Shared helper function that runs the nmap subprocess command with given arguments, handles timeout and errors, and returns structured results used by the nmap_service_detection handler.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 }