Skip to content

Automate browser based workflows with AI

fix: preserve credential parameters during MCP workflow updates (#5205)

Marc Kelechavae3a26099413e
diff --git a/skyvern/cli/mcp_tools/workflow.py b/skyvern/cli/mcp_tools/workflow.py
index 203c06e53..69fdb46e7 100644
--- a/skyvern/cli/mcp_tools/workflow.py
+++ b/skyvern/cli/mcp_tools/workflow.py
@@ -636,6 +636,65 @@ async def _inject_workflow_update_proxy_default(definition: str, fmt: str, workf
return _dump_definition_dict(raw, parsed_format)
+# Parameter types that are auto-managed (not user-visible input parameters) and should
+# be preserved during MCP updates when the caller omits them from the definition.
+_AUTO_MANAGED_PARAMETER_TYPES = frozenset({"credential"})
+
+
+async def _inject_workflow_update_parameters(definition: str, fmt: str, workflow_id: str) -> str:
+ """Preserve auto-managed parameters (e.g. credential) when MCP update omits them.
+
+ When an MCP client updates a workflow, it may provide blocks and workflow-level
+ input parameters but omit credential parameters that were created automatically
+ when the user selected a credential in the UI. This function reads the existing
+ workflow's parameters and injects any auto-managed parameters whose keys are
+ missing from the update definition, preventing accidental data loss.
+ """
+
+ raw, parsed_format = _load_definition_dict(definition, fmt)
+ if raw is None or parsed_format is None:
+ return definition
+
+ wf_def = raw.get("workflow_definition")
+ if not isinstance(wf_def, dict):
+ return definition
+
+ update_params: list[dict[str, Any]] = wf_def.get("parameters", [])
+ update_keys = {p.get("key") for p in update_params if isinstance(p, dict)}
+
+ existing_workflow = await _get_workflow_by_id(workflow_id)
+ existing_wf_def = existing_workflow.get("workflow_definition")
+ if not isinstance(existing_wf_def, dict):
+ return definition
+
+ existing_params: list[dict[str, Any]] = existing_wf_def.get("parameters", [])
+
+ injected = False
+ for param in existing_params:
+ if not isinstance(param, dict):
+ continue
+ ptype = param.get("parameter_type")
+ pkey = param.get("key")
+ if ptype in _AUTO_MANAGED_PARAMETER_TYPES and pkey and pkey not in update_keys:
+ # Build a minimal YAML-compatible parameter dict (strip runtime-only fields)
+ injected_param: dict[str, Any] = {
+ "parameter_type": ptype,
+ "key": pkey,
+ }
+ if param.get("description"):
+ injected_param["description"] = param["description"]
+ if ptype == "credential" and param.get("credential_id"):
+ injected_param["credential_id"] = param["credential_id"]
+ update_params.append(injected_param)
+ injected = True
+
+ if not injected:
+ return definition
+
+ wf_def["parameters"] = update_params
+ return _dump_definition_dict(raw, parsed_format)
+
+
def _parse_definition(
definition: str, fmt: str
) -> tuple[WorkflowCreateYamlRequest | None, str | None, dict[str, Any] | None]:
@@ -894,6 +953,7 @@ async def skyvern_workflow_update(
try:
definition = await _inject_workflow_update_proxy_default(definition, format, workflow_id)
+ definition = await _inject_workflow_update_parameters(definition, format, workflow_id)
except NotFoundError:
return make_result(
"skyvern_workflow_update",
diff --git a/tests/unit/test_mcp_workflow_tools.py b/tests/unit/test_mcp_workflow_tools.py
index 50db933b7..b9f88b754 100644
--- a/tests/unit/test_mcp_workflow_tools.py
+++ b/tests/unit/test_mcp_workflow_tools.py
@@ -630,3 +630,162 @@ async def test_workflow_status_full_via_mcp_client(monkeypatch: pytest.MonkeyPat
assert data["run_id"] == "wr_heavy"
assert data["recording_url"] == payload["recording_url"]
assert data["output"] == payload["outputs"]
+
+
+# ---------------------------------------------------------------------------
+# Credential parameter preservation during workflow update
+# ---------------------------------------------------------------------------
+
+
+async def test_workflow_update_preserves_credential_parameters_when_omitted(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """When the update definition omits credential parameters, they should be
+ injected from the existing workflow so the login block keeps working."""
+
+ fake_client = SimpleNamespace(update_workflow=AsyncMock(return_value=_fake_workflow_response()))
+ monkeypatch.setattr(workflow_tools, "get_skyvern", lambda: fake_client)
+
+ async def fake_get_workflow_by_id(workflow_id: str, version: int | None = None) -> dict[str, object]:
+ return {
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [
+ {
+ "parameter_type": "credential",
+ "key": "credentials",
+ "credential_id": "cred_abc123",
+ "credential_parameter_id": "cp_xyz",
+ "workflow_id": "wf_test",
+ "description": None,
+ },
+ {
+ "parameter_type": "workflow",
+ "key": "url_input",
+ "workflow_parameter_type": "string",
+ "default_value": "https://example.com",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "login_block",
+ "parameter_keys": ["credentials"],
+ }
+ ],
+ },
+ }
+
+ monkeypatch.setattr(workflow_tools, "_get_workflow_by_id", fake_get_workflow_by_id)
+
+ # The update definition includes the workflow parameter but omits the credential parameter
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [
+ {
+ "parameter_type": "workflow",
+ "key": "url_input",
+ "workflow_parameter_type": "string",
+ "default_value": "https://new-url.com",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "login_block",
+ "parameter_keys": ["credentials"],
+ "navigation_goal": "Login to the site",
+ }
+ ],
+ },
+ }
+
+ result = await workflow_tools.skyvern_workflow_update(
+ workflow_id="wpid_test",
+ definition=json.dumps(definition),
+ format="json",
+ )
+
+ assert result["ok"] is True
+
+ sent_def = fake_client.update_workflow.await_args.kwargs["json_definition"]
+ # Verify credential parameter was injected
+ params = sent_def.workflow_definition.parameters
+ cred_params = [p for p in params if getattr(p, "parameter_type", None) == "credential"]
+ assert len(cred_params) == 1
+ assert cred_params[0].credential_id == "cred_abc123"
+ assert cred_params[0].key == "credentials"
+
+
+async def test_workflow_update_does_not_duplicate_existing_credential_parameter(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """When the update definition already includes the credential parameter,
+ it should NOT be duplicated."""
+
+ fake_client = SimpleNamespace(update_workflow=AsyncMock(return_value=_fake_workflow_response()))
+ monkeypatch.setattr(workflow_tools, "get_skyvern", lambda: fake_client)
+
+ async def fake_get_workflow_by_id(workflow_id: str, version: int | None = None) -> dict[str, object]:
+ return {
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [
+ {
+ "parameter_type": "credential",
+ "key": "credentials",
+ "credential_id": "cred_abc123",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "login_block",
+ "parameter_keys": ["credentials"],
+ }
+ ],
+ },
+ }
+
+ monkeypatch.setattr(workflow_tools, "_get_workflow_by_id", fake_get_workflow_by_id)
+
+ # The update definition already includes the credential parameter
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [
+ {
+ "parameter_type": "credential",
+ "key": "credentials",
+ "credential_id": "cred_abc123",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "login_block",
+ "parameter_keys": ["credentials"],
+ "navigation_goal": "Login to the site",
+ }
+ ],
+ },
+ }
+
+ result = await workflow_tools.skyvern_workflow_update(
+ workflow_id="wpid_test",
+ definition=json.dumps(definition),
+ format="json",
+ )
+
+ assert result["ok"] is True
+
+ sent_def = fake_client.update_workflow.await_args.kwargs["json_definition"]
+ params = sent_def.workflow_definition.parameters
+ cred_params = [p for p in params if getattr(p, "parameter_type", None) == "credential"]
+ # Should still be exactly 1, not duplicated
+ assert len(cred_params) == 1