UNPKG

tts-mcp

Version:

OpenAI Text to Speech APIを活用したコマンドラインツールとMCPサーバー

111 lines (110 loc) 3.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.startMcpServer = startMcpServer; exports.createMcpClient = createMcpClient; exports.cleanupFile = cleanupFile; const path_1 = __importDefault(require("path")); const fs_1 = require("fs"); const child_process_1 = require("child_process"); const index_js_1 = require("@modelcontextprotocol/sdk/client/index.js"); const stdio_js_1 = require("@modelcontextprotocol/sdk/client/stdio.js"); /** * MCPサーバーを起動する * @param {Object} options - 起動オプション * @param {string} options.apiKey - OpenAI APIキー * @param {string} options.voice - 使用する音声 * @param {string} options.model - 使用するモデル * @returns {Promise<ChildProcess>} - サーバープロセス */ async function startMcpServer(options = {}) { const serverPath = path_1.default.join(__dirname, '../../dist/bin/tts-mcp-server.js'); // コマンドライン引数を構築 const args = [serverPath]; if (options.voice) { args.push('--voice', options.voice); } if (options.model) { args.push('--model', options.model); } if (options.format) { args.push('--format', options.format); } // 環境変数を設定 const env = { ...process.env, OPENAI_API_KEY: options.apiKey || process.env.OPENAI_API_KEY || 'dummy_key_for_test' }; // サーバープロセスを起動 const serverProcess = (0, child_process_1.spawn)('node', args, { env }); // エラー処理 serverProcess.on('error', (error) => { console.error('サーバープロセスエラー:', error); }); // プロセスの標準エラー出力をキャプチャ(デバッグ用) serverProcess.stderr.on('data', (data) => { console.error('Server stderr:', data.toString()); }); // サーバーが起動するのを少し待つ await new Promise(resolve => setTimeout(resolve, 1000)); return serverProcess; } /** * MCPクライアントを作成して接続する * @param {Object} options - クライアントオプション * @param {string} options.apiKey - OpenAI APIキー * @param {string} options.voice - 使用する音声 * @param {string} options.model - 使用するモデル * @returns {Promise<Client>} - 接続済みMCPクライアント */ async function createMcpClient(options = {}) { const serverPath = path_1.default.join(__dirname, '../../dist/bin/tts-mcp-server.js'); // コマンドライン引数を構築 const args = [serverPath]; if (options.voice) { args.push('--voice', options.voice); } if (options.model) { args.push('--model', options.model); } if (options.format) { args.push('--format', options.format); } // 環境変数を設定 const env = { ...process.env, OPENAI_API_KEY: options.apiKey || process.env.OPENAI_API_KEY || 'dummy_key_for_test' }; // トランスポートを作成 const transport = new stdio_js_1.StdioClientTransport({ command: 'node', args, env }); // クライアントを作成 const client = new index_js_1.Client({ name: 'test-client', version: '1.0.0' }, { capabilities: { tools: {} } }); // 接続 try { await client.connect(transport); return client; } catch (error) { console.error('クライアント接続エラー:', error); throw error; } } /** * テスト終了後にファイルを削除する * @param {string} filePath - 削除するファイルパス */ async function cleanupFile(filePath) { try { await fs_1.promises.access(filePath); await fs_1.promises.unlink(filePath); } catch (error) { // ファイルが存在しない場合は何もしない } }