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
81 changes: 81 additions & 0 deletions src/everything/__tests__/resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ import {
registerSessionResource,
} from '../resources/session.js';
import { registerFileResources } from '../resources/files.js';
import {
SubscribeRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import {
setSubscriptionHandlers,
beginSimulatedResourceUpdates,
stopSimulatedResourceUpdates,
removeSubscriber,
} from '../resources/subscriptions.js';

describe('Resource Templates', () => {
Expand Down Expand Up @@ -298,10 +302,87 @@ describe('Subscriptions', () => {
});
});

describe('removeSubscriber', () => {
const testUri = 'demo://resource/dynamic/text/1';
const sessionId = 'disconnect-test-session';

let subscribeHandler: (
request: { params: { uri: string } },
extra: { sessionId: string }
) => Promise<unknown>;

beforeEach(() => {
const handlers = new Map<unknown, typeof subscribeHandler>();
const mockServer = {
server: {
setRequestHandler: vi.fn((schema, handler) => {
handlers.set(schema, handler);
}),
notification: vi.fn(),
},
sendLoggingMessage: vi.fn(),
} as unknown as McpServer;

setSubscriptionHandlers(mockServer);
subscribeHandler = handlers.get(SubscribeRequestSchema)!;
});

afterEach(() => {
stopSimulatedResourceUpdates(sessionId);
removeSubscriber(sessionId);
});

it('should drop a disconnected session from all subscriptions', async () => {
const notification = vi.fn();
const mockServer = {
server: {
notification,
},
} as unknown as McpServer;

await subscribeHandler({ params: { uri: testUri } }, { sessionId });

beginSimulatedResourceUpdates(mockServer, sessionId);
expect(notification).toHaveBeenCalled();

notification.mockClear();
removeSubscriber(sessionId);
stopSimulatedResourceUpdates(sessionId);

beginSimulatedResourceUpdates(mockServer, sessionId);
expect(notification).not.toHaveBeenCalled();
});

it('should not affect other sessions subscribed to the same URI', async () => {
const otherSessionId = 'other-session';
const notification = vi.fn();
const mockServer = {
server: {
notification,
},
} as unknown as McpServer;

await subscribeHandler({ params: { uri: testUri } }, { sessionId });
await subscribeHandler(
{ params: { uri: testUri } },
{ sessionId: otherSessionId }
);

removeSubscriber(sessionId);

beginSimulatedResourceUpdates(mockServer, otherSessionId);
expect(notification).toHaveBeenCalled();

stopSimulatedResourceUpdates(otherSessionId);
removeSubscriber(otherSessionId);
});
});

describe('simulated resource updates lifecycle', () => {
afterEach(() => {
// Clean up any intervals
stopSimulatedResourceUpdates('lifecycle-test-session');
removeSubscriber('lifecycle-test-session');
});

it('should start and stop updates without errors', () => {
Expand Down
19 changes: 19 additions & 0 deletions src/everything/resources/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,22 @@ export const stopSimulatedResourceUpdates = (sessionId?: string) => {
subsUpdateIntervals.delete(sessionId);
}
};

/**
* Removes a session from every URI's subscriber set, dropping any URI entry
* that ends up with no remaining subscribers.
*
* A session that disconnects without explicitly unsubscribing otherwise stays
* in `subscriptions` for the life of the process. Call this from the
* transport's `cleanup(sessionId)` when a session ends.
*
* @param {string} [sessionId]
*/
export const removeSubscriber = (sessionId?: string) => {
for (const [uri, subscribers] of subscriptions) {
subscribers.delete(sessionId);
if (subscribers.size === 0) {
subscriptions.delete(uri);
}
}
};
2 changes: 2 additions & 0 deletions src/everything/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import {
setSubscriptionHandlers,
stopSimulatedResourceUpdates,
removeSubscriber,
} from "../resources/subscriptions.js";
import { registerConditionalTools, registerTools } from "../tools/index.js";
import { registerResources, readInstructions } from "../resources/index.js";
Expand Down Expand Up @@ -110,6 +111,7 @@ export const createServer: () => ServerFactoryResponse = () => {
// Stop any simulated logging or resource updates that may have been initiated.
stopSimulatedLogging(sessionId);
stopSimulatedResourceUpdates(sessionId);
removeSubscriber(sessionId);
// Clean up task store timers
taskStore.cleanup();
if (initializeTimeout) clearTimeout(initializeTimeout);
Expand Down