import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { HttpAppModule } from './app-http.module';
/**
* 트랜스포트 모드 결정
* - MCP_TRANSPORT=http → HTTP/SSE 서버 모드 (원격 접속용)
* - 기본값 → STDIO 모드 (로컬 VS Code용)
*/
const transportMode = process.env.MCP_TRANSPORT || 'stdio';
async function bootstrapStdio() {
// STDIO 서버는 로깅을 비활성화해야 함 (stdout/stderr 충돌 방지)
// McpModule이 STDIO transport로 설정되면 자동으로 stdin/stdout 통신을 처리함
await NestFactory.createApplicationContext(AppModule, {
logger: false,
});
// 개발 환경에서만 stderr로 시작 메시지 출력 (STDIO 프로토콜과 충돌하지 않음)
if (process.env.NODE_ENV !== 'production') {
console.error('[Logseq MCP] Server started successfully (STDIO mode)');
}
// STDIO 서버는 클라이언트가 종료할 때까지 프로세스를 유지
// McpModule 내부에서 프로세스 lifecycle을 관리함
}
async function bootstrapHttp() {
const port = parseInt(process.env.MCP_PORT || '3100', 10);
const host = process.env.MCP_HOST || '0.0.0.0';
const app = await NestFactory.create(HttpAppModule, {
logger: ['error', 'warn', 'log'],
});
// CORS 활성화 (원격 클라이언트 허용)
const allowedOrigins = process.env.MCP_CORS_ORIGINS?.split(',') || ['*'];
app.enableCors({
origin: allowedOrigins.includes('*') ? true : allowedOrigins,
credentials: true,
});
await app.listen(port, host);
console.log(`[Logseq MCP] HTTP Server started on http://${host}:${port}`);
console.log(` - SSE endpoint: http://${host}:${port}/sse`);
console.log(` - Messages endpoint: http://${host}:${port}/messages`);
console.log(` - Streamable HTTP endpoint: http://${host}:${port}/mcp`);
}
async function bootstrap() {
if (transportMode === 'http') {
await bootstrapHttp();
} else {
await bootstrapStdio();
}
}
void bootstrap();