get_next_new_message
Retrieve the next unread message from chat windows in WeChat automation workflows, with optional filtering for muted conversations.
Instructions
获取下一个聊天窗口的新消息。参数: filter_mute (是否过滤消息免打扰,默认false)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filter_mute | No |
Implementation Reference
- Main handler implementation of get_next_new_message tool. Gets the next new messages from chat windows with optional mute filtering. Returns JSON formatted messages organized by chat name.
@tool( name="get_next_new_message", description="获取下一个聊天窗口的新消息。参数: filter_mute (是否过滤消息免打扰,默认false)" ) def get_next_new_message(filter_mute: bool = False) -> str: """ 获取下一个聊天窗口的新消息 Args: filter_mute: 是否过滤消息免打扰的会话 Returns: JSON 格式的消息列表 """ wrapper = _get_wrapper() if not wrapper: return format_result(False, "微信未初始化,请先调用 wechat_initialize") try: # 调用 GetNextNewMessage 方法 messages_dict = wrapper.wx.GetNextNewMessage(filter_mute=filter_mute) if not messages_dict: return format_result(True, "无新消息", {"messages": {}}) # 格式化结果 formatted = {} for chat_name, msg_list in messages_dict.items(): if isinstance(msg_list, list): formatted_msgs = [] for msg in msg_list[:50]: # 限制每个聊天最多50条消息 formatted_msgs.append({ "type": getattr(msg, 'type', 'unknown'), "content": getattr(msg, 'content', ''), "sender": getattr(msg, 'sender', ''), "time": getattr(msg, 'time', ''), }) formatted[chat_name] = formatted_msgs return format_result( True, f"获取成功,共 {len(formatted)} 个聊天有新消息", {"messages": formatted} ) except Exception as e: logger.error(f"获取新消息失败: {e}") return format_result(False, f"获取新消息失败: {e}") - src/wxauto_mcp/wxauto_mcp/api_messages.py:291-294 (registration)Tool registration using @tool decorator that registers the function with the MCP tool registry, providing the tool name and description.
@tool( name="get_next_new_message", description="获取下一个聊天窗口的新消息。参数: filter_mute (是否过滤消息免打扰,默认false)" ) - Helper function _get_wrapper() that retrieves the global WeChatWrapper instance. Used by get_next_new_message to access the underlying WeChat API.
def _get_wrapper(): """获取 WeChatWrapper 实例""" if not WECHAT_AVAILABLE: return None try: return get_wechat() except Exception as e: logger.error(f"获取 WeChat 实例失败: {e}") return None - Helper function format_result() that formats operation results as JSON strings. Used by get_next_new_message to return standardized success/error responses.
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) - Tool decorator implementation that registers functions to the TOOL_REGISTRY. The @tool decorator applies output limiting and registers the tool for MCP protocol discovery.
def tool(name: Optional[str] = None, description: Optional[str] = None) -> Callable: """ 工具装饰器 注册函数为 MCP 工具,自动应用输出限制。 Args: name: 工具名称,默认使用函数名 description: 工具描述 Returns: 装饰后的函数 """ def decorator(func: Callable) -> Callable: # 应用输出限制 limited_func = limit_output(func) # 注册到工具注册表 tool_name = name or func.__name__ TOOL_REGISTRY.register( name=tool_name, func=limited_func, description=description or func.__doc__ or "", ) # 返回原函数,不修改其行为 return func return decorator