|
| 1 | +import { debug } from './logger.js'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Check if MCP is running in cloud mode |
| 5 | + * Cloud mode is enabled by: |
| 6 | + * 1. Command line argument --cloud-mode |
| 7 | + * 2. Environment variable CLOUDBASE_MCP_CLOUD_MODE=true |
| 8 | + * 3. Environment variable MCP_CLOUD_MODE=true |
| 9 | + */ |
| 10 | +export function isCloudMode(): boolean { |
| 11 | + const cloudModeEnabled = process.env.CLOUDBASE_MCP_CLOUD_MODE === 'true' || |
| 12 | + process.env.MCP_CLOUD_MODE === 'true'; |
| 13 | + |
| 14 | + if (cloudModeEnabled) { |
| 15 | + debug('Cloud mode is enabled'); |
| 16 | + } |
| 17 | + |
| 18 | + return cloudModeEnabled; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Enable cloud mode by setting environment variable |
| 23 | + */ |
| 24 | +export function enableCloudMode(): void { |
| 25 | + process.env.CLOUDBASE_MCP_CLOUD_MODE = 'true'; |
| 26 | + debug('Cloud mode enabled via API call'); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Get cloud mode status for logging/debugging |
| 31 | + */ |
| 32 | +export function getCloudModeStatus(): { |
| 33 | + enabled: boolean; |
| 34 | + source: string | null; |
| 35 | +} { |
| 36 | + if (process.env.CLOUDBASE_MCP_CLOUD_MODE === 'true') { |
| 37 | + return { enabled: true, source: 'CLOUDBASE_MCP_CLOUD_MODE' }; |
| 38 | + } |
| 39 | + if (process.env.MCP_CLOUD_MODE === 'true') { |
| 40 | + return { enabled: true, source: 'MCP_CLOUD_MODE' }; |
| 41 | + } |
| 42 | + return { enabled: false, source: null }; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Check if a tool should be registered in cloud mode |
| 47 | + * @param toolName - The name of the tool |
| 48 | + * @returns true if the tool should be registered in current mode |
| 49 | + */ |
| 50 | +export function shouldRegisterTool(toolName: string): boolean { |
| 51 | + // If not in cloud mode, register all tools |
| 52 | + if (!isCloudMode()) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + // Cloud-incompatible tools that involve local file operations |
| 57 | + const cloudIncompatibleTools = [ |
| 58 | + // Storage tools - local file uploads |
| 59 | + 'uploadFile', |
| 60 | + |
| 61 | + // Hosting tools - local file uploads |
| 62 | + 'uploadFiles', |
| 63 | + |
| 64 | + // Function tools - local code uploads |
| 65 | + 'updateFunctionCode', |
| 66 | + 'createFunction', // also involves local files |
| 67 | + |
| 68 | + // Miniprogram tools - local code uploads |
| 69 | + 'uploadMiniprogramCode', |
| 70 | + |
| 71 | + // Download tools - local file downloads |
| 72 | + 'downloadTemplate', |
| 73 | + |
| 74 | + // Setup tools - local config file operations |
| 75 | + 'setupEnvironmentId', |
| 76 | + 'clearUserEnvId', |
| 77 | + |
| 78 | + // Interactive tools - local server and file operations |
| 79 | + 'interactiveDialog' |
| 80 | + ]; |
| 81 | + |
| 82 | + const shouldRegister = !cloudIncompatibleTools.includes(toolName); |
| 83 | + |
| 84 | + if (!shouldRegister) { |
| 85 | + debug(`Cloud mode: skipping registration of incompatible tool: ${toolName}`); |
| 86 | + } |
| 87 | + |
| 88 | + return shouldRegister; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Conditional tool registration wrapper |
| 93 | + * Only registers the tool if it's compatible with current mode |
| 94 | + */ |
| 95 | +export function conditionalRegisterTool( |
| 96 | + server: any, |
| 97 | + toolName: string, |
| 98 | + toolConfig: any, |
| 99 | + handler: any |
| 100 | +): void { |
| 101 | + if (shouldRegisterTool(toolName)) { |
| 102 | + server.registerTool?.(toolName, toolConfig, handler); |
| 103 | + } else { |
| 104 | + debug(`Skipped registering tool '${toolName}' in cloud mode`); |
| 105 | + } |
| 106 | +} |
0 commit comments