-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathexample_client_test.go
More file actions
143 lines (128 loc) · 4.21 KB
/
example_client_test.go
File metadata and controls
143 lines (128 loc) · 4.21 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package acp
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// clientExample mirrors go/example/client in a compact form: prints
// streamed updates, handles simple file ops, and picks the first
// permission option.
type clientExample struct{}
var _ Client = (*clientExample)(nil)
func (clientExample) RequestPermission(ctx context.Context, p RequestPermissionRequest) (RequestPermissionResponse, error) {
if len(p.Options) == 0 {
return RequestPermissionResponse{
Outcome: RequestPermissionOutcome{
Cancelled: &RequestPermissionOutcomeCancelled{},
},
}, nil
}
return RequestPermissionResponse{
Outcome: RequestPermissionOutcome{
Selected: &RequestPermissionOutcomeSelected{OptionId: p.Options[0].OptionId},
},
}, nil
}
func (clientExample) SessionUpdate(ctx context.Context, n SessionNotification) error {
u := n.Update
switch {
case u.AgentMessageChunk != nil:
c := u.AgentMessageChunk.Content
if c.Text != nil {
fmt.Print(c.Text.Text)
}
case u.ToolCall != nil:
title := u.ToolCall.Title
fmt.Printf("\n[tool] %s (%s)\n", title, u.ToolCall.Status)
case u.ToolCallUpdate != nil:
fmt.Printf("\n[tool] %s -> %v\n", u.ToolCallUpdate.ToolCallId, u.ToolCallUpdate.Status)
}
return nil
}
func (clientExample) WriteTextFile(ctx context.Context, p WriteTextFileRequest) (WriteTextFileResponse, error) {
if !filepath.IsAbs(p.Path) {
return WriteTextFileResponse{}, fmt.Errorf("path must be absolute: %s", p.Path)
}
if dir := filepath.Dir(p.Path); dir != "" {
_ = os.MkdirAll(dir, 0o755)
}
return WriteTextFileResponse{}, os.WriteFile(p.Path, []byte(p.Content), 0o644)
}
func (clientExample) ReadTextFile(ctx context.Context, p ReadTextFileRequest) (ReadTextFileResponse, error) {
if !filepath.IsAbs(p.Path) {
return ReadTextFileResponse{}, fmt.Errorf("path must be absolute: %s", p.Path)
}
b, err := os.ReadFile(p.Path)
if err != nil {
return ReadTextFileResponse{}, err
}
content := string(b)
if p.Line != nil || p.Limit != nil {
lines := strings.Split(content, "\n")
start := 0
if p.Line != nil && *p.Line > 0 {
if *p.Line-1 > 0 {
start = *p.Line - 1
}
if start > len(lines) {
start = len(lines)
}
}
end := len(lines)
if p.Limit != nil && *p.Limit > 0 && start+*p.Limit < end {
end = start + *p.Limit
}
content = strings.Join(lines[start:end], "\n")
}
return ReadTextFileResponse{Content: content}, nil
}
// Terminal interface implementations (minimal stubs for examples)
func (clientExample) CreateTerminal(ctx context.Context, p CreateTerminalRequest) (CreateTerminalResponse, error) {
// Return a dummy terminal id
return CreateTerminalResponse{TerminalId: "t-1"}, nil
}
func (clientExample) KillTerminal(ctx context.Context, p KillTerminalRequest) (KillTerminalResponse, error) {
return KillTerminalResponse{}, nil
}
func (clientExample) ReleaseTerminal(ctx context.Context, p ReleaseTerminalRequest) (ReleaseTerminalResponse, error) {
return ReleaseTerminalResponse{}, nil
}
func (clientExample) TerminalOutput(ctx context.Context, p TerminalOutputRequest) (TerminalOutputResponse, error) {
// Provide non-empty output to satisfy validation
return TerminalOutputResponse{Output: "ok", Truncated: false}, nil
}
func (clientExample) WaitForTerminalExit(ctx context.Context, p WaitForTerminalExitRequest) (WaitForTerminalExitResponse, error) {
return WaitForTerminalExitResponse{}, nil
}
// Example_client launches the Go agent example, negotiates protocol,
// opens a session, and sends a simple prompt.
func Example_client() {
ctx := context.Background()
cmd := exec.Command("go", "run", "./example/agent")
stdin, _ := cmd.StdinPipe()
stdout, _ := cmd.StdoutPipe()
_ = cmd.Start()
conn := NewClientSideConnection(clientExample{}, stdin, stdout)
_, _ = conn.Initialize(ctx, InitializeRequest{
ProtocolVersion: ProtocolVersionNumber,
ClientCapabilities: ClientCapabilities{
Fs: FileSystemCapabilities{
ReadTextFile: true,
WriteTextFile: true,
},
Terminal: true,
},
})
sess, _ := conn.NewSession(ctx, NewSessionRequest{
Cwd: "/",
McpServers: []McpServer{},
})
_, _ = conn.Prompt(ctx, PromptRequest{
SessionId: sess.SessionId,
Prompt: []ContentBlock{TextBlock("Hello, agent!")},
})
_ = cmd.Process.Kill()
}