-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathmain.go
More file actions
171 lines (143 loc) · 3.44 KB
/
main.go
File metadata and controls
171 lines (143 loc) · 3.44 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/mark3labs/mcp-go/client"
"github.com/mark3labs/mcp-go/mcp"
)
func main() {
args := os.Args[1:]
if len(args) == 0 {
usage()
return
}
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
switch args[0] {
case "list":
verbose := true
if err := list(ctx, verbose); err != nil {
log.Fatal(err)
}
case "count":
verbose := false
if err := list(ctx, verbose); err != nil {
log.Fatal(err)
}
case "call":
if err := call(ctx, args[1:]); err != nil {
log.Fatal(err)
}
default:
usage()
}
}
func usage() {
fmt.Println("Usage: client COMMAND [ARGS]")
fmt.Println()
fmt.Println("A command-line debug client for the MCP.")
fmt.Println()
fmt.Println("Commands:")
fmt.Println(" list List all available tools")
fmt.Println(" count Count all available tools")
fmt.Println(" call Call a specific tool with arguments")
}
func list(ctx context.Context, verbose bool) error {
c, err := start(ctx)
if err != nil {
return fmt.Errorf("starting client: %w", err)
}
defer c.Close()
response, err := c.ListTools(ctx, mcp.ListToolsRequest{})
if err != nil {
return fmt.Errorf("listing tools: %w", err)
}
buf, err := json.MarshalIndent(response.Tools, "", " ")
if err != nil {
return fmt.Errorf("marshalling tools: %w", err)
}
if verbose {
fmt.Println(len(response.Tools), "tools:")
fmt.Println(string(buf))
} else {
fmt.Println(len(response.Tools), "tools")
}
return nil
}
func call(ctx context.Context, args []string) error {
if len(args) == 0 {
return fmt.Errorf("no tool name provided")
}
toolName := args[0]
c, err := start(ctx)
if err != nil {
return fmt.Errorf("starting client: %w", err)
}
defer c.Close()
request := mcp.CallToolRequest{}
request.Params.Name = toolName
request.Params.Arguments = parseArgs(args[1:])
start := time.Now()
response, err := c.CallTool(ctx, request)
if err != nil {
return fmt.Errorf("calling tool: %w", err)
}
fmt.Println("Tool call took:", time.Since(start))
if response.IsError {
return fmt.Errorf("error calling tool: %s", toolName)
}
for _, content := range response.Content {
if textContent, ok := content.(mcp.TextContent); ok {
fmt.Println(textContent.Text)
} else {
fmt.Println(content)
}
}
return nil
}
func start(ctx context.Context) (*client.Client, error) {
host := os.Getenv("MCPGATEWAY_ENDPOINT")
c, err := client.NewSSEMCPClient("http://" + host + "/sse")
if err != nil {
return nil, err
}
if err := c.Start(ctx); err != nil {
return nil, err
}
initRequest := mcp.InitializeRequest{}
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
initRequest.Params.ClientInfo = mcp.Implementation{
Name: "docker",
Version: "1.0.0",
}
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()
if _, err := c.Initialize(ctx, initRequest); err != nil {
return nil, fmt.Errorf("initializing: %w", err)
}
return c, nil
}
func parseArgs(args []string) map[string]any {
parsed := map[string]any{}
for _, arg := range args {
parts := strings.SplitN(arg, "=", 2)
if len(parts) == 2 {
parsed[parts[0]] = parts[1]
} else {
parsed[arg] = nil
}
}
// MCP servers return an error if the args are empty so we make sure
// there is at least one argument
if len(parsed) == 0 {
parsed["args"] = "..."
}
return parsed
}