|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | +) |
| 8 | + |
| 9 | +type McpClient struct { |
| 10 | + Transport Transport |
| 11 | +} |
| 12 | + |
| 13 | +func NewMcpClient(t Transport) *McpClient { |
| 14 | + return &McpClient{Transport: t} |
| 15 | +} |
| 16 | + |
| 17 | +func ConnectStdio(command string, args []string, env map[string]string) (*McpClient, error) { |
| 18 | + transport, err := NewStdioTransport(command, args, env) |
| 19 | + if err != nil { |
| 20 | + return nil, err |
| 21 | + } |
| 22 | + return &McpClient{Transport: transport}, nil |
| 23 | +} |
| 24 | + |
| 25 | +func ConnectHTTP(url string, headers map[string]string) *McpClient { |
| 26 | + transport := NewHTTPTransport(url, headers) |
| 27 | + return &McpClient{Transport: transport} |
| 28 | +} |
| 29 | + |
| 30 | +func (c *McpClient) ListTools(ctx context.Context) ([]Tool, error) { |
| 31 | + req := JsonRpcRequest{ |
| 32 | + JSONRPC: "2.0", |
| 33 | + Method: "tools/list", |
| 34 | + Params: nil, |
| 35 | + ID: 1, |
| 36 | + } |
| 37 | + |
| 38 | + resp, err := c.Transport.Send(req) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + var result struct { |
| 44 | + Tools []Tool `json:"tools"` |
| 45 | + } |
| 46 | + if resp.Result != nil { |
| 47 | + if err := json.Unmarshal(resp.Result, &result); err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + } |
| 51 | + return result.Tools, nil |
| 52 | +} |
| 53 | + |
| 54 | +func (c *McpClient) CallTool(ctx context.Context, name string, args map[string]string) (string, error) { |
| 55 | + paramsRaw, _ := json.Marshal(map[string]interface{}{ |
| 56 | + "name": name, |
| 57 | + "arguments": args, |
| 58 | + }) |
| 59 | + |
| 60 | + req := JsonRpcRequest{ |
| 61 | + JSONRPC: "2.0", |
| 62 | + Method: "tools/call", |
| 63 | + Params: paramsRaw, |
| 64 | + ID: 2, |
| 65 | + } |
| 66 | + |
| 67 | + resp, err := c.Transport.Send(req) |
| 68 | + if err != nil { |
| 69 | + return "", err |
| 70 | + } |
| 71 | + |
| 72 | + if resp.Error != nil { |
| 73 | + return "", fmt.Errorf("MCP error: %s", resp.Error.Message) |
| 74 | + } |
| 75 | + |
| 76 | + var result struct { |
| 77 | + Content []struct { |
| 78 | + Text string `json:"text"` |
| 79 | + } `json:"content"` |
| 80 | + } |
| 81 | + if resp.Result != nil { |
| 82 | + if err := json.Unmarshal(resp.Result, &result); err != nil { |
| 83 | + return "", err |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + text := "" |
| 88 | + for _, c := range result.Content { |
| 89 | + text += c.Text |
| 90 | + } |
| 91 | + return text, nil |
| 92 | +} |
| 93 | + |
| 94 | +func RunMCPCommand(ctx context.Context, cmd string, args []string) ([]Tool, error) { |
| 95 | + parts := append([]string{cmd}, args...) |
| 96 | + command := parts[0] |
| 97 | + commandArgs := parts[1:] |
| 98 | + |
| 99 | + transport, err := NewStdioTransport(command, commandArgs, nil) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + client := &McpClient{Transport: transport} |
| 105 | + return client.ListTools(ctx) |
| 106 | +} |
0 commit comments