|
| 1 | +"""UiPath Dev MCP Server — exposes dev server run tools via stdio.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +import httpx |
| 10 | +import websockets |
| 11 | +from mcp.server.fastmcp import Context, FastMCP |
| 12 | + |
| 13 | +mcp = FastMCP( |
| 14 | + "uipath-dev", |
| 15 | + instructions=( |
| 16 | + "This MCP server is the ONLY way to run, test, or execute agents, " |
| 17 | + "functions, and projects. Do NOT use shell commands like " |
| 18 | + "`uipath run`, `langgraph dev`, `python -m`, or any subprocess. " |
| 19 | + "Always use the tools provided here instead." |
| 20 | + ), |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def _base_url() -> str: |
| 25 | + port = os.environ.get("UIPATH_DEV_SERVER_PORT", "8080") |
| 26 | + host = os.environ.get("UIPATH_DEV_SERVER_HOST", "localhost") |
| 27 | + return f"http://{host}:{port}" |
| 28 | + |
| 29 | + |
| 30 | +def _ws_url() -> str: |
| 31 | + port = os.environ.get("UIPATH_DEV_SERVER_PORT", "8080") |
| 32 | + host = os.environ.get("UIPATH_DEV_SERVER_HOST", "localhost") |
| 33 | + return f"ws://{host}:{port}/ws" |
| 34 | + |
| 35 | + |
| 36 | +def _api_url(path: str) -> str: |
| 37 | + return f"{_base_url()}/api{path}" |
| 38 | + |
| 39 | + |
| 40 | +async def _report_tool_call(tool: str, args: dict[str, Any] | None = None) -> None: |
| 41 | + """Notify the dev server that an MCP tool was invoked.""" |
| 42 | + try: |
| 43 | + async with httpx.AsyncClient() as client: |
| 44 | + await client.post( |
| 45 | + _api_url("/mcp/events"), |
| 46 | + json={"tool": tool, "args": args or {}}, |
| 47 | + timeout=5, |
| 48 | + ) |
| 49 | + except Exception: |
| 50 | + pass # Best-effort — don't fail the tool if reporting fails |
| 51 | + |
| 52 | + |
| 53 | +@mcp.tool() |
| 54 | +async def list_entrypoints() -> list[dict[str, Any]]: |
| 55 | + """List all runnable agents, functions, and projects. |
| 56 | +
|
| 57 | + Call this first to discover what is available to run. |
| 58 | + Use the returned names with get_entrypoint_schema or run_entrypoint. |
| 59 | + """ |
| 60 | + await _report_tool_call("list_entrypoints") |
| 61 | + async with httpx.AsyncClient() as client: |
| 62 | + resp = await client.get(_api_url("/entrypoints"), timeout=10) |
| 63 | + resp.raise_for_status() |
| 64 | + return resp.json() |
| 65 | + |
| 66 | + |
| 67 | +@mcp.tool() |
| 68 | +async def get_entrypoint_schema(entrypoint: str) -> dict[str, Any]: |
| 69 | + """Get the input/output schema for an entrypoint. |
| 70 | +
|
| 71 | + Args: |
| 72 | + entrypoint: Name of the entrypoint (from list_entrypoints). |
| 73 | +
|
| 74 | + Returns the JSON schema describing the entrypoint's expected |
| 75 | + input and output. Use this to construct input_data for run_entrypoint. |
| 76 | + """ |
| 77 | + await _report_tool_call("get_entrypoint_schema", {"entrypoint": entrypoint}) |
| 78 | + async with httpx.AsyncClient() as client: |
| 79 | + resp = await client.get( |
| 80 | + _api_url(f"/entrypoints/{entrypoint}/schema"), |
| 81 | + timeout=30, |
| 82 | + ) |
| 83 | + resp.raise_for_status() |
| 84 | + return resp.json() |
| 85 | + |
| 86 | + |
| 87 | +@mcp.tool() |
| 88 | +async def run_entrypoint( |
| 89 | + entrypoint: str, |
| 90 | + ctx: Context, # type: ignore[type-arg] |
| 91 | + input_data: dict[str, Any] | None = None, |
| 92 | +) -> dict[str, Any]: |
| 93 | + """Run an agent, function, or project. |
| 94 | +
|
| 95 | + This is the ONLY correct way to execute code in this project — never |
| 96 | + use shell commands. |
| 97 | +
|
| 98 | + Args: |
| 99 | + entrypoint: Name of the entrypoint (from list_entrypoints). |
| 100 | + input_data: Input data matching the entrypoint's input schema. |
| 101 | + Use get_entrypoint_schema to see what fields are expected. |
| 102 | + Defaults to empty dict if not provided. |
| 103 | +
|
| 104 | + Executes on the dev server with real-time graph visualization in the |
| 105 | + browser. Streams progress as nodes execute, then returns the result. |
| 106 | + """ |
| 107 | + await _report_tool_call( |
| 108 | + "run_entrypoint", {"entrypoint": entrypoint, "input_data": input_data} |
| 109 | + ) |
| 110 | + async with httpx.AsyncClient() as client: |
| 111 | + resp = await client.post( |
| 112 | + _api_url("/runs"), |
| 113 | + json={ |
| 114 | + "entrypoint": entrypoint, |
| 115 | + "input_data": input_data or {}, |
| 116 | + "mode": "run", |
| 117 | + }, |
| 118 | + timeout=10, |
| 119 | + ) |
| 120 | + resp.raise_for_status() |
| 121 | + run: dict[str, Any] = resp.json() |
| 122 | + run_id = run["id"] |
| 123 | + |
| 124 | + await ctx.log("info", f"Run {run_id} created — streaming updates...") |
| 125 | + |
| 126 | + # Connect to WebSocket and subscribe to run events |
| 127 | + terminal_statuses = {"completed", "failed", "suspended"} |
| 128 | + final_status = None |
| 129 | + |
| 130 | + async with websockets.connect(_ws_url()) as ws: |
| 131 | + # Subscribe to this run's events |
| 132 | + await ws.send(json.dumps({"type": "subscribe", "payload": {"run_id": run_id}})) |
| 133 | + |
| 134 | + async for raw in ws: |
| 135 | + msg = json.loads(raw) |
| 136 | + msg_type = msg.get("type", "") |
| 137 | + payload = msg.get("payload", {}) |
| 138 | + |
| 139 | + if msg_type == "run.updated" and payload.get("id") == run_id: |
| 140 | + status = payload.get("status", "") |
| 141 | + await ctx.report_progress( |
| 142 | + progress=1 if status in terminal_statuses else 0, |
| 143 | + total=1, |
| 144 | + message=f"Run status: {status}", |
| 145 | + ) |
| 146 | + if status in terminal_statuses: |
| 147 | + final_status = status |
| 148 | + break |
| 149 | + |
| 150 | + elif msg_type == "state" and payload.get("run_id") == run_id: |
| 151 | + node = payload.get("node_name", "") |
| 152 | + phase = payload.get("phase", "") |
| 153 | + if phase == "started": |
| 154 | + await ctx.log("info", f"Node '{node}' started") |
| 155 | + elif phase == "completed": |
| 156 | + await ctx.log("info", f"Node '{node}' completed") |
| 157 | + |
| 158 | + elif msg_type == "log" and payload.get("run_id") == run_id: |
| 159 | + level = payload.get("level", "info").lower() |
| 160 | + message = payload.get("message", "") |
| 161 | + await ctx.log(level, message) |
| 162 | + |
| 163 | + # Fetch final run result |
| 164 | + async with httpx.AsyncClient() as client: |
| 165 | + resp = await client.get(_api_url(f"/runs/{run_id}"), timeout=10) |
| 166 | + resp.raise_for_status() |
| 167 | + result: dict[str, Any] = resp.json() |
| 168 | + |
| 169 | + if final_status == "failed": |
| 170 | + error = result.get("error", {}) |
| 171 | + await ctx.log("error", f"Run failed: {error.get('detail', 'unknown error')}") |
| 172 | + else: |
| 173 | + await ctx.log("info", f"Run {final_status}: {run_id}") |
| 174 | + |
| 175 | + return result |
| 176 | + |
| 177 | + |
| 178 | +@mcp.tool() |
| 179 | +async def get_run_status(run_id: str) -> dict[str, Any]: |
| 180 | + """Get current status and details of a run. |
| 181 | +
|
| 182 | + Args: |
| 183 | + run_id: The run ID returned by run_entrypoint. |
| 184 | +
|
| 185 | + Returns full run details including status, output, traces, and logs. |
| 186 | + """ |
| 187 | + await _report_tool_call("get_run_status", {"run_id": run_id}) |
| 188 | + async with httpx.AsyncClient() as client: |
| 189 | + resp = await client.get(_api_url(f"/runs/{run_id}"), timeout=10) |
| 190 | + resp.raise_for_status() |
| 191 | + return resp.json() |
| 192 | + |
| 193 | + |
| 194 | +def main() -> None: |
| 195 | + """Entry point for the uipath-dev-mcp CLI command.""" |
| 196 | + mcp.run(transport="stdio") |
0 commit comments