src/vs/workbench/contrib/mcp/node/mcpStdioStateHandler.ts
101 LOC · 82 covered · 19 uncovered · 19 ranges · 5 concepts · 3 introducers · 9 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
mcpStdioStateHandler.ts ×8
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ChildProcessWithoutNullStreams } from 'child_process';
import { TimeoutTimer } from '../../../../base/common/async.js';
import { IDisposable } from '../../../../base/common/lifecycle.js';
import { killTree } from '../../../../base/node/processes.js';
import { isWindows } from '../../../../base/common/platform.js';
const enum McpProcessState {
Running,
StdinEnded,
KilledPolite,
KilledForceful,
}
/**
* Manages graceful shutdown of MCP stdio connections following the MCP specification.
*
* Per spec, shutdown should:
* 1. Close the input stream to the child process
* 2. Wait for the server to exit, or send SIGTERM if it doesn't exit within 10 seconds
* 3. Send SIGKILL if the server doesn't exit within 10 seconds after SIGTERM
* 4. Allow forceful killing if called twice
*/
export class McpStdioStateHandler implements IDisposable {
private static readonly GRACE_TIME_MS = 10_000;
private _procState = McpProcessState.Running;
private _nextTimeout?: IDisposable;
public get stopped() {
return this._procState !== McpProcessState.Running;
}
constructor(
private readonly _graceTimeMs: number = McpStdioStateHandler.GRACE_TIME_MS
) { }
/**
* Initiates graceful shutdown. If called while shutdown is already in progress,
* forces immediate termination.
*/
public stop(): void {
let graceTime = this._graceTimeMs;
try {
this._child.stdin.end();
} catch (error) {
// If stdin.end() fails, continue with termination sequence
// This can happen if the stream is already in an error state
graceTime = 1;
}
this._nextTimeout = new TimeoutTimer(() => this.killPolite(), graceTime);
} else {
this._nextTimeout?.dispose();
this.killForceful();
}
private async killPolite() {
this._nextTimeout = new TimeoutTimer(() => this.killForceful(), this._graceTimeMs);
if (this._child.pid) {
if (!isWindows) {
await killTree(this._child.pid, false).catch(() => {
this._child.kill('SIGTERM');
}
} else {
this._child.kill('SIGTERM');
}
private async killForceful() {
if (this._child.pid) {
await killTree(this._child.pid, true).catch(() => {
this._child.kill('SIGKILL');
} else {
this._child.kill();
}
public write(message: string): void {
if (!this.stopped) {
this._child.stdin.write(message + '\n');
}
}
public dispose() {
}