Commit f5f576b
Handle shutdown signals gracefully in MCP server (#4227)
## Summary
- Add signal handling for SIGINT and SIGTERM in MCP server
- Call `Shutdown()` method when signals are received
- Exit cleanly with code 0 instead of crashing
## Problem
When Claude Agent SDK terminates the MCP server process, the Go process
doesn't handle signals gracefully. This causes the process to crash
during cleanup, resulting in "Command failed with exit code 1" error
that propagates back to the SDK.
This is a known issue pattern documented in:
- anthropics/claude-code#7718 (SIGABRT during
MCP shutdown)
- anthropics/claude-code#5506 (MCP Server
Shutdown Failure)
## Solution
Run the server in a goroutine and wait for either:
1. Server completion (EOF, error, or context cancellation)
2. Shutdown signal (SIGINT or SIGTERM)
When a signal is received, cancel the context and call `Shutdown()` to
clean up resources gracefully.
## Test plan
- [x] Build succeeds
- [ ] Manual test with Claude Agent SDK - verify no "exit code 1" errors
on session end
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>1 parent d35b0e3 commit f5f576b
1 file changed
+35
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
4 | 9 | | |
5 | 10 | | |
6 | 11 | | |
| |||
25 | 30 | | |
26 | 31 | | |
27 | 32 | | |
28 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
29 | 40 | | |
30 | 41 | | |
31 | 42 | | |
| |||
41 | 52 | | |
42 | 53 | | |
43 | 54 | | |
44 | | - | |
45 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
46 | 78 | | |
47 | 79 | | |
48 | 80 | | |
| |||
0 commit comments