diff --git a/packages/api/src/protected-api.ts b/packages/api/src/protected-api.ts index e8601dd2..4397ae28 100644 --- a/packages/api/src/protected-api.ts +++ b/packages/api/src/protected-api.ts @@ -17,7 +17,7 @@ import { createMcpHandler, McpServer, ResourceTemplate, ProtocolError, ProtocolE import { createMcpHonoApp } from "@modelcontextprotocol/hono"; import { z } from "zod"; import { ApiService, ServiceError } from "./api-service"; -import { triggerConditionsTestSchema, triggerIdSchema, triggerInputSchema, fireTriggerInputSchema } from "./trigger-schemas"; +import { triggerConditionsTestSchema, triggerIdSchema, mcpTriggerInputSchema, fireTriggerInputSchema, type TriggerInput } from "./trigger-schemas"; import type { Env, OAuthProps } from "./types"; import { executeAuth, validBrowserOrigin } from "./auth-api"; import { matchApiOperation } from "./api-contract"; @@ -213,8 +213,8 @@ function createMcpServer(service: ApiService, subscriptions = true): McpServer { tool("list_event_occurrences", "Read durable Event occurrences in reverse order.", z.object({eventId:z.uuid(),...eventOccurrenceQuery.shape}), ({eventId,limit,cursor}:any) => service.listEventOccurrences(eventId,limit,cursor)); tool("list_triggers", "List triggers and their parent Events", z.object({}), () => service.listTriggers()); tool("get_trigger", "Get a Trigger, its parent Event, and input configuration", triggerIdSchema, ({ triggerId }: any) => service.getTrigger(triggerId)); - tool("create_trigger", "Create an input Trigger attached to an Event. Credentials are never accepted or returned.", triggerInputSchema, (input: any) => service.createTrigger(input)); - tool("update_trigger", "Replace a Trigger's input configuration and Event parent.", triggerInputSchema.extend({ triggerId: z.string().min(1) }), ({ triggerId, ...input }: any) => service.updateTrigger(triggerId, input)); + tool("create_trigger", "Create an input Trigger attached to an Event. Credentials are never accepted or returned.", mcpTriggerInputSchema, (input: TriggerInput) => service.createTrigger(input)); + tool("update_trigger", "Replace a Trigger's input configuration and Event parent.", mcpTriggerInputSchema.extend({ triggerId: z.string().min(1) }), ({ triggerId, ...input }: TriggerInput & { triggerId: string }) => service.updateTrigger(triggerId, input)); tool("delete_trigger", "Delete an input Trigger", triggerIdSchema, ({ triggerId }: any) => service.deleteTrigger(triggerId)); tool("enable_trigger", "Enable a trigger", triggerIdSchema, ({ triggerId }: any) => service.setTriggerEnabled(triggerId, true)); tool("disable_trigger", "Disable a trigger", triggerIdSchema, ({ triggerId }: any) => service.setTriggerEnabled(triggerId, false)); diff --git a/packages/api/src/trigger-schemas.ts b/packages/api/src/trigger-schemas.ts index 8123ab72..1a101a40 100644 --- a/packages/api/src/trigger-schemas.ts +++ b/packages/api/src/trigger-schemas.ts @@ -19,7 +19,14 @@ export const triggerSourceSchema = z.discriminatedUnion("kind", [ z.object({ id: z.string().min(1).optional(), slug: z.string().regex(/^[a-z][a-z0-9_-]{0,63}$/).optional(), kind: z.literal("schedule"), template: sourceTemplate, enabled: z.boolean().default(true), config: z.object({ cron: z.string().trim().min(1), timezone: z.string().trim().min(1) }).strict() }).strict(), z.object({ id: z.string().min(1).optional(), slug: z.string().regex(/^[a-z][a-z0-9_-]{0,63}$/).optional(), kind: z.literal("webhook"), template: sourceTemplate, enabled: z.boolean().default(true), config: webhookSourceConfigSchema }).strict(), ]); -export const triggerInputSchema = z.object({ name: resourceName, eventId: z.guid(), sources: z.array(triggerSourceSchema).min(1).max(50).default([{ kind: "manual", slug: "source-1", enabled: true, config: {} }]) }).strict(); +const triggerSources = z.array(triggerSourceSchema).min(1).max(50); +export const triggerInputSchema = z.object({ name: resourceName, eventId: z.guid(), sources: triggerSources.default([{ kind: "manual", slug: "source-1", enabled: true, config: {} }]) }).strict(); + +// Require explicit MCP input so a stale tool catalog cannot discard the +// source configuration and fall back to creating or restoring a manual source. +export const mcpTriggerInputSchema = triggerInputSchema.extend({ + sources: triggerSources.describe("Input sources for the Trigger."), +}); export const triggerIdSchema = z.object({ triggerId: z.string().min(1) }); export const fireTriggerInputSchema = z.object({ data: z.record(z.string(), z.unknown()).default({}), idempotencyKey: z.string().trim().min(1).max(200).optional() }).strict(); export const triggerConditionsTestSchema = z.object({ conditions: conditionsSchema.optional(), webhook: preparedWebhookSchema }).strict(); diff --git a/packages/api/test/mcp-events.postgres.test.ts b/packages/api/test/mcp-events.postgres.test.ts index 2cb5710b..2dd50d82 100644 --- a/packages/api/test/mcp-events.postgres.test.ts +++ b/packages/api/test/mcp-events.postgres.test.ts @@ -119,6 +119,39 @@ describe.skipIf(!process.env.AUTH_TEST_DATABASE_URL)("MCP event webhook lifecycl "SELECT occurrence_id,status,attempts,http_status FROM app.mcp_event_deliveries ORDER BY updated_at,occurrence_id", )).rows; + it("requires canonical MCP sources and persists the configured Linear webhook", async () => { + const catalog = (await request("tools/list")).message.result.tools; + for (const toolName of ["create_trigger", "update_trigger"]) { + const schema = catalog.find((item: { name: string }) => item.name === toolName).inputSchema; + expect(schema.properties).toMatchObject({ sources: { type: "array" } }); + expect(schema.properties).not.toHaveProperty("events"); + expect(schema.required).toContain("sources"); + } + const config = { + provider: "linear", projectId: "fixture-project", + matchRules: [{ type: "status", targetId: "fixture-todo" }], + }; + const webhook = { kind: "webhook", enabled: true, config }; + const input = { name: "fixture-linear", eventId: source.definitionId }; + expect((await tool("create_trigger", input)).isError).toBe(true); + expect((await tool("create_trigger", { ...input, events: [webhook] })).isError).toBe(true); + const create = await tool("create_trigger", { ...input, sources: [webhook] }); + expect(create.isError, JSON.stringify(create)).not.toBe(true); + const triggerId = create.structuredContent.id; + const get = () => tool("get_trigger", { triggerId }); + const original = (await get()).structuredContent.sources; + expect(original).toMatchObject([{ kind: "manual" }, webhook]); + const update = { ...input, triggerId }; + const manual = { kind: "manual", id: original[0].id, config: {} }; + const newWebhook = { ...webhook, id: original[1].id, config: { ...config, matchRules: [{ type: "status", targetId: "fixture-review" }] } }; + expect((await tool("update_trigger", { ...update, sources: [manual, newWebhook] })).isError).not.toBe(true); + const updated = (await get()).structuredContent.sources; + expect(updated).toMatchObject([manual, newWebhook]); + expect((await tool("update_trigger", update)).isError).toBe(true); + expect((await tool("update_trigger", { ...update, events: [webhook] })).isError).toBe(true); + expect((await get()).structuredContent.sources).toEqual(updated); + }); + it("discovers, subscribes, commits, dispatches and reads a deduplicated occurrence", async () => { expect((await request("server/discover")).message.result.capabilities).toMatchObject({ events: {}, resources: { subscribe: true } }); expect((await request("events/list")).message.result.events).toEqual([{ diff --git a/packages/docs/llms-full.txt b/packages/docs/llms-full.txt index ba030116..247ad3d1 100644 --- a/packages/docs/llms-full.txt +++ b/packages/docs/llms-full.txt @@ -314,8 +314,9 @@ input, or manual fire request. Duplicate deliveries are deduplicated. Reuse the same `idempotencyKey` after an uncertain manual fire. Webhook activity tools show safe processing outcomes without exposing secrets or raw payloads. -`create_trigger` and `update_trigger` accept a parent `eventId` and a -`sources` array of input configurations. Read a Trigger before replacing its +`create_trigger` and `update_trigger` require a parent `eventId` and an +explicit `sources` array of input configurations. For a manual-only Trigger, +send `sources: [{ "kind": "manual" }]`. Read a Trigger before replacing its configuration. ## Integration APIs diff --git a/packages/docs/mcp/tools.mdx b/packages/docs/mcp/tools.mdx index a2313e70..6b132d71 100644 --- a/packages/docs/mcp/tools.mdx +++ b/packages/docs/mcp/tools.mdx @@ -87,8 +87,9 @@ input, or manual fire request. Duplicate deliveries are deduplicated. Reuse the same `idempotencyKey` after an uncertain manual fire. Webhook activity tools show safe processing outcomes without exposing secrets or raw payloads. -`create_trigger` and `update_trigger` accept a parent `eventId` and a -`sources` array of input configurations. Read a Trigger before replacing its +`create_trigger` and `update_trigger` require a parent `eventId` and an +explicit `sources` array of input configurations. For a manual-only Trigger, +send `sources: [{ "kind": "manual" }]`. Read a Trigger before replacing its configuration. ## Integration APIs