@solangii/upbit-mcp-server
Version:
Upbit 암호화폐 거래소 API와 연동된 MCP 서버
115 lines (98 loc) • 2.9 kB
JavaScript
/**
* @solangii/upbit-mcp-server
*
* Upbit 암호화폐 거래소 API와 연동된 MCP 서버
*
* 이 모듈은 Python 기반 Upbit MCP 서버를 직접 실행합니다.
* 주로 bin/upbit-mcp-server.js를 통해 CLI로 실행됩니다.
*/
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
const dotenv = require('dotenv');
// 기본 설정 로드
dotenv.config();
/**
* Python 종속성 설치 함수
* @returns {Promise<void>}
*/
async function installDependencies() {
process.stderr.write('Python 패키지 설치 중...\n');
try {
const pip = spawn('pip', ['install', '-r', path.join(__dirname, 'requirements.txt')], {
stdio: 'inherit',
shell: true
});
return new Promise((resolve, reject) => {
pip.on('close', (code) => {
if (code === 0) {
process.stderr.write('패키지 설치 완료\n');
resolve();
} else {
process.stderr.write('패키지 설치 실패\n');
reject(new Error('패키지 설치 실패'));
}
});
});
} catch (error) {
process.stderr.write(`패키지 설치 중 오류: ${error.message}\n`);
throw error;
}
}
/**
* MCP 서버 실행
* @returns {ChildProcess} 생성된 프로세스
*/
function startServer() {
process.stderr.write('업비트 MCP 서버 시작 중...\n');
const pythonPath = path.join(__dirname, 'main.py');
if (!fs.existsSync(pythonPath)) {
throw new Error(`메인 스크립트를 찾을 수 없습니다: ${pythonPath}`);
}
// Python 프로세스 시작, JSON-RPC 포맷의 stdio로 통신
const pythonProcess = spawn('python', [pythonPath], {
stdio: ['pipe', 'pipe', process.stderr],
shell: true,
env: process.env
});
// 에러 핸들링
pythonProcess.on('error', (error) => {
process.stderr.write(`서버 시작 오류: ${error.message}\n`);
throw error;
});
return pythonProcess;
}
// CLI에서 직접 실행 시 메인 함수
async function main() {
try {
await installDependencies();
const pythonProcess = startServer();
// 표준 입출력 연결
process.stdin.pipe(pythonProcess.stdin);
pythonProcess.stdout.pipe(process.stdout);
// 종료 시그널 처리
process.on('SIGINT', () => {
pythonProcess.kill('SIGINT');
process.exit(0);
});
// 프로세스 종료 처리
pythonProcess.on('close', (code) => {
if (code !== 0) {
process.stderr.write(`서버가 코드 ${code}로 종료되었습니다\n`);
process.exit(code);
}
});
} catch (error) {
process.stderr.write(`오류 발생: ${error.message}\n`);
process.exit(1);
}
}
// CLI에서 직접 실행된 경우
if (require.main === module) {
main();
}
// 모듈로 export
module.exports = {
installDependencies,
startServer
};