list_ports
Lists all available serial port devices with their device paths, descriptions, and hardware IDs to enable selection and management of serial connections.
Instructions
列出所有可用串口设备,返回设备路径、描述信息和硬件ID
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/uart_mcp/tools/list_ports.py:11-21 (handler)The main handler function for the list_ports tool. Calls the serial manager's list_ports() and converts PortInfo objects to dictionaries.
def list_ports() -> list[dict[str, str]]: """列出所有可用串口设备 返回系统中所有可用的串口设备列表,已过滤黑名单中的串口。 Returns: 串口信息列表,每个元素包含 port、description、hwid 字段 """ manager = get_serial_manager() ports = manager.list_ports() return [p.to_dict() for p in ports] - Tool definition/schema for list_ports, including name, description, and empty inputSchema (no parameters required).
LIST_PORTS_TOOL: dict[str, Any] = { "name": "list_ports", "description": "列出所有可用串口设备,返回设备路径、描述信息和硬件ID", "inputSchema": { "type": "object", "properties": {}, "required": [], }, } - src/uart_mcp/server.py:63-71 (registration)Registration in handle_list_tools() - the tool is listed as an available tool via types.Tool using LIST_PORTS_TOOL constants.
@server.list_tools() # type: ignore[no-untyped-call, untyped-decorator] async def handle_list_tools() -> list[types.Tool]: """返回可用工具列表""" return [ types.Tool( name=LIST_PORTS_TOOL["name"], description=LIST_PORTS_TOOL["description"], inputSchema=LIST_PORTS_TOOL["inputSchema"], ), - src/uart_mcp/server.py:148-149 (registration)Registration in handle_call_tool() dispatcher - when name=='list_ports', calls the list_ports() handler function and returns JSON result.
if name == "list_ports": result = list_ports() - The SerialManager.list_ports() helper method that queries pyserial for comports(), filters blacklisted ports, and returns PortInfo objects.
def list_ports(self) -> list[PortInfo]: """列出所有可用串口 返回系统中所有可用的串口,已过滤黑名单中的串口。 Returns: 串口信息列表 """ blacklist = get_blacklist_manager() ports: list[PortInfo] = [] for port_info in serial.tools.list_ports.comports(): if blacklist.is_blacklisted(port_info.device): logger.debug("串口在黑名单中,已过滤:%s", port_info.device) continue ports.append( PortInfo( port=port_info.device, description=port_info.description or "", hwid=port_info.hwid or "", ) ) return ports