-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix(mcp): harden notification system against race conditions #3168
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 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cf25fb0
fix(mcp): harden notification system against race conditions
waleedlatif1 7c6927d
updated tests
waleedlatif1 f844e25
plugged in new mcp event based system and create sse route to publish…
waleedlatif1 ec69103
ack commetns
waleedlatif1 3de6e3b
fix reconnect timer
waleedlatif1 4d53e35
cleanup when running onClose
waleedlatif1 0ba38ce
fixed spacing on mcp settings tab
waleedlatif1 44ebc51
keep error listeners before quiet in redis
waleedlatif1 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
Next
Next commit
fix(mcp): harden notification system against race conditions
- Guard concurrent connect() calls in connection manager with connectingServers Set - Suppress post-disconnect notification handler firing in MCP client - Clean up Redis event listeners in pub/sub dispose() - Add tests for all three hardening fixes (11 new tests)
- Loading branch information
commit cf25fb084389e082b2e3ff033e77b34f9d95c732
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,111 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { loggerMock } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('@sim/logger', () => loggerMock) | ||
|
|
||
| /** | ||
| * Capture the notification handler registered via `client.setNotificationHandler()`. | ||
| * This lets us simulate the MCP SDK delivering a `tools/list_changed` notification. | ||
| */ | ||
| let capturedNotificationHandler: (() => Promise<void>) | null = null | ||
|
|
||
| vi.mock('@modelcontextprotocol/sdk/client/index.js', () => ({ | ||
| Client: vi.fn().mockImplementation(() => ({ | ||
| connect: vi.fn().mockResolvedValue(undefined), | ||
| close: vi.fn().mockResolvedValue(undefined), | ||
| getServerVersion: vi.fn().mockReturnValue('2025-06-18'), | ||
| getServerCapabilities: vi.fn().mockReturnValue({ tools: { listChanged: true } }), | ||
| setNotificationHandler: vi | ||
| .fn() | ||
| .mockImplementation((_schema: unknown, handler: () => Promise<void>) => { | ||
| capturedNotificationHandler = handler | ||
| }), | ||
| listTools: vi.fn().mockResolvedValue({ tools: [] }), | ||
| })), | ||
| })) | ||
|
|
||
| vi.mock('@modelcontextprotocol/sdk/client/streamableHttp.js', () => ({ | ||
| StreamableHTTPClientTransport: vi.fn().mockImplementation(() => ({ | ||
| onclose: null, | ||
| sessionId: 'test-session', | ||
| })), | ||
| })) | ||
|
|
||
| vi.mock('@modelcontextprotocol/sdk/types.js', () => ({ | ||
| ToolListChangedNotificationSchema: { method: 'notifications/tools/list_changed' }, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/core/execution-limits', () => ({ | ||
| getMaxExecutionTimeout: vi.fn().mockReturnValue(30000), | ||
| })) | ||
|
|
||
| import { McpClient } from './client' | ||
| import type { McpServerConfig } from './types' | ||
|
|
||
| function createConfig(): McpServerConfig { | ||
| return { | ||
| id: 'server-1', | ||
| name: 'Test Server', | ||
| transport: 'streamable-http', | ||
| url: 'https://test.example.com/mcp', | ||
| } | ||
| } | ||
|
|
||
| describe('McpClient notification handler', () => { | ||
| beforeEach(() => { | ||
| capturedNotificationHandler = null | ||
| }) | ||
|
|
||
| it('fires onToolsChanged when a notification arrives while connected', async () => { | ||
| const onToolsChanged = vi.fn() | ||
|
|
||
| const client = new McpClient({ | ||
| config: createConfig(), | ||
| securityPolicy: { requireConsent: false, auditLevel: 'basic' }, | ||
| onToolsChanged, | ||
| }) | ||
|
|
||
| await client.connect() | ||
|
|
||
| expect(capturedNotificationHandler).not.toBeNull() | ||
|
|
||
| await capturedNotificationHandler!() | ||
|
|
||
| expect(onToolsChanged).toHaveBeenCalledTimes(1) | ||
| expect(onToolsChanged).toHaveBeenCalledWith('server-1') | ||
| }) | ||
|
|
||
| it('suppresses notifications after disconnect', async () => { | ||
| const onToolsChanged = vi.fn() | ||
|
|
||
| const client = new McpClient({ | ||
| config: createConfig(), | ||
| securityPolicy: { requireConsent: false, auditLevel: 'basic' }, | ||
| onToolsChanged, | ||
| }) | ||
|
|
||
| await client.connect() | ||
| expect(capturedNotificationHandler).not.toBeNull() | ||
|
|
||
| await client.disconnect() | ||
|
|
||
| // Simulate a late notification arriving after disconnect | ||
| await capturedNotificationHandler!() | ||
|
|
||
| expect(onToolsChanged).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not register a notification handler when onToolsChanged is not provided', async () => { | ||
| const client = new McpClient({ | ||
| config: createConfig(), | ||
| securityPolicy: { requireConsent: false, auditLevel: 'basic' }, | ||
| }) | ||
|
|
||
| await client.connect() | ||
|
|
||
| expect(capturedNotificationHandler).toBeNull() | ||
| }) | ||
| }) |
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.
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.