Skip to main content
Glama

get_processes_by_ports

Identify processes listening on specific network ports to monitor and debug local services by returning process names and PIDs.

Instructions

Return all running processes listening on the given ports.

Args: ports: List of port numbers to check.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
portsYes

Implementation Reference

  • server.py:9-46 (handler)
    Implementation of the get_processes_by_ports MCP tool handler.
    @mcp.tool()
    def get_processes_by_ports(ports: list[int]) -> str:
        """Return all running processes listening on the given ports.
    
        Args:
            ports: List of port numbers to check.
        """
        results = {}
    
        for port in ports:
            try:
                output = subprocess.check_output(
                    ["ss", "-tlnp", f"sport = :{port}"],
                    stderr=subprocess.STDOUT,
                    text=True,
                )
                lines = [l for l in output.strip().splitlines() if str(port) in l]
                processes = []
                for line in lines:
                    # Extract PID/process name from ss output: users:(("name",pid=N,fd=N))
                    if "users:((" in line:
                        users_part = line.split("users:((")[1].rstrip(")")
                        for entry in users_part.split("),("):
                            entry = entry.strip('(")')
                            parts = entry.split(",")
                            name = parts[0].strip('"')
                            pid = next(
                                (p.split("=")[1] for p in parts if p.strip().startswith("pid=")),
                                None,
                            )
                            processes.append({"name": name, "pid": pid})
                    else:
                        processes.append({"raw": line})
                results[port] = processes if processes else []
            except subprocess.CalledProcessError as e:
                results[port] = {"error": e.output.strip()}
    
        return json.dumps(results, indent=2)
Install Server

Other Tools

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/chalshik/mcp-infra'

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