Skip to content

Fix [mcp client times out after 60 seconds (ignoring timeout option) #245] #849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
70 changes: 69 additions & 1 deletion src/shared/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ describe("protocol tests", () => {
});

describe("_meta preservation with onprogress", () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});

test("should preserve existing _meta when adding progressToken", async () => {
await protocol.connect(transport);
const request = {
Expand All @@ -101,6 +108,8 @@ describe("protocol tests", () => {

protocol.request(request, mockSchema, {
onprogress: onProgressMock,
resetTimeoutOnProgress: false,

});

expect(sendSpy).toHaveBeenCalledWith(expect.objectContaining({
Expand Down Expand Up @@ -133,6 +142,8 @@ describe("protocol tests", () => {

protocol.request(request, mockSchema, {
onprogress: onProgressMock,
resetTimeoutOnProgress: false,

});

expect(sendSpy).toHaveBeenCalledWith(expect.objectContaining({
Expand Down Expand Up @@ -163,7 +174,10 @@ describe("protocol tests", () => {
result: z.string(),
});

protocol.request(request, mockSchema);
protocol.request(request, mockSchema, {
resetTimeoutOnProgress: false,

});

expect(sendSpy).toHaveBeenCalledWith(expect.objectContaining({
method: "example",
Expand All @@ -190,6 +204,8 @@ describe("protocol tests", () => {

protocol.request(request, mockSchema, {
onprogress: onProgressMock,
resetTimeoutOnProgress: false,

});

expect(sendSpy).toHaveBeenCalledWith(expect.objectContaining({
Expand Down Expand Up @@ -465,6 +481,58 @@ describe("protocol tests", () => {
await Promise.resolve();
await expect(requestPromise).resolves.toEqual({ result: "success" });
});

test("should reset timeout by default when progress notification is received", async () => {
await protocol.connect(transport);
const request = { method: "example", params: {} };
const mockSchema: ZodType<{ result: string }> = z.object({
result: z.string(),
});
const onProgressMock = jest.fn();
// Don't specify resetTimeoutOnProgress, should default to true
const requestPromise = protocol.request(request, mockSchema, {
timeout: 1000,
onprogress: onProgressMock,
});

// Advance past most of the timeout period
jest.advanceTimersByTime(900);

// Send progress notification - should reset timeout
if (transport.onmessage) {
transport.onmessage({
jsonrpc: "2.0",
method: "notifications/progress",
params: {
progressToken: 0,
progress: 50,
total: 100,
},
});
}
await Promise.resolve();

expect(onProgressMock).toHaveBeenCalledWith({
progress: 50,
total: 100,
});

// Advance another 900ms (would have timed out without reset)
jest.advanceTimersByTime(900);

// Send final response
if (transport.onmessage) {
transport.onmessage({
jsonrpc: "2.0",
id: 0,
result: { result: "completed" },
});
}
await Promise.resolve();

// Should complete successfully because timeout was reset
await expect(requestPromise).resolves.toEqual({ result: "completed" });
});
});

describe("Debounced Notifications", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/shared/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export type RequestOptions = {
/**
* If true, receiving a progress notification will reset the request timeout.
* This is useful for long-running operations that send periodic progress updates.
* Default: false
* Default: true
*/
resetTimeoutOnProgress?: boolean;

Expand Down Expand Up @@ -628,7 +628,7 @@ export abstract class Protocol<
{ timeout }
));

this._setupTimeout(messageId, timeout, options?.maxTotalTimeout, timeoutHandler, options?.resetTimeoutOnProgress ?? false);
this._setupTimeout(messageId, timeout, options?.maxTotalTimeout, timeoutHandler, options?.resetTimeoutOnProgress ?? true);

this._transport.send(jsonrpcRequest, { relatedRequestId, resumptionToken, onresumptiontoken }).catch((error) => {
this._cleanupTimeout(messageId);
Expand Down