Skip to content

Commit 0a47fe0

Browse files
committed
Add mcp configuration button
1 parent 7fcb137 commit 0a47fe0

File tree

8 files changed

+22
-48
lines changed

8 files changed

+22
-48
lines changed

packages/core/src/codewhisperer/activation.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ import { syncSecurityIssueWebview } from './views/securityIssue/securityIssueWeb
9292
import { detectCommentAboveLine } from '../shared/utilities/commentUtils'
9393
import { globalMcpConfigPath } from '../codewhispererChat/constants'
9494
import { McpManager } from '../codewhispererChat/tools/mcp/mcpManager'
95-
import globals from '../shared/extensionGlobals'
96-
import { ToolUtils } from '../codewhispererChat/tools/toolUtils'
9795

9896
let localize: nls.LocalizeFunc
9997

@@ -380,8 +378,7 @@ export async function activate(context: ExtContext): Promise<void> {
380378
/**
381379
* MCP client initialization
382380
*/
383-
const mgr = await McpManager.initMcpManager(globalMcpConfigPath)
384-
globals.mcpManager = mgr
381+
await McpManager.initMcpManager(globalMcpConfigPath)
385382
setSubscriptionsForMcp()
386383

387384
function shouldRunAutoScan(editor: vscode.TextEditor | undefined, isScansEnabled?: boolean) {
@@ -526,10 +523,7 @@ export async function activate(context: ExtContext): Promise<void> {
526523
if (doc.uri.fsPath === globalMcpConfigPath) {
527524
const newContent = doc.getText()
528525
if (lastMcpContent === undefined || newContent !== lastMcpContent) {
529-
const manager = await McpManager.initMcpManager(globalMcpConfigPath)
530-
if (manager) {
531-
globals.mcpManager = manager
532-
}
526+
await McpManager.initMcpManager(globalMcpConfigPath)
533527
lastMcpContent = newContent
534528
}
535529
}

packages/core/src/codewhispererChat/controllers/chat/controller.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ import {
9292
additionalContentInnerContextLimit,
9393
workspaceChunkMaxSize,
9494
defaultContextLengths,
95-
globalMcpConfigPath,
9695
} from '../../constants'
9796
import { ChatSession } from '../../clients/chat/v0/chat'
9897
import { amazonQTabSuffix } from '../../../shared/constants'
@@ -103,7 +102,6 @@ import { tempDirPath } from '../../../shared/filesystemUtilities'
103102
import { Database } from '../../../shared/db/chatDb/chatDb'
104103
import { TabBarController } from './tabBarController'
105104
import { messageToChatMessage } from '../../../shared/db/chatDb/util'
106-
import { McpManager } from '../../tools/mcp/mcpManager'
107105

108106
export interface ChatControllerMessagePublishers {
109107
readonly processPromptChatMessage: MessagePublisher<PromptMessage>
@@ -199,17 +197,6 @@ export class ChatController {
199197
this.userIntentRecognizer = new UserIntentRecognizer()
200198
this.tabBarController = new TabBarController(this.messenger)
201199

202-
// todo: move to activate function?
203-
// McpManager.initMcpManager(globalMcpConfigPath)
204-
// .then((manager) => {
205-
// if (manager) {
206-
// globals.mcpManager = manager
207-
// }
208-
// })
209-
// .catch((err) => {
210-
// getLogger().error(`Failed to initialize MCP manager in controller: ${err}`)
211-
// })
212-
213200
onDidChangeAmazonQVisibility((visible) => {
214201
if (visible) {
215202
this.telemetryHelper.recordOpenChat()

packages/core/src/codewhispererChat/controllers/chat/tabBarController.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import { Database } from '../../../shared/db/chatDb/chatDb'
1515
import { TabBarButtonClick, SaveChatMessage } from './model'
1616
import { Conversation, messageToChatItem, Tab } from '../../../shared/db/chatDb/util'
1717
import { DetailedListItemGroup, MynahIconsType } from '@aws/mynah-ui'
18-
import path from 'path'
19-
import { UserWrittenCodeTracker } from '../../../codewhisperer/tracker/userWrittenCodeTracker'
2018
import { globalMcpConfigPath } from '../../constants'
2119

2220
export class TabBarController {

packages/core/src/codewhispererChat/tools/mcp/mcpManager.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@ export interface McpToolDefinition {
1919
}
2020

2121
export class McpManager {
22+
static #instance: McpManager | undefined
2223
private mcpServers: Record<string, MCPServerConfig> = {}
2324
private clients: Map<string, Client> = new Map() // key: serverName, val: MCP client
2425
private mcpTools: McpToolDefinition[] = []
2526

2627
private constructor(private readonly configPath: string) {}
2728

28-
public static async create(configPath: string): Promise<McpManager> {
29-
const instance = new McpManager(configPath)
30-
await instance.loadConfig()
31-
await instance.initAllServers()
32-
return instance
29+
public static get instance(): McpManager {
30+
if (!McpManager.#instance) {
31+
throw new Error('McpManager not initialized—call initMcpManager() first')
32+
}
33+
return McpManager.#instance
3334
}
3435

3536
public async loadConfig(): Promise<void> {
@@ -45,6 +46,7 @@ export class McpManager {
4546
}
4647

4748
public async initAllServers(): Promise<void> {
49+
this.mcpTools = []
4850
for (const [serverName, serverConfig] of Object.entries(this.mcpServers)) {
4951
if (serverConfig.disabled) {
5052
getLogger().info(`MCP server [${serverName}] is disabled, skipping.`)
@@ -107,8 +109,13 @@ export class McpManager {
107109

108110
public static async initMcpManager(configPath: string): Promise<McpManager | undefined> {
109111
try {
110-
const manager = await McpManager.create(configPath)
111-
const discovered = manager.getAllMcpTools()
112+
if (!McpManager.#instance) {
113+
const mgr = new McpManager(configPath)
114+
McpManager.#instance = mgr
115+
}
116+
await McpManager.#instance.loadConfig()
117+
await McpManager.#instance.initAllServers()
118+
const discovered = McpManager.#instance.getAllMcpTools()
112119
const builtInNames = new Set<string>(Object.values(ToolType))
113120
const discoveredNames = new Set(discovered.map((d) => d.toolName))
114121

@@ -142,7 +149,7 @@ export class McpManager {
142149
}
143150
}
144151
getLogger().info(`MCP: successfully discovered ${discovered.length} new tools.`)
145-
return manager
152+
return McpManager.instance
146153
} catch (err) {
147154
getLogger().error(`Failed to init MCP manager: ${(err as Error).message}`)
148155
return undefined

packages/core/src/codewhispererChat/tools/mcp/mcpTool.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
*/
55
import { Writable } from 'stream'
66
import { getLogger } from '../../../shared/logger/logger'
7-
import { ToolUtils } from '../toolUtils'
87
import { CommandValidation, InvokeOutput, OutputKind } from '../toolShared'
9-
import globals from '../../../shared/extensionGlobals'
8+
import { McpManager } from './mcpManager'
109

1110
export interface McpToolParams {
1211
serverName: string
@@ -39,8 +38,7 @@ export class McpTool {
3938

4039
public async invoke(updates?: Writable): Promise<InvokeOutput> {
4140
try {
42-
const mcpManager = globals.mcpManager
43-
const result = await mcpManager?.callTool(this.serverName, this.toolName, this.input)
41+
const result = await McpManager.instance.callTool(this.serverName, this.toolName, this.input)
4442
const content = typeof result === 'object' ? JSON.stringify(result) : String(result)
4543

4644
return {

packages/core/src/codewhispererChat/tools/toolUtils.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from './toolShared'
1717
import { ListDirectory, ListDirectoryParams } from './listDirectory'
1818
import { McpTool } from './mcp/mcpTool'
19-
import globals from '../../shared/extensionGlobals'
19+
import { McpManager } from './mcp/mcpManager'
2020

2121
export enum ToolType {
2222
FsRead = 'fsRead',
@@ -60,7 +60,7 @@ export class ToolUtils {
6060
case ToolType.ListDirectory:
6161
return tool.tool.requiresAcceptance()
6262
case ToolType.Mcp:
63-
return { requiresAcceptance: false }
63+
return { requiresAcceptance: true }
6464
}
6565
}
6666

@@ -170,8 +170,7 @@ export class ToolUtils {
170170
tool: new ListDirectory(value.input as unknown as ListDirectoryParams),
171171
}
172172
default: {
173-
const mcpMgr = globals.mcpManager
174-
const mcpToolDef = mcpMgr?.findTool(value.name as string)
173+
const mcpToolDef = McpManager.instance.findTool(value.name as string)
175174
if (mcpToolDef) {
176175
return {
177176
type: ToolType.Mcp,

packages/core/src/extension.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ import { setupUninstallHandler } from './shared/handleUninstall'
5555
import { maybeShowMinVscodeWarning } from './shared/extensionStartup'
5656
import { getLogger } from './shared/logger/logger'
5757
import { setContext } from './shared/vscode/setContext'
58-
import { McpManager } from './codewhispererChat/tools/mcp/mcpManager'
59-
import { globalMcpConfigPath } from './codewhispererChat/constants'
6058

6159
disableAwsSdkWarning()
6260

@@ -118,12 +116,6 @@ export async function activateCommon(
118116
)
119117
globals.regionProvider = RegionProvider.fromEndpointsProvider(makeEndpointsProvider())
120118

121-
/**
122-
* MCP client initialization
123-
*/
124-
// const mgr = await McpManager.initMcpManager(globalMcpConfigPath)
125-
// globals.mcpManager = mgr
126-
127119
// telemetry
128120
await activateTelemetry(context, globals.awsContext, Settings.instance, 'AWS Toolkit For VS Code')
129121

packages/core/src/shared/extensionGlobals.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,4 @@ export interface ToolkitGlobals {
228228
}
229229
/** If this extension is running in Web mode (the browser), compared to running on the desktop (node) */
230230
isWeb: boolean
231-
mcpManager?: McpManager
232231
}

0 commit comments

Comments
 (0)