Skip to content

Commit 7eb9cbc

Browse files
authored
fix: refresh mcp server functionality (#1349)
1 parent 8bc318b commit 7eb9cbc

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpManager.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,49 @@ describe('close()', () => {
647647
expect(() => McpManager.instance).to.throw()
648648
})
649649
})
650+
651+
// reinitializeMcpServers()
652+
describe('reinitializeMcpServers()', () => {
653+
let loadStub: sinon.SinonStub
654+
655+
beforeEach(() => {
656+
loadStub = sinon.stub(mcpUtils, 'loadMcpServerConfigs')
657+
})
658+
659+
afterEach(async () => {
660+
sinon.restore()
661+
try {
662+
await McpManager.instance.close()
663+
} catch {}
664+
})
665+
666+
it('calls necessary methods during reinitialization', async () => {
667+
const cfg: MCPServerConfig = {
668+
command: 'cmd',
669+
args: [],
670+
env: {},
671+
disabled: false,
672+
autoApprove: false,
673+
toolOverrides: {},
674+
__configPath__: 'cfg.json',
675+
}
676+
loadStub.resolves(new Map([['srv', cfg]]))
677+
678+
const mgr = await McpManager.init(['cfg.json'], features)
679+
680+
const closeStub = sinon.stub(mgr, 'close').resolves()
681+
const initStub = sinon.stub(McpManager, 'init').resolves(mgr)
682+
683+
loadStub.resetHistory()
684+
685+
await mgr.reinitializeMcpServers()
686+
687+
expect(closeStub.calledOnce).to.be.true
688+
expect(initStub.calledOnce).to.be.true
689+
expect(initStub.firstCall.args[0]).to.deep.equal(['cfg.json'])
690+
expect(initStub.firstCall.args[1]).to.equal(features)
691+
692+
closeStub.restore()
693+
initStub.restore()
694+
})
695+
})

server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpManager.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,26 @@ export class McpManager {
413413
McpManager.#instance = undefined
414414
}
415415

416+
/**
417+
* Reinitialize all MCP servers by closing existing connections and rediscovering servers
418+
*/
419+
public async reinitializeMcpServers(): Promise<void> {
420+
this.features.logging.info('Reinitializing MCP servers')
421+
422+
try {
423+
await this.close()
424+
await McpManager.init(this.configPaths, this.features)
425+
426+
const reinitializedServerCount = McpManager.#instance?.mcpServers.size
427+
this.features.logging.info(
428+
`MCP servers reinitialized completed. Total servers: ${reinitializedServerCount}`
429+
)
430+
} catch (err: any) {
431+
this.features.logging.error(`Error reinitializing MCP servers: ${err.message}`)
432+
throw err
433+
}
434+
}
435+
416436
/**
417437
* Check if a tool requires approval.
418438
*/

0 commit comments

Comments
 (0)