|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Roopik. All rights reserved. |
| 3 | + * Licensed under the MIT License. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +/** |
| 7 | + * MCP Commands |
| 8 | + * |
| 9 | + * Commands for controlling MCP (Model Context Protocol) integrations. |
| 10 | + * - roopik.mcp.toggle: Toggle MCP server on/off |
| 11 | + * - roopik.mcp.showStatus: Show MCP integration status |
| 12 | + * - roopik.mcp.enableAgent: Enable a specific agent integration |
| 13 | + * - roopik.mcp.disableAgent: Disable a specific agent integration |
| 14 | + */ |
| 15 | + |
| 16 | +import { localize, localize2 } from '../../../../../nls.js'; |
| 17 | +import { registerAction2, Action2 } from '../../../../../platform/actions/common/actions.js'; |
| 18 | +import { ServicesAccessor } from '../../../../../platform/instantiation/common/instantiation.js'; |
| 19 | +import { INotificationService, Severity } from '../../../../../platform/notification/common/notification.js'; |
| 20 | +import { IQuickInputService, IQuickPickItem } from '../../../../../platform/quickinput/common/quickInput.js'; |
| 21 | +import { IMcpServerService, AgentId, AgentStatus } from '../../common/mcp/index.js'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Agent display info for quick pick |
| 25 | + */ |
| 26 | +interface AgentQuickPickItem extends IQuickPickItem { |
| 27 | + agentId: AgentId; |
| 28 | + status: AgentStatus; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Register all MCP-related commands |
| 33 | + */ |
| 34 | +export function registerMcpCommands(): void { |
| 35 | + // Toggle MCP Server (master switch) |
| 36 | + registerAction2(class extends Action2 { |
| 37 | + constructor() { |
| 38 | + super({ |
| 39 | + id: 'roopik.mcp.toggle', |
| 40 | + title: localize2('roopik.mcp.toggle', 'Toggle MCP Server'), |
| 41 | + category: localize2('roopik.category', 'Roopik'), |
| 42 | + f1: true |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + async run(accessor: ServicesAccessor): Promise<void> { |
| 47 | + const notificationService = accessor.get(INotificationService); |
| 48 | + const mcpServerService = accessor.get(IMcpServerService); |
| 49 | + |
| 50 | + try { |
| 51 | + const isEnabled = await mcpServerService.isEnabled(); |
| 52 | + await mcpServerService.setEnabled(!isEnabled); |
| 53 | + |
| 54 | + if (!isEnabled) { |
| 55 | + notificationService.info('MCP Server enabled - AI agents can now connect'); |
| 56 | + } else { |
| 57 | + notificationService.info('MCP Server disabled - All agent connections closed'); |
| 58 | + } |
| 59 | + } catch (error) { |
| 60 | + notificationService.error(`Failed to toggle MCP Server: ${error}`); |
| 61 | + } |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + // Show MCP Status |
| 66 | + registerAction2(class extends Action2 { |
| 67 | + constructor() { |
| 68 | + super({ |
| 69 | + id: 'roopik.mcp.showStatus', |
| 70 | + title: localize2('roopik.mcp.showStatus', 'Show MCP Status'), |
| 71 | + category: localize2('roopik.category', 'Roopik'), |
| 72 | + f1: true |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + async run(accessor: ServicesAccessor): Promise<void> { |
| 77 | + const notificationService = accessor.get(INotificationService); |
| 78 | + const mcpServerService = accessor.get(IMcpServerService); |
| 79 | + |
| 80 | + try { |
| 81 | + const integrationStatus = await mcpServerService.getIntegrationStatus(); |
| 82 | + const { server, agents } = integrationStatus; |
| 83 | + |
| 84 | + // Build status message |
| 85 | + const serverStatus = server.running ? 'Running' : 'Stopped'; |
| 86 | + const registeredAgents = agents.filter(a => a.registered); |
| 87 | + const installedAgents = agents.filter(a => a.installed); |
| 88 | + |
| 89 | + let message = `MCP Server: ${serverStatus}`; |
| 90 | + if (server.running) { |
| 91 | + message += ` (HTTP: ${server.port}, WebSocket: ${server.wsPort})`; |
| 92 | + } |
| 93 | + message += `\n\nAgents: ${registeredAgents.length} registered, ${installedAgents.length} installed`; |
| 94 | + |
| 95 | + if (registeredAgents.length > 0) { |
| 96 | + message += '\n\nRegistered:'; |
| 97 | + for (const agent of registeredAgents) { |
| 98 | + message += `\n - ${agent.name}`; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + notificationService.notify({ |
| 103 | + severity: Severity.Info, |
| 104 | + message, |
| 105 | + sticky: true |
| 106 | + }); |
| 107 | + } catch (error) { |
| 108 | + notificationService.error(`Failed to get MCP status: ${error}`); |
| 109 | + } |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + // Enable Agent Integration |
| 114 | + registerAction2(class extends Action2 { |
| 115 | + constructor() { |
| 116 | + super({ |
| 117 | + id: 'roopik.mcp.enableAgent', |
| 118 | + title: localize2('roopik.mcp.enableAgent', 'Enable Agent Integration'), |
| 119 | + category: localize2('roopik.category', 'Roopik'), |
| 120 | + f1: true |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + async run(accessor: ServicesAccessor): Promise<void> { |
| 125 | + const quickInputService = accessor.get(IQuickInputService); |
| 126 | + const notificationService = accessor.get(INotificationService); |
| 127 | + const mcpServerService = accessor.get(IMcpServerService); |
| 128 | + |
| 129 | + try { |
| 130 | + const agents = await mcpServerService.getAgentStatus(); |
| 131 | + |
| 132 | + // Filter to agents that are installed but not registered |
| 133 | + const availableAgents = agents.filter(a => a.installed && !a.registered); |
| 134 | + |
| 135 | + if (availableAgents.length === 0) { |
| 136 | + notificationService.info('All installed agents are already enabled, or no agents are detected.'); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + // Build quick pick items |
| 141 | + const items: AgentQuickPickItem[] = availableAgents.map(agent => ({ |
| 142 | + label: agent.name, |
| 143 | + description: agent.installed ? 'Installed' : 'Not detected', |
| 144 | + agentId: agent.id, |
| 145 | + status: agent |
| 146 | + })); |
| 147 | + |
| 148 | + const selected = await quickInputService.pick(items, { |
| 149 | + placeHolder: localize('selectAgentToEnable', 'Select an agent to enable') |
| 150 | + }); |
| 151 | + |
| 152 | + if (selected) { |
| 153 | + await mcpServerService.enableAgent(selected.agentId); |
| 154 | + notificationService.info(`Enabled ${selected.label}`); |
| 155 | + } |
| 156 | + } catch (error) { |
| 157 | + notificationService.error(`Failed to enable agent: ${error}`); |
| 158 | + } |
| 159 | + } |
| 160 | + }); |
| 161 | + |
| 162 | + // Disable Agent Integration |
| 163 | + registerAction2(class extends Action2 { |
| 164 | + constructor() { |
| 165 | + super({ |
| 166 | + id: 'roopik.mcp.disableAgent', |
| 167 | + title: localize2('roopik.mcp.disableAgent', 'Disable Agent Integration'), |
| 168 | + category: localize2('roopik.category', 'Roopik'), |
| 169 | + f1: true |
| 170 | + }); |
| 171 | + } |
| 172 | + |
| 173 | + async run(accessor: ServicesAccessor): Promise<void> { |
| 174 | + const quickInputService = accessor.get(IQuickInputService); |
| 175 | + const notificationService = accessor.get(INotificationService); |
| 176 | + const mcpServerService = accessor.get(IMcpServerService); |
| 177 | + |
| 178 | + try { |
| 179 | + const agents = await mcpServerService.getAgentStatus(); |
| 180 | + |
| 181 | + // Filter to agents that are registered |
| 182 | + const registeredAgents = agents.filter(a => a.registered); |
| 183 | + |
| 184 | + if (registeredAgents.length === 0) { |
| 185 | + notificationService.info('No agents are currently enabled.'); |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + // Build quick pick items |
| 190 | + const items: AgentQuickPickItem[] = registeredAgents.map(agent => ({ |
| 191 | + label: agent.name, |
| 192 | + description: 'Registered', |
| 193 | + agentId: agent.id, |
| 194 | + status: agent |
| 195 | + })); |
| 196 | + |
| 197 | + const selected = await quickInputService.pick(items, { |
| 198 | + placeHolder: localize('selectAgentToDisable', 'Select an agent to disable') |
| 199 | + }); |
| 200 | + |
| 201 | + if (selected) { |
| 202 | + await mcpServerService.disableAgent(selected.agentId); |
| 203 | + notificationService.info(`Disabled ${selected.label}`); |
| 204 | + } |
| 205 | + } catch (error) { |
| 206 | + notificationService.error(`Failed to disable agent: ${error}`); |
| 207 | + } |
| 208 | + } |
| 209 | + }); |
| 210 | + |
| 211 | + // Sync Agent Registrations (re-sync with current settings) |
| 212 | + registerAction2(class extends Action2 { |
| 213 | + constructor() { |
| 214 | + super({ |
| 215 | + id: 'roopik.mcp.syncAgents', |
| 216 | + title: localize2('roopik.mcp.syncAgents', 'Sync Agent Registrations'), |
| 217 | + category: localize2('roopik.category', 'Roopik'), |
| 218 | + f1: true |
| 219 | + }); |
| 220 | + } |
| 221 | + |
| 222 | + async run(accessor: ServicesAccessor): Promise<void> { |
| 223 | + const notificationService = accessor.get(INotificationService); |
| 224 | + const mcpServerService = accessor.get(IMcpServerService); |
| 225 | + |
| 226 | + try { |
| 227 | + notificationService.info('Syncing agent registrations...'); |
| 228 | + await mcpServerService.syncAgentRegistrations(); |
| 229 | + |
| 230 | + const agents = await mcpServerService.getAgentStatus(); |
| 231 | + const registered = agents.filter(a => a.registered); |
| 232 | + |
| 233 | + notificationService.info(`Agent sync complete: ${registered.length} agents registered`); |
| 234 | + } catch (error) { |
| 235 | + notificationService.error(`Failed to sync agents: ${error}`); |
| 236 | + } |
| 237 | + } |
| 238 | + }); |
| 239 | +} |
0 commit comments