Skip to content

Commit 4cf4c96

Browse files
authored
Merge pull request #427 from GenerationSoftware/codex/fix-mcp-trigger-source-input
Require canonical sources in MCP trigger writes
2 parents 74f4402 + 6c28bb4 commit 4cf4c96

5 files changed

Lines changed: 50 additions & 8 deletions

File tree

‎packages/api/src/protected-api.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { createMcpHandler, McpServer, ResourceTemplate, ProtocolError, ProtocolE
1717
import { createMcpHonoApp } from "@modelcontextprotocol/hono";
1818
import { z } from "zod";
1919
import { ApiService, ServiceError } from "./api-service";
20-
import { triggerConditionsTestSchema, triggerIdSchema, triggerInputSchema, fireTriggerInputSchema } from "./trigger-schemas";
20+
import { triggerConditionsTestSchema, triggerIdSchema, mcpTriggerInputSchema, fireTriggerInputSchema, type TriggerInput } from "./trigger-schemas";
2121
import type { Env, OAuthProps } from "./types";
2222
import { executeAuth, validBrowserOrigin } from "./auth-api";
2323
import { matchApiOperation } from "./api-contract";
@@ -213,8 +213,8 @@ function createMcpServer(service: ApiService, subscriptions = true): McpServer {
213213
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));
214214
tool("list_triggers", "List triggers and their parent Events", z.object({}), () => service.listTriggers());
215215
tool("get_trigger", "Get a Trigger, its parent Event, and input configuration", triggerIdSchema, ({ triggerId }: any) => service.getTrigger(triggerId));
216-
tool("create_trigger", "Create an input Trigger attached to an Event. Credentials are never accepted or returned.", triggerInputSchema, (input: any) => service.createTrigger(input));
217-
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));
216+
tool("create_trigger", "Create an input Trigger attached to an Event. Credentials are never accepted or returned.", mcpTriggerInputSchema, (input: TriggerInput) => service.createTrigger(input));
217+
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));
218218
tool("delete_trigger", "Delete an input Trigger", triggerIdSchema, ({ triggerId }: any) => service.deleteTrigger(triggerId));
219219
tool("enable_trigger", "Enable a trigger", triggerIdSchema, ({ triggerId }: any) => service.setTriggerEnabled(triggerId, true));
220220
tool("disable_trigger", "Disable a trigger", triggerIdSchema, ({ triggerId }: any) => service.setTriggerEnabled(triggerId, false));

‎packages/api/src/trigger-schemas.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ export const triggerSourceSchema = z.discriminatedUnion("kind", [
1919
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(),
2020
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(),
2121
]);
22-
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();
22+
const triggerSources = z.array(triggerSourceSchema).min(1).max(50);
23+
export const triggerInputSchema = z.object({ name: resourceName, eventId: z.guid(), sources: triggerSources.default([{ kind: "manual", slug: "source-1", enabled: true, config: {} }]) }).strict();
24+
25+
// Require explicit MCP input so a stale tool catalog cannot discard the
26+
// source configuration and fall back to creating or restoring a manual source.
27+
export const mcpTriggerInputSchema = triggerInputSchema.extend({
28+
sources: triggerSources.describe("Input sources for the Trigger."),
29+
});
2330
export const triggerIdSchema = z.object({ triggerId: z.string().min(1) });
2431
export const fireTriggerInputSchema = z.object({ data: z.record(z.string(), z.unknown()).default({}), idempotencyKey: z.string().trim().min(1).max(200).optional() }).strict();
2532
export const triggerConditionsTestSchema = z.object({ conditions: conditionsSchema.optional(), webhook: preparedWebhookSchema }).strict();

‎packages/api/test/mcp-events.postgres.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,39 @@ describe.skipIf(!process.env.AUTH_TEST_DATABASE_URL)("MCP event webhook lifecycl
119119
"SELECT occurrence_id,status,attempts,http_status FROM app.mcp_event_deliveries ORDER BY updated_at,occurrence_id",
120120
)).rows;
121121

122+
it("requires canonical MCP sources and persists the configured Linear webhook", async () => {
123+
const catalog = (await request("tools/list")).message.result.tools;
124+
for (const toolName of ["create_trigger", "update_trigger"]) {
125+
const schema = catalog.find((item: { name: string }) => item.name === toolName).inputSchema;
126+
expect(schema.properties).toMatchObject({ sources: { type: "array" } });
127+
expect(schema.properties).not.toHaveProperty("events");
128+
expect(schema.required).toContain("sources");
129+
}
130+
const config = {
131+
provider: "linear", projectId: "fixture-project",
132+
matchRules: [{ type: "status", targetId: "fixture-todo" }],
133+
};
134+
const webhook = { kind: "webhook", enabled: true, config };
135+
const input = { name: "fixture-linear", eventId: source.definitionId };
136+
expect((await tool("create_trigger", input)).isError).toBe(true);
137+
expect((await tool("create_trigger", { ...input, events: [webhook] })).isError).toBe(true);
138+
const create = await tool("create_trigger", { ...input, sources: [webhook] });
139+
expect(create.isError, JSON.stringify(create)).not.toBe(true);
140+
const triggerId = create.structuredContent.id;
141+
const get = () => tool("get_trigger", { triggerId });
142+
const original = (await get()).structuredContent.sources;
143+
expect(original).toMatchObject([{ kind: "manual" }, webhook]);
144+
const update = { ...input, triggerId };
145+
const manual = { kind: "manual", id: original[0].id, config: {} };
146+
const newWebhook = { ...webhook, id: original[1].id, config: { ...config, matchRules: [{ type: "status", targetId: "fixture-review" }] } };
147+
expect((await tool("update_trigger", { ...update, sources: [manual, newWebhook] })).isError).not.toBe(true);
148+
const updated = (await get()).structuredContent.sources;
149+
expect(updated).toMatchObject([manual, newWebhook]);
150+
expect((await tool("update_trigger", update)).isError).toBe(true);
151+
expect((await tool("update_trigger", { ...update, events: [webhook] })).isError).toBe(true);
152+
expect((await get()).structuredContent.sources).toEqual(updated);
153+
});
154+
122155
it("discovers, subscribes, commits, dispatches and reads a deduplicated occurrence", async () => {
123156
expect((await request("server/discover")).message.result.capabilities).toMatchObject({ events: {}, resources: { subscribe: true } });
124157
expect((await request("events/list")).message.result.events).toEqual([{

‎packages/docs/llms-full.txt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,9 @@ input, or manual fire request. Duplicate deliveries are deduplicated. Reuse the
314314
same `idempotencyKey` after an uncertain manual fire. Webhook activity tools
315315
show safe processing outcomes without exposing secrets or raw payloads.
316316

317-
`create_trigger` and `update_trigger` accept a parent `eventId` and a
318-
`sources` array of input configurations. Read a Trigger before replacing its
317+
`create_trigger` and `update_trigger` require a parent `eventId` and an
318+
explicit `sources` array of input configurations. For a manual-only Trigger,
319+
send `sources: [{ "kind": "manual" }]`. Read a Trigger before replacing its
319320
configuration.
320321

321322
## Integration APIs

‎packages/docs/mcp/tools.mdx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ input, or manual fire request. Duplicate deliveries are deduplicated. Reuse the
8787
same `idempotencyKey` after an uncertain manual fire. Webhook activity tools
8888
show safe processing outcomes without exposing secrets or raw payloads.
8989

90-
`create_trigger` and `update_trigger` accept a parent `eventId` and a
91-
`sources` array of input configurations. Read a Trigger before replacing its
90+
`create_trigger` and `update_trigger` require a parent `eventId` and an
91+
explicit `sources` array of input configurations. For a manual-only Trigger,
92+
send `sources: [{ "kind": "manual" }]`. Read a Trigger before replacing its
9293
configuration.
9394

9495
## Integration APIs

0 commit comments

Comments
 (0)