**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.
This commit is contained in:
parent
830322fef9
commit
c641467854
3 changed files with 85 additions and 4 deletions
|
|
@ -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}`);
|
||||
|
|
|
|||
41
src/index.ts
41
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue