close_session
Terminate an active serial port session to release system resources and prevent unauthorized access to connected devices.
Instructions
关闭指定的终端会话
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| session_id | Yes | 会话ID(即串口路径),如 /dev/ttyUSB0 或 COM1 |
Implementation Reference
- src/uart_mcp/tools/terminal.py:39-49 (handler)The top-level handler function for the 'close_session' tool. It retrieves the terminal manager and calls its close_session method.def close_session(session_id: str) -> dict[str, Any]: """关闭指定终端会话 Args: session_id: 会话ID(串口路径) Returns: 操作结果 """ manager = get_terminal_manager() return manager.close_session(session_id)
- Core implementation in TerminalManager that safely closes a session: removes it from the sessions dict, stops the read thread, and returns success.def close_session(self, session_id: str) -> dict[str, Any]: """关闭终端会话 Args: session_id: 会话ID(串口路径) Returns: 操作结果 Raises: SessionNotFoundError: 会话不存在 """ with self._lock: if session_id not in self._sessions: raise SessionNotFoundError(session_id) session = self._sessions.pop(session_id) session.stop() logger.info("关闭终端会话:%s", session_id) return {"success": True, "session_id": session_id}
- Tool schema definition including name, description, and input schema requiring 'session_id'.CLOSE_SESSION_TOOL: dict[str, Any] = { "name": "close_session", "description": "关闭指定的终端会话", "inputSchema": { "type": "object", "properties": { "session_id": { "type": "string", "description": "会话ID(即串口路径),如 /dev/ttyUSB0 或 COM1", }, }, "required": ["session_id"], }, }
- src/uart_mcp/server.py:108-112 (registration)Registration of the close_session tool in the MCP server's list_tools handler.types.Tool( name=CLOSE_SESSION_TOOL["name"], description=CLOSE_SESSION_TOOL["description"], inputSchema=CLOSE_SESSION_TOOL["inputSchema"], ),
- src/uart_mcp/server.py:165-166 (registration)Dispatch to the close_session handler in the MCP server's call_tool handler.elif name == "close_session": result = close_session(**arguments)