Skip to content

Automate browser based workflows with AI

Deliver the MCP user-agent header so MCP-triggered runs persist trigger_type=mcp (#7982)

Aaron Perez9fe888e80f1c
diff --git a/skyvern/cli/core/client.py b/skyvern/cli/core/client.py
index 59d986637..afb65e92b 100644
--- a/skyvern/cli/core/client.py
+++ b/skyvern/cli/core/client.py
@@ -6,6 +6,7 @@ from collections import OrderedDict
from contextvars import ContextVar, Token
from threading import RLock
+import httpx
import structlog
from skyvern.client import SkyvernEnvironment
@@ -64,11 +65,19 @@ def _build_cloud_client(api_key: str) -> Skyvern:
from .session_manager import is_stateless_http_mode # noqa: PLC0415 — circular import
base_url: str | None = _resolve_self_base_url() if is_stateless_http_mode() else _resolve_base_url()
+ # Generated SDK methods send "x-user-agent": None when no per-call user_agent is given,
+ # clobbering constructor-level headers in the merge; httpx client-level defaults survive
+ # because None-valued headers are stripped before the request is sent (SKY-13333).
return Skyvern(
api_key=api_key,
environment=SkyvernEnvironment.CLOUD,
base_url=base_url,
headers={"x-user-agent": SKYVERN_MCP_USER_AGENT},
+ httpx_client=httpx.AsyncClient(
+ timeout=60,
+ follow_redirects=True,
+ headers={"x-user-agent": SKYVERN_MCP_USER_AGENT},
+ ),
)
diff --git a/skyvern/forge/sdk/core/skyvern_context.py b/skyvern/forge/sdk/core/skyvern_context.py
index 269336371..e3b533b79 100644
--- a/skyvern/forge/sdk/core/skyvern_context.py
+++ b/skyvern/forge/sdk/core/skyvern_context.py
@@ -124,6 +124,11 @@ class SkyvernContext:
browser_container_ip: str | None = None
browser_container_task_arn: str | None = None
feature_flag_entries: dict[str, bool | str | None] = field(default_factory=dict)
+ # Absolute event-loop time the run body's elapsed-time budget expires, set alongside the
+ # asyncio.timeout that enforces it. None when nothing is enforcing one. Read by work that
+ # may block for a long time, so it can give up and return rather than be cancelled — a
+ # cancellation propagates as BaseException and skips handlers that degrade gracefully.
+ max_elapsed_deadline: float | None = None
# feature flags
enable_page_ready_wait: bool = False
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index f9b9629f6..90915a4db 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -3506,6 +3506,11 @@ class WorkflowService:
return workflow_run
else:
timeout_context = asyncio.timeout(max_elapsed_timeout_seconds)
+ # Publish the same deadline the timeout enforces, so work that can block for
+ # a long time can give up in time to still do something useful instead of
+ # being cancelled with nothing done.
+ if body_context := skyvern_context.current():
+ body_context.max_elapsed_deadline = asyncio.get_running_loop().time() + max_elapsed_timeout_seconds
try:
async with timeout_context:
await execute_workflow_blocks()
diff --git a/skyvern/services/task_v2_service.py b/skyvern/services/task_v2_service.py
index cdcc1337a..feb4145f7 100644
--- a/skyvern/services/task_v2_service.py
+++ b/skyvern/services/task_v2_service.py
@@ -582,6 +582,10 @@ async def run_task_v2(
trigger_type=parent_context.trigger_type if parent_context else None,
use_flex_llm_routing=parent_context.use_flex_llm_routing if parent_context else False,
consecutive_captcha_timeouts=parent_context.consecutive_captcha_timeouts if parent_context else 0,
+ # The parent run body's asyncio.timeout stays binding inside this scope; dropping the
+ # deadline would let long-blocking work (e.g. a throttled vendor create) wait past it
+ # and be cancelled instead of giving up in time to degrade.
+ max_elapsed_deadline=parent_context.max_elapsed_deadline if parent_context else None,
preserve_transient_ui_capture=parent_context.preserve_transient_ui_capture if parent_context else None,
preserve_transient_ui_capture_resolved=(
parent_context.preserve_transient_ui_capture_resolved if parent_context else False
diff --git a/tests/unit/test_mcp_session_lifecycle.py b/tests/unit/test_mcp_session_lifecycle.py
index c3b9f6141..fbb2e5c36 100644
--- a/tests/unit/test_mcp_session_lifecycle.py
+++ b/tests/unit/test_mcp_session_lifecycle.py
@@ -4,6 +4,7 @@ from types import SimpleNamespace
from typing import cast
from unittest.mock import AsyncMock, MagicMock
+import httpx
import pytest
from skyvern.cli.core import client as client_mod
@@ -211,6 +212,37 @@ def test_build_cloud_client_passes_mcp_user_agent_header(monkeypatch: pytest.Mon
assert captured_kwargs[0]["headers"] == {"x-user-agent": SKYVERN_MCP_USER_AGENT}
+async def test_build_cloud_client_user_agent_header_reaches_the_wire(monkeypatch: pytest.MonkeyPatch) -> None:
+ """Generated SDK methods send x-user-agent: None per call, which must not clobber the MCP header (SKY-13333)."""
+ monkeypatch.setattr(client_mod.settings, "SKYVERN_BASE_URL", "http://skyvern.test")
+ session_manager.set_stateless_http_mode(False)
+
+ requests: list[httpx.Request] = []
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ requests.append(request)
+ return httpx.Response(
+ 200,
+ json={
+ "run_id": "wr_123",
+ "status": "queued",
+ "created_at": "2026-01-01T00:00:00Z",
+ "modified_at": "2026-01-01T00:00:00Z",
+ },
+ )
+
+ instance = client_mod._build_cloud_client("sk_test")
+ try:
+ instance._client_wrapper.httpx_client.httpx_client._transport = httpx.MockTransport(handler)
+ await instance.run_workflow(workflow_id="wpid_123")
+ finally:
+ await instance.aclose()
+
+ assert requests
+ assert requests[0].headers.get("x-user-agent") == SKYVERN_MCP_USER_AGENT
+
+
def test_build_cloud_client_uses_settings_url_in_normal_mode(monkeypatch: pytest.MonkeyPatch) -> None:
"""Outside stateless HTTP mode the SDK client should use the configured URL."""
captured_kwargs: list[dict[str, object]] = []