Coverage for apps / backend / src / orcheo_backend / app / mcp_server / workflow_tools.py: 99%

201 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-11-16 14:04 +0000

1"""MCP tools for inspecting and managing workflows.""" 

2 

3from __future__ import annotations 

4import logging 

5import re 

6from typing import Annotated, Any 

7from fastmcp import FastMCP 

8from fastmcp.exceptions import ToolError 

9from pydantic import Field 

10from orcheo.models import WorkflowChatKitConfig, WorkflowDraftAccess 

11from orcheo.models.workflow_refs import normalize_workflow_handle, workflow_ref_is_uuid 

12from orcheo.triggers.cron import CronTriggerConfig 

13from orcheo.triggers.cron_extraction import ( 

14 CronExtractionError, 

15 extract_version_cron_config, 

16) 

17from orcheo_backend.app.mcp_server._shared import ( 

18 DESTRUCTIVE, 

19 IDEMPOTENT_WRITE, 

20 MCP_ACTOR, 

21 READ_ONLY, 

22 WRITE, 

23 VersionArg, 

24 WorkflowArg, 

25 WorkspaceArg, 

26 api_client, 

27 fetch_version, 

28 version_summary, 

29) 

30from orcheo_backend.app.mcp_server.api_client import InProcessApiClient, McpApiError 

31from orcheo_backend.app.mcp_server.scopes import requires 

32 

33 

34logger = logging.getLogger(__name__) 

35 

36_MAIN_GUARD = re.compile(r"^\s*if\s+__name__\s*==\s*[\"']__main__[\"']\s*:") 

37_SLUG_RE = re.compile(r"[^a-z0-9]+") 

38 

39 

40def _strip_main_block(script: str) -> str: 

41 """Drop everything from an ``if __name__ == "__main__":`` guard onwards.""" 

42 lines = script.split("\n") 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

43 for index, line in enumerate(lines): 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

44 if _MAIN_GUARD.match(line): 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

45 return "\n".join(lines[:index]) 1uvhijwxyzABCDEFGHIJKcklmnoaqbLfepgr

46 return script 1stbd

47 

48 

49def _slugify(value: str) -> str: 

50 return _SLUG_RE.sub("-", value.strip().lower()).strip("-") or "workflow" 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

51 

52 

53async def _find_or_create_workflow( 

54 api: InProcessApiClient, 

55 *, 

56 workflow: str | None, 

57 name: str | None, 

58 description: str | None, 

59) -> tuple[dict[str, Any], bool]: 

60 """Resolve the upload target, creating it when needed. 

61 

62 A UUID ``workflow`` must already exist. A handle is created on first 

63 upload, so re-running the same upload is idempotent. 

64 """ 

65 handle: str | None = None 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepMgQPNr

66 if workflow is not None: 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepMgQPNr

67 is_uuid = workflow_ref_is_uuid(workflow) 1hijcklmnabdeMgPN

68 if not is_uuid: 1hijcklmnabdeMgPN

69 try: 1kbdeMgP

70 handle = normalize_workflow_handle(workflow) 1kbdeMgP

71 except ValueError as exc: 1P

72 raise ToolError(str(exc)) from exc 1P

73 try: 1hijcklmnabdeMgN

74 existing = await api.get(f"/api/workflows/{handle or workflow}") 1hijcklmnabdeMgN

75 except McpApiError as exc: 1kbdeMgN

76 if is_uuid or exc.status_code != 404: 1kbdeMgN

77 raise 1MN

78 else: 

79 if is_uuid or not existing.get("is_archived"): 1hijclmnabdeg

80 return existing, False 1hijclmnabde

81 

82 workflow_name = name or handle 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgQr

83 if not workflow_name: 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgQr

84 raise ToolError("Provide 'name' (or a workflow handle) for a new workflow.") 1Q

85 payload: dict[str, Any] = { 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

86 "name": workflow_name, 

87 "slug": _slugify(workflow_name), 

88 "description": description or "LangGraph workflow uploaded via MCP", 

89 "tags": ["langgraph", "mcp-upload"], 

90 "actor": MCP_ACTOR, 

91 } 

92 if handle is not None: 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

93 payload["handle"] = handle 1kbdeg

94 return await api.post("/api/workflows", json_body=payload), True 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

95 

96 

97async def _sync_existing_schedule( 

98 api: InProcessApiClient, workflow_id: str 

99) -> dict[str, Any] | None: 

100 """Refresh an existing cron schedule after a new version is ingested.""" 

101 try: 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

102 existing = await api.get(f"/api/workflows/{workflow_id}/triggers/cron/config") 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

103 except McpApiError as exc: 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

104 if exc.status_code == 404: 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

105 return None 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdfepgr

106 raise 1L

107 latest = await fetch_version(api, workflow_id, None) 1bd

108 try: 1bd

109 config = extract_version_cron_config(latest) 1bd

110 except CronExtractionError as exc: 1d

111 return {"status": "error", "message": str(exc)} 1d

112 if config is None or config == CronTriggerConfig.model_validate(existing): 1b

113 return None 1b

114 updated = await api.put( 1b

115 f"/api/workflows/{workflow_id}/triggers/cron/config", 

116 json_body=config.model_dump(mode="json"), 

117 ) 

118 return {"status": "updated", "config": updated} 1b

119 

120 

121async def _credential_readiness( 

122 api: InProcessApiClient, workflow_id: str 

123) -> dict[str, Any] | None: 

124 """Return vault readiness for a fresh upload, or None if it can't be checked. 

125 

126 The upload has already succeeded at this point, so a readiness failure is 

127 logged rather than reported as a failed tool call. 

128 """ 

129 try: 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdfepgr

130 return await api.get(f"/api/workflows/{workflow_id}/credentials/readiness") 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdfepgr

131 except Exception: # noqa: BLE001 - informational follow-up only 1r

132 logger.warning( 1r

133 "Credential readiness check failed after MCP upload of workflow %s", 

134 workflow_id, 

135 exc_info=True, 

136 ) 

137 return None 1r

138 

139 

140def register_workflow_tools(server: FastMCP) -> None: 

141 """Register workflow inspection and lifecycle tools on ``server``.""" 

142 _register_read_tools(server) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

143 _register_authoring_tools(server) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

144 _register_lifecycle_tools(server) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

145 _register_monitoring_tools(server) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

146 

147 

148def _register_read_tools(server: FastMCP) -> None: 

149 """Register workflow listing, detail and download tools.""" 

150 

151 @server.tool(annotations=READ_ONLY, tags=requires("workflows:read")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

152 async def list_workflows( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

153 include_archived: bool = False, 

154 workspace: WorkspaceArg = None, 

155 ) -> dict[str, Any]: 

156 """List workflows in the workspace with publish and schedule status.""" 

157 async with api_client(workspace) as api: 1aST

158 items = await api.get( 1aST

159 "/api/workflows", params={"include_archived": include_archived} 

160 ) 

161 for item in items: 1aST

162 if isinstance(item.get("latest_version"), dict): 162 ↛ 161line 162 didn't jump to line 161 because the condition on line 162 was always true1a

163 item["latest_version"] = version_summary(item["latest_version"]) 1a

164 return {"workflows": items} 1aST

165 

166 @server.tool(annotations=READ_ONLY, tags=requires("workflows:read")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

167 async def get_workflow( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

168 workflow: WorkflowArg, 

169 version: VersionArg = None, 

170 workspace: WorkspaceArg = None, 

171 ) -> dict[str, Any]: 

172 """Show a workflow with one version's metadata and its five latest runs. 

173 

174 Use download_workflow to read the version's Python source. 

175 """ 

176 async with api_client(workspace) as api: 1aO

177 record = await api.get(f"/api/workflows/{workflow}") 1aO

178 try: 1aO

179 selected = version_summary(await fetch_version(api, workflow, version)) 1aO

180 except ToolError: 1aO

181 if version is not None: 1aO

182 raise 1a

183 selected = None 1O

184 runs = await api.get(f"/api/workflows/{workflow}/runs", params={"limit": 5}) 1aO

185 return {"workflow": record, "version": selected, "recent_runs": runs} 1aO

186 

187 @server.tool(annotations=READ_ONLY, tags=requires("workflows:read")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

188 async def list_workflow_versions( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

189 workflow: WorkflowArg, 

190 workspace: WorkspaceArg = None, 

191 ) -> dict[str, Any]: 

192 """List a workflow's versions (metadata only, newest last).""" 

193 async with api_client(workspace) as api: 1Ua

194 versions = await api.get(f"/api/workflows/{workflow}/versions") 1Ua

195 return {"versions": [version_summary(entry) for entry in versions]} 1Ua

196 

197 @server.tool(annotations=READ_ONLY, tags=requires("workflows:read")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

198 async def download_workflow( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

199 workflow: WorkflowArg, 

200 version: VersionArg = None, 

201 workspace: WorkspaceArg = None, 

202 ) -> dict[str, Any]: 

203 """Return the Python source and runnable config of a workflow version.""" 

204 async with api_client(workspace) as api: 1cRapO

205 selected = await fetch_version(api, workflow, version) 1cRapO

206 graph = selected.get("graph") or {} 1cRap

207 source = graph.get("source") 1cRap

208 if not isinstance(source, str) or not source.strip(): 1cRap

209 raise ToolError( 1R

210 f"Version {selected.get('version')} has no Python source " 

211 f"(format '{graph.get('format', 'unknown')}'); re-upload the script." 

212 ) 

213 return { 1cap

214 "workflow_id": selected.get("workflow_id"), 

215 "version": selected.get("version"), 

216 "source": source, 

217 "runnable_config": selected.get("runnable_config"), 

218 } 

219 

220 

221def _register_authoring_tools(server: FastMCP) -> None: 

222 """Register upload, update and delete tools.""" 

223 

224 @server.tool(annotations=WRITE, tags=requires("workflows:write")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

225 async def upload_workflow( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

226 script: Annotated[ 

227 str, 

228 Field( 

229 description=( 

230 "Full Python source of an Orcheo LangGraph workflow script, " 

231 "as accepted by `orcheo workflow upload`." 

232 ) 

233 ), 

234 ], 

235 workflow: Annotated[ 

236 str | None, 

237 Field( 

238 description=( 

239 "Existing workflow ID, or a handle (created if missing). " 

240 "Omit to create a new workflow named 'name'." 

241 ) 

242 ), 

243 ] = None, 

244 name: Annotated[str | None, Field(description="Workflow name.")] = None, 

245 description: Annotated[ 

246 str | None, Field(description="Workflow description.") 

247 ] = None, 

248 entrypoint: Annotated[ 

249 str | None, 

250 Field(description="Graph builder to use when the script has several."), 

251 ] = None, 

252 runnable_config: Annotated[ 

253 dict[str, Any] | None, 

254 Field(description="Runnable config stored on the new version."), 

255 ] = None, 

256 workspace: WorkspaceArg = None, 

257 ) -> dict[str, Any]: 

258 """Upload a workflow script as a new version, creating the workflow if needed. 

259 

260 An existing cron schedule is re-synced when the new version changes it. 

261 """ 

262 async with api_client(workspace) as api: 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepMgQPNr

263 record, created = await _find_or_create_workflow( 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepMgQPNr

264 api, workflow=workflow, name=name, description=description 

265 ) 

266 workflow_id = str(record["id"]) 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

267 if not created: 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

268 changes = { 1hijclmnabde

269 key: value 

270 for key, value in (("name", name), ("description", description)) 

271 if value is not None and record.get(key) != value 

272 } 

273 if changes: 1hijclmnabde

274 record = await api.put( 1e

275 f"/api/workflows/{workflow_id}", 

276 json_body={**changes, "actor": MCP_ACTOR}, 

277 ) 

278 ingest: dict[str, Any] = { 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

279 "script": _strip_main_block(script), 

280 "entrypoint": entrypoint, 

281 "metadata": {"source": "mcp-upload"}, 

282 "notes": "Uploaded via MCP", 

283 "created_by": MCP_ACTOR, 

284 } 

285 if runnable_config is not None: 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

286 ingest["runnable_config"] = runnable_config 1hijp

287 new_version = await api.post( 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

288 f"/api/workflows/{workflow_id}/versions/ingest", json_body=ingest 

289 ) 

290 schedule = await _sync_existing_schedule(api, workflow_id) 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdLfepgr

291 readiness = await _credential_readiness(api, workflow_id) 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdfepgr

292 result: dict[str, Any] = { 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdfepgr

293 "workflow": record, 

294 "created": created, 

295 "version": version_summary(new_version), 

296 "credential_readiness": readiness, 

297 } 

298 if schedule is not None: 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdfepgr

299 result["cron_schedule"] = schedule 1bd

300 return result 1stuvhijwxyzABCDEFGHIJKcklmnoaqbdfepgr

301 

302 _register_update_tool(server) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

303 

304 @server.tool(annotations=DESTRUCTIVE, tags=requires("workflows:write")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

305 async def delete_workflow( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

306 workflow: WorkflowArg, 

307 workspace: WorkspaceArg = None, 

308 ) -> dict[str, Any]: 

309 """Archive a workflow so it no longer runs or appears in listings.""" 

310 async with api_client(workspace) as api: 1fg

311 return await api.delete( 1fg

312 f"/api/workflows/{workflow}", params={"actor": MCP_ACTOR} 

313 ) 

314 

315 

316def _register_update_tool(server: FastMCP) -> None: 

317 @server.tool(annotations=IDEMPOTENT_WRITE, tags=requires("workflows:write")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

318 async def update_workflow( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

319 workflow: WorkflowArg, 

320 name: Annotated[str | None, Field(description="New name.")] = None, 

321 handle: Annotated[str | None, Field(description="New handle.")] = None, 

322 description: Annotated[ 

323 str | None, Field(description="New description.") 

324 ] = None, 

325 tags: list[str] | None = None, 

326 draft_access: WorkflowDraftAccess | None = None, 

327 chatkit: WorkflowChatKitConfig | None = None, 

328 clear_chatkit_start_screen_prompts: bool = False, 

329 clear_chatkit_supported_models: bool = False, 

330 workspace: WorkspaceArg = None, 

331 ) -> dict[str, Any]: 

332 """Update workflow metadata, draft access and ChatKit presentation.""" 

333 changes: dict[str, Any] = { 1cf

334 key: value 

335 for key, value in ( 

336 ("name", name), 

337 ("handle", handle), 

338 ("description", description), 

339 ("tags", tags), 

340 ("draft_access", draft_access), 

341 ( 

342 "chatkit", 

343 chatkit.model_dump(mode="json", exclude_unset=True) 

344 if chatkit is not None 

345 else None, 

346 ), 

347 ) 

348 if value is not None 

349 } 

350 if clear_chatkit_start_screen_prompts: 1cf

351 changes["clear_chatkit_start_screen_prompts"] = True 1c

352 if clear_chatkit_supported_models: 1cf

353 changes["clear_chatkit_supported_models"] = True 1c

354 if not changes: 1cf

355 raise ToolError("Provide at least one workflow field to update.") 1f

356 async with api_client(workspace) as api: 1cf

357 return await api.put( 1cf

358 f"/api/workflows/{workflow}", 

359 json_body={**changes, "actor": MCP_ACTOR}, 

360 ) 

361 

362 

363def _register_lifecycle_tools(server: FastMCP) -> None: 

364 """Register publish, schedule and runnable-config tools.""" 

365 

366 @server.tool(annotations=IDEMPOTENT_WRITE, tags=requires("workflows:write")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

367 async def publish_workflow( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

368 workflow: WorkflowArg, 

369 require_login: Annotated[ 

370 bool, Field(description="Require visitors to sign in to chat.") 

371 ] = False, 

372 workspace: WorkspaceArg = None, 

373 ) -> dict[str, Any]: 

374 """Publish a workflow for public ChatKit access and return its share URL.""" 

375 async with api_client(workspace) as api: 1f

376 return await api.post( 1f

377 f"/api/workflows/{workflow}/publish", 

378 json_body={"require_login": require_login, "actor": MCP_ACTOR}, 

379 ) 

380 

381 @server.tool(annotations=IDEMPOTENT_WRITE, tags=requires("workflows:write")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

382 async def unpublish_workflow( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

383 workflow: WorkflowArg, 

384 workspace: WorkspaceArg = None, 

385 ) -> dict[str, Any]: 

386 """Revoke public ChatKit access for a workflow.""" 

387 async with api_client(workspace) as api: 1f

388 return await api.post( 1f

389 f"/api/workflows/{workflow}/publish/revoke", 

390 json_body={"actor": MCP_ACTOR}, 

391 ) 

392 

393 @server.tool(annotations=IDEMPOTENT_WRITE, tags=requires("workflows:write")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

394 async def schedule_workflow( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

395 workflow: WorkflowArg, 

396 workspace: WorkspaceArg = None, 

397 ) -> dict[str, Any]: 

398 """Enable the cron schedule declared by the workflow's latest version.""" 

399 async with api_client(workspace) as api: 1bd

400 latest = await fetch_version(api, workflow, None) 1bd

401 try: 1bd

402 config = extract_version_cron_config(latest) 1bd

403 except CronExtractionError as exc: 1d

404 raise ToolError(str(exc)) from exc 1d

405 if config is None: 1bd

406 return { 1b

407 "status": "noop", 

408 "message": f"Workflow '{workflow}' has no cron trigger.", 

409 } 

410 applied = await api.put( 1bd

411 f"/api/workflows/{workflow}/triggers/cron/config", 

412 json_body=config.model_dump(mode="json"), 

413 ) 

414 return {"status": "scheduled", "config": applied} 1bd

415 

416 @server.tool(annotations=IDEMPOTENT_WRITE, tags=requires("workflows:write")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

417 async def unschedule_workflow( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

418 workflow: WorkflowArg, 

419 workspace: WorkspaceArg = None, 

420 ) -> dict[str, Any]: 

421 """Disable cron-based execution for a workflow.""" 

422 async with api_client(workspace) as api: 1b

423 await api.delete(f"/api/workflows/{workflow}/triggers/cron/config") 1b

424 return {"status": "unscheduled"} 1b

425 

426 @server.tool(annotations=IDEMPOTENT_WRITE, tags=requires("workflows:write")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

427 async def save_workflow_config( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

428 workflow: WorkflowArg, 

429 runnable_config: Annotated[ 

430 dict[str, Any] | None, 

431 Field( 

432 description=( 

433 "Runnable config (configurable, tags, metadata, ...). " 

434 "Pass null to clear it." 

435 ) 

436 ), 

437 ], 

438 version: VersionArg = None, 

439 workspace: WorkspaceArg = None, 

440 ) -> dict[str, Any]: 

441 """Replace the runnable config stored on a workflow version.""" 

442 async with api_client(workspace) as api: 1cq

443 target = version 1cq

444 if target is None: 1cq

445 target = (await fetch_version(api, workflow, None))["version"] 1cq

446 updated = await api.put( 1cq

447 f"/api/workflows/{workflow}/versions/{target}/runnable-config", 

448 json_body={"runnable_config": runnable_config, "actor": MCP_ACTOR}, 

449 ) 

450 return version_summary(updated) 1cq

451 

452 

453def _register_monitoring_tools(server: FastMCP) -> None: 

454 """Register credential readiness and listener tools.""" 

455 

456 @server.tool(annotations=READ_ONLY, tags=requires("workflows:read", "vault:read")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

457 async def check_workflow_credentials( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

458 workflow: WorkflowArg, 

459 workspace: WorkspaceArg = None, 

460 ) -> dict[str, Any]: 

461 """Report whether the credentials a workflow references exist in the vault.""" 

462 async with api_client(workspace) as api: 1a

463 return await api.get(f"/api/workflows/{workflow}/credentials/readiness") 1a

464 

465 @server.tool(annotations=READ_ONLY, tags=requires("workflows:read")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

466 async def list_workflow_listeners( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

467 workflow: WorkflowArg, 

468 workspace: WorkspaceArg = None, 

469 ) -> dict[str, Any]: 

470 """List a workflow's listener subscriptions with live health.""" 

471 async with api_client(workspace) as api: 1o

472 listeners = await api.get(f"/api/workflows/{workflow}/listeners") 1o

473 return {"listeners": listeners} 1o

474 

475 async def _set_listener_status( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

476 workflow: str, 

477 subscription_id: str, 

478 action: str, 

479 workspace: str | None, 

480 ) -> dict[str, Any]: 

481 async with api_client(workspace) as api: 1o

482 return await api.post( 1o

483 f"/api/workflows/{workflow}/listeners/{subscription_id}/{action}", 

484 json_body={"actor": MCP_ACTOR}, 

485 ) 

486 

487 @server.tool(annotations=IDEMPOTENT_WRITE, tags=requires("workflows:write")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

488 async def pause_workflow_listener( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

489 workflow: WorkflowArg, 

490 subscription_id: Annotated[str, Field(description="Listener subscription ID.")], 

491 workspace: WorkspaceArg = None, 

492 ) -> dict[str, Any]: 

493 """Pause one listener subscription (e.g. a Telegram or Slack listener).""" 

494 return await _set_listener_status(workflow, subscription_id, "pause", workspace) 1o

495 

496 @server.tool(annotations=IDEMPOTENT_WRITE, tags=requires("workflows:write")) 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

497 async def resume_workflow_listener( 2V W X Y Z 0 s 1 2 3 t 4 5 6 7 8 9 ! # $ % ' ( ) * + U u v h i j w x y z A B C D , E F G H I J K - c k l m . n / : ; = ? @ [ ] R o a q b d L ^ f e p M g Q P N r O _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbS ubvbT wbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTb

498 workflow: WorkflowArg, 

499 subscription_id: Annotated[str, Field(description="Listener subscription ID.")], 

500 workspace: WorkspaceArg = None, 

501 ) -> dict[str, Any]: 

502 """Resume a paused listener subscription.""" 

503 return await _set_listener_status( 1o

504 workflow, subscription_id, "resume", workspace 

505 ) 

506 

507 

508__all__ = ["register_workflow_tools"]