Add MCP Root change notifications (#6502)

This commit is contained in:
Jacob MacDonald
2025-08-18 14:09:02 -07:00
committed by GitHub
parent 465ac9f547
commit 3960ccf781
4 changed files with 200 additions and 7 deletions

View File

@@ -280,6 +280,46 @@ describe('mcp-client', () => {
});
describe('connectToMcpServer', () => {
it('should send a notification when directories change', async () => {
const mockedClient = {
registerCapabilities: vi.fn(),
setRequestHandler: vi.fn(),
notification: vi.fn(),
callTool: vi.fn(),
connect: vi.fn(),
};
vi.mocked(ClientLib.Client).mockReturnValue(
mockedClient as unknown as ClientLib.Client,
);
vi.spyOn(SdkClientStdioLib, 'StdioClientTransport').mockReturnValue(
{} as SdkClientStdioLib.StdioClientTransport,
);
let onDirectoriesChangedCallback: () => void = () => {};
const mockWorkspaceContext = {
getDirectories: vi
.fn()
.mockReturnValue(['/test/dir', '/another/project']),
onDirectoriesChanged: vi.fn().mockImplementation((callback) => {
onDirectoriesChangedCallback = callback;
}),
} as unknown as WorkspaceContext;
await connectToMcpServer(
'test-server',
{
command: 'test-command',
},
false,
mockWorkspaceContext,
);
onDirectoriesChangedCallback();
expect(mockedClient.notification).toHaveBeenCalledWith({
method: 'notifications/roots/list_changed',
});
});
it('should register a roots/list handler', async () => {
const mockedClient = {
registerCapabilities: vi.fn(),
@@ -297,6 +337,7 @@ describe('mcp-client', () => {
getDirectories: vi
.fn()
.mockReturnValue(['/test/dir', '/another/project']),
onDirectoriesChanged: vi.fn(),
} as unknown as WorkspaceContext;
await connectToMcpServer(
@@ -309,7 +350,9 @@ describe('mcp-client', () => {
);
expect(mockedClient.registerCapabilities).toHaveBeenCalledWith({
roots: {},
roots: {
listChanged: true,
},
});
expect(mockedClient.setRequestHandler).toHaveBeenCalledOnce();
const handler = mockedClient.setRequestHandler.mock.calls[0][1];