Repository navigation
Expand file tree
/
Copy pathstdio.go
More file actions
91 lines (83 loc) · 1.88 KB
/
Copy pathstdio.go
File metadata and controls
91 lines (83 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"context"
"errors"
"io"
"sync"
"github.com/modelcontextprotocol/go-sdk/jsonrpc"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
type stdioTransport struct {
mcp.IOTransport
mcp.Connection
eof, drained, closed chan struct{}
mu sync.Mutex
pending int
stopping bool
closeOnce sync.Once
}
func (t *stdioTransport) Connect(ctx context.Context) (mcp.Connection, error) {
var err error
t.Connection, err = t.IOTransport.Connect(ctx)
return t, err
}
func (t *stdioTransport) Read(ctx context.Context) (jsonrpc.Message, error) {
msg, err := t.Connection.Read(ctx)
if errors.Is(err, io.EOF) {
// The SDK cancels active requests on EOF. Delay it until they have
// written their responses, keeping stdout open during the drain.
close(t.eof)
select {
case <-t.closed:
case <-ctx.Done():
}
return msg, err
}
if err != nil {
return msg, err
}
t.mu.Lock()
if t.stopping {
t.mu.Unlock()
select {
case <-t.closed:
case <-ctx.Done():
}
return nil, io.EOF
}
if req, ok := msg.(*jsonrpc.Request); ok && req.IsCall() {
t.pending++
}
t.mu.Unlock()
return msg, nil
}
func (t *stdioTransport) Write(ctx context.Context, msg jsonrpc.Message) error {
err := t.Connection.Write(ctx, msg)
if _, ok := msg.(*jsonrpc.Response); ok {
t.mu.Lock()
t.pending--
if t.stopping && t.pending == 0 {
close(t.drained)
}
t.mu.Unlock()
}
return err
}
// drain stops accepting requests and waits for their responses. Session.Close
// cannot start sooner: the SDK drops responses once its session starts closing.
func (t *stdioTransport) drain() {
t.mu.Lock()
t.stopping = true
if t.pending == 0 {
close(t.drained)
}
t.mu.Unlock()
select {
case <-t.drained:
case <-t.closed:
}
}
func (t *stdioTransport) Close() error {
t.closeOnce.Do(func() { close(t.closed) })
return t.Connection.Close()
}