Skip to main content
Glama
muhenan

Xiaohongshu MCP Server

by muhenan

xiaohongshu_login

Log in to Xiaohongshu accounts using browser automation with optional headless mode and custom Chrome path for account management.

Instructions

登录小红书账号

Args: headless: 是否使用无头模式,默认False(建议使用有界面模式进行扫码) chrome_path: Chrome浏览器可执行文件路径,可选

Returns: 登录结果消息

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
chrome_pathNo
headlessNo

Implementation Reference

  • The main handler function for the 'xiaohongshu_login' MCP tool. It is decorated with @mcp.tool() for registration and uses type hints/docstring for schema. Orchestrates browser management and delegates login to XiaohongshuLogin class.
    @mcp.tool()
    async def xiaohongshu_login(
        headless: bool = False,
        chrome_path: Optional[str] = None
    ) -> str:
        """
        登录小红书账号
    
        Args:
            headless: 是否使用无头模式,默认False(建议使用有界面模式进行扫码)
            chrome_path: Chrome浏览器可执行文件路径,可选
    
        Returns:
            登录结果消息
        """
        try:
            logger.info("开始小红书登录流程...")
    
            # 创建浏览器管理器
            browser_manager = BrowserManager(
                headless=headless,
                chrome_path=chrome_path
            )
    
            async with browser_manager.get_page() as page:
                # 创建登录处理器
                login_handler = XiaohongshuLogin(page)
    
                # 执行登录
                success = await login_handler.login()
    
                if success:
                    logger.success("🎉 登录成功!")
                    return "✅ 小红书登录成功"
                else:
                    logger.error("❌ 登录失败")
                    return "❌ 小红书登录失败"
    
        except Exception as e:
            error_msg = f"登录过程中发生错误: {e}"
            logger.error(error_msg)
            return f"❌ {error_msg}"
  • The core login logic implementation in XiaohongshuLogin class, called by the tool handler. Handles navigation, QR code waiting, and login status detection.
    async def login(self) -> bool:
        """
        执行登录流程
    
        Returns:
            bool: 登录成功返回True,失败返回False
        """
        try:
            logger.info("开始登录流程...")
    
            # 导航到小红书探索页,这会触发登录弹窗
            await self.page.goto(self.base_url)
            await self.page.wait_for_load_state("networkidle")
    
            # 等待2秒让页面完全加载
            await asyncio.sleep(2)
    
            # 检查是否已经登录
            if await self.check_login_status():
                logger.info("已经处于登录状态,无需重新登录")
                return True
    
            logger.info("等待用户扫码登录...")
            logger.info("请在浏览器中扫描二维码完成登录")
    
            # 等待登录成功的元素出现,最多等待5分钟
            try:
                await self.page.wait_for_selector(
                    self.login_indicator,
                    timeout=300000  # 5分钟超时
                )
                logger.success("🎉 检测到登录成功!正在保存登录状态...")
                return True
    
            except PlaywrightTimeoutError:
                logger.error("❌ 登录超时,请检查是否完成扫码")
                return False
    
        except Exception as e:
            logger.error(f"登录过程中出错: {e}")
            return False
  • Helper method to check login status, used within the login process.
    async def check_login_status(self) -> bool:
        """
        检查登录状态
    
        Returns:
            bool: True表示已登录,False表示未登录
        """
        try:
            logger.info("正在检查登录状态...")
    
            # 导航到小红书探索页,使用更短的超时时间
            await self.page.goto(self.base_url, timeout=15000)
    
            # 只等待domcontentloaded,不等待networkidle
            await self.page.wait_for_load_state("domcontentloaded")
    
            # 等待页面基本元素加载
            await asyncio.sleep(2)
    
            # 尝试多种方式检查登录状态
            return await self._check_login_with_multiple_methods()
    
        except Exception as e:
            logger.error(f"检查登录状态时出错: {e}")
            return False

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/muhenan/xiaohongshu-mcp-python'

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