Skip to main content
Glama
cluic
by cluic

get_sessions

Retrieve a list of active WeChat chat windows to manage conversations and automate messaging workflows through the wxauto MCP Server.

Instructions

获取微信会话列表(聊天窗口列表)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The get_sessions MCP tool handler. Decorated with @tool to register as an MCP tool, it retrieves the WeChat instance, calls wrapper.get_sessions(), formats the SessionInfo objects into dictionaries, and returns a JSON-formatted result with success status, message, and sessions list.
    @tool(
        name="get_sessions",
        description="获取微信会话列表(聊天窗口列表)"
    )
    def get_sessions() -> str:
        """
        获取会话列表
    
        Returns:
            JSON 格式的会话列表
        """
        wrapper = _get_wrapper()
        if not wrapper:
            return format_result(False, "微信未初始化")
    
        try:
            sessions = wrapper.get_sessions()
            if not sessions:
                return format_result(True, "无会话数据", {"sessions": []})
    
            formatted = []
            for session in sessions:
                formatted.append({
                    "name": session.name,
                    "time": session.time,
                    "content": session.content,
                    "isnew": session.isnew,
                    "new_count": session.new_count,
                    "ismute": session.ismute,
                })
    
            return format_result(True, f"获取成功,共 {len(sessions)} 个会话", {"sessions": formatted})
        except Exception as e:
            logger.error(f"获取会话列表失败: {e}")
            return format_result(False, f"获取会话列表失败: {e}")
  • Tool registration using the @tool decorator. Registers the get_sessions function with the MCP server, specifying the tool name 'get_sessions' and description '获取微信会话列表(聊天窗口列表)'.
    @tool(
        name="get_sessions",
        description="获取微信会话列表(聊天窗口列表)"
    )
  • Core implementation of get_sessions in WeChatWrapper class. Calls self.wx.GetSession() from the wxauto library, iterates through the sessions, and creates SessionInfo objects with name, time, content, isnew, new_count, and ismute fields.
    def get_sessions(self) -> list[SessionInfo]:
        """
        获取会话列表
    
        Returns:
            list[SessionInfo]: 会话信息列表
        """
        try:
            sessions = self.wx.GetSession()
            result = []
            for session in sessions:
                info = session.info
                result.append(SessionInfo(
                    name=info.get("name", ""),
                    time=info.get("time"),
                    content=info.get("content"),
                    isnew=info.get("isnew", False),
                    new_count=info.get("new_count", 0),
                    ismute=info.get("ismute", False)
                ))
            return result
        except Exception as e:
            logger.error(f"获取会话列表失败: {e}")
            return []
  • SessionInfo dataclass schema. Defines the structure for WeChat session information including name (str), time (Optional[str]), content (Optional[str]), isnew (bool), new_count (int), and ismute (bool) fields.
    @dataclass
    class SessionInfo:
        """
        会话信息模型
    
        Attributes:
            name: 聊天名称
            time: 最后消息时间
            content: 最后一条消息内容
            isnew: 是否是新会话
            new_count: 未读消息数
            ismute: 是否免打扰
        """
        name: str
        time: Optional[str] = None
        content: Optional[str] = None
        isnew: bool = False
        new_count: int = 0
        ismute: bool = False
  • format_result helper function. Used by the get_sessions handler to format the response as a JSON string containing success status, message, and data fields.
    def format_result(success: bool, message: str, data: Optional[dict[str, Any]] = None) -> str:
        """
        格式化操作结果为 JSON 字符串
    
        Args:
            success: 操作是否成功
            message: 结果消息
            data: 附加数据
    
        Returns:
            JSON 格式的结果字符串
        """
        result = {
            "success": success,
            "message": message,
            "data": data or {},
        }
        return json.dumps(result, ensure_ascii=False, indent=2)

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/cluic/wxauto-mcp'

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