Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix MCP remote tool handlers calling wrong API environment (#4869)
Marc Kelechava1f2ee7b8b44e
diff --git a/skyvern/cli/core/client.py b/skyvern/cli/core/client.pyindex 623f3ec1c..1fe2b3cfe 100644--- a/skyvern/cli/core/client.py+++ b/skyvern/cli/core/client.py@@ -46,11 +46,27 @@ def _resolve_base_url() -> str | None:return settings.SKYVERN_BASE_URL or os.environ.get("SKYVERN_BASE_URL")+def _resolve_self_base_url() -> str:+ """Build the base URL for the current server process.++ When the MCP server is hosted inside the FastAPI process (stateless HTTP+ mode), tool handlers must call back to the *same* server rather than the+ default ``SkyvernEnvironment.CLOUD`` URL. This prevents staging API keys+ from being sent to production (or vice-versa).++ Always uses HTTP on localhost since TLS terminates at the load balancer.+ """+ return f"http://127.0.0.1:{settings.PORT}"++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()return Skyvern(api_key=api_key,environment=SkyvernEnvironment.CLOUD,- base_url=_resolve_base_url(),+ base_url=base_url,)diff --git a/tests/unit/test_mcp_session_lifecycle.py b/tests/unit/test_mcp_session_lifecycle.pyindex a0d1bce73..a0e2f0b18 100644--- a/tests/unit/test_mcp_session_lifecycle.py+++ b/tests/unit/test_mcp_session_lifecycle.py@@ -160,6 +160,43 @@ def test_get_skyvern_override_cache_closes_evicted_client(monkeypatch: pytest.Moassert closed_keys == ["sk_key_a"]+def test_build_cloud_client_uses_self_url_in_stateless_mode(monkeypatch: pytest.MonkeyPatch) -> None:+ """In stateless HTTP mode the SDK client must call back to the same process."""+ captured_kwargs: list[dict[str, object]] = []++ class FakeSkyvern:+ def __init__(self, *args: object, **kwargs: object) -> None:+ captured_kwargs.append(dict(kwargs))++ monkeypatch.setattr(client_mod, "Skyvern", FakeSkyvern)+ session_manager.set_stateless_http_mode(True)++ client_mod._build_cloud_client("sk_test")++ assert len(captured_kwargs) == 1+ base_url = captured_kwargs[0]["base_url"]+ assert isinstance(base_url, str)+ assert "127.0.0.1" in base_url+++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]] = []++ class FakeSkyvern:+ def __init__(self, *args: object, **kwargs: object) -> None:+ captured_kwargs.append(dict(kwargs))++ monkeypatch.setattr(client_mod, "Skyvern", FakeSkyvern)+ monkeypatch.setattr(client_mod.settings, "SKYVERN_BASE_URL", "https://api-staging.skyvern.com")+ session_manager.set_stateless_http_mode(False)++ client_mod._build_cloud_client("sk_test")++ assert len(captured_kwargs) == 1+ assert captured_kwargs[0]["base_url"] == "https://api-staging.skyvern.com"++@pytest.mark.asyncioasync def test_close_skyvern_closes_singleton() -> None:fake = MagicMock()