1+ import polka from "polka" ;
12import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js" ;
23import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js" ;
3- import { optional , union , z } from "zod" ;
4+ import { z } from "zod" ;
45import { logger } from "../utilities/logger.js" ;
56import { CliApiClient } from "../apiClient.js" ;
67import { ApiClient , RunStatus } from "@trigger.dev/core/v3" ;
7- import polka from "polka " ;
8+ import { eventBus } from "../utilities/eventBus.js " ;
89
10+ let allTaskIds : string [ ] = [ ] ;
911let projectRef : string ;
1012let 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+
2341server . 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+
216238export const startMcpServer = async ( options : {
217239 port : number ;
218240 cliApiClient : CliApiClient ;
0 commit comments