MCPcopy Create free account
hub / github.com/modelscope/ms-agent / find_available_port

Function find_available_port

ms_agent/tools/code/code_executor.py:58–80  ·  view source on GitHub ↗

Find an available port starting from start_port. Args: start_port: Starting port number to check max_attempts: Maximum number of ports to try host: Host address to check Returns: Available port number, or None if no port found

(start_port: int = 8888,
                        max_attempts: int = 100,
                        host: str = '127.0.0.1')

Source from the content-addressed store, hash-verified

56
57
58def find_available_port(start_port: int = 8888,
59 max_attempts: int = 100,
60 host: str = '127.0.0.1') -> Optional[int]:
61 """
62 Find an available port starting from start_port.
63
64 Args:
65 start_port: Starting port number to check
66 max_attempts: Maximum number of ports to try
67 host: Host address to check
68
69 Returns:
70 Available port number, or None if no port found
71 """
72 for port in range(start_port, start_port + max_attempts):
73 if check_port_available(port, host):
74 logger.info(f'Found available port: {port}')
75 return port
76
77 logger.error(
78 f'Could not find available port in range {start_port}-{start_port + max_attempts - 1}'
79 )
80 return None
81
82
83class CodeExecutionTool(ToolBase):

Callers 1

connectMethod · 0.85

Calls 1

check_port_availableFunction · 0.70

Tested by

no test coverage detected