Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/everything/__tests__/tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ describe('Tools', () => {
);

expect(result.content[0].text).toContain('Long running operation completed');
expect(result.content[0].text).toContain('Operation ID:');
expect(result.content[0].text).toContain('Duration: 0.1 seconds');
expect(result.content[0].text).toContain('Steps: 2');
}, 10000);
Expand All @@ -358,6 +359,28 @@ describe('Tools', () => {
expect.any(Object)
);
}, 10000);

it('should generate a collision-resistant request id when missing', async () => {
const { mockServer, handlers } = createMockServer();
registerTriggerLongRunningOperationTool(mockServer);

const handler = handlers.get('trigger-long-running-operation')!;
await handler(
{ duration: 0.1, steps: 1 },
{ _meta: { progressToken: 'token-generated' } }
);

expect(mockServer.server.notification).toHaveBeenCalledWith(
expect.objectContaining({
method: 'notifications/progress',
}),
expect.objectContaining({
relatedRequestId: expect.stringMatching(
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
),
})
);
}, 10000);
});

describe('get-resource-links', () => {
Expand Down
7 changes: 5 additions & 2 deletions src/everything/tools/trigger-long-running-operation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { randomUUID } from "node:crypto";
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
Expand Down Expand Up @@ -42,6 +43,8 @@ export const registerTriggerLongRunningOperationTool = (server: McpServer) => {
const { duration, steps } = validatedArgs;
const stepDuration = duration / steps;
const progressToken = extra._meta?.progressToken;
const requestId = extra.requestId ?? randomUUID();
const operationId = randomUUID();

for (let i = 1; i < steps + 1; i++) {
await new Promise((resolve) =>
Expand All @@ -58,7 +61,7 @@ export const registerTriggerLongRunningOperationTool = (server: McpServer) => {
progressToken,
},
},
{ relatedRequestId: extra.requestId }
{ relatedRequestId: requestId }
);
}
}
Expand All @@ -67,7 +70,7 @@ export const registerTriggerLongRunningOperationTool = (server: McpServer) => {
content: [
{
type: "text",
text: `Long running operation completed. Duration: ${duration} seconds, Steps: ${steps}.`,
text: `Long running operation completed. Operation ID: ${operationId}. Duration: ${duration} seconds, Steps: ${steps}.`,
},
],
};
Expand Down