| 
 | 1 | +import { AnyRunShape } from "@trigger.dev/core/v3";  | 
1 | 2 | import { toolsMetadata } from "../config.js";  | 
2 |  | -import { formatRun, formatRunList, formatRunTrace } from "../formatters.js";  | 
 | 3 | +import { formatRun, formatRunList, formatRunShape, formatRunTrace } from "../formatters.js";  | 
3 | 4 | import { CommonRunsInput, GetRunDetailsInput, ListRunsInput } from "../schemas.js";  | 
4 | 5 | import { respondWithError, toolHandler } from "../utils.js";  | 
5 | 6 | 
 
  | 
@@ -60,6 +61,59 @@ export const getRunDetailsTool = {  | 
60 | 61 |   }),  | 
61 | 62 | };  | 
62 | 63 | 
 
  | 
 | 64 | +export const waitForRunToCompleteTool = {  | 
 | 65 | +  name: toolsMetadata.wait_for_run_to_complete.name,  | 
 | 66 | +  title: toolsMetadata.wait_for_run_to_complete.title,  | 
 | 67 | +  description: toolsMetadata.wait_for_run_to_complete.description,  | 
 | 68 | +  inputSchema: CommonRunsInput.shape,  | 
 | 69 | +  handler: toolHandler(CommonRunsInput.shape, async (input, { ctx, signal }) => {  | 
 | 70 | +    ctx.logger?.log("calling wait_for_run_to_complete", { input });  | 
 | 71 | + | 
 | 72 | +    if (ctx.options.devOnly && input.environment !== "dev") {  | 
 | 73 | +      return respondWithError(  | 
 | 74 | +        `This MCP server is only available for the dev environment. You tried to access the ${input.environment} environment. Remove the --dev-only flag to access other environments.`  | 
 | 75 | +      );  | 
 | 76 | +    }  | 
 | 77 | + | 
 | 78 | +    const projectRef = await ctx.getProjectRef({  | 
 | 79 | +      projectRef: input.projectRef,  | 
 | 80 | +      cwd: input.configPath,  | 
 | 81 | +    });  | 
 | 82 | + | 
 | 83 | +    const apiClient = await ctx.getApiClient({  | 
 | 84 | +      projectRef,  | 
 | 85 | +      environment: input.environment,  | 
 | 86 | +      scopes: [`read:runs:${input.runId}`],  | 
 | 87 | +      branch: input.branch,  | 
 | 88 | +    });  | 
 | 89 | + | 
 | 90 | +    const runSubscription = apiClient.subscribeToRun(input.runId, { signal });  | 
 | 91 | +    const readableStream = runSubscription.getReader();  | 
 | 92 | + | 
 | 93 | +    let run: AnyRunShape | null = null;  | 
 | 94 | + | 
 | 95 | +    while (true) {  | 
 | 96 | +      const { done, value } = await readableStream.read();  | 
 | 97 | +      if (done) {  | 
 | 98 | +        break;  | 
 | 99 | +      }  | 
 | 100 | +      run = value;  | 
 | 101 | + | 
 | 102 | +      if (value.isCompleted) {  | 
 | 103 | +        break;  | 
 | 104 | +      }  | 
 | 105 | +    }  | 
 | 106 | + | 
 | 107 | +    if (!run) {  | 
 | 108 | +      return respondWithError("Run not found");  | 
 | 109 | +    }  | 
 | 110 | + | 
 | 111 | +    return {  | 
 | 112 | +      content: [{ type: "text", text: formatRunShape(run) }],  | 
 | 113 | +    };  | 
 | 114 | +  }),  | 
 | 115 | +};  | 
 | 116 | + | 
63 | 117 | export const cancelRunTool = {  | 
64 | 118 |   name: toolsMetadata.cancel_run.name,  | 
65 | 119 |   title: toolsMetadata.cancel_run.title,  | 
 | 
0 commit comments