Skip to content

Commit 603c903

Browse files
committed
Add MCP tool to list all available tasks and enable fuzzy task matching
This enables LLMs to figure out which task you are referring to, without needing to specify the full task ID
1 parent 2e2632d commit 603c903

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import polka from "polka";
12
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
23
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
3-
import { optional, union, z } from "zod";
4+
import { z } from "zod";
45
import { logger } from "../utilities/logger.js";
56
import { CliApiClient } from "../apiClient.js";
67
import { ApiClient, RunStatus } from "@trigger.dev/core/v3";
7-
import polka from "polka";
8+
import { eventBus } from "../utilities/eventBus.js";
89

10+
let allTaskIds: string[] = [];
911
let projectRef: string;
1012
let dashboardUrl: string;
1113
// there is some overlap between `ApiClient` and `CliApiClient` which is not ideal
@@ -20,6 +22,22 @@ const server = new McpServer({
2022
version: "1.0.0",
2123
});
2224

25+
// The `list-all-tasks` tool primarily helps to enable fuzzy matching of task IDs (names).
26+
// This way, one doesn't need to specify the full task ID and rather let the LLM figure it out.
27+
// This could be a good fit for the `resource` entity in MCP.
28+
// Also, a custom `prompt` entity could be useful to instruct the LLM to prompt the user
29+
// for selecting a task from a list of matching tasks, when the confidence for an exact match is low.
30+
server.tool("list-all-tasks", "List all available task IDs in the worker.", async () => {
31+
return {
32+
content: [
33+
{
34+
text: JSON.stringify(allTaskIds, null, 2),
35+
type: "text",
36+
},
37+
],
38+
};
39+
});
40+
2341
server.tool(
2442
"trigger-task",
2543
"Trigger a task",
@@ -213,6 +231,10 @@ app.post("/messages", (req, res) => {
213231
}
214232
});
215233

234+
eventBus.on("backgroundWorkerInitialized", (worker) => {
235+
allTaskIds = worker.manifest?.tasks.map((task) => task.id) ?? [];
236+
});
237+
216238
export const startMcpServer = async (options: {
217239
port: number;
218240
cliApiClient: CliApiClient;

0 commit comments

Comments
 (0)