Skip to main content
Glama

close_port

Close an active serial port connection to free system resources and prevent unauthorized access to UART devices.

Instructions

关闭指定串口连接

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
portYes串口路径

Implementation Reference

  • The main handler function for the 'close_port' tool. It retrieves the serial manager and calls its close_port method, returning the result as a dict.
    def close_port(port: str) -> dict[str, Any]:
        """关闭串口
    
        Args:
            port: 串口路径
    
        Returns:
            操作结果
        """
        manager = get_serial_manager()
        return manager.close_port(port)
  • The JSON schema definition for the 'close_port' tool input, defining the required 'port' parameter.
    CLOSE_PORT_TOOL: dict[str, Any] = {
        "name": "close_port",
        "description": "关闭指定串口连接",
        "inputSchema": {
            "type": "object",
            "properties": {
                "port": {
                    "type": "string",
                    "description": "串口路径",
                },
            },
            "required": ["port"],
        },
    }
  • Registration of the 'close_port' tool in the MCP server's list_tools handler.
    types.Tool(
        name=CLOSE_PORT_TOOL["name"],
        description=CLOSE_PORT_TOOL["description"],
        inputSchema=CLOSE_PORT_TOOL["inputSchema"],
    ),
  • Dispatch logic in the MCP server's call_tool handler that invokes the close_port function.
    elif name == "close_port":
        result = close_port(**arguments)
  • Core implementation in SerialManager that performs the actual port closing logic, removing the port from the managed ports and closing the serial connection.
    def close_port(self, port: str) -> dict[str, Any]:
        """关闭串口
    
        Args:
            port: 串口路径
    
        Returns:
            操作结果
    
        Raises:
            PortClosedError: 串口未打开
        """
        with self._lock:
            if port not in self._ports:
                raise PortClosedError(port)
    
            managed = self._ports.pop(port)
            try:
                managed.serial.close()
            except Exception as e:
                logger.warning("关闭串口时发生异常:%s - %s", port, e)
    
            logger.info("串口关闭成功:%s", port)
            return {"success": True, "port": port}

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/donnel666/uart-mcp'

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