* Starts the server process and prepares to communicate with it.
()
| 110 | * Starts the server process and prepares to communicate with it. |
| 111 | */ |
| 112 | async start(): Promise<void> { |
| 113 | if (this._process) { |
| 114 | throw new Error( |
| 115 | 'StdioClientTransport already started! If using Client class, note that connect() calls start() automatically.' |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | return new Promise((resolve, reject) => { |
| 120 | this._process = spawn(this._serverParams.command, this._serverParams.args ?? [], { |
| 121 | // merge default env with server env because mcp server needs some env vars |
| 122 | env: { |
| 123 | ...getDefaultEnvironment(), |
| 124 | ...this._serverParams.env |
| 125 | }, |
| 126 | stdio: ['pipe', 'pipe', this._serverParams.stderr ?? 'inherit'], |
| 127 | shell: false, |
| 128 | windowsHide: process.platform === 'win32', |
| 129 | cwd: this._serverParams.cwd |
| 130 | }); |
| 131 | |
| 132 | this._process.on('error', error => { |
| 133 | reject(error); |
| 134 | this.onerror?.(error); |
| 135 | }); |
| 136 | |
| 137 | this._process.on('spawn', () => { |
| 138 | resolve(); |
| 139 | }); |
| 140 | |
| 141 | this._process.on('close', _code => { |
| 142 | this._process = undefined; |
| 143 | this.onclose?.(); |
| 144 | }); |
| 145 | |
| 146 | this._process.stdin?.on('error', error => { |
| 147 | this.onerror?.(error); |
| 148 | }); |
| 149 | |
| 150 | this._process.stdout?.on('data', chunk => { |
| 151 | this._readBuffer.append(chunk); |
| 152 | this.processReadBuffer(); |
| 153 | }); |
| 154 | |
| 155 | this._process.stdout?.on('error', error => { |
| 156 | this.onerror?.(error); |
| 157 | }); |
| 158 | |
| 159 | if (this._stderrStream && this._process.stderr) { |
| 160 | this._process.stderr.pipe(this._stderrStream); |
| 161 | } |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * The stderr stream of the child process, if `StdioServerParameters.stderr` was set to "pipe" or "overlapped". |
nothing calls this directly
no test coverage detected