Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
feat(mcp): code-only mode for block validation (Copilot connector, M4) (#6997)
Marc Kelechava5950e00c100a
diff --git a/skyvern/cli/mcp_tools/blocks.py b/skyvern/cli/mcp_tools/blocks.pyindex 24ac90773..d91923c29 100644--- a/skyvern/cli/mcp_tools/blocks.py+++ b/skyvern/cli/mcp_tools/blocks.py@@ -464,6 +464,10 @@ async def skyvern_block_validate(str,Field(description="JSON string of a single block definition to validate"),],+ code_only: Annotated[+ bool,+ Field(description="When true, structurally reject non-code browser/page block types (code-only mode)"),+ ] = False,) -> dict[str, Any]:"""Validate a workflow block definition before using it in skyvern_workflow_create. Returns field-level errors."""action = "skyvern_block_validate"@@ -492,6 +496,27 @@ async def skyvern_block_validate(),)+ if code_only:+ # Preserve the lightweight-install constraint; copilot helpers require server-only deps.+ from skyvern.forge.sdk.copilot.tools.banned_blocks import ( # noqa: PLC0415+ collect_code_only_banned_items,+ )++ banned_items = collect_code_only_banned_items([raw])+ if banned_items:+ labels = ", ".join(sorted({label for label, _ in banned_items}))+ types = ", ".join(sorted({block_type for _, block_type in banned_items}))+ return make_result(+ action,+ ok=False,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ f"Block type(s) {types} are not allowed in code-only mode (offending labels: {labels})",+ "In code-only mode, use a `code` block for durable browser/page work instead of "+ "task/navigation/extraction/etc.",+ ),+ )+adapter = _get_block_adapter()try:block = adapter.validate_python(raw)diff --git a/skyvern/forge/sdk/copilot/tools/banned_blocks.py b/skyvern/forge/sdk/copilot/tools/banned_blocks.pyindex 326363b2a..729ca6198 100644--- a/skyvern/forge/sdk/copilot/tools/banned_blocks.py+++ b/skyvern/forge/sdk/copilot/tools/banned_blocks.py@@ -349,6 +349,30 @@ def _collect_banned_block_items(return items+def _blocks_with_default_labels(blocks: list[Any]) -> list[Any]:+ normalized_blocks: list[Any] = []+ for block in blocks:+ if not isinstance(block, dict):+ normalized_blocks.append(block)+ continue+ normalized = dict(block)+ if not isinstance(normalized.get("label"), str):+ normalized["label"] = "(unlabeled)"+ loop_blocks = normalized.get("loop_blocks")+ if isinstance(loop_blocks, list):+ normalized["loop_blocks"] = _blocks_with_default_labels(loop_blocks)+ normalized_blocks.append(normalized)+ return normalized_blocks+++def collect_code_only_banned_items(blocks: list[Any]) -> list[tuple[str, str]]:+ """Banned (label, block_type) pairs under code-only browser mode; unlabeled blocks included."""+ return _collect_banned_block_items(+ _blocks_with_default_labels(blocks),+ _COPILOT_CODE_ONLY_BROWSER_BANNED_BLOCK_TYPES,+ )++def _detect_new_banned_blocks(submitted_yaml: str,prior_workflow_yaml: str | None,diff --git a/tests/unit/test_block_validate_code_only.py b/tests/unit/test_block_validate_code_only.pynew file mode 100644index 000000000..2ac4dbfc9--- /dev/null+++ b/tests/unit/test_block_validate_code_only.py@@ -0,0 +1,92 @@+from __future__ import annotations++import json+from typing import Any++import pytest++from skyvern.cli.mcp_tools.blocks import skyvern_block_validate+from skyvern.forge.sdk.copilot.tools.banned_blocks import collect_code_only_banned_items+++def _navigation_block() -> dict[str, str]:+ return {+ "block_type": "navigation",+ "label": "do_search",+ "url": "https://example.com",+ "navigation_goal": "Search and click the first result",+ }+++async def test_navigation_block_rejected_in_code_only_mode() -> None:+ result = await skyvern_block_validate(json.dumps(_navigation_block()), code_only=True)++ assert result["ok"] is False+ assert "code-only" in result["error"]["message"].lower()+++async def test_label_less_navigation_block_rejected_in_code_only_mode() -> None:+ block = _navigation_block()+ block.pop("label")++ result = await skyvern_block_validate(json.dumps(block), code_only=True)++ assert result["ok"] is False+ assert "code-only" in result["error"]["message"].lower()+++async def test_task_block_rejected_in_code_only_mode() -> None:+ block = {+ "block_type": "task",+ "label": "do_search",+ "url": "https://example.com",+ "navigation_goal": "Search and click the first result",+ }+ result = await skyvern_block_validate(json.dumps(block), code_only=True)++ assert result["ok"] is False+ assert "code-only" in result["error"]["message"].lower()+++async def test_code_block_accepted_in_code_only_mode() -> None:+ block = {+ "block_type": "code",+ "label": "do_it_in_code",+ "code": 'await page.goto("https://example.com")\nreturn {"ok": True}',+ }+ result = await skyvern_block_validate(json.dumps(block), code_only=True)++ assert result["ok"] is True+ assert result["data"]["valid"] is True+ assert result["data"]["block_type"] == "code"+++async def test_navigation_block_validates_when_code_only_defaults_off() -> None:+ result = await skyvern_block_validate(json.dumps(_navigation_block()))++ assert result["ok"] is True+ assert result["data"]["block_type"] == "navigation"+++def test_collect_code_only_banned_items_includes_label_less_nested_blocks() -> None:+ nested_block: dict[str, Any] = {+ "block_type": "navigation",+ "url": "https://example.com",+ "navigation_goal": "Open the item",+ }+ blocks: list[Any] = [+ {+ "block_type": "for_loop",+ "label": "for_each_item",+ "loop_over_parameter_key": "items",+ "loop_blocks": [nested_block],+ }+ ]++ assert collect_code_only_banned_items(blocks) == [("(unlabeled)", "navigation")]+ assert "label" not in nested_block