diff --git a/.changeset/mcp-project-ref-fallback.md b/.changeset/mcp-project-ref-fallback.md new file mode 100644 index 00000000000..de0f62c8415 --- /dev/null +++ b/.changeset/mcp-project-ref-fallback.md @@ -0,0 +1,7 @@ +--- +"trigger.dev": patch +--- + +fix(cli): use the --project-ref option in MCP tools when a call passes no projectRef + +`trigger mcp --project-ref ` started in a directory without a `trigger.config.ts` previously still failed with "No project ref found. Please provide a projectRef." because `McpContext.getProjectRef` never read the server option. Tools now fall back to the `--project-ref` passed at server start before trying config detection. diff --git a/packages/cli-v3/src/mcp/context.test.ts b/packages/cli-v3/src/mcp/context.test.ts new file mode 100644 index 00000000000..840414a15cd --- /dev/null +++ b/packages/cli-v3/src/mcp/context.test.ts @@ -0,0 +1,48 @@ +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; +import { describe, expect, it, vi } from "vitest"; +import { McpContext } from "./context.js"; + +vi.mock("@trigger.dev/core/utils", () => ({ + tryCatch: async (p: Promise): Promise<[Error | null, T | null]> => { + try { + return [null, await p]; + } catch (e) { + return [e as Error, null]; + } + }, +})); + +vi.mock("@trigger.dev/core/v3", () => ({ + ApiClient: class {}, +})); + +vi.mock("../apiClient.js", () => ({ + CliApiClient: class {}, +})); + +vi.mock("../config.js", () => ({ + loadConfig: async () => ({}), +})); + +vi.mock("./auth.js", () => ({ + mcpAuth: async () => ({ ok: true, auth: { apiUrl: "", accessToken: "" } }), +})); + +const server = () => new McpServer({ name: "trigger.dev", version: "test" }); + +describe("McpContext.getProjectRef", () => { + it("prefers the projectRef passed on the tool call", async () => { + const ctx = new McpContext(server(), { projectRef: "proj_flag" }); + await expect(ctx.getProjectRef({ projectRef: "proj_call" })).resolves.toBe("proj_call"); + }); + + it("falls back to the --project-ref option when the call omits one", async () => { + const ctx = new McpContext(server(), { projectRef: "proj_flag" }); + await expect(ctx.getProjectRef({})).resolves.toBe("proj_flag"); + }); + + it("still errors when no projectRef is provided anywhere", async () => { + const ctx = new McpContext(server(), {}); + await expect(ctx.getProjectRef({})).rejects.toThrow(); + }); +}); diff --git a/packages/cli-v3/src/mcp/context.ts b/packages/cli-v3/src/mcp/context.ts index b008ebf62ef..fc0adfd0541 100644 --- a/packages/cli-v3/src/mcp/context.ts +++ b/packages/cli-v3/src/mcp/context.ts @@ -146,6 +146,10 @@ export class McpContext { return options.projectRef; } + if (this.options.projectRef) { + return this.options.projectRef; + } + const projectDir = await this.getProjectDir({ cwd: options.cwd }); if (!projectDir.ok) {