Repository navigation
refactor(mcp): share execution timeout handling across tools #3338
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
86bd48c
refactor(mcp): share execution timeout handling across tools
brandon-pereira de0e1d5
fix(mcp): report a timeout when zero-grace work resolves on abort (gr…
brandon-pereira 9323934
fix(mcp): derive MCP_REQUEST_TIMEOUT from the query cap it buffers (r…
brandon-pereira 4c55a4a
refactor(mcp): derive the tool timeout from the query cap in seconds
brandon-pereira 042499c
Merge branch 'main' into brandon/mcp-dedupe
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@hyperdx/api': patch | ||
| --- | ||
|
|
||
| refactor(mcp): share wall-clock timeout handling across MCP tools via | ||
| `runWithTimeout`, and source the 30s ClickHouse query cap from one constant. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { runWithTimeout } from '@/mcp/utils/timeout'; | ||
|
|
||
| const never = () => new Promise<never>(() => {}); | ||
|
|
||
| describe('runWithTimeout', () => { | ||
| beforeEach(() => jest.useFakeTimers()); | ||
| afterEach(() => jest.useRealTimers()); | ||
|
|
||
| it('returns the value when work finishes in time and aborts the signal on exit', async () => { | ||
| let captured: AbortSignal | undefined; | ||
| const outcome = await runWithTimeout( | ||
| async signal => { | ||
| captured = signal; | ||
| return 'done'; | ||
| }, | ||
| { timeoutMs: 1000 }, | ||
| ); | ||
| expect(outcome).toEqual({ timedOut: false, value: 'done' }); | ||
| expect(captured?.aborted).toBe(true); | ||
| expect(jest.getTimerCount()).toBe(0); | ||
| }); | ||
|
|
||
| it('times out and aborts the signal at the deadline', async () => { | ||
| let captured: AbortSignal | undefined; | ||
| const pending = runWithTimeout( | ||
| signal => { | ||
| captured = signal; | ||
| return never(); | ||
| }, | ||
| { timeoutMs: 1000 }, | ||
| ); | ||
| await jest.advanceTimersByTimeAsync(999); | ||
| expect(captured?.aborted).toBe(false); | ||
| await jest.advanceTimersByTimeAsync(1); | ||
| await expect(pending).resolves.toEqual({ timedOut: true }); | ||
| expect(captured?.aborted).toBe(true); | ||
| }); | ||
|
|
||
| it('reports a timeout without grace even when work resolves on abort', async () => { | ||
| const pending = runWithTimeout( | ||
| signal => | ||
| new Promise<string>(resolve => | ||
| signal.addEventListener('abort', () => resolve('too late')), | ||
| ), | ||
| { timeoutMs: 1000 }, | ||
| ); | ||
| await jest.advanceTimersByTimeAsync(1000); | ||
| await expect(pending).resolves.toEqual({ timedOut: true }); | ||
| }); | ||
|
|
||
| it('reports a timeout without grace when work rejects on abort', async () => { | ||
| const pending = runWithTimeout( | ||
| signal => | ||
| new Promise<never>((_, reject) => | ||
| signal.addEventListener('abort', () => reject(new Error('aborted'))), | ||
| ), | ||
| { timeoutMs: 1000 }, | ||
| ); | ||
| await jest.advanceTimersByTimeAsync(1000); | ||
| await expect(pending).resolves.toEqual({ timedOut: true }); | ||
| expect(jest.getTimerCount()).toBe(0); | ||
| }); | ||
|
|
||
| it('keeps a result returned inside the grace window after the abort', async () => { | ||
| const pending = runWithTimeout( | ||
| signal => | ||
| new Promise<string>(resolve => | ||
| signal.addEventListener('abort', () => resolve('partial')), | ||
| ), | ||
| { timeoutMs: 1000, graceMs: 500 }, | ||
| ); | ||
| await jest.advanceTimersByTimeAsync(1000); | ||
| await expect(pending).resolves.toEqual({ | ||
| timedOut: false, | ||
| value: 'partial', | ||
| }); | ||
| }); | ||
|
|
||
| it('stops waiting at the end of the grace window when work ignores the signal', async () => { | ||
| let settled = false; | ||
| const pending = runWithTimeout(never, { | ||
| timeoutMs: 1000, | ||
| graceMs: 500, | ||
| }).finally(() => { | ||
| settled = true; | ||
| }); | ||
| await jest.advanceTimersByTimeAsync(1000); | ||
| expect(settled).toBe(false); | ||
| await jest.advanceTimersByTimeAsync(500); | ||
| await expect(pending).resolves.toEqual({ timedOut: true }); | ||
| }); | ||
|
|
||
| it('treats a rejection after the abort as a timeout', async () => { | ||
| const pending = runWithTimeout( | ||
| signal => | ||
| new Promise<never>((_, reject) => | ||
| signal.addEventListener('abort', () => reject(new Error('aborted'))), | ||
| ), | ||
| { timeoutMs: 1000, graceMs: 500 }, | ||
| ); | ||
| await jest.advanceTimersByTimeAsync(1000); | ||
| await expect(pending).resolves.toEqual({ timedOut: true }); | ||
| }); | ||
|
|
||
| it('rethrows a rejection raised before the deadline and aborts the signal', async () => { | ||
| let captured: AbortSignal | undefined; | ||
| await expect( | ||
| runWithTimeout( | ||
| signal => { | ||
| captured = signal; | ||
| return Promise.reject(new Error('boom')); | ||
| }, | ||
| { timeoutMs: 1000 }, | ||
| ), | ||
| ).rejects.toThrow('boom'); | ||
| expect(captured?.aborted).toBe(true); | ||
| expect(jest.getTimerCount()).toBe(0); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.