Skip to main content
Glama

open_port

Configure and open a serial port for device communication, supporting adjustable baud rate, data bits, parity, stop bits, flow control, timeouts, and automatic reconnection.

Instructions

打开指定串口,支持自定义配置参数

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
portYes串口路径,如 /dev/ttyUSB0 或 COM1
baudrateNo波特率,支持的值:[300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600]
bytesizeNo数据位,支持的值:[5, 6, 7, 8]
parityNo校验位,支持的值:['N', 'E', 'O', 'M', 'S']N
stopbitsNo停止位,支持的值:[1.0, 1.5, 2.0]
flow_controlNo流控制,支持的值:['none', 'hardware', 'software']none
read_timeout_msNo读取超时(毫秒),范围 0-60000
write_timeout_msNo写入超时(毫秒),范围 0-60000
auto_reconnectNo是否启用自动重连

Implementation Reference

  • The main handler function `open_port()` for the open_port tool. It takes serial port parameters (port, baudrate, bytesize, parity, stopbits, flow_control, timeouts, auto_reconnect), calls `SerialManager.open_port()`, and returns the port status as a dict.
    def open_port(
        port: str,
        baudrate: int = DEFAULT_BAUDRATE,
        bytesize: int = DEFAULT_BYTESIZE,
        parity: str = DEFAULT_PARITY.value,
        stopbits: float = float(DEFAULT_STOPBITS.value),
        flow_control: str = DEFAULT_FLOW_CONTROL.value,
        read_timeout_ms: int = DEFAULT_TIMEOUT_MS,
        write_timeout_ms: int = DEFAULT_WRITE_TIMEOUT_MS,
        auto_reconnect: bool = True,
    ) -> dict[str, Any]:
        """打开串口
    
        Args:
            port: 串口路径(如 /dev/ttyUSB0 或 COM1)
            baudrate: 波特率,默认 9600
            bytesize: 数据位,默认 8
            parity: 校验位,默认 N(无校验)
            stopbits: 停止位,默认 1
            flow_control: 流控制,默认 none
            read_timeout_ms: 读取超时(毫秒),默认 1000
            write_timeout_ms: 写入超时(毫秒),默认 1000
            auto_reconnect: 是否启用自动重连,默认 True
    
        Returns:
            串口状态信息
        """
        manager = get_serial_manager()
        status = manager.open_port(
            port=port,
            baudrate=baudrate,
            bytesize=bytesize,
            parity=parity,
            stopbits=stopbits,
            flow_control=flow_control,
            read_timeout_ms=read_timeout_ms,
            write_timeout_ms=write_timeout_ms,
            auto_reconnect=auto_reconnect,
        )
        return status.to_dict()
  • The `OPEN_PORT_TOOL` dictionary defining the MCP tool schema (name, description, inputSchema) for the open_port tool. Specifies all parameters including port (required), baudrate, bytesize, parity, stopbits, flow_control, read/write timeouts, and auto_reconnect.
    # 工具定义(用于 MCP 注册)
    OPEN_PORT_TOOL: dict[str, Any] = {
        "name": "open_port",
        "description": "打开指定串口,支持自定义配置参数",
        "inputSchema": {
            "type": "object",
            "properties": {
                "port": {
                    "type": "string",
                    "description": "串口路径,如 /dev/ttyUSB0 或 COM1",
                },
                "baudrate": {
                    "type": "integer",
                    "description": f"波特率,支持的值:{list(SUPPORTED_BAUDRATES)}",
                    "default": DEFAULT_BAUDRATE,
                },
                "bytesize": {
                    "type": "integer",
                    "description": f"数据位,支持的值:{list(SUPPORTED_BYTESIZES)}",
                    "default": DEFAULT_BYTESIZE,
                },
                "parity": {
                    "type": "string",
                    "description": f"校验位,支持的值:{[p.value for p in Parity]}",
                    "default": DEFAULT_PARITY.value,
                },
                "stopbits": {
                    "type": "number",
                    "description": f"停止位,支持的值:{[s.value for s in StopBits]}",
                    "default": float(DEFAULT_STOPBITS.value),
                },
                "flow_control": {
                    "type": "string",
                    "description": f"流控制,支持的值:{[f.value for f in FlowControl]}",
                    "default": DEFAULT_FLOW_CONTROL.value,
                },
                "read_timeout_ms": {
                    "type": "integer",
                    "description": "读取超时(毫秒),范围 0-60000",
                    "default": DEFAULT_TIMEOUT_MS,
                },
                "write_timeout_ms": {
                    "type": "integer",
                    "description": "写入超时(毫秒),范围 0-60000",
                    "default": DEFAULT_WRITE_TIMEOUT_MS,
                },
                "auto_reconnect": {
                    "type": "boolean",
                    "description": "是否启用自动重连",
                    "default": True,
                },
            },
            "required": ["port"],
        },
    }
  • Registration of the open_port tool in the `handle_list_tools()` function, exposing it as a `types.Tool` using the OPEN_PORT_TOOL schema.
    types.Tool(
        name=OPEN_PORT_TOOL["name"],
        description=OPEN_PORT_TOOL["description"],
        inputSchema=OPEN_PORT_TOOL["inputSchema"],
    ),
  • Route in `handle_call_tool()` that dispatches the 'open_port' name to the `open_port()` handler function with parsed arguments.
    elif name == "open_port":
        result = open_port(**arguments)
  • The core implementation `SerialManager.open_port()` that validates parameters via `_validate_and_create_config()`, checks blacklist, creates a pyserial Serial object, and returns a PortStatus.
    def open_port(
        self,
        port: str,
        baudrate: int | None = None,
        bytesize: int | None = None,
        parity: str | None = None,
        stopbits: float | None = None,
        flow_control: str | None = None,
        read_timeout_ms: int | None = None,
        write_timeout_ms: int | None = None,
        auto_reconnect: bool | None = None,
    ) -> PortStatus:
        """打开串口
    
        Args:
            port: 串口路径
            baudrate: 波特率(None 时使用配置默认值)
            bytesize: 数据位(None 时使用配置默认值)
            parity: 校验位(None 时使用配置默认值)
            stopbits: 停止位(None 时使用配置默认值)
            flow_control: 流控制(None 时使用配置默认值)
            read_timeout_ms: 读取超时(毫秒,None 时使用配置默认值)
            write_timeout_ms: 写入超时(毫秒,None 时使用配置默认值)
            auto_reconnect: 是否启用自动重连(None 时使用配置默认值)
    
        Returns:
            串口状态
    
        Raises:
            PortBlacklistedError: 串口在黑名单中
            PortNotFoundError: 串口不存在
            PortBusyError: 串口被占用
            InvalidParamError: 参数无效
        """
        # 检查黑名单
        blacklist = get_blacklist_manager()
        if blacklist.is_blacklisted(port):
            raise PortBlacklistedError(port)
    
        # 获取全局配置作为默认值
        global_config = get_config_manager().config
    
        # 使用用户参数或配置默认值
        final_baudrate = baudrate if baudrate is not None else global_config.baudrate
        final_bytesize = bytesize if bytesize is not None else global_config.bytesize
        final_parity = parity if parity is not None else global_config.parity
        final_stopbits = stopbits if stopbits is not None else global_config.stopbits
        # 流控:显式传入时优先使用传入值
        if flow_control is None:
            final_flow_control = (
                "software" if global_config.xonxoff else
                "hardware" if global_config.rtscts else
                "none"
            )
        else:
            final_flow_control = flow_control
        cc = global_config
        rt = read_timeout_ms
        wt = write_timeout_ms
        ar = auto_reconnect
        final_read_timeout = cc.read_timeout if rt is None else rt
        final_write_timeout = cc.write_timeout if wt is None else wt
        final_auto_reconnect = cc.auto_reconnect if ar is None else ar
    
        # 验证参数
        config = self._validate_and_create_config(
            final_baudrate,
            final_bytesize,
            final_parity,
            final_stopbits,
            final_flow_control,
            final_read_timeout,
            final_write_timeout,
        )
    
        with self._lock:
            # 检查是否已打开(幂等操作)
            if port in self._ports:
                managed = self._ports[port]
                logger.info("串口已打开,返回当前状态:%s", port)
                return PortStatus(
                    port=port,
                    is_open=True,
                    config=managed.config,
                    connected=managed.is_connected,
                    reconnecting=managed.reconnecting,
                )
    
            # 打开串口
            serial_obj = self._create_serial(port, config)
            managed = ManagedPort(port, serial_obj, config, final_auto_reconnect)
            self._ports[port] = managed
            logger.info("串口打开成功:%s", port)
    
            return PortStatus(
                port=port,
                is_open=True,
                config=config,
                connected=managed.is_connected,
                reconnecting=False,
            )
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations provided; the description does not disclose behavioral traits such as blocking behavior, whether reusing a port fails, or permission requirements. The description only restates the obvious.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness3/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single sentence, concise but overly terse. It could benefit from additional context without becoming verbose.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given 9 parameters and no output schema, the description is insufficient. It lacks information about return values, error conditions, or the effect on port state, leaving gaps for the agent.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already fully documents each parameter. The description adds no extra meaning beyond the schema, meeting the baseline.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('打开' meaning open) and resource ('指定串口' meaning specified serial port), and is distinct from siblings like close_port or read_data.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance on when to use this tool vs alternatives, no prerequisites or exclusions mentioned. For example, it doesn't advise checking available ports with list_ports first.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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

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