diff --git a/packages/cli-v3/src/dev/mcpServer.ts b/packages/cli-v3/src/dev/mcpServer.ts index c63926e1d..8c4e57da3 100644 --- a/packages/cli-v3/src/dev/mcpServer.ts +++ b/packages/cli-v3/src/dev/mcpServer.ts @@ -1,11 +1,13 @@ +import polka from "polka"; import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js"; -import { optional, union, z } from "zod"; +import { z } from "zod"; import { logger } from "../utilities/logger.js"; import { CliApiClient } from "../apiClient.js"; import { ApiClient, RunStatus } from "@trigger.dev/core/v3"; -import polka from "polka"; +import { eventBus } from "../utilities/eventBus.js"; +let allTaskIds: string[] = []; let projectRef: string; let dashboardUrl: string; // there is some overlap between `ApiClient` and `CliApiClient` which is not ideal @@ -20,6 +22,22 @@ const server = new McpServer({ version: "1.0.0", }); +// The `list-all-tasks` tool primarily helps to enable fuzzy matching of task IDs (names). +// This way, one doesn't need to specify the full task ID and rather let the LLM figure it out. +// This could be a good fit for the `resource` entity in MCP. +// Also, a custom `prompt` entity could be useful to instruct the LLM to prompt the user +// for selecting a task from a list of matching tasks, when the confidence for an exact match is low. +server.tool("list-all-tasks", "List all available task IDs in the worker.", async () => { + return { + content: [ + { + text: JSON.stringify(allTaskIds, null, 2), + type: "text", + }, + ], + }; +}); + server.tool( "trigger-task", "Trigger a task", @@ -213,6 +231,10 @@ app.post("/messages", (req, res) => { } }); +eventBus.on("backgroundWorkerInitialized", (worker) => { + allTaskIds = worker.manifest?.tasks.map((task) => task.id) ?? []; +}); + export const startMcpServer = async (options: { port: number; cliApiClient: CliApiClient;