From c6414678544a63e2bf7595edb8e1f51e748093e6 Mon Sep 17 00:00:00 2001 From: David Snelling Date: Fri, 20 Jun 2025 10:57:31 -0700 Subject: [PATCH] **feat: add MCP service integration and enhance environment detection** ### Changes: - Introduced `initializeMCPService` in `cloud-wrapper/src/index.ts` to support initializing Model Control Protocol (MCP) services. - Enabled configuration for WebSocket and REST APIs with customizable options like ports, authentication, rate limiting, and CORS. - Exported MCP modules, types, and services (`BrainyMCPAdapter`, `BrainyMCPService`, etc.) in `src/index.ts`. - Enhanced `unified.ts` to include global environment detection by populating `globalThis.__ENV__`. - Added environment-specific logging for browser, Node.js, and serverless scenarios. - Improved the structure of `environment` object and its global accessibility. ### Purpose: Implemented MCP service integration to expand Brainy's external compatibility via WebSocket and REST interfaces. Enhanced environment detection provides better global access and debugging insights, ensuring robust operation across various runtime environments. --- cloud-wrapper/src/index.ts | 20 +++++++++++++++++++ src/index.ts | 41 ++++++++++++++++++++++++++++++++++++++ src/unified.ts | 28 ++++++++++++++++++++++---- 3 files changed, 85 insertions(+), 4 deletions(-) diff --git a/cloud-wrapper/src/index.ts b/cloud-wrapper/src/index.ts index d072e821..f820daff 100644 --- a/cloud-wrapper/src/index.ts +++ b/cloud-wrapper/src/index.ts @@ -9,6 +9,7 @@ import { BrainyData } from '@soulcraft/brainy'; import { setupRoutes } from './routes.js'; import { initializeBrainy } from './services/brainyService.js'; import { setupWebSocketHandlers } from './websocket.js'; +import { initializeMCPService } from './services/mcpService.js'; // Load environment variables dotenv.config(); @@ -56,6 +57,25 @@ async function startServer() { setupWebSocketHandlers(wss, brainyInstance); console.log('WebSocket server initialized'); + // Initialize MCP service + const mcpWsPort = process.env.MCP_WS_PORT ? parseInt(process.env.MCP_WS_PORT, 10) : undefined; + const mcpRestPort = process.env.MCP_REST_PORT ? parseInt(process.env.MCP_REST_PORT, 10) : undefined; + + if (mcpWsPort || mcpRestPort) { + initializeMCPService(brainyInstance, { + wsPort: mcpWsPort, + restPort: mcpRestPort, + enableAuth: process.env.MCP_ENABLE_AUTH === 'true', + apiKeys: process.env.MCP_API_KEYS ? process.env.MCP_API_KEYS.split(',') : undefined, + rateLimit: process.env.MCP_RATE_LIMIT_REQUESTS ? { + windowMs: parseInt(process.env.MCP_RATE_LIMIT_WINDOW_MS || '60000', 10), + maxRequests: parseInt(process.env.MCP_RATE_LIMIT_REQUESTS, 10) + } : undefined, + cors: process.env.MCP_ENABLE_CORS === 'true' ? {} : undefined + }); + console.log('MCP service initialized'); + } + // Start HTTP server server.listen(port, () => { console.log(`Server running on port ${port}`); diff --git a/src/index.ts b/src/index.ts index 5fc2c2a3..1adf052c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -261,3 +261,44 @@ export type { Content } export { NounType, VerbType } + +// Export MCP (Model Control Protocol) components +import { + BrainyMCPAdapter, + MCPAugmentationToolset, + BrainyMCPService +} from './mcp/index.js' // Import from mcp/index.js +import { + MCPRequest, + MCPResponse, + MCPDataAccessRequest, + MCPToolExecutionRequest, + MCPSystemInfoRequest, + MCPAuthenticationRequest, + MCPRequestType, + MCPServiceOptions, + MCPTool, + MCP_VERSION +} from './types/mcpTypes.js' + +export { + // MCP classes + BrainyMCPAdapter, + MCPAugmentationToolset, + BrainyMCPService, + + // MCP types + MCPRequestType, + MCP_VERSION +} + +export type { + MCPRequest, + MCPResponse, + MCPDataAccessRequest, + MCPToolExecutionRequest, + MCPSystemInfoRequest, + MCPAuthenticationRequest, + MCPServiceOptions, + MCPTool +} diff --git a/src/unified.ts b/src/unified.ts index 8c737095..a2dbd38f 100644 --- a/src/unified.ts +++ b/src/unified.ts @@ -1,16 +1,36 @@ /** * Unified entry point for Brainy * This file exports everything from index.ts - * Environment detection is handled by the build process + * Environment detection is handled here and made available to all components */ // Export environment information -// These values will be populated by the build process intro code export const environment = { isBrowser: typeof window !== 'undefined', - isNode: typeof process !== 'undefined' && process.versions && process.versions.node, - isServerless: typeof window === 'undefined' && (typeof process === 'undefined' || !process.versions || !process.versions.node) + isNode: + typeof process !== 'undefined' && process.versions && process.versions.node, + isServerless: + typeof window === 'undefined' && + (typeof process === 'undefined' || + !process.versions || + !process.versions.node) } +// Make environment information available globally +if (typeof globalThis !== 'undefined') { + globalThis.__ENV__ = environment +} + +// Log the detected environment +console.log( + `Brainy running in ${ + environment.isBrowser + ? 'browser' + : environment.isNode + ? 'Node.js' + : 'serverless/unknown' + } environment` +) + // Re-export everything from index.ts export * from './index.js'