The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(mcp): echo the client's progressToken instead of fabricating one (#24582)
MD. EBRAHIM ALIe27f01abecaa
diff --git a/packages/twenty-server/src/engine/api/mcp/constants/mcp-progress-notification.const.ts b/packages/twenty-server/src/engine/api/mcp/constants/mcp-progress-notification.const.tsindex 75b713ed694..1163c3e1e09 100644--- a/packages/twenty-server/src/engine/api/mcp/constants/mcp-progress-notification.const.ts+++ b/packages/twenty-server/src/engine/api/mcp/constants/mcp-progress-notification.const.ts@@ -1,3 +1 @@export const MCP_PROGRESS_NOTIFICATION_METHOD = 'notifications/progress';--export const TOOL_CALL_PROGRESS_TOKEN_PREFIX = 'tool-call-';diff --git a/packages/twenty-server/src/engine/api/mcp/services/mcp-tool-executor.service.ts b/packages/twenty-server/src/engine/api/mcp/services/mcp-tool-executor.service.tsindex 777891d8da2..cc475a80aed 100644--- a/packages/twenty-server/src/engine/api/mcp/services/mcp-tool-executor.service.ts+++ b/packages/twenty-server/src/engine/api/mcp/services/mcp-tool-executor.service.ts@@ -14,11 +14,9 @@ import { isToolOutputSuccessful } from 'src/engine/core-modules/tool-provider/utimport { resolveToolName } from 'src/engine/core-modules/tool-provider/utils/resolve-tool-name.util';import { JSON_RPC_ERROR_CODE } from 'src/engine/api/mcp/constants/json-rpc-error-code.const';-import {- MCP_PROGRESS_NOTIFICATION_METHOD,- TOOL_CALL_PROGRESS_TOKEN_PREFIX,-} from 'src/engine/api/mcp/constants/mcp-progress-notification.const';+import { MCP_PROGRESS_NOTIFICATION_METHOD } from 'src/engine/api/mcp/constants/mcp-progress-notification.const';import { type McpToolAnnotations } from 'src/engine/api/mcp/types/mcp-tool-annotations.type';+import { getProgressToken } from 'src/engine/api/mcp/utils/get-progress-token.util';import { wrapJsonRpcResponse } from 'src/engine/api/mcp/utils/wrap-jsonrpc-response.util';type McpToolDefinition = ToolSet[string] & {@@ -61,12 +59,14 @@ export class McpToolExecutorService {});}- if (isDefined(sseWriter)) {+ const progressToken = getProgressToken(params);++ if (isDefined(sseWriter) && isDefined(progressToken)) {sseWriter({jsonrpc: '2.0',method: MCP_PROGRESS_NOTIFICATION_METHOD,params: {- progressToken: `${TOOL_CALL_PROGRESS_TOKEN_PREFIX}${String(id)}`,+ progressToken,progress: 0,total: 1,},diff --git a/packages/twenty-server/src/engine/api/mcp/utils/__tests__/get-progress-token.util.spec.ts b/packages/twenty-server/src/engine/api/mcp/utils/__tests__/get-progress-token.util.spec.tsnew file mode 100644index 00000000000..7d8189a64b0--- /dev/null+++ b/packages/twenty-server/src/engine/api/mcp/utils/__tests__/get-progress-token.util.spec.ts@@ -0,0 +1,43 @@+import { getProgressToken } from 'src/engine/api/mcp/utils/get-progress-token.util';++describe('getProgressToken', () => {+ it.each([['abc123'], [4], [0], [-7]])(+ 'should return the token %p supplied by the client',+ (progressToken) => {+ expect(getProgressToken({ _meta: { progressToken } })).toBe(+ progressToken,+ );+ },+ );++ it('should return undefined when params carry no _meta', () => {+ expect(getProgressToken({ name: 'learn_tools' })).toBeUndefined();+ });++ it('should return undefined when _meta carries no progressToken', () => {+ expect(getProgressToken({ _meta: { other: 'value' } })).toBeUndefined();+ });++ it.each([[null], ['abc123']])(+ 'should return undefined when _meta is %p',+ (meta) => {+ expect(getProgressToken({ _meta: meta })).toBeUndefined();+ },+ );++ it.each([+ [true],+ [{}],+ [[]],+ [null],+ [1.5],+ [-0.25],+ [Number.NaN],+ [Number.POSITIVE_INFINITY],+ ])(+ 'should return undefined for the non conformant token %p',+ (progressToken) => {+ expect(getProgressToken({ _meta: { progressToken } })).toBeUndefined();+ },+ );+});diff --git a/packages/twenty-server/src/engine/api/mcp/utils/get-progress-token.util.ts b/packages/twenty-server/src/engine/api/mcp/utils/get-progress-token.util.tsnew file mode 100644index 00000000000..62bde2e6a4e--- /dev/null+++ b/packages/twenty-server/src/engine/api/mcp/utils/get-progress-token.util.ts@@ -0,0 +1,13 @@+import { z } from 'zod';++const progressTokenParamsSchema = z.object({+ _meta: z.object({+ progressToken: z.union([z.string(), z.number().int()]),+ }),+});++export const getProgressToken = (params: Record<string, unknown>) => {+ const parsed = progressTokenParamsSchema.safeParse(params);++ return parsed.success ? parsed.data._meta.progressToken : undefined;+};diff --git a/packages/twenty-server/test/integration/ai/suites/mcp.controller.integration-spec.ts b/packages/twenty-server/test/integration/ai/suites/mcp.controller.integration-spec.tsindex 6cf01eb2366..e20562e94f8 100644--- a/packages/twenty-server/test/integration/ai/suites/mcp.controller.integration-spec.ts+++ b/packages/twenty-server/test/integration/ai/suites/mcp.controller.integration-spec.ts@@ -253,13 +253,17 @@ describe('MCP Controller (integration)', () => {});});- it('should include progress notification before tool call result in SSE', async () => {+ it('should echo the client progress token before the tool call result in SSE', async () => {const res = await postMcp({jsonrpc: '2.0',method: 'tools/call',id: 'sse-tool-1',- params: { name: 'learn_tools', arguments: { toolNames: [] } },+ params: {+ name: 'learn_tools',+ arguments: { toolNames: [] },+ _meta: { progressToken: 'client-token-1' },+ },},API_KEY_ACCESS_TOKEN,'application/json, text/event-stream',@@ -277,7 +281,7 @@ describe('MCP Controller (integration)', () => {jsonrpc: '2.0',method: 'notifications/progress',params: {- progressToken: 'tool-call-sse-tool-1',+ progressToken: 'client-token-1',progress: 0,total: 1,},@@ -295,5 +299,31 @@ describe('MCP Controller (integration)', () => {expect(hasResultContent || hasError).toBe(true);});++ it('should send no progress notification when the client did not supply a token', async () => {+ const res = await postMcp(+ {+ jsonrpc: '2.0',+ method: 'tools/call',+ id: 'sse-tool-no-progress',+ params: { name: 'learn_tools', arguments: { toolNames: [] } },+ },+ API_KEY_ACCESS_TOKEN,+ 'application/json, text/event-stream',+ ).expect(200);++ const events = parseSseEvents(res.text);++ expect(+ events.some((event) => event.method === 'notifications/progress'),+ ).toBe(false);++ const lastEvent = events[events.length - 1];++ expect(lastEvent).toMatchObject({+ id: 'sse-tool-no-progress',+ jsonrpc: '2.0',+ });+ });});});