Skip to content

Implement MCP stdio shutdown spec compliance with graceful shutdown manager #250207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 13, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented May 31, 2025

The MCP stdio transport shutdown implementation was not following the MCP specification. Previously, it used AbortController.abort() to immediately terminate processes, but the spec requires a graceful shutdown sequence.

Changes Made

Replaced the simple abort controller approach with a new McpStdioConnectionManager class that implements the exact shutdown sequence specified in the MCP specification:

  1. Close input stream: Calls stdin.end() to gracefully close communication with the server
  2. Wait for natural exit: Gives the process 2 seconds to exit cleanly
  3. Send SIGTERM: If still running, sends SIGTERM and waits another 2 seconds
  4. Send SIGKILL: If still unresponsive, forces termination with SIGKILL
  5. Force kill on double call: If $stopMcp called twice, immediately sends SIGKILL

Key Features

  • Platform-aware termination: Uses taskkill on Windows, process.kill on POSIX systems
  • Memory efficient: Proper timeout cleanup prevents memory leaks
  • Error resistant: Handles edge cases like stdin stream errors gracefully
  • Non-breaking: Maintains all existing functionality and interfaces
  • Spec compliant: Follows MCP specification requirements exactly

Implementation Details

The new McpStdioConnectionManager class:

  • Manages the graceful shutdown state machine
  • Uses 2-second grace periods as specified
  • Handles platform-specific process termination
  • Provides proper cleanup and error handling
  • Integrates seamlessly with existing MCP infrastructure

Before:

nodeServer.abortCtrl.abort(); // Immediate termination

After:

nodeServer.connectionManager.stop(); // Graceful shutdown sequence

This ensures MCP servers have proper opportunity to clean up resources and exit gracefully, improving reliability and following the established protocol specification.

Fixes #250206.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits May 31, 2025 23:59
…anager

Co-authored-by: connor4312 <2230985+connor4312@users.noreply.github.com>
Co-authored-by: connor4312 <2230985+connor4312@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] MCP stdio shutdown does not follow spec Implement MCP stdio shutdown spec compliance with graceful shutdown manager Jun 1, 2025
@Copilot Copilot AI requested a review from connor4312 June 1, 2025 00:03
Copilot finished work on behalf of connor4312 June 1, 2025 00:04
@connor4312 connor4312 marked this pull request as ready for review June 12, 2025 21:12
@connor4312 connor4312 enabled auto-merge (squash) June 12, 2025 21:12
@vs-code-engineering vs-code-engineering bot added this to the June 2025 milestone Jun 12, 2025
@connor4312 connor4312 merged commit 7e4e0f4 into main Jun 13, 2025
8 checks passed
@connor4312 connor4312 deleted the copilot/fix-250206 branch June 13, 2025 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Error loading sessions

Retrying...

Successfully merging this pull request may close these issues.

MCP stdio shutdown does not follow spec
4 participants