save_cookies_from_browser
Store browser cookies for persistent authentication in the YST KPI Daily Report Collector, enabling automated daily report generation without repeated logins.
Instructions
保存浏览器 Cookie(用于首次登录)
使用方法:
使用 chrome_devtools_mcp 登录 https://kpi.drojian.dev
登录成功后,从浏览器复制 Cookie 字符串
调用此工具保存 Cookie
Args: cookie_string: Cookie 字符串,格式如 "name1=value1; name2=value2" 或者完整的 curl 命令中的 -b 参数内容
Returns: 保存结果
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cookie_string | Yes |
Input Schema (JSON Schema)
{
"properties": {
"cookie_string": {
"type": "string"
}
},
"required": [
"cookie_string"
],
"type": "object"
}
Implementation Reference
- server.py:163-194 (handler)Main handler function for the 'save_cookies_from_browser' tool. Decorated with @mcp.tool() for automatic registration and schema inference. Parses the input cookie string and delegates saving to ReportCollector instance.@mcp.tool() async def save_cookies_from_browser(cookie_string: str) -> str: """ 保存浏览器 Cookie(用于首次登录) 使用方法: 1. 使用 chrome_devtools_mcp 登录 https://kpi.drojian.dev 2. 登录成功后,从浏览器复制 Cookie 字符串 3. 调用此工具保存 Cookie Args: cookie_string: Cookie 字符串,格式如 "name1=value1; name2=value2" 或者完整的 curl 命令中的 -b 参数内容 Returns: 保存结果 """ collector = ReportCollector() try: # 加载 Cookie if collector.load_cookies_from_string(cookie_string): # 保存到文件 if collector.save_current_cookies(): return safe_text("✓ Cookie 保存成功!现在可以使用 collect_reports 工具采集数据了") else: return safe_text("❌ Cookie 保存失败") else: return safe_text("❌ Cookie 格式错误") except Exception as e: return safe_text(f"保存失败: {str(e)}")
- report_collector.py:101-120 (helper)Helper method in ReportCollector that parses semicolon-separated cookie string into a dictionary and loads it into the requests session cookies.def load_cookies_from_string(self, cookie_string: str) -> bool: """ 从 Cookie 字符串加载(浏览器复制的格式) Args: cookie_string: Cookie 字符串,格式如 "name1=value1; name2=value2" Returns: 是否加载成功 """ try: cookie_dict = {} for item in cookie_string.split('; '): if '=' in item: name, value = item.split('=', 1) cookie_dict[name] = value return self.load_cookies_from_dict(cookie_dict) except Exception as e: print(f"解析 Cookie 字符串失败: {e}") return False
- report_collector.py:122-133 (helper)Helper method in ReportCollector that extracts current session cookies and persists them via CookieManager.def save_current_cookies(self) -> bool: """保存当前 session 的 Cookie""" cookies = [] for cookie in self.session.cookies: cookies.append({ 'name': cookie.name, 'value': cookie.value, 'domain': cookie.domain, 'path': cookie.path, }) return self.cookie_manager.save_cookies(cookies)