accept_new_friend
Accept new WeChat friend requests by matching request content, with options to add remarks and tags for contact management.
Instructions
接受新的好友请求。参数: content (好友请求内容,必填), exact (是否精确匹配,默认false), remark (备注名,可选), tags (标签列表,可选)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | ||
| exact | No | ||
| remark | No | ||
| tags | No |
Implementation Reference
- Main handler function accept_new_friend that accepts new friend requests. The function takes content (required), exact match flag (optional), remark (optional), and tags (optional) parameters. It retrieves new friend requests, matches them by content using exact or fuzzy matching, and accepts the matched friend request using the friend.accept() method.
@tool( name="accept_new_friend", description="接受新的好友请求。参数: content (好友请求内容,必填), exact (是否精确匹配,默认false), remark (备注名,可选), tags (标签列表,可选)" ) def accept_new_friend(content: str, exact: bool = False, remark: Optional[str] = None, tags: Optional[list[str]] = None) -> str: """ 接受新的好友请求 Args: content: 好友请求内容(必填) exact: 是否精确匹配,默认 false(模糊匹配,包含即可) remark: 备注名(可选) tags: 标签列表(可选) Returns: JSON 格式的操作结果 """ if not content: return format_result(False, "好友请求内容不能为空") wrapper = _get_wrapper() if not wrapper: return format_result(False, "微信未初始化") try: # 获取新的好友请求列表 new_friends = wrapper.wx.GetNewFriends(acceptable=True) if not new_friends: return format_result(False, "暂无新的好友请求") # 查找对应的好友请求 found = False matched_content = None for friend in new_friends: friend_content = getattr(friend, 'content', '') # 根据 exact 参数决定匹配方式 if exact: # 精确匹配 if content == friend_content: matched_content = friend_content found = True break else: # 模糊匹配(包含即可) if content in friend_content: matched_content = friend_content found = True break if found: # 调用 friend.accept() 方法接受好友请求 friend.accept(remark=remark or "", tags=tags or []) return format_result( True, f"已接受好友请求: {matched_content[:50]}{'...' if len(matched_content) > 50 else ''}", {"content": matched_content, "remark": remark, "tags": tags} ) else: # 列出所有可用的好友请求内容,方便用户查找 available_contents = [getattr(f, 'content', '') for f in new_friends] return format_result( False, f"未找到匹配的好友请求: {content}", {"search_content": content, "available_requests": available_contents} ) except Exception as e: logger.error(f"接受好友请求失败: {e}") return format_result(False, f"接受好友请求失败: {e}") - src/wxauto_mcp/wxauto_mcp/api_friends.py:95-98 (registration)Tool registration using the @tool decorator with name='accept_new_friend' and a Chinese description of the parameters and functionality.
@tool( name="accept_new_friend", description="接受新的好友请求。参数: content (好友请求内容,必填), exact (是否精确匹配,默认false), remark (备注名,可选), tags (标签列表,可选)" ) - Helper function _get_wrapper() that retrieves the WeChatWrapper instance for interacting with 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 - src/wxauto_mcp/wxauto_mcp/rpc.py:51-80 (registration)The tool decorator function that registers MCP tools. It applies output limiting, registers the tool in TOOL_REGISTRY, and provides the registration mechanism for accept_new_friend.
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 - Helper function format_result() that formats operation results as JSON strings with success status, message, and optional data.
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)