Skip to content

Commit b6b25b0

Browse files
committed
Add MCP tool to cancel task runs
1 parent 9cd6d40 commit b6b25b0

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

packages/cli-v3/src/dev/mcpServer.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,55 @@ server.tool(
133133
}
134134
);
135135

136+
server.tool(
137+
"cancel-run",
138+
"Cancel an in-progress run. Runs that have already completed cannot be cancelled.",
139+
{
140+
runId: z.string().describe("The ID of the task run to cancel"),
141+
},
142+
async ({ runId }) => {
143+
const run = await sdkApiClient.retrieveRun(runId);
144+
145+
if (run?.status === "COMPLETED") {
146+
return {
147+
content: [
148+
{
149+
type: "text",
150+
text: JSON.stringify(
151+
{ message: "This run is already completed, no action taken.", run },
152+
null,
153+
2
154+
),
155+
},
156+
],
157+
};
158+
}
159+
160+
await sdkApiClient.cancelRun(runId);
161+
// we could also skip fetching the run again, but it provides more context to the LLM
162+
// and one extra API call is no big deal
163+
const updatedRun = await sdkApiClient.retrieveRun(runId);
164+
165+
return {
166+
content: [
167+
{
168+
type: "text",
169+
text: JSON.stringify(
170+
{
171+
message: "Task run was cancelled",
172+
previousStatus: run.status,
173+
currentStatus: updatedRun.status,
174+
updatedTaskRun: updatedRun,
175+
},
176+
null,
177+
2
178+
),
179+
},
180+
],
181+
};
182+
}
183+
);
184+
136185
const app = polka();
137186
app.get("/sse", (_req, res) => {
138187
mcpTransport = new SSEServerTransport("/messages", res);

0 commit comments

Comments
 (0)