# =============================================================================
# Grammarly Browser Use MCP Server - Environment Configuration
# =============================================================================
# Copy this file to .env and fill in the values for your environment.
# IMPORTANT: Add .env to .gitignore — it contains secrets and must not be committed.
# All variables marked (required) must be set before the server will start.
# =============================================================================
# =============================================================================
# ENVIRONMENT ISOLATION (optional)
# =============================================================================
# When set to true, the server ONLY uses environment variables from this .env file,
# ignoring any system-level or shell-inherited environment variables.
#
# Use this when:
# - You have global API keys (e.g., OPENAI_API_KEY in ~/.zshrc) that you don't want used
# - You want deterministic, reproducible configuration
# - The MCP server is spawned by an IDE that inherits your full shell profile
#
# When false (default), system env vars are merged with .env (standard Node.js behavior).
# IGNORE_SYSTEM_ENV=false
# -----------------------------------------------------------------------------
# Browser Provider Selection (optional)
# -----------------------------------------------------------------------------
# Choose your browser automation provider:
# - "stagehand" (default): Uses Stagehand + Browserbase for deterministic,
# AI-powered browser automation with better reliability and self-healing.
# - "browser-use": Uses Browser Use Cloud (legacy, for backward compatibility).
#
# The provider you select determines which configuration section below is required.
BROWSER_PROVIDER=stagehand
# =============================================================================
# STAGEHAND + BROWSERBASE CONFIGURATION (required when BROWSER_PROVIDER=stagehand)
# =============================================================================
# Get your API key and project ID from https://www.browserbase.com/
# Browserbase provides cloud browser infrastructure with stealth mode,
# CAPTCHA solving, and persistent login contexts.
# Browserbase API key for authentication
BROWSERBASE_API_KEY=bb_REPLACE_WITH_YOUR_API_KEY
# Browserbase project ID (found in your Browserbase dashboard)
BROWSERBASE_PROJECT_ID=REPLACE_WITH_YOUR_PROJECT_ID
# -----------------------------------------------------------------------------
# Stagehand Session Persistence (optional - recommended for faster execution)
# -----------------------------------------------------------------------------
# Reuse an existing session ID to skip browser initialization (saves ~30s).
# Get this from a previous session or leave empty for new sessions.
# BROWSERBASE_SESSION_ID=
# Persistent context ID for login state (cookies, localStorage).
# Create once via Browserbase API, then reuse to avoid re-logging into Grammarly.
# This is the key to avoiding login overhead on every request.
# BROWSERBASE_CONTEXT_ID=
# =============================================================================
# STAGEHAND LLM PROVIDER (browser automation)
# =============================================================================
# Select the LLM provider for Stagehand browser automation (observe/act/extract).
# If not set, auto-detects based on available API keys (OpenAI > Google > Anthropic > Claude Code).
#
# Values: claude-code (uses CLI auth), openai, google, anthropic
# Default: auto-detect (falls back to claude-code if no API keys found)
# STAGEHAND_LLM_PROVIDER=google
# =============================================================================
# REWRITE LLM PROVIDER (text rewriting/analysis)
# =============================================================================
# Select the LLM provider for text rewriting, analysis, and summarization.
# This is SEPARATE from STAGEHAND_LLM_PROVIDER - you can use different providers!
#
# Examples:
# - Google for fast Stagehand automation + Claude for quality rewriting
# - Claude Code for everything (uses your Pro/Max subscription)
#
# Values: claude-code (uses CLI auth), openai, google, anthropic
# Default: auto-detect (falls back to claude-code if no API keys found)
# REWRITE_LLM_PROVIDER=claude-code
# =============================================================================
# CLAUDE MODEL SELECTION (when using claude-code provider)
# =============================================================================
# Control which Claude model is used when STAGEHAND_LLM_PROVIDER=claude-code
# or REWRITE_LLM_PROVIDER=claude-code.
#
# Default "auto" selects based on text length and iteration count:
# - haiku: text < 3k chars AND iterations <= 3 (fastest, cheapest)
# - sonnet: moderate complexity (balanced)
# - opus: text > 12k chars OR iterations > 8 (highest quality)
#
# Set to force a specific model regardless of text size:
# - haiku: Always use haiku (cost-optimized)
# - sonnet: Always use sonnet (balanced)
# - opus: Always use opus (quality-first)
#
# Values: auto (default), haiku, sonnet, opus
# CLAUDE_MODEL=auto
# =============================================================================
# NON-CLAUDE MODEL SELECTION (when using openai/google providers)
# =============================================================================
# Specify which model to use for OpenAI and Google providers.
# These apply to both STAGEHAND_LLM_PROVIDER and REWRITE_LLM_PROVIDER.
# OpenAI model (default: gpt-4o)
# OPENAI_MODEL=gpt-4o
# Google model (default: gemini-2.5-flash)
# GOOGLE_MODEL=gemini-2.5-flash
# Deprecated: STAGEHAND_MODEL is now controlled by the provider-specific model settings above.
# STAGEHAND_MODEL is still read for backwards compatibility but will be removed.
STAGEHAND_MODEL=gemini-2.5-flash
# Optional cache directory for Stagehand action caching.
# Enables 2-3x faster repeated actions by caching LLM responses.
# STAGEHAND_CACHE_DIR=./cache
# =============================================================================
# API KEYS
# =============================================================================
# Provide API keys for the providers you want to use.
# API keys are auto-detected for provider selection if not explicitly set.
# Google/Gemini API key (required when using Google models)
# Accepts either environment variable name:
# - GOOGLE_GENERATIVE_AI_API_KEY (recommended - Google AI SDK standard)
# - GEMINI_API_KEY (alternative)
# Get your API key from https://aistudio.google.com/apikey
# GOOGLE_GENERATIVE_AI_API_KEY=your_google_api_key_here
# OpenAI API key (required when using OpenAI models)
# Get your API key from https://platform.openai.com/api-keys
# OPENAI_API_KEY=sk-...
# Anthropic API key (required when using Anthropic provider directly)
# Get your API key from https://console.anthropic.com/
# ANTHROPIC_API_KEY=sk-ant-...
# =============================================================================
# BROWSER USE CLOUD CONFIGURATION (required when BROWSER_PROVIDER=browser-use)
# =============================================================================
# Get your API key from https://cloud.browser-use.com
# This key authenticates all Browser Use Cloud API requests.
BROWSER_USE_API_KEY=bu_your_api_key_here
# Browser Use profile ID containing your logged-in Grammarly session.
# Create a profile by syncing your browser session: npx browser-use-profile sync
# The profile stores Grammarly cookies/auth so the agent can access AI Detector
# and Plagiarism Checker features without manual login.
BROWSER_USE_PROFILE_ID=profile_your_profile_id_here
# =============================================================================
# CLAUDE AUTHENTICATION (optional - choose ONE method)
# =============================================================================
# Used for text rewriting and analysis when REWRITE_LLM_PROVIDER=claude-code.
# Option 1 (RECOMMENDED): Use Claude CLI authentication (Pro/Max subscription)
# Run 'claude login' once to authenticate, then leave CLAUDE_API_KEY unset.
# This uses your Claude Pro/Max subscription - no per-use billing.
# Also works with ai-sdk-provider-claude-code for Stagehand's LLM operations.
#
# Option 2: Use API key authentication (pay-per-use billing)
# Set CLAUDE_API_KEY below to use traditional API key billing.
#
# If CLAUDE_API_KEY is set, it takes precedence over CLI authentication.
# CLAUDE_API_KEY=sk-ant-...
# -----------------------------------------------------------------------------
# Logging Configuration (optional)
# -----------------------------------------------------------------------------
# Log level for server output (written to stderr, not stdout).
# MCP protocol messages use stdout; logs must go to stderr.
# Options: debug | info | warn | error
# Default: info
LOG_LEVEL=info
# -----------------------------------------------------------------------------
# Timeout Configuration (optional)
# -----------------------------------------------------------------------------
# LLM API request timeout in milliseconds (default: 120000 = 2 minutes)
# CLAUDE_REQUEST_TIMEOUT_MS=120000
# Browser connection timeout in milliseconds (default: 30000 = 30 seconds)
# CONNECT_TIMEOUT_MS=30000
# =============================================================================
# CONFIGURATION EXAMPLES
# =============================================================================
#
# Example 1: Claude Pro/Max Subscription Only (Cost-Controlled)
# -----------------------------------------------------------------------------
# BROWSER_PROVIDER=stagehand
# BROWSERBASE_API_KEY=bb_xxx
# BROWSERBASE_PROJECT_ID=xxx
# STAGEHAND_LLM_PROVIDER=claude-code
# REWRITE_LLM_PROVIDER=claude-code
# CLAUDE_MODEL=haiku # Always use cheapest model
#
# Example 2: Google for Stagehand, Claude for Rewriting (Mixed Providers)
# -----------------------------------------------------------------------------
# BROWSER_PROVIDER=stagehand
# BROWSERBASE_API_KEY=bb_xxx
# BROWSERBASE_PROJECT_ID=xxx
# GOOGLE_GENERATIVE_AI_API_KEY=xxx
# STAGEHAND_LLM_PROVIDER=google # Fast Gemini for browser automation
# GOOGLE_MODEL=gemini-2.5-flash
# REWRITE_LLM_PROVIDER=claude-code # Quality Claude for text rewriting
# CLAUDE_MODEL=sonnet
#
# Example 3: Full Environment Isolation (Hermetic Mode)
# -----------------------------------------------------------------------------
# IGNORE_SYSTEM_ENV=true # Only use values from this file
# BROWSER_PROVIDER=stagehand
# BROWSERBASE_API_KEY=bb_xxx
# BROWSERBASE_PROJECT_ID=xxx
# # No API keys = claude-code will be auto-detected for both
#
# =============================================================================
# -----------------------------------------------------------------------------
# Notes
# -----------------------------------------------------------------------------
# - The server exposes one MCP tool: grammarly_optimize_text
# - Grammarly Premium/Business is required for AI Detector and Plagiarism Checker
# - Text is processed through browser automation, Grammarly UI, and LLM API
# - All logs are written to stderr (stdout is reserved for MCP JSON-RPC)
# - Stagehand provides deterministic automation with observe()->act()->extract()
# - Session/context reuse can reduce init time from 30s to <5s