vibe-coder-mcp
Version:
Production-ready MCP server with complete agent integration, multi-transport support, and comprehensive development automation tools for AI-assisted workflows.
161 lines (160 loc) • 5.66 kB
JavaScript
import { transportManager } from './transport-manager/index.js';
import logger from '../logger.js';
const DEFAULT_TRANSPORT_CONFIG = {
websocket: {
enabled: true,
port: 8080,
path: '/agent-ws'
},
http: {
enabled: true,
port: 3001,
cors: true
},
sse: {
enabled: true
},
stdio: {
enabled: true
}
};
export class TransportCoordinator {
static instance;
static isInitializing = false;
initializationPromise;
isInitialized = false;
config;
static getInstance() {
if (TransportCoordinator.isInitializing) {
logger.warn('Circular initialization detected in TransportCoordinator, using safe fallback');
return TransportCoordinator.createSafeFallback();
}
if (!TransportCoordinator.instance) {
TransportCoordinator.isInitializing = true;
try {
TransportCoordinator.instance = new TransportCoordinator();
}
finally {
TransportCoordinator.isInitializing = false;
}
}
return TransportCoordinator.instance;
}
static createSafeFallback() {
const fallback = Object.create(TransportCoordinator.prototype);
fallback.config = { ...DEFAULT_TRANSPORT_CONFIG };
fallback.isInitialized = false;
fallback.initializationPromise = undefined;
fallback.ensureTransportsStarted = async () => {
logger.warn('TransportCoordinator fallback: ensureTransportsStarted called during initialization');
};
return fallback;
}
constructor() {
this.config = { ...DEFAULT_TRANSPORT_CONFIG };
}
configure(config) {
this.config = {
...this.config,
...config,
websocket: { ...this.config.websocket, ...config.websocket },
http: { ...this.config.http, ...config.http },
sse: { ...this.config.sse, ...config.sse },
stdio: { ...this.config.stdio, ...config.stdio }
};
logger.debug({ config: this.config }, 'Transport coordinator configured');
}
async ensureTransportsStarted() {
if (this.isInitialized) {
logger.debug('Transport services already initialized');
return;
}
if (this.initializationPromise) {
logger.debug('Transport initialization in progress, waiting...');
await this.initializationPromise;
return;
}
this.initializationPromise = this.initializeTransports();
try {
await this.initializationPromise;
this.isInitialized = true;
logger.info('Transport services initialization completed');
}
catch (error) {
logger.error('Transport services initialization failed:', error);
throw error;
}
finally {
this.initializationPromise = undefined;
}
}
async initializeTransports() {
logger.info('Initializing transport services through coordinator...');
const status = transportManager.getStatus();
if (status.isStarted) {
logger.debug('Transport manager already started');
return;
}
if (status.startupInProgress) {
logger.debug('Transport manager startup in progress, waiting...');
await transportManager.startAll();
return;
}
logger.debug('Configuring transport manager...');
transportManager.configure({
websocket: {
enabled: this.config.websocket.enabled,
port: this.config.websocket.port,
path: this.config.websocket.path
},
http: {
enabled: this.config.http.enabled,
port: this.config.http.port,
cors: this.config.http.cors
},
sse: {
enabled: this.config.sse.enabled
},
stdio: {
enabled: this.config.stdio.enabled
}
});
logger.debug('Starting transport services...');
await transportManager.startAll();
logger.info('Transport services started successfully through coordinator');
}
getStatus() {
return {
isInitialized: this.isInitialized,
initializationInProgress: !!this.initializationPromise,
transportManagerStatus: transportManager.getStatus()
};
}
getAllocatedPorts() {
return transportManager.getAllocatedPorts();
}
getTransportEndpoints() {
const allocatedPorts = this.getAllocatedPorts();
const endpoints = {};
if (this.config.websocket.enabled && allocatedPorts.websocket !== undefined) {
endpoints.websocket = `ws://localhost:${allocatedPorts.websocket}${this.config.websocket.path}`;
}
if (this.config.http.enabled && allocatedPorts.http !== undefined) {
endpoints.http = `http://localhost:${allocatedPorts.http}`;
}
if (this.config.sse.enabled) {
endpoints.sse = 'Integrated with MCP server';
}
if (this.config.stdio.enabled) {
endpoints.stdio = 'stdio://mcp-server';
}
return endpoints;
}
reset() {
this.isInitialized = false;
this.initializationPromise = undefined;
this.config = { ...DEFAULT_TRANSPORT_CONFIG };
logger.debug('Transport coordinator reset');
}
}
export const transportCoordinator = TransportCoordinator.getInstance();