* Stdio 连接
(config: MCPConnectionConfig)
| 268 | * Stdio 连接 |
| 269 | */ |
| 270 | private async connectStdio(config: MCPConnectionConfig): Promise<any> { |
| 271 | const { spawn } = await import('child_process'); |
| 272 | |
| 273 | if (!config.command) { |
| 274 | throw new Error('Command is required for stdio transport'); |
| 275 | } |
| 276 | |
| 277 | const childProcess = spawn(config.command, config.args || [], { |
| 278 | stdio: ['pipe', 'pipe', 'pipe'], |
| 279 | env: { ...process.env, ...config.env }, |
| 280 | }); |
| 281 | |
| 282 | return new Promise((resolve, reject) => { |
| 283 | const timeout = setTimeout(() => { |
| 284 | reject(new Error('Process start timeout')); |
| 285 | }, config.timeout || 10000); |
| 286 | |
| 287 | childProcess.on('spawn', () => { |
| 288 | clearTimeout(timeout); |
| 289 | resolve(childProcess); |
| 290 | }); |
| 291 | |
| 292 | childProcess.on('error', (error: Error) => { |
| 293 | clearTimeout(timeout); |
| 294 | reject(error); |
| 295 | }); |
| 296 | |
| 297 | childProcess.stdout?.on('data', (data: Buffer) => { |
| 298 | this.handleMessage(data.toString()); |
| 299 | }); |
| 300 | }); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * 处理消息 |
no test coverage detected