How the Agent-Reach Configure Command Extracts Cookies from Browsers Automatically
The agent-reach configure --from-browser sub-command calls configure_from_browser, which reads local browser cookie stores via rookiepy or browser_cookie3, filters the results against platform-specific rules in PLATFORM_SPECS, and persists the extracted values to ~/.agent-reach/config.yaml without manual copying.
The open-source Panniantong/Agent-Reach repository automates social-media agent authentication by enabling users to import session cookies directly from their desktop browsers. Understanding how the configure command extracts cookies from browsers automatically shows why the tool can bootstrap Twitter, XiaoHongShu, Bilibili, and Xueqiu channels with zero manual token entry.
How the Configure Command Automatically Extracts Browser Cookies
CLI Parsing and Dispatch in agent_reach/cli.py
When you run agent-reach configure --from-browser chrome, the CLI handler _cmd_configure parses the flag and forwards the browser name to configure_from_browser. The relevant dispatch logic resides in agent_reach/cli.py around lines 78–86 and 1017–1044. The handler instantiates a Config object that points to the user’s local YAML configuration file before invoking the extractor.
Browser Cookie Extraction via extract_all
The core extraction happens in agent_reach/cookie_extract.py inside extract_all. This helper first attempts to import the Rust-based rookiepy library for high-performance cookie reading. If that module is unavailable, it falls back to the pure-Python browser_cookie3 package. Either backend returns the full cookie jar from the selected browser profile, which extract_all then scans linearly. This fallback chain is implemented around lines 55–69 of agent_reach/cookie_extract.py.
Platform Specification Matching
Before scanning, extract_all loads PLATFORM_SPECS, a table defined near lines 15–41 of agent_reach/cookie_extract.py. Each entry declares a target platform—Twitter/X, XiaoHongShu, Bilibili, or Xueqiu—along with the required cookie domains and specific cookie names. As the iterator walks the browser cookie jar, it compares every cookie’s domain against these specs. For Twitter, it isolates auth_token and ct0; for XiaoHongShu, it assembles the full header-style cookie string. The matching loop appears around lines 122–148 of agent_reach/cookie_extract.py.
Persisting Extracted Cookies to the Config Store
Once extract_all returns a dictionary of platform credentials, configure_from_browser updates the in-memory Config instance. Twitter tokens are stored individually via config.set; XiaoHongShu receives the bundled cookie string as xhs_cookie; Bilibili gets SESSDATA and optionally bili_jct; Xueqiu is written only if xq_a_token is present. The method returns a list of (platform, success, message) tuples so the CLI can print human-readable results. This persistence logic spans roughly lines 202–267 of agent_reach/cookie_extract.py. After the call returns, _cmd_configure flushes the YAML file, completing the automatic extraction pipeline.
Supported Platforms and Required Cookies
- Twitter/X – requires
auth_tokenandct0for Bearer session negotiation. - XiaoHongShu – requires the full cookie header string for the domain.
- Bilibili – requires
SESSDATAfor API authentication and optionallybili_jctfor write operations. - Xueqiu – requires the full cookie string but only if
xq_a_tokenis detected.
Code Examples
Auto-extract from the terminal
agent-reach configure --from-browser chrome
Programmatic extraction in Python
from agent_reach.cookie_extract import configure_from_browser
from agent_reach.config import Config
cfg = Config() # loads ~/.agent-reach/config.yaml
results = configure_from_browser("chrome", cfg)
for platform, ok, msg in results:
status = "✅" if ok else "❌"
print(f"{platform}: {status} {msg}")
Typical dictionary returned by extract_all
{
"twitter": {"auth_token": "abc123", "ct0": "def456"},
"xhs": {"cookie_string": "a=1; b=2; ..."},
"bilibili": {"SESSDATA": "xyz", "bili_jct": "pqr"},
}
Key Source Files
- agent_reach/cookie_extract.py – houses
PLATFORM_SPECS,extract_all, andconfigure_from_browser. - agent_reach/cli.py – defines the
configuresub-command, flag parsing, and the_cmd_configurehandler. - agent_reach/config.py – lightweight YAML wrapper where extracted cookies are permanently stored.
Summary
- The
configure --from-browserflag triggers_cmd_configureinagent_reach/cli.py, which delegates toconfigure_from_browser. extract_allattemptsrookiepyand falls back tobrowser_cookie3to read the raw browser cookie jar.- Cookies are filtered against
PLATFORM_SPECSto isolate domain-specific credentials for Twitter, XiaoHongShu, Bilibili, and Xueqiu. - Valid tokens are written immediately to
~/.agent-reach/config.yamlvia theConfigclass, eliminating manual copy-pasting.
Frequently Asked Questions
Which browsers does the configure command support for automatic cookie extraction?
The exact set depends on the underlying backends. Because extract_all tries rookiepy first and falls back to browser_cookie3, any browser profile accessible to those libraries—commonly Chrome, Edge, Firefox, and Safari—can be targeted by passing the appropriate name to --from-browser.
Do I need to install rookiepy separately for the configure command to work?
No. The code around lines 55–69 of agent_reach/cookie_extract.py wraps the import in a try/except block. If rookiepy is missing, the module automatically falls back to browser_cookie3, so extraction still succeeds as long as one of the two packages is present in your environment.
How does the tool know which cookies belong to Twitter or XiaoHongShu?
extract_all references the PLATFORM_SPECS table defined near lines 15–41 of agent_reach/cookie_extract.py. This table maps platform names to their respective domains and required cookie keys, allowing the scanner to distinguish auth_token for Twitter from SESSDATA for Bilibili without user intervention.
Is the extracted cookie data stored securely?
The cookies are persisted as plain text in the user’s local ~/.agent-reach/config.yaml file through the Config class in agent_reach/config.py. The tool itself does not encrypt the file, so you should ensure that your home directory permissions restrict access to the configuration.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →