Debian Codegraph @

debian/go/src/github.com/modelcontextprotocol/go-sdk/mcp/cmd.go

raw symbols SCIP
1 // Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2 // Use of this source code is governed by an MIT-style
3 // license that can be found in the LICENSE file.
4
5 package mcp
6
7 import (
8 "context"
9 "fmt"
10 "io"
11 "os/exec"
12 "syscall"
13 "time"
14 )
15
16 var defaultTerminateDuration = 5 * time.Second // mutable for testing
17
18 // A CommandTransport is a [Transport] that runs a command and communicates
19 // with it over stdin/stdout, using newline-delimited JSON.
20 type CommandTransport struct {
21 Command *exec.Cmd
22 // TerminateDuration controls how long Close waits after closing stdin
23 // for the process to exit before sending SIGTERM.
24 // If zero or negative, the default of 5s is used.
25 TerminateDuration time.Duration
26 }
27
28 // Connect starts the command, and connects to it over stdin/stdout.
29 func (t *CommandTransport) Connect(ctx context.Context) (Connection, error) {
30 stdout, err := t.Command.StdoutPipe()
31 if err != nil {
32 return nil, err
33 }
34 stdout = io.NopCloser(stdout) // close the connection by closing stdin, not stdout
35 stdin, err := t.Command.StdinPipe()
36 if err != nil {
37 return nil, err
38 }
39 if err := t.Command.Start(); err != nil {
40 return nil, err
41 }
42 td := t.TerminateDuration
43 if td <= 0 {
44 td = defaultTerminateDuration
45 }
46 return newIOConn(&pipeRWC{t.Command, stdout, stdin, td}), nil
47 }
48
49 // A pipeRWC is an io.ReadWriteCloser that communicates with a subprocess over
50 // stdin/stdout pipes.
51 type pipeRWC struct {
52 cmd *exec.Cmd
53 stdout io.ReadCloser
54 stdin io.WriteCloser
55 terminateDuration time.Duration
56 }
57
58 func (s *pipeRWC) Read(p []byte) (n int, err error) {
59 return s.stdout.Read(p)
60 }
61
62 func (s *pipeRWC) Write(p []byte) (n int, err error) {
63 return s.stdin.Write(p)
64 }
65
66 // Close closes the input stream to the child process, and awaits normal
67 // termination of the command. If the command does not exit, it is signalled to
68 // terminate, and then eventually killed.
69 func (s *pipeRWC) Close() error {
70 // Spec:
71 // "For the stdio transport, the client SHOULD initiate shutdown by:...
72
73 // "...First, closing the input stream to the child process (the server)"
74 if err := s.stdin.Close(); err != nil {
75 return fmt.Errorf("closing stdin: %v", err)
76 }
77 resChan := make(chan error, 1)
78 go func() {
79 resChan <- s.cmd.Wait()
80 }()
81 // "...Waiting for the server to exit, or sending SIGTERM if the server does not exit within a reasonable time"
82 wait := func() (error, bool) {
83 select {
84 case err := <-resChan:
85 return err, true
86 case <-time.After(s.terminateDuration):
87 }
88 return nil, false
89 }
90 if err, ok := wait(); ok {
91 return err
92 }
93 // Note the condition here: if sending SIGTERM fails, don't wait and just
94 // move on to SIGKILL.
95 if err := s.cmd.Process.Signal(syscall.SIGTERM); err == nil {
96 if err, ok := wait(); ok {
97 return err
98 }
99 }
100 // "...Sending SIGKILL if the server does not exit within a reasonable time after SIGTERM"
101 if err := s.cmd.Process.Kill(); err != nil {
102 return err
103 }
104 if err, ok := wait(); ok {
105 return err
106 }
107 return fmt.Errorf("unresponsive subprocess")
108 }