Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
feat(SKY-8672): add browser profile MCP and CLI auth reuse (#5632)
Marc Kelechava515f632849fb
diff --git a/skyvern/cli/browser_profile.py b/skyvern/cli/browser_profile.pynew file mode 100644index 000000000..f1273b501--- /dev/null+++ b/skyvern/cli/browser_profile.py@@ -0,0 +1,138 @@+"""Browser-profile CLI commands for cloud saved-login state."""++from __future__ import annotations++from typing import Any++import typer+from dotenv import load_dotenv++from skyvern.config import settings+from skyvern.utils.env_paths import resolve_backend_env_path++from .commands._output import run_tool+from .mcp_tools.browser_profile import skyvern_browser_profile_create as tool_browser_profile_create+from .mcp_tools.browser_profile import skyvern_browser_profile_delete as tool_browser_profile_delete+from .mcp_tools.browser_profile import skyvern_browser_profile_get as tool_browser_profile_get+from .mcp_tools.browser_profile import skyvern_browser_profile_list as tool_browser_profile_list++browser_profile_app = typer.Typer(+ help=(+ "Manage cloud saved-login browser profiles. "+ "Create one from a persisted workflow run or eligible browser session, "+ "then reuse its bp_ ID on workflow runs or cloud sessions."+ ),+ no_args_is_help=True,+)+++@browser_profile_app.callback()+def browser_profile_callback(+ api_key: str | None = typer.Option(+ None,+ "--api-key",+ help="Skyvern API key",+ envvar="SKYVERN_API_KEY",+ ),+) -> None:+ """Load browser-profile CLI environment and optional API key override."""+ load_dotenv(resolve_backend_env_path())+ if api_key:+ settings.SKYVERN_API_KEY = api_key+++@browser_profile_app.command("list")+def browser_profile_list(+ include_deleted: bool = typer.Option(False, "--include-deleted", help="Include soft-deleted profiles."),+ json_output: bool = typer.Option(False, "--json", help="Output as JSON."),+) -> None:+ """List cloud browser profiles for reusable authenticated state."""++ async def _run() -> dict[str, Any]:+ return await tool_browser_profile_list(include_deleted=include_deleted)++ run_tool(+ _run,+ json_output=json_output,+ hint_on_exception="Check your API key and Skyvern connection.",+ action="skyvern_browser_profile_list",+ telemetry_tool_name="skyvern_browser_profile_list",+ )+++@browser_profile_app.command("get")+def browser_profile_get(+ browser_profile_id: str = typer.Option(..., "--id", "--browser-profile-id", help="Browser profile ID (bp_...)."),+ json_output: bool = typer.Option(False, "--json", help="Output as JSON."),+) -> None:+ """Get a cloud browser profile by ID."""++ async def _run() -> dict[str, Any]:+ return await tool_browser_profile_get(browser_profile_id=browser_profile_id)++ run_tool(+ _run,+ json_output=json_output,+ hint_on_exception="Check the browser profile ID and your API key.",+ action="skyvern_browser_profile_get",+ telemetry_tool_name="skyvern_browser_profile_get",+ )+++@browser_profile_app.command("create")+def browser_profile_create(+ name: str = typer.Option(..., "--name", help="Human-readable name for the saved login browser profile."),+ workflow_run_id: str | None = typer.Option(+ None,+ "--workflow-run-id",+ "--from-run",+ help=(+ "Workflow run ID (wr_...) whose persisted browser state should be archived. "+ "The workflow definition must have persist_browser_session=true."+ ),+ ),+ browser_session_id: str | None = typer.Option(+ None,+ "--browser-session-id",+ "--from-session",+ help="Persistent browser session ID (pbs_...) to snapshot into a cloud browser profile.",+ ),+ description: str | None = typer.Option(None, "--description", help="Optional profile description."),+ json_output: bool = typer.Option(False, "--json", help="Output as JSON."),+) -> None:+ """Create a cloud saved-login browser profile from exactly one source."""++ async def _run() -> dict[str, Any]:+ return await tool_browser_profile_create(+ name=name,+ browser_session_id=browser_session_id,+ workflow_run_id=workflow_run_id,+ description=description,+ )++ run_tool(+ _run,+ json_output=json_output,+ hint_on_exception="Check the source run/session ID and whether its browser archive is ready.",+ action="skyvern_browser_profile_create",+ telemetry_tool_name="skyvern_browser_profile_create",+ )+++@browser_profile_app.command("delete")+def browser_profile_delete(+ browser_profile_id: str = typer.Option(..., "--id", "--browser-profile-id", help="Browser profile ID (bp_...)."),+ json_output: bool = typer.Option(False, "--json", help="Output as JSON."),+) -> None:+ """Delete a cloud browser profile by ID."""++ async def _run() -> dict[str, Any]:+ return await tool_browser_profile_delete(browser_profile_id=browser_profile_id)++ run_tool(+ _run,+ json_output=json_output,+ hint_on_exception="Check the browser profile ID and your API key.",+ action="skyvern_browser_profile_delete",+ telemetry_tool_name="skyvern_browser_profile_delete",+ )diff --git a/skyvern/cli/commands/__init__.py b/skyvern/cli/commands/__init__.pyindex 2df09a223..14df00155 100644--- a/skyvern/cli/commands/__init__.py+++ b/skyvern/cli/commands/__init__.py@@ -35,6 +35,12 @@ register_lazy_command("Read and update organization settings (max_steps_per_run, webhook URL, retries, artifact URL expiry).",)register_lazy_command("workflow", "skyvern.cli.workflow", "workflow_app", "Workflow management commands.")+register_lazy_command(+ "browser-profile",+ "skyvern.cli.browser_profile",+ "browser_profile_app",+ "Manage cloud saved-login browser profiles for reusable authenticated state.",+)register_lazy_command("schedule","skyvern.cli.schedule_command",diff --git a/skyvern/cli/commands/browser.py b/skyvern/cli/commands/browser.pyindex bf375dca8..8118370d8 100644--- a/skyvern/cli/commands/browser.py+++ b/skyvern/cli/commands/browser.py@@ -189,6 +189,11 @@ def _handle_tool_error(e: Exception, *, tool: str, hint: str, json_output: bool)def session_create(timeout: int = typer.Option(60, help="Session timeout in minutes."),proxy: str | None = typer.Option(None, help="Proxy location (e.g. RESIDENTIAL)."),+ browser_profile_id: str | None = typer.Option(+ None,+ "--browser-profile-id",+ help="Cloud browser profile ID (bp_...) with saved authenticated state to load.",+ ),local: bool = typer.Option(False, "--local", help="Launch a local browser instead of cloud."),headless: bool = typer.Option(False, "--headless", help="Run local browser headless."),json_output: bool = typer.Option(False, "--json", help="Output as JSON."),@@ -207,13 +212,17 @@ def session_create(skyvern,timeout=timeout,proxy_location=proxy,+ browser_profile_id=browser_profile_id,)save_state(CLIState(session_id=result.session_id, cdp_url=None, mode="cloud"))- return {+ data = {"session_id": result.session_id,"mode": "cloud","timeout_minutes": result.timeout_minutes,}+ if result.browser_profile_id:+ data["browser_profile_id"] = result.browser_profile_id+ return datatry:data = asyncio.run(_run())diff --git a/skyvern/cli/core/session_ops.py b/skyvern/cli/core/session_ops.pyindex d495bbc43..fd28ac08c 100644--- a/skyvern/cli/core/session_ops.py+++ b/skyvern/cli/core/session_ops.py@@ -16,6 +16,7 @@ class SessionCreateResult:local: bool = Falseheadless: bool = Falsetimeout_minutes: int | None = None+ browser_profile_id: str | None = None@dataclass@@ -65,20 +66,28 @@ async def do_session_create(extensions: list[Extensions] | None = None,local: bool = False,headless: bool = False,+ browser_profile_id: str | None = None,) -> tuple[Any, SessionCreateResult]:"""Create browser session. Returns (browser, result)."""if local:+ if browser_profile_id:+ raise ValueError("browser_profile_id is only supported for cloud browser sessions")browser = await skyvern.launch_local_browser(headless=headless)return browser, SessionCreateResult(session_id=None, local=True, headless=headless)proxy = coerce_proxy_location(proxy_location)launch_kwargs: dict[str, Any] = {"timeout": timeout, "proxy_location": proxy}+ if browser_profile_id is not None:+ launch_kwargs["browser_profile_id"] = browser_profile_idif extensions is not None:launch_kwargs["extensions"] = extensionsbrowser = await skyvern.launch_cloud_browser(**launch_kwargs)+ # SkyvernBrowser does not expose browser_profile_id today; surface the+ # requested id so callers (CLI result, MCP data) can display what was sent.return browser, SessionCreateResult(session_id=browser.browser_session_id,timeout_minutes=timeout,+ browser_profile_id=browser_profile_id,)diff --git a/skyvern/cli/mcp_tools/__init__.py b/skyvern/cli/mcp_tools/__init__.pyindex 47173af0b..ea97b020b 100644--- a/skyvern/cli/mcp_tools/__init__.py+++ b/skyvern/cli/mcp_tools/__init__.py@@ -75,6 +75,12 @@ from .browser import (skyvern_validate,skyvern_wait,)+from .browser_profile import (+ skyvern_browser_profile_create,+ skyvern_browser_profile_delete,+ skyvern_browser_profile_get,+ skyvern_browser_profile_list,+)from .credential import (skyvern_credential_delete,skyvern_credential_get,@@ -265,6 +271,7 @@ file_upload, find, navigate, screenshot, evaluate**Tier 3 — Management** (no session needed):- **Sessions:** browser_session_create/close/list/get/connect- **Workflows:** workflow_create/run/status/get/list/update/delete/cancel/update_folder+- **Browser Profiles:** browser_profile_list/get/create/delete for cloud saved-login management- **Schedules:** schedule_list/list_for_workflow/get/create/update/enable/disable/delete- **Scripts:** script_list_for_workflow, script_get_code, script_versions, script_fallback_episodes, script_deploy- **Credentials:** credential_list/get/delete@@ -289,6 +296,15 @@ Use skyvern_browser_session_connect(cdp_url="...") to attach to an existing browMulti-tab flow: tab_list -> tab_new or click link -> tab_wait_for_new -> tab_switch -> work -> tab_switch back.+## Cloud Auth Reuse++Use browser profiles for durable cloud login reuse. Only create a browser profile when the user clearly asks to "save" or "remember" authenticated state; do not silently persist cookies from vague future-use language.+Two source paths:+- Session path: log in -> skyvern_browser_session_close(...) -> wait a few seconds -> skyvern_browser_profile_create(browser_session_id="pbs_..."). Calling create against an OPEN session returns ARCHIVE_NOT_READY indefinitely; the session must be closed and its archive uploaded first.+- Workflow-run path: log in via a workflow with persist_browser_session=true -> skyvern_browser_profile_create(workflow_run_id="wr_..."). The server polls internally (~30s), so no close-wait dance is needed. Only wr_ IDs; tsk_v2_ task IDs are not accepted.+Reuse by passing browser_profile_id to skyvern_browser_session_create or skyvern_workflow_run.+state_save/state_load are local JSON browser-state files for local/OSS workflows; they are not the cloud browser-profile path. Browser-profile CRUD tools are secondary management tools; use normal session/workflow entrypoints for actual work.+## WorkflowsSplit into multiple blocks — one intent per block. Use **navigation** blocks for actions, **extraction** for data.@@ -395,6 +411,12 @@ mcp.tool(tags={"browser_primitive"}, annotations=_web_ro("List Iframes"))(skyvermcp.tool(tags={"state"}, annotations=_web_mut("Save Browser State"))(skyvern_state_save)mcp.tool(tags={"state"}, annotations=_web_mut("Load Browser State"))(skyvern_state_load)+# -- Browser profiles (cloud-durable auth state) --+mcp.tool(tags={"browser_profile"}, annotations=_ro("List Browser Profiles"))(skyvern_browser_profile_list)+mcp.tool(tags={"browser_profile"}, annotations=_ro("Get Browser Profile"))(skyvern_browser_profile_get)+mcp.tool(tags={"browser_profile"}, annotations=_mut("Create Browser Profile"))(skyvern_browser_profile_create)+mcp.tool(tags={"browser_profile"}, annotations=_dest("Delete Browser Profile"))(skyvern_browser_profile_delete)+# -- Inspection tools (console, network, dialog, page errors, DOM) --# console_messages, network_requests, handle_dialog, and get_errors accept# clear=True, which mutates captured session buffers.@@ -577,6 +599,11 @@ __all__ = [# Auth state persistence"skyvern_state_save","skyvern_state_load",+ # Browser profiles (cloud-durable auth state)+ "skyvern_browser_profile_list",+ "skyvern_browser_profile_get",+ "skyvern_browser_profile_create",+ "skyvern_browser_profile_delete",# Prompts"build_workflow","debug_automation",diff --git a/skyvern/cli/mcp_tools/_validation.py b/skyvern/cli/mcp_tools/_validation.pyindex 473cb338b..fb0544f31 100644--- a/skyvern/cli/mcp_tools/_validation.py+++ b/skyvern/cli/mcp_tools/_validation.py@@ -77,6 +77,57 @@ def validate_run_id(run_id: str, action: str) -> dict[str, Any] | None:return None+def validate_workflow_run_id(workflow_run_id: str, action: str) -> dict[str, Any] | None:+ """Accept only wr_-prefixed IDs. The browser-profile-create source+ lookup is not compatible with tsk_v2_ IDs, so a tsk_v2_ ID must not+ slip through client-side validation."""+ if "/" in workflow_run_id or "\\" in workflow_run_id:+ return make_result(+ action,+ ok=False,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ "workflow_run_id must not contain path separators",+ "Provide a valid workflow run ID (starts with wr_)",+ ),+ )+ if not workflow_run_id.startswith("wr_"):+ return make_result(+ action,+ ok=False,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ f"Invalid workflow_run_id format: {workflow_run_id!r}",+ "Workflow run IDs start with wr_. Task-v2 IDs (tsk_v2_) are not accepted here. Use skyvern_workflow_status or skyvern_workflow_run output to find the wr_ ID.",+ ),+ )+ return None+++def validate_browser_profile_id(browser_profile_id: str, action: str) -> dict[str, Any] | None:+ if "/" in browser_profile_id or "\\" in browser_profile_id:+ return make_result(+ action,+ ok=False,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ "browser_profile_id must not contain path separators",+ "Provide a valid browser profile ID (starts with bp_)",+ ),+ )+ if not browser_profile_id.startswith("bp_"):+ return make_result(+ action,+ ok=False,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ f"Invalid browser_profile_id format: {browser_profile_id!r}",+ "Browser profile IDs start with bp_. Use skyvern_browser_profile_list to find valid IDs.",+ ),+ )+ return None++def validate_schedule_id(workflow_schedule_id: str, action: str) -> dict[str, Any] | None:if "/" in workflow_schedule_id or "\\" in workflow_schedule_id:return make_result(diff --git a/skyvern/cli/mcp_tools/browser_profile.py b/skyvern/cli/mcp_tools/browser_profile.pynew file mode 100644index 000000000..631ced9da--- /dev/null+++ b/skyvern/cli/mcp_tools/browser_profile.py@@ -0,0 +1,387 @@+"""Skyvern MCP browser profile tools — cloud-durable auth state persistence.++Browser profiles store cookies, localStorage, and the full user-data-dir from a+browser session or workflow run, so future sessions can resume a logged-in state+without re-authenticating. These tools expose the cloud `/v1/browser_profiles`+surface as MCP calls so agents can manage durable auth state without dropping+out to the REST API.++This complements `skyvern_state_save` / `skyvern_state_load` (local-file state)+with a cloud-durable option: pass the returned `browser_profile_id` as+`browser_profile_id` when creating a new session or running a workflow.+"""++from __future__ import annotations++from typing import Annotated, Any++import structlog+from pydantic import Field++from skyvern.client.core.api_error import ApiError+from skyvern.client.errors import BadRequestError, ConflictError, NotFoundError++from ._common import ErrorCode, Timer, make_error, make_result+from ._session import get_skyvern+from ._validation import validate_browser_profile_id, validate_workflow_run_id++LOG = structlog.get_logger()+++def _extract_detail(exc: Exception) -> str:+ """Pull a human-readable detail from a Fern client error body."""+ body = getattr(exc, "body", None)+ if isinstance(body, dict):+ detail = body.get("detail")+ if isinstance(detail, str) and detail:+ return detail+ message = str(exc)+ return message if message else "request rejected"+++def _profile_to_dict(profile: Any) -> dict[str, Any]:+ """Normalize a BrowserProfile response into a JSON-serializable dict.++ BrowserProfile may be a pydantic model (attributes) or a dict-like response+ depending on client version. Handle both.+ """+ if isinstance(profile, dict):+ source: dict[str, Any] = profile+ else:+ dumper = getattr(profile, "model_dump", None)+ if callable(dumper):+ source = dumper(mode="json")+ else:+ source = {+ "browser_profile_id": getattr(profile, "browser_profile_id", None),+ "organization_id": getattr(profile, "organization_id", None),+ "name": getattr(profile, "name", None),+ "description": getattr(profile, "description", None),+ "created_at": getattr(profile, "created_at", None),+ "modified_at": getattr(profile, "modified_at", None),+ "deleted_at": getattr(profile, "deleted_at", None),+ }++ result: dict[str, Any] = {}+ for key in (+ "browser_profile_id",+ "organization_id",+ "name",+ "description",+ "created_at",+ "modified_at",+ "deleted_at",+ ):+ value = source.get(key)+ if value is None:+ result[key] = None+ elif hasattr(value, "isoformat"):+ result[key] = value.isoformat()+ else:+ result[key] = value+ return result+++async def skyvern_browser_profile_list(+ include_deleted: Annotated[+ bool,+ Field(description="Include soft-deleted profiles in the response."),+ ] = False,+) -> dict[str, Any]:+ """List browser profiles for the current organization. Returns profile metadata+ including browser_profile_id (starts with bp_), name, description, created_at,+ and modified_at. Use the returned browser_profile_id when creating a new+ browser session or running a workflow to resume an authenticated state."""+ with Timer() as timer:+ try:+ skyvern = get_skyvern()+ profiles = await skyvern.list_browser_profiles(include_deleted=include_deleted)+ timer.mark("api")+ except Exception as e:+ LOG.error("browser_profile_list_failed", error=str(e))+ return make_result(+ "skyvern_browser_profile_list",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(ErrorCode.API_ERROR, str(e), "Check your API key and network connection"),+ )++ items = [_profile_to_dict(p) for p in (profiles or [])]+ return make_result(+ "skyvern_browser_profile_list",+ data={"browser_profiles": items, "count": len(items)},+ timing_ms=timer.timing_ms,+ )+++async def skyvern_browser_profile_get(+ browser_profile_id: Annotated[+ str,+ Field(description="Browser profile ID (starts with bp_)."),+ ],+) -> dict[str, Any]:+ """Get a single browser profile by ID. Returns full profile metadata. Use+ skyvern_browser_profile_list first to discover available profile IDs."""+ if err := validate_browser_profile_id(browser_profile_id, "skyvern_browser_profile_get"):+ return err++ with Timer() as timer:+ try:+ skyvern = get_skyvern()+ profile = await skyvern.get_browser_profile(browser_profile_id)+ timer.mark("api")+ except NotFoundError:+ return make_result(+ "skyvern_browser_profile_get",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ f"Browser profile {browser_profile_id!r} not found",+ "Use skyvern_browser_profile_list to discover valid browser_profile_ids",+ ),+ )+ except Exception as e:+ LOG.error("browser_profile_get_failed", browser_profile_id=browser_profile_id, error=str(e))+ return make_result(+ "skyvern_browser_profile_get",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(ErrorCode.API_ERROR, str(e), "Check the profile ID and your API key"),+ )++ return make_result(+ "skyvern_browser_profile_get",+ data=_profile_to_dict(profile),+ timing_ms=timer.timing_ms,+ )+++async def skyvern_browser_profile_create(+ name: Annotated[+ str,+ Field(description="Human-readable name for the browser profile. Must be unique within the organization."),+ ],+ browser_session_id: Annotated[+ str | None,+ Field(+ description="Persistent browser session to snapshot (starts with pbs_). Provide EXACTLY ONE of browser_session_id OR workflow_run_id."+ ),+ ] = None,+ workflow_run_id: Annotated[+ str | None,+ Field(+ description="Workflow run whose persisted session should be captured (starts with wr_; tsk_v2_ task IDs are NOT accepted). Provide EXACTLY ONE of browser_session_id OR workflow_run_id."+ ),+ ] = None,+ description: Annotated[+ str | None,+ Field(description="Optional free-text description of what this profile represents."),+ ] = None,+) -> dict[str, Any]:+ """Create a cloud-durable browser profile by snapshotting the authenticated+ state of a browser session or workflow run. Returns the new browser_profile_id.++ Provide exactly one of browser_session_id OR workflow_run_id. Subsequent+ sessions/runs can pass browser_profile_id to resume the logged-in state+ without re-authenticating.++ IMPORTANT: the browser session's persisted archive uploads asynchronously+ AFTER the session closes. The typical correct sequence for a session_id+ source is: sign in -> skyvern_browser_session_close(...) -> retry-loop+ skyvern_browser_profile_create(browser_session_id=...) until the archive+ is ready (roughly 5-10 seconds after close). Calling this tool against an+ OPEN session returns ARCHIVE_NOT_READY and will never succeed until the+ session is closed.++ For workflow_run_id sources (wr_...), the server polls internally for up+ to 30 seconds so a close-then-call dance is not required. tsk_v2_ IDs are+ not accepted here."""+ if not name or not name.strip():+ return make_result(+ "skyvern_browser_profile_create",+ ok=False,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ "name is required and must be non-empty",+ "Provide a descriptive profile name, e.g. 'example-app-signed-in'",+ ),+ )++ # Require exactly one of the two sources: XOR via bool-equality.+ # Equality is True when both are None (neither provided) or both are truthy+ # (both provided) — in either case we reject with the same INVALID_INPUT.+ if bool(browser_session_id) == bool(workflow_run_id):+ return make_result(+ "skyvern_browser_profile_create",+ ok=False,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ "Provide exactly ONE of browser_session_id or workflow_run_id",+ "Set browser_session_id (pbs_...) for an active session, or workflow_run_id (wr_...) for a completed workflow run",+ ),+ )++ if browser_session_id is not None:+ if "/" in browser_session_id or "\\" in browser_session_id:+ return make_result(+ "skyvern_browser_profile_create",+ ok=False,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ "browser_session_id must not contain path separators",+ "Provide a valid browser session ID (starts with pbs_)",+ ),+ )+ if not browser_session_id.startswith("pbs_"):+ return make_result(+ "skyvern_browser_profile_create",+ ok=False,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ f"Invalid browser_session_id format: {browser_session_id!r}",+ "Browser session IDs start with pbs_. Use skyvern_browser_session_list to find valid IDs.",+ ),+ )++ if workflow_run_id is not None:+ if err := validate_workflow_run_id(workflow_run_id, "skyvern_browser_profile_create"):+ return err++ with Timer() as timer:+ try:+ skyvern = get_skyvern()+ profile = await skyvern.create_browser_profile(+ name=name,+ description=description,+ browser_session_id=browser_session_id,+ workflow_run_id=workflow_run_id,+ )+ timer.mark("api")+ except ConflictError:+ return make_result(+ "skyvern_browser_profile_create",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ f"A browser profile named {name!r} already exists",+ "Choose a different name, or delete the existing profile with skyvern_browser_profile_delete",+ ),+ )+ except BadRequestError as e:+ # Most common 400 is "session archive not ready yet" — the server-side+ # upload pipeline is asynchronous after session close. Surface a clear,+ # retryable hint so agents know to wait instead of giving up.+ detail = _extract_detail(e)+ return make_result(+ "skyvern_browser_profile_create",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(+ ErrorCode.ACTION_FAILED,+ f"Profile creation rejected: {detail}",+ "If the session recently closed, the profile archive may still be uploading. Wait 5-10 seconds and retry.",+ ),+ )+ except ApiError as e:+ # The Fern-generated create_browser_profile client only explicitly+ # maps 400/409/422; a 404 (bad browser_session_id or+ # workflow_run_id source) is raised as a generic ApiError, not+ # NotFoundError. Catch ApiError and key off status_code so the+ # 404-routing fires in production, not only in tests that happen+ # to raise NotFoundError directly.+ if getattr(e, "status_code", None) == 404:+ detail = ""+ if isinstance(e.body, dict):+ detail = str(e.body.get("detail") or "")+ return make_result(+ "skyvern_browser_profile_create",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ detail or "Source browser_session_id or workflow_run_id not found",+ "Verify the source ID with skyvern_browser_session_list or skyvern_workflow_status",+ ),+ )+ LOG.error(+ "browser_profile_create_failed",+ name=name,+ browser_session_id=browser_session_id,+ workflow_run_id=workflow_run_id,+ error=str(e),+ )+ return make_result(+ "skyvern_browser_profile_create",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(ErrorCode.API_ERROR, str(e), "Check the source ID and your API key"),+ )+ except Exception as e:+ LOG.error(+ "browser_profile_create_failed",+ name=name,+ browser_session_id=browser_session_id,+ workflow_run_id=workflow_run_id,+ error=str(e),+ )+ return make_result(+ "skyvern_browser_profile_create",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(ErrorCode.API_ERROR, str(e), "Check the source ID and your API key"),+ )++ return make_result(+ "skyvern_browser_profile_create",+ data=_profile_to_dict(profile),+ timing_ms=timer.timing_ms,+ )+++async def skyvern_browser_profile_delete(+ browser_profile_id: Annotated[+ str,+ Field(+ description="Browser profile ID to delete (starts with bp_). Soft delete — the profile is marked deleted but not immediately purged."+ ),+ ],+) -> dict[str, Any]:+ """Soft-delete a browser profile. The profile is marked deleted but not+ immediately purged; it will stop appearing in skyvern_browser_profile_list+ unless include_deleted=true is passed. Existing sessions that loaded this+ profile continue to work until they close."""+ if err := validate_browser_profile_id(browser_profile_id, "skyvern_browser_profile_delete"):+ return err++ with Timer() as timer:+ try:+ skyvern = get_skyvern()+ await skyvern.delete_browser_profile(browser_profile_id)+ timer.mark("api")+ except NotFoundError:+ return make_result(+ "skyvern_browser_profile_delete",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ f"Browser profile {browser_profile_id!r} not found",+ "Use skyvern_browser_profile_list to discover valid browser_profile_ids",+ ),+ )+ except Exception as e:+ LOG.error("browser_profile_delete_failed", browser_profile_id=browser_profile_id, error=str(e))+ return make_result(+ "skyvern_browser_profile_delete",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(ErrorCode.API_ERROR, str(e), "Check the profile ID and your API key"),+ )++ return make_result(+ "skyvern_browser_profile_delete",+ data={"browser_profile_id": browser_profile_id, "deleted": True},+ timing_ms=timer.timing_ms,+ )diff --git a/skyvern/cli/mcp_tools/prompts.py b/skyvern/cli/mcp_tools/prompts.pyindex ddc13bda4..a162ef53d 100644--- a/skyvern/cli/mcp_tools/prompts.py+++ b/skyvern/cli/mcp_tools/prompts.py@@ -120,6 +120,21 @@ BAD (1 block):**Login + Action**: login (authenticate) -> navigation (do work) -> extraction (results)**Batch processing**: for_loop over URLs -> navigation (process each) -> extraction (gather data)+### Authenticated remote reuse++For repeated authenticated remote workflows, prefer cloud browser profiles over logging in every run.+If the user clearly asks to "save" or "remember" the authenticated state, save it once with+skyvern_browser_profile_create and reuse the returned browser_profile_id on future runs.+Two source paths:+- From a browser_session_id (pbs_...): close the session first with skyvern_browser_session_close+ and wait ~5-10s for the archive to upload before calling skyvern_browser_profile_create; the+ server returns ARCHIVE_NOT_READY until it finishes.+- From a workflow_run_id (wr_...): the run must have been defined with persist_browser_session=true,+ and the server polls internally so no extra close-wait is required. tsk_v2_ task IDs are not accepted.+Reuse the returned browser_profile_id with skyvern_workflow_run for automated runs or+skyvern_browser_session_create for interactive cloud work.+Do not use state_save/state_load for cloud profile reuse; those are local browser-state files.+---## Parameterize for Reusediff --git a/skyvern/cli/mcp_tools/session.py b/skyvern/cli/mcp_tools/session.pyindex a842a7116..15013862e 100644--- a/skyvern/cli/mcp_tools/session.py+++ b/skyvern/cli/mcp_tools/session.py@@ -9,6 +9,7 @@ from skyvern.cli.core.api_key_hash import hash_api_key_for_cachefrom skyvern.cli.core.client import get_active_api_keyfrom skyvern.cli.core.session_manager import is_stateless_http_modefrom skyvern.cli.core.session_ops import coerce_proxy_location, do_session_close, do_session_create, do_session_list+from skyvern.client.errors import NotFoundErrorfrom skyvern.client.types.extensions import Extensionsfrom skyvern.schemas.runs import proxy_location_to_request@@ -20,6 +21,7 @@ from ._session import (resolve_browser,set_current_session,)+from ._validation import validate_browser_profile_iddef _session_api_key_hash() -> str | None:@@ -49,6 +51,10 @@ async def skyvern_browser_session_create()),] = None,+ browser_profile_id: Annotated[+ str | None,+ Field(description="Cloud browser profile ID (bp_...) to load saved authenticated state."),+ ] = None,extensions: Annotated[list[Extensions] | None,Field(description='Browser extensions to install, for example ["captcha-solver"].'),@@ -65,6 +71,25 @@ async def skyvern_browser_session_create(# resolve_browser() stores the browser in session state via set_current_session()# internally, so we don't need to call it again here.use_cdp, cdp_url = _should_default_to_cdp()+ if browser_profile_id and (local or use_cdp):+ return make_result(+ "skyvern_browser_session_create",+ ok=False,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ "browser_profile_id is only supported for cloud browser sessions",+ "Use local=false with a cloud session, or omit browser_profile_id for local/CDP sessions.",+ ),+ )+ # Accept `is not None` (not truthy) so an empty string reaches the+ # validator and is rejected with a pointed INVALID_INPUT instead of+ # silently passing "" through to the SDK, where the server also+ # truthy-checks and treats "" as "no profile" — which would quietly+ # break login-reuse flows when callers pass an empty env-expanded value.+ if browser_profile_id is not None:+ if err := validate_browser_profile_id(browser_profile_id, "skyvern_browser_session_create"):+ return err+if use_cdp and not local and cdp_url:with Timer() as timer:try:@@ -106,29 +131,43 @@ async def skyvern_browser_session_create(if is_stateless_http_mode():proxy = proxy_location_to_request(coerce_proxy_location(proxy_location))create_kwargs: dict[str, Any] = {"timeout": timeout or 60, "proxy_location": proxy}+ if browser_profile_id is not None:+ create_kwargs["browser_profile_id"] = browser_profile_idif extensions is not None:create_kwargs["extensions"] = extensionssession = await skyvern.create_browser_session(**create_kwargs)timer.mark("sdk")ctx = BrowserContext(mode="cloud_session", session_id=session.browser_session_id)+ response_data = {+ "session_id": session.browser_session_id,+ "timeout_minutes": timeout or 60,+ }+ # The create-browser-session response does not currently echo+ # back browser_profile_id even when the server loaded one, so+ # fall back to the requested id for display purposes. Callers+ # should treat the returned value as "requested, not confirmed+ # by the server." Tracked as a downstream API gap.+ loaded_profile_id = getattr(session, "browser_profile_id", None) or browser_profile_id+ if loaded_profile_id:+ response_data["browser_profile_id"] = loaded_profile_idreturn make_result("skyvern_browser_session_create",browser_context=ctx,- data={- "session_id": session.browser_session_id,- "timeout_minutes": timeout or 60,- },+ data=response_data,timing_ms=timer.timing_ms,)- browser, result = await do_session_create(- skyvern,- timeout=timeout or 60,- proxy_location=coerce_proxy_location(proxy_location),- extensions=extensions,- local=local,- headless=headless,- )+ session_create_kwargs: dict[str, Any] = {+ "timeout": timeout or 60,+ "proxy_location": coerce_proxy_location(proxy_location),+ "local": local,+ "headless": headless,+ }+ if browser_profile_id is not None:+ session_create_kwargs["browser_profile_id"] = browser_profile_id+ if extensions is not None:+ session_create_kwargs["extensions"] = extensions+ browser, result = await do_session_create(skyvern, **session_create_kwargs)timer.mark("sdk")if result.local:@@ -148,6 +187,31 @@ async def skyvern_browser_session_create("Cloud sessions require SKYVERN_API_KEY. Check your environment.",),)+ except NotFoundError as e:+ # The session-create route returns 404 "Browser profile {id} not found"+ # when browser_profile_id is invalid or deleted. Surface it as an+ # input error so agents/operators fix the profile ID rather than+ # chase an opaque SDK failure.+ detail = ""+ if isinstance(e.body, dict):+ detail = str(e.body.get("detail") or "")+ if browser_profile_id and "browser profile" in detail.lower():+ return make_result(+ "skyvern_browser_session_create",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ detail or f"Browser profile {browser_profile_id} not found",+ "Verify browser_profile_id with skyvern_browser_profile_list, or drop the flag to create a fresh session.",+ ),+ )+ return make_result(+ "skyvern_browser_session_create",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(ErrorCode.SDK_ERROR, detail or str(e), "Failed to create browser session"),+ )except Exception as e:return make_result("skyvern_browser_session_create",@@ -164,13 +228,17 @@ async def skyvern_browser_session_create(timing_ms=timer.timing_ms,)+ response_data = {+ "session_id": result.session_id,+ "timeout_minutes": result.timeout_minutes,+ }+ result_profile_id = getattr(result, "browser_profile_id", None)+ if result_profile_id:+ response_data["browser_profile_id"] = result_profile_idreturn make_result("skyvern_browser_session_create",browser_context=ctx,- data={- "session_id": result.session_id,- "timeout_minutes": result.timeout_minutes,- },+ data=response_data,timing_ms=timer.timing_ms,)diff --git a/skyvern/cli/mcp_tools/workflow.py b/skyvern/cli/mcp_tools/workflow.pyindex 602cc92c9..ec0ff0bfb 100644--- a/skyvern/cli/mcp_tools/workflow.py+++ b/skyvern/cli/mcp_tools/workflow.py@@ -17,6 +17,7 @@ import structlogimport yamlfrom pydantic import Field+from skyvern.client.core.api_error import ApiErrorfrom skyvern.client.errors import BadRequestError, NotFoundErrorfrom skyvern.forge.sdk.workflow.models.parameter import ParameterType, WorkflowParameterTypefrom skyvern.schemas.runs import ProxyLocation@@ -25,7 +26,7 @@ from skyvern.utils.yaml_loader import safe_load_no_datesfrom ._common import ErrorCode, Timer, make_error, make_resultfrom ._session import get_skyvern-from ._validation import validate_folder_id, validate_run_id, validate_workflow_id+from ._validation import validate_browser_profile_id, validate_folder_id, validate_run_id, validate_workflow_idfrom ._workflow_http import (coerce_timestamp,create_workflow_raw,@@ -112,6 +113,7 @@ def _serialize_run(run: Any) -> dict[str, Any]:"recording_url","app_url","browser_session_id",+ "browser_profile_id","run_with","ai_fallback",):@@ -1180,6 +1182,12 @@ async def skyvern_workflow_run(browser_session_id: Annotated[str | None, Field(description="Reuse an existing browser session (pbs_...) to preserve login state")] = None,+ browser_profile_id: Annotated[+ str | None,+ Field(+ description="Load a cloud browser profile (bp_...) with saved authenticated state. Can be combined with browser_session_id to explicitly choose the profile."+ ),+ ] = None,webhook_url: Annotated[str | None, Field(description="URL for status webhook callbacks after completion")] = None,proxy_location: Annotated[str | None, Field(description="Geographic proxy: RESIDENTIAL, RESIDENTIAL_GB, NONE, etc.")@@ -1201,6 +1209,12 @@ async def skyvern_workflow_run(or use wait=false and poll with skyvern_workflow_status."""if err := validate_workflow_id(workflow_id, "skyvern_workflow_run"):return err+ # Accept `is not None` (not truthy) so an empty string reaches the+ # validator and is rejected with a pointed INVALID_INPUT instead of+ # silently passing "" through to the SDK.+ if browser_profile_id is not None:+ if err := validate_browser_profile_id(browser_profile_id, "skyvern_workflow_run"):+ return errparsed_params: dict[str, Any] | None = Noneif parameters is not None:@@ -1240,6 +1254,7 @@ async def skyvern_workflow_run(workflow_id=workflow_id,parameters=parsed_params,browser_session_id=browser_session_id,+ browser_profile_id=browser_profile_id,webhook_url=webhook_url,proxy_location=proxy,wait_for_completion=wait,@@ -1258,16 +1273,46 @@ async def skyvern_workflow_run("Increase timeout_seconds or set wait=false and poll with skyvern_workflow_status",),)- except NotFoundError:+ except ApiError as e:+ # The Fern-generated run_workflow client only explicitly maps+ # 400/422; a 404 (bad workflow_id OR bad browser_profile_id) is+ # raised as a generic ApiError, not NotFoundError. Catch ApiError+ # and key off status_code so the 404-routing fires in production,+ # not only in tests that happen to raise NotFoundError directly.+ # NotFoundError IS-A ApiError(status_code=404), so tests that+ # raise NotFoundError still land here.+ if getattr(e, "status_code", None) == 404:+ detail = ""+ if isinstance(e.body, dict):+ detail = str(e.body.get("detail") or "")+ if browser_profile_id and "browser profile" in detail.lower():+ return make_result(+ "skyvern_workflow_run",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ detail or f"Browser profile {browser_profile_id} not found",+ "Verify browser_profile_id with skyvern_browser_profile_list, or drop the flag to run without a saved profile.",+ ),+ )+ return make_result(+ "skyvern_workflow_run",+ ok=False,+ timing_ms=timer.timing_ms,+ error=make_error(+ ErrorCode.WORKFLOW_NOT_FOUND,+ f"Workflow {workflow_id!r} not found",+ "Verify the workflow ID with skyvern_workflow_list",+ ),+ )+ # Non-404 ApiError -> fall through to the generic handler+ LOG.error("workflow_run_failed", workflow_id=workflow_id, error=str(e))return make_result("skyvern_workflow_run",ok=False,timing_ms=timer.timing_ms,- error=make_error(- ErrorCode.WORKFLOW_NOT_FOUND,- f"Workflow {workflow_id!r} not found",- "Verify the workflow ID with skyvern_workflow_list",- ),+ error=make_error(ErrorCode.API_ERROR, str(e), "Check the workflow ID, parameters, and API key"),)except Exception as e:LOG.error("workflow_run_failed", workflow_id=workflow_id, error=str(e))@@ -1280,9 +1325,19 @@ async def skyvern_workflow_run(LOG.info("workflow_run_started", workflow_id=workflow_id, run_id=run.run_id, wait=wait)data = _serialize_run(run)+ # The run_workflow response does not always echo back browser_profile_id+ # even when the server used the profile; fall back to the requested value+ # so agents can see what they sent. Treat the surfaced value as+ # "requested, not confirmed by the server" (downstream API gap).+ if browser_profile_id and "browser_profile_id" not in data:+ data["browser_profile_id"] = browser_profile_idparams_str = f", parameters={parsed_params}" if parsed_params else ""+ session_str = f", browser_session_id={browser_session_id!r}" if browser_session_id else ""+ profile_str = f", browser_profile_id={browser_profile_id!r}" if browser_profile_id else ""wait_str = f", wait_for_completion=True, timeout={timeout_seconds}" if wait else ""- data["sdk_equivalent"] = f"await skyvern.run_workflow(workflow_id={workflow_id!r}{params_str}{wait_str})"+ data["sdk_equivalent"] = (+ f"await skyvern.run_workflow(workflow_id={workflow_id!r}{params_str}{session_str}{profile_str}{wait_str})"+ )return make_result("skyvern_workflow_run", data=data, timing_ms=timer.timing_ms)diff --git a/skyvern/cli/skills/skyvern/SKILL.md b/skyvern/cli/skills/skyvern/SKILL.mdindex 4af725fb2..d5d35c6bc 100644--- a/skyvern/cli/skills/skyvern/SKILL.md+++ b/skyvern/cli/skills/skyvern/SKILL.md@@ -49,6 +49,45 @@ skyvern browser session connect --cdp "ws://localhost:9222"Session state persists between commands. After `session create`, subsequent commands auto-attach.Override with `--session pbs_...`. Close when done: `skyvern browser session close`.+## Cloud Saved Login Reuse++When a user asks to "save this login", "remember this session", or reuse an authenticated cloud state later,+use a browser profile. A cloud browser profile is a durable saved-login archive identified by `bp_...`; it is+not the same as `skyvern browser state save/load` or MCP `state_save/state_load`, which are local browser-state+file tools.++Save explicitly after login:++```bash+# From a completed workflow run whose workflow definition has persist_browser_session=true+# (server polls internally for up to 30s, no close-first dance required).+skyvern browser-profile create --name "site-signed-in" --workflow-run-id wr_123++# Or from an eligible persisted browser session. The session archive only+# uploads AFTER the session is closed, so the correct sequence is:+# log in -> close the session -> wait a few seconds -> create the profile.+# Calling create against an OPEN session returns ARCHIVE_NOT_READY and will+# never succeed until the session is closed.+skyvern browser session close --id pbs_123+skyvern browser-profile create --name "site-signed-in" --browser-session-id pbs_123+```++persist_browser_session=true is a workflow-definition property, not a `workflow run` flag. A plain+`create_browser_session()` session alone does not create a browser-profile archive; the source must have+persisted browser state that Skyvern can archive. Archive creation is asynchronous, so if profile creation says+the archive is not ready, wait briefly and retry with a small bounded retry loop.++Reuse through normal entrypoints, then validate before logging in again:++```bash+skyvern workflow run --id wpid_123 --browser-profile-id bp_123 --wait+skyvern browser session create --browser-profile-id bp_123+skyvern browser validate --prompt "Is the user already logged in? Look for the account dashboard or avatar."+```++If the profile state is valid, skip or conditionalize login blocks instead of blindly repeating login. Product UI+is not required for this MCP/CLI/skill path.+## Step 4: Execute by Classification### Quick check (yes/no)diff --git a/skyvern/cli/skills/skyvern/references/cli-parity.md b/skyvern/cli/skills/skyvern/references/cli-parity.mdindex 64eac5202..72c6c312b 100644--- a/skyvern/cli/skills/skyvern/references/cli-parity.md+++ b/skyvern/cli/skills/skyvern/references/cli-parity.md@@ -6,10 +6,25 @@ Common mappings:- `skyvern browser act` -> `skyvern_act`- `skyvern browser extract` -> `skyvern_extract`- `skyvern workflow run` -> `skyvern_workflow_run`+- `skyvern workflow run --browser-profile-id bp_...` -> `skyvern_workflow_run(browser_profile_id=...)`+- `skyvern browser session create --browser-profile-id bp_...` -> `skyvern_browser_session_create(browser_profile_id=...)`+- `skyvern browser-profile create` -> `skyvern_browser_profile_create`+- `skyvern browser-profile list/get/delete` -> `skyvern_browser_profile_list/get/delete`- `skyvern credential list` -> `skyvern_credential_list`Use CLI for local operator workflows and MCP tools for agent-driven integrations.+## Browser-profile lifecycle++Cloud saved-login reuse uses browser profiles. Create one from a completed persisted workflow run or eligible+browser session, then pass the returned `browser_profile_id` to a normal workflow run or cloud session. A+workflow-run source needs `persist_browser_session=true` on the workflow definition. A plain+`create_browser_session()` session alone does not create a browser-profile archive, and archive creation can be+asynchronous, so retry profile creation briefly if the source archive is not ready.++Do not map this to `skyvern browser state save/load` or MCP `state_save/state_load`; those are local/browser-file+state tools. Product UI is not required for MCP or CLI browser-profile save/reuse.+## Agent-Aware CLIThe CLI supports structured JSON output and non-interactive mode for AI agents:diff --git a/skyvern/cli/skills/skyvern/references/sessions.md b/skyvern/cli/skills/skyvern/references/sessions.mdindex ecdf6e333..4f18ff258 100644--- a/skyvern/cli/skills/skyvern/references/sessions.md+++ b/skyvern/cli/skills/skyvern/references/sessions.md@@ -1,5 +1,40 @@# Session Reuse+## Cloud browser profiles for saved logins++Use a cloud browser profile when the user wants a durable saved login across future runs or sessions.+Browser profiles are identified by `bp_...` and are created explicitly:++```bash+# workflow_run source (server polls internally for up to 30s)+skyvern browser-profile create --name "site-signed-in" --workflow-run-id wr_123++# browser_session source: CLOSE THE SESSION FIRST. The archive only uploads+# after the session closes. Calling create against an open session returns+# ARCHIVE_NOT_READY indefinitely.+skyvern browser session close --id pbs_123+skyvern browser-profile create --name "site-signed-in" --browser-session-id pbs_123+```++For workflow-run sources, persist_browser_session=true is a workflow-definition property. It is not a+`skyvern workflow run` flag. Only wr_ IDs are accepted; tsk_v2_ task IDs are not. A plain+`create_browser_session()` session alone does not create a profile archive; the source session or workflow+run must have a persisted browser archive. Archive creation is asynchronous, so a freshly completed source+can require a short bounded retry before profile creation succeeds.++Reuse profiles on normal cloud entrypoints:++```bash+skyvern workflow run --id wpid_123 --browser-profile-id bp_123+skyvern browser session create --browser-profile-id bp_123+```++After reuse, validate logged-in state before re-login. If the user is already authenticated, skip or+conditionalize login blocks rather than logging in on every run.++`skyvern browser state save/load` and MCP `state_save/state_load` are local/browser-file tools. They are not the+cloud browser-profile reuse path. Product UI is not required for browser-profile save/reuse through MCP or CLI.+## When to reuse a session- Multiple actions on one authenticated site.diff --git a/skyvern/cli/workflow.py b/skyvern/cli/workflow.pyindex 96dbc6ca9..d45995c0e 100644--- a/skyvern/cli/workflow.py+++ b/skyvern/cli/workflow.py@@ -233,6 +233,11 @@ def workflow_run(help="Workflow parameters as JSON string or @file path.",),session: str | None = typer.Option(None, "--session", help="Browser session ID (pbs_...) to reuse."),+ browser_profile_id: str | None = typer.Option(+ None,+ "--browser-profile-id",+ help="Cloud browser profile ID (bp_...) with saved authenticated state to load for this run.",+ ),webhook: str | None = typer.Option(None, "--webhook", help="Status webhook callback URL."),proxy: str | None = typer.Option(None, "--proxy", help="Proxy location (e.g., RESIDENTIAL)."),wait: bool = typer.Option(False, "--wait", help="Wait for workflow completion before returning."),@@ -254,6 +259,7 @@ def workflow_run(workflow_id=workflow_id,parameters=resolved_params,browser_session_id=session,+ browser_profile_id=browser_profile_id,webhook_url=webhook,proxy_location=proxy,wait=wait,diff --git a/skyvern/library/skyvern.py b/skyvern/library/skyvern.pyindex 3b308e7e0..0b9cc1558 100644--- a/skyvern/library/skyvern.py+++ b/skyvern/library/skyvern.py@@ -547,6 +547,7 @@ class Skyvern(AsyncSkyvern):*,timeout: int | None = None,proxy_location: ProxyLocationInput = None,+ browser_profile_id: str | None = None,extensions: list[Extensions] | None = None,) -> SkyvernBrowser:"""Launch a new cloud-hosted browser session.@@ -558,6 +559,7 @@ class Skyvern(AsyncSkyvern):Must be between 5 and 1440. Defaults to 60.proxy_location: Geographic proxy location to route the browser traffic through.This is only available in Skyvern Cloud.+ browser_profile_id: Browser profile ID to load into the new session.extensions: Browser extensions to install in the session.Returns:@@ -568,6 +570,8 @@ class Skyvern(AsyncSkyvern):"timeout": timeout,"proxy_location": proxy_location_to_request(proxy_location),}+ if browser_profile_id is not None:+ create_kwargs["browser_profile_id"] = browser_profile_idif extensions is not None:create_kwargs["extensions"] = extensionsbrowser_session = await self.create_browser_session(**create_kwargs)diff --git a/tests/unit/test_cli_agent_aware.py b/tests/unit/test_cli_agent_aware.pyindex 0feda230d..d2d1940c0 100644--- a/tests/unit/test_cli_agent_aware.py+++ b/tests/unit/test_cli_agent_aware.py@@ -336,6 +336,7 @@ class TestCapabilities:assert "subcommands" in treesub_names = [s["name"] for s in tree["subcommands"]]assert "workflow" in sub_names+ assert "browser-profile" in sub_namesassert "credential" in sub_namesassert "capabilities" in sub_namesdiff --git a/tests/unit/test_cli_browser_profile.py b/tests/unit/test_cli_browser_profile.pynew file mode 100644index 000000000..3650861ae--- /dev/null+++ b/tests/unit/test_cli_browser_profile.py@@ -0,0 +1,128 @@+from __future__ import annotations++import json+from unittest.mock import AsyncMock++import pytest+from typer.testing import CliRunner+++def _mcp_result(action: str, data: dict) -> dict:+ return {+ "ok": True,+ "action": action,+ "browser_context": {"mode": "none", "session_id": None, "cdp_url": None},+ "data": data,+ "artifacts": [],+ "timing_ms": {},+ "warnings": [],+ "error": None,+ }+++class TestBrowserProfileCli:+ def test_list_delegates_to_mcp_tool(self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture) -> None:+ from skyvern.cli import browser_profile as browser_profile_cmd++ tool = AsyncMock(return_value=_mcp_result("skyvern_browser_profile_list", {"browser_profiles": [], "count": 0}))+ monkeypatch.setattr(browser_profile_cmd, "tool_browser_profile_list", tool)++ browser_profile_cmd.browser_profile_list(include_deleted=True, json_output=True)++ tool.assert_awaited_once_with(include_deleted=True)+ parsed = json.loads(capsys.readouterr().out)+ assert parsed["ok"] is True+ assert parsed["action"] == "skyvern_browser_profile_list"++ def test_get_delegates_to_mcp_tool(self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture) -> None:+ from skyvern.cli import browser_profile as browser_profile_cmd++ tool = AsyncMock(return_value=_mcp_result("skyvern_browser_profile_get", {"browser_profile_id": "bp_saved"}))+ monkeypatch.setattr(browser_profile_cmd, "tool_browser_profile_get", tool)++ browser_profile_cmd.browser_profile_get(browser_profile_id="bp_saved", json_output=True)++ tool.assert_awaited_once_with(browser_profile_id="bp_saved")+ parsed = json.loads(capsys.readouterr().out)+ assert parsed["data"]["browser_profile_id"] == "bp_saved"++ def test_create_delegates_alias_sources_to_mcp_tool(+ self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture+ ) -> None:+ from skyvern.cli import browser_profile as browser_profile_cmd++ tool = AsyncMock(return_value=_mcp_result("skyvern_browser_profile_create", {"browser_profile_id": "bp_saved"}))+ monkeypatch.setattr(browser_profile_cmd, "tool_browser_profile_create", tool)++ browser_profile_cmd.browser_profile_create(+ name="site-signed-in",+ workflow_run_id="wr_123",+ browser_session_id=None,+ description="logged in",+ json_output=True,+ )++ tool.assert_awaited_once_with(+ name="site-signed-in",+ browser_session_id=None,+ workflow_run_id="wr_123",+ description="logged in",+ )+ parsed = json.loads(capsys.readouterr().out)+ assert parsed["data"]["browser_profile_id"] == "bp_saved"++ def test_delete_delegates_to_mcp_tool(self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture) -> None:+ from skyvern.cli import browser_profile as browser_profile_cmd++ tool = AsyncMock(return_value=_mcp_result("skyvern_browser_profile_delete", {"browser_profile_id": "bp_saved"}))+ monkeypatch.setattr(browser_profile_cmd, "tool_browser_profile_delete", tool)++ browser_profile_cmd.browser_profile_delete(browser_profile_id="bp_saved", json_output=True)++ tool.assert_awaited_once_with(browser_profile_id="bp_saved")+ parsed = json.loads(capsys.readouterr().out)+ assert parsed["data"]["browser_profile_id"] == "bp_saved"++ def test_browser_profile_help_describes_cloud_saved_login_lifecycle(self) -> None:+ # Inspect registered Click options directly rather than asserting on rendered+ # Rich help output, which wraps unpredictably on narrow CI terminals.+ from typer.main import get_command++ from skyvern.cli.commands import cli_app++ click_app = get_command(cli_app)+ create = click_app.get_command(None, "browser-profile").get_command(None, "create") # type: ignore[union-attr]+ opts = {flag for p in create.params for flag in getattr(p, "opts", [])}++ assert opts >= {"--name", "--workflow-run-id", "--from-run", "--browser-session-id"}+ # Help wording ("cloud" + "saved-login") lives in the command docstring+ # that Click surfaces as the command's short help. This avoids relying on+ # rendered Rich panel output, which wraps unpredictably across terminals.+ help_source = (create.help or create.short_help or "").lower()+ assert "cloud" in help_source+ assert "saved login" in help_source or "saved-login" in help_source++ def test_browser_profile_save_alias_is_not_registered(self) -> None:+ from skyvern.cli.commands import cli_app++ result = CliRunner().invoke(cli_app, ["browser-profile", "save", "--help"])++ assert result.exit_code != 0+ assert "save" in result.output++ def test_workflow_and_session_help_expose_browser_profile_id(self) -> None:+ from typer.main import get_command++ from skyvern.cli.commands import cli_app++ click_app = get_command(cli_app)+ workflow_run = click_app.get_command(None, "workflow").get_command(None, "run") # type: ignore[union-attr]+ session_create = (+ click_app.get_command(None, "browser").get_command(None, "session").get_command(None, "create") # type: ignore[union-attr]+ )++ workflow_opts = {flag for p in workflow_run.params for flag in getattr(p, "opts", [])}+ session_opts = {flag for p in session_create.params for flag in getattr(p, "opts", [])}++ assert "--browser-profile-id" in workflow_opts+ assert "--browser-profile-id" in session_optsdiff --git a/tests/unit/test_cli_commands.py b/tests/unit/test_cli_commands.pyindex db94b4ce6..1ead736de 100644--- a/tests/unit/test_cli_commands.py+++ b/tests/unit/test_cli_commands.py@@ -444,6 +444,7 @@ class TestWorkflowCommands:workflow_id="wpid_123",params=f"@{params_file}",session="pbs_456",+ browser_profile_id="bp_saved",webhook="https://example.com/webhook",proxy="RESIDENTIAL",wait=True,@@ -456,6 +457,7 @@ class TestWorkflowCommands:"workflow_id": "wpid_123","parameters": '{"company": "Acme"}',"browser_session_id": "pbs_456",+ "browser_profile_id": "bp_saved","webhook_url": "https://example.com/webhook","proxy_location": "RESIDENTIAL","wait": True,@@ -466,6 +468,41 @@ class TestWorkflowCommands:assert parsed["ok"] is Trueassert parsed["data"]["run_id"] == "wr_123"+ def test_session_create_passes_browser_profile_id_and_outputs_it(+ self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture+ ) -> None:+ from skyvern.cli.commands import browser as browser_cmd++ skyvern = object()+ result = SimpleNamespace(session_id="pbs_123", timeout_minutes=30, browser_profile_id="bp_saved")+ do_create = AsyncMock(return_value=(object(), result))+ monkeypatch.setattr(browser_cmd, "get_skyvern", lambda: skyvern)+ monkeypatch.setattr(browser_cmd, "do_session_create", do_create)+ monkeypatch.setattr(browser_cmd, "save_state", MagicMock())+ capture_mock = MagicMock()+ monkeypatch.setattr(browser_cmd, "capture_cli_tool_call", capture_mock)++ browser_cmd.session_create(+ timeout=30,+ proxy="RESIDENTIAL",+ browser_profile_id="bp_saved",+ local=False,+ headless=False,+ json_output=True,+ )++ do_create.assert_awaited_once_with(+ skyvern,+ timeout=30,+ proxy_location="RESIDENTIAL",+ browser_profile_id="bp_saved",+ )+ parsed = json.loads(capsys.readouterr().out)+ assert parsed["ok"] is True+ assert parsed["data"]["session_id"] == "pbs_123"+ assert parsed["data"]["browser_profile_id"] == "bp_saved"+ capture_mock.assert_called_once_with("skyvern_browser_session_create", ok=True)+def test_workflow_status_json_error_exits_nonzero(self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture) -> None:diff --git a/tests/unit/test_mcp_browser_profile_tools.py b/tests/unit/test_mcp_browser_profile_tools.pynew file mode 100644index 000000000..4b950572c--- /dev/null+++ b/tests/unit/test_mcp_browser_profile_tools.py@@ -0,0 +1,299 @@+from __future__ import annotations++from datetime import datetime, timezone+from types import SimpleNamespace+from typing import Any+from unittest.mock import AsyncMock++import pytest++from skyvern.cli.core.result import ErrorCode+from skyvern.cli.mcp_tools import browser_profile as mcp_bp+from skyvern.client.errors import BadRequestError, ConflictError, NotFoundError+++def _make_profile(**overrides: Any) -> SimpleNamespace:+ created = datetime(2026, 4, 20, 12, 0, 0, tzinfo=timezone.utc)+ defaults = {+ "browser_profile_id": "bp_abc123",+ "organization_id": "o_123",+ "name": "example-app-signed-in",+ "description": "logged in",+ "created_at": created,+ "modified_at": created,+ "deleted_at": None,+ }+ defaults.update(overrides)+ return SimpleNamespace(**defaults)+++async def test_list_returns_normalized_profiles(monkeypatch: pytest.MonkeyPatch) -> None:+ skyvern = SimpleNamespace(+ list_browser_profiles=AsyncMock(return_value=[_make_profile(), _make_profile(browser_profile_id="bp_def456")])+ )+ monkeypatch.setattr(mcp_bp, "get_skyvern", lambda: skyvern)++ result = await mcp_bp.skyvern_browser_profile_list()++ assert result["ok"] is True+ assert result["action"] == "skyvern_browser_profile_list"+ assert result["data"]["count"] == 2+ ids = [p["browser_profile_id"] for p in result["data"]["browser_profiles"]]+ assert ids == ["bp_abc123", "bp_def456"]+ # Datetimes must be serialized to ISO strings.+ assert isinstance(result["data"]["browser_profiles"][0]["created_at"], str)+ skyvern.list_browser_profiles.assert_awaited_once_with(include_deleted=False)+++async def test_list_passes_include_deleted(monkeypatch: pytest.MonkeyPatch) -> None:+ skyvern = SimpleNamespace(list_browser_profiles=AsyncMock(return_value=[]))+ monkeypatch.setattr(mcp_bp, "get_skyvern", lambda: skyvern)++ result = await mcp_bp.skyvern_browser_profile_list(include_deleted=True)++ assert result["ok"] is True+ assert result["data"]["count"] == 0+ skyvern.list_browser_profiles.assert_awaited_once_with(include_deleted=True)+++async def test_list_sdk_exception_returns_error(monkeypatch: pytest.MonkeyPatch) -> None:+ skyvern = SimpleNamespace(list_browser_profiles=AsyncMock(side_effect=RuntimeError("boom")))+ monkeypatch.setattr(mcp_bp, "get_skyvern", lambda: skyvern)++ result = await mcp_bp.skyvern_browser_profile_list()++ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.API_ERROR+ assert "boom" in result["error"]["message"]+++ ("browser_profile_id", "expected_text"),+ [+ ("not-a-real-id", "bp_"),+ ("bp_../etc/passwd", "path separators"),+ ],+)+async def test_get_rejects_invalid_id(browser_profile_id: str, expected_text: str) -> None:+ result = await mcp_bp.skyvern_browser_profile_get(browser_profile_id=browser_profile_id)++ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.INVALID_INPUT+ assert expected_text in f"{result['error']['message']} {result['error']['hint']}"+++async def test_get_returns_profile(monkeypatch: pytest.MonkeyPatch) -> None:+ skyvern = SimpleNamespace(get_browser_profile=AsyncMock(return_value=_make_profile()))+ monkeypatch.setattr(mcp_bp, "get_skyvern", lambda: skyvern)++ result = await mcp_bp.skyvern_browser_profile_get(browser_profile_id="bp_abc123")++ assert result["ok"] is True+ assert result["data"]["browser_profile_id"] == "bp_abc123"+ assert result["data"]["name"] == "example-app-signed-in"+ skyvern.get_browser_profile.assert_awaited_once_with("bp_abc123")+++async def test_get_404_maps_to_invalid_input(monkeypatch: pytest.MonkeyPatch) -> None:+ skyvern = SimpleNamespace(get_browser_profile=AsyncMock(side_effect=NotFoundError(body={"detail": "not found"})))+ monkeypatch.setattr(mcp_bp, "get_skyvern", lambda: skyvern)++ result = await mcp_bp.skyvern_browser_profile_get(browser_profile_id="bp_missing")++ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.INVALID_INPUT+ assert "not found" in result["error"]["message"]+++async def test_create_requires_name() -> None:+ result = await mcp_bp.skyvern_browser_profile_create(name="", browser_session_id="pbs_123")+ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.INVALID_INPUT+ assert "name" in result["error"]["message"]+++async def test_create_requires_exactly_one_source() -> None:+ both = await mcp_bp.skyvern_browser_profile_create(name="n", browser_session_id="pbs_1", workflow_run_id="wr_1")+ assert both["ok"] is False+ assert both["error"]["code"] == ErrorCode.INVALID_INPUT+ assert "exactly ONE" in both["error"]["message"]++ neither = await mcp_bp.skyvern_browser_profile_create(name="n")+ assert neither["ok"] is False+ assert neither["error"]["code"] == ErrorCode.INVALID_INPUT+++ ("source_kwargs", "expected_hint"),+ [+ ({"browser_session_id": "wr_wrong_prefix"}, "pbs_"),+ ({"workflow_run_id": "pbs_wrong_prefix"}, "wr_"),+ # tsk_v2_ IDs look valid to the generic run-id validator but the+ # browser-profile-create source lookup only accepts wr_. Reject+ # client-side so agents get a pointed INVALID_INPUT instead of a+ # less helpful server-side failure.+ ({"workflow_run_id": "tsk_v2_abc123"}, "wr_"),+ ],+)+async def test_create_rejects_bad_source_id(source_kwargs: dict[str, str], expected_hint: str) -> None:+ result = await mcp_bp.skyvern_browser_profile_create(name="n", **source_kwargs)++ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.INVALID_INPUT+ assert expected_hint in result["error"]["hint"]+++async def test_create_from_session_happy_path(monkeypatch: pytest.MonkeyPatch) -> None:+ skyvern = SimpleNamespace(create_browser_profile=AsyncMock(return_value=_make_profile()))+ monkeypatch.setattr(mcp_bp, "get_skyvern", lambda: skyvern)++ result = await mcp_bp.skyvern_browser_profile_create(+ name="example-app-signed-in", browser_session_id="pbs_abc", description="logged in"+ )++ assert result["ok"] is True+ assert result["data"]["browser_profile_id"] == "bp_abc123"+ skyvern.create_browser_profile.assert_awaited_once_with(+ name="example-app-signed-in",+ description="logged in",+ browser_session_id="pbs_abc",+ workflow_run_id=None,+ )+++async def test_create_from_workflow_run_happy_path(monkeypatch: pytest.MonkeyPatch) -> None:+ skyvern = SimpleNamespace(create_browser_profile=AsyncMock(return_value=_make_profile()))+ monkeypatch.setattr(mcp_bp, "get_skyvern", lambda: skyvern)++ result = await mcp_bp.skyvern_browser_profile_create(name="wf-run-profile", workflow_run_id="wr_xyz")++ assert result["ok"] is True+ skyvern.create_browser_profile.assert_awaited_once_with(+ name="wf-run-profile",+ description=None,+ browser_session_id=None,+ workflow_run_id="wr_xyz",+ )+++async def test_create_conflict_surfaces_clear_hint(monkeypatch: pytest.MonkeyPatch) -> None:+ skyvern = SimpleNamespace(+ create_browser_profile=AsyncMock(side_effect=ConflictError(body={"detail": "already exists"}))+ )+ monkeypatch.setattr(mcp_bp, "get_skyvern", lambda: skyvern)++ result = await mcp_bp.skyvern_browser_profile_create(name="dup", browser_session_id="pbs_1")++ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.INVALID_INPUT+ assert "already exists" in result["error"]["message"]+++async def test_create_archive_not_ready_is_retryable(monkeypatch: pytest.MonkeyPatch) -> None:+ skyvern = SimpleNamespace(+ create_browser_profile=AsyncMock(+ side_effect=BadRequestError(body={"detail": "Browser session does not have a persisted profile archive."})+ )+ )+ monkeypatch.setattr(mcp_bp, "get_skyvern", lambda: skyvern)++ result = await mcp_bp.skyvern_browser_profile_create(name="n", browser_session_id="pbs_pending")++ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.ACTION_FAILED+ hint = result["error"]["hint"].lower()+ assert "retry" in hint+ assert "wait" in hint+++async def test_create_source_not_found_maps_to_invalid_input(monkeypatch: pytest.MonkeyPatch) -> None:+ skyvern = SimpleNamespace(create_browser_profile=AsyncMock(side_effect=NotFoundError(body={"detail": "not found"})))+ monkeypatch.setattr(mcp_bp, "get_skyvern", lambda: skyvern)++ result = await mcp_bp.skyvern_browser_profile_create(name="n", browser_session_id="pbs_ghost")++ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.INVALID_INPUT+ assert "not found" in result["error"]["message"].lower()+++async def test_create_source_not_found_via_generic_api_error_maps_to_invalid_input(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # Reproduce Fern's actual behavior: create_browser_profile only explicitly+ # maps 400/409/422, so a 404 arrives as a generic ApiError, not+ # NotFoundError. The handler must key off status_code to still route to+ # INVALID_INPUT.+ from skyvern.client.core.api_error import ApiError++ skyvern = SimpleNamespace(+ create_browser_profile=AsyncMock(+ side_effect=ApiError(status_code=404, body={"detail": "Browser session pbs_ghost not found"})+ )+ )+ monkeypatch.setattr(mcp_bp, "get_skyvern", lambda: skyvern)++ result = await mcp_bp.skyvern_browser_profile_create(name="n", browser_session_id="pbs_ghost")++ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.INVALID_INPUT+ assert "not found" in result["error"]["message"].lower()+++async def test_delete_rejects_invalid_id() -> None:+ result = await mcp_bp.skyvern_browser_profile_delete(browser_profile_id="not-valid")+ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.INVALID_INPUT+++async def test_delete_happy_path(monkeypatch: pytest.MonkeyPatch) -> None:+ skyvern = SimpleNamespace(delete_browser_profile=AsyncMock(return_value=None))+ monkeypatch.setattr(mcp_bp, "get_skyvern", lambda: skyvern)++ result = await mcp_bp.skyvern_browser_profile_delete(browser_profile_id="bp_abc123")++ assert result["ok"] is True+ assert result["data"] == {"browser_profile_id": "bp_abc123", "deleted": True}+ skyvern.delete_browser_profile.assert_awaited_once_with("bp_abc123")+++async def test_delete_404_maps_to_invalid_input(monkeypatch: pytest.MonkeyPatch) -> None:+ skyvern = SimpleNamespace(delete_browser_profile=AsyncMock(side_effect=NotFoundError(body={"detail": "gone"})))+ monkeypatch.setattr(mcp_bp, "get_skyvern", lambda: skyvern)++ result = await mcp_bp.skyvern_browser_profile_delete(browser_profile_id="bp_ghost")++ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.INVALID_INPUT+ assert "not found" in result["error"]["message"]+++def test_tools_registered_on_fastmcp() -> None:+ from skyvern.cli import mcp_tools as pkg++ for name in (+ "skyvern_browser_profile_list",+ "skyvern_browser_profile_get",+ "skyvern_browser_profile_create",+ "skyvern_browser_profile_delete",+ ):+ assert hasattr(pkg, name), f"{name} not re-exported from mcp_tools package"+ assert name in pkg.__all__, f"{name} missing from mcp_tools.__all__"diff --git a/tests/unit/test_mcp_session_lifecycle.py b/tests/unit/test_mcp_session_lifecycle.pyindex 855b9115b..7f2effe57 100644--- a/tests/unit/test_mcp_session_lifecycle.py+++ b/tests/unit/test_mcp_session_lifecycle.py@@ -658,6 +658,27 @@ async def test_session_create_stateless_mode_rejects_local() -> None:assert result["error"]["code"] == mcp_session.ErrorCode.INVALID_INPUT+ ("local", "browser_type"),+ [(True, None), (False, "cdp-connect")],+ ids=["local", "cdp"],+)+async def test_session_create_rejects_local_or_cdp_browser_profile_id(+ monkeypatch: pytest.MonkeyPatch,+ local: bool,+ browser_type: str | None,+) -> None:+ if browser_type:+ monkeypatch.setenv("BROWSER_TYPE", browser_type)++ result = await mcp_session.skyvern_browser_session_create(local=local, browser_profile_id="bp_saved")++ assert result["ok"] is False+ assert result["error"]["code"] == mcp_session.ErrorCode.INVALID_INPUT+ assert "cloud" in result["error"]["hint"]++@pytest.mark.asyncioasync def test_session_create_forwards_extensions_to_stateful_session_create(monkeypatch: pytest.MonkeyPatch,@@ -718,3 +739,40 @@ async def test_session_create_persists_active_api_key_hash_in_session_state(assert current.context == BrowserContext(mode="cloud_session", session_id="pbs_123")assert current.api_key_hash == session_manager._api_key_hash("sk_key_create")assert current.api_key_hash != "sk_key_create"+++async def test_session_create_rejects_empty_browser_profile_id() -> None:+ # Empty string must not silently slip through to the SDK. The server's+ # profile lookup is also truthy-gated, so "" would be treated as+ # "no profile" and silently skip login-reuse. validate_browser_profile_id+ # rejects "" because it does not start with bp_.+ result = await mcp_session.skyvern_browser_session_create(browser_profile_id="")++ assert result["ok"] is False+ assert result["error"]["code"] == mcp_session.ErrorCode.INVALID_INPUT+ assert "bp_" in result["error"]["hint"]+++async def test_session_create_maps_browser_profile_not_found_to_invalid_input(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ from skyvern.client.errors import NotFoundError++ session_manager.set_stateless_http_mode(True)+ fake_skyvern = MagicMock()+ fake_skyvern.create_browser_session = AsyncMock(+ side_effect=NotFoundError(body={"detail": "Browser profile bp_missing not found"})+ )+ monkeypatch.setattr(mcp_session, "get_skyvern", lambda: fake_skyvern)++ try:+ result = await mcp_session.skyvern_browser_session_create(browser_profile_id="bp_missing")+ finally:+ session_manager.set_stateless_http_mode(False)++ assert result["ok"] is False+ assert result["error"]["code"] == mcp_session.ErrorCode.INVALID_INPUT+ assert "browser profile" in result["error"]["message"].lower()+ assert "skyvern_browser_profile_list" in result["error"]["hint"]diff --git a/tests/unit/test_mcp_workflow_guidance.py b/tests/unit/test_mcp_workflow_guidance.pyindex c9f0285e2..3c6b307e8 100644--- a/tests/unit/test_mcp_workflow_guidance.py+++ b/tests/unit/test_mcp_workflow_guidance.py@@ -1,5 +1,7 @@from __future__ import annotations+from pathlib import Path+import pytestfrom skyvern.cli.mcp_tools import mcp@@ -28,6 +30,26 @@ def test_mcp_instructions_guide_text_prompt_defaults() -> None:assert "skyvern_observe" in mcp.instructions+def test_bundled_skyvern_skill_documents_browser_profile_lifecycle() -> None:+ # Anchor to this test file rather than CWD so the test doesn't depend on+ # where pytest is invoked from.+ repo_root = Path(__file__).resolve().parents[2]+ skill_dir = repo_root / "skyvern/cli/skills/skyvern"+ skill_md = (skill_dir / "SKILL.md").read_text(encoding="utf-8")+ sessions = (skill_dir / "references" / "sessions.md").read_text(encoding="utf-8")+ cli_parity = (skill_dir / "references" / "cli-parity.md").read_text(encoding="utf-8")+ combined = "\n".join([skill_md, sessions, cli_parity])+ normalized = " ".join(combined.split())++ assert "save" in normalized.lower()+ assert "reuse" in normalized.lower()+ assert "skyvern workflow run --id wpid_123 --browser-profile-id" in normalized+ assert "skyvern browser session create --browser-profile-id" in normalized+ assert "validate logged-in state before re-login" in normalized.lower()+ assert "state_save/state_load" in combined+ assert "not the cloud browser-profile reuse path" in normalized++@pytest.mark.asyncioasync def test_expected_prompts_registered() -> None:prompts = await mcp.list_prompts()diff --git a/tests/unit/test_mcp_workflow_tools.py b/tests/unit/test_mcp_workflow_tools.pyindex f31e384e2..9f232cf25 100644--- a/tests/unit/test_mcp_workflow_tools.py+++ b/tests/unit/test_mcp_workflow_tools.py@@ -30,6 +30,31 @@ def _fake_workflow_response() -> SimpleNamespace:)+def _fake_workflow_run_response(**overrides: object) -> SimpleNamespace:+ defaults: dict[str, object] = {+ "run_id": "wr_run",+ "status": "running",+ "run_type": "workflow_run",+ "step_count": None,+ "failure_reason": None,+ "recording_url": None,+ "app_url": None,+ "browser_session_id": None,+ "browser_profile_id": None,+ "run_with": None,+ "ai_fallback": None,+ "output": None,+ "created_at": None,+ "modified_at": None,+ "started_at": None,+ "finished_at": None,+ "queued_at": None,+ "script_run": None,+ }+ defaults.update(overrides)+ return SimpleNamespace(**defaults)++def _fake_http_response(payload: dict[str, object], status_code: int = 200) -> SimpleNamespace:return SimpleNamespace(status_code=status_code,@@ -151,6 +176,7 @@ def _heavy_workflow_run_payload(*, include_expanded_outputs: bool = True) -> dic}],"outputs": outputs,+ "browser_profile_id": "bp_status","run_with": "code",}@@ -609,6 +635,106 @@ async def test_mcp_text_prompt_without_llm_key_stays_null(monkeypatch: pytest.Moassert sent_block.get("model") is None+async def test_workflow_run_passes_browser_profile_id_and_surfaces_debug(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ fake_client = SimpleNamespace(+ run_workflow=AsyncMock(+ return_value=_fake_workflow_run_response(+ run_id="wr_profile",+ browser_session_id="pbs_existing",+ browser_profile_id="bp_saved",+ )+ )+ )+ monkeypatch.setattr(workflow_tools, "get_skyvern", lambda: fake_client)++ result = await workflow_tools.skyvern_workflow_run(+ workflow_id="wpid_test",+ parameters='{"account": "123"}',+ browser_session_id="pbs_existing",+ browser_profile_id="bp_saved",+ )++ assert result["ok"] is True+ fake_client.run_workflow.assert_awaited_once()+ sent = fake_client.run_workflow.await_args.kwargs+ assert sent["browser_session_id"] == "pbs_existing"+ assert sent["browser_profile_id"] == "bp_saved"+ assert result["data"]["browser_profile_id"] == "bp_saved"+ assert "browser_session_id='pbs_existing'" in result["data"]["sdk_equivalent"]+ assert "browser_profile_id='bp_saved'" in result["data"]["sdk_equivalent"]+++async def test_workflow_run_maps_browser_profile_not_found_to_invalid_input(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ from skyvern.cli.core.result import ErrorCode+ from skyvern.client.core.api_error import ApiError++ # Reproduce Fern's actual production behavior: run_workflow's Fern client+ # only explicitly maps 400/422, so a 404 arrives as a generic ApiError+ # (NOT NotFoundError). The handler must key off status_code to catch it.+ fake_client = SimpleNamespace(+ run_workflow=AsyncMock(+ side_effect=ApiError(+ status_code=404,+ body={"detail": "Browser profile bp_missing not found"},+ )+ )+ )+ monkeypatch.setattr(workflow_tools, "get_skyvern", lambda: fake_client)++ result = await workflow_tools.skyvern_workflow_run(+ workflow_id="wpid_exists",+ browser_profile_id="bp_missing",+ )++ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.INVALID_INPUT+ assert "browser profile" in result["error"]["message"].lower()+ assert "skyvern_browser_profile_list" in result["error"]["hint"]+++async def test_workflow_run_maps_workflow_not_found_to_workflow_not_found(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ from skyvern.cli.core.result import ErrorCode+ from skyvern.client.core.api_error import ApiError++ # A 404 whose body does NOT mention "browser profile" must still route+ # to WORKFLOW_NOT_FOUND even when it arrives as generic ApiError.+ fake_client = SimpleNamespace(+ run_workflow=AsyncMock(side_effect=ApiError(status_code=404, body={"detail": "Workflow wpid_ghost not found"}))+ )+ monkeypatch.setattr(workflow_tools, "get_skyvern", lambda: fake_client)++ result = await workflow_tools.skyvern_workflow_run(workflow_id="wpid_ghost")++ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.WORKFLOW_NOT_FOUND+ assert "wpid_ghost" in result["error"]["message"]+++async def test_workflow_run_rejects_empty_browser_profile_id() -> None:+ from skyvern.cli.core.result import ErrorCode++ result = await workflow_tools.skyvern_workflow_run(+ workflow_id="wpid_exists",+ browser_profile_id="",+ )++ # Empty string must not silently reach the SDK; validate_browser_profile_id+ # rejects it because "" does not start with bp_.+ assert result["ok"] is False+ assert result["error"]["code"] == ErrorCode.INVALID_INPUT+ assert "bp_" in result["error"]["hint"]++@pytest.mark.asyncioasync def test_workflow_status_uses_workflow_run_route_for_wr_ids(monkeypatch: pytest.MonkeyPatch) -> None:payload = _heavy_workflow_run_payload(include_expanded_outputs=False)@@ -662,6 +788,7 @@ async def test_workflow_status_full_preserves_expanded_workflow_details(monkeypaassert data["recording_url"] == payload["recording_url"]assert data["output"] == payload["outputs"]assert data["workflow_title"] == "Heavy workflow"+ assert data["browser_profile_id"] == "bp_status"@pytest.mark.asyncio@@ -738,6 +865,7 @@ async def test_workflow_status_full_via_mcp_client(monkeypatch: pytest.MonkeyPatassert data["run_id"] == "wr_heavy"assert data["recording_url"] == payload["recording_url"]assert data["output"] == payload["outputs"]+ assert data["browser_profile_id"] == "bp_status"# ---------------------------------------------------------------------------