-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathutil.go
More file actions
135 lines (119 loc) · 3.37 KB
/
util.go
File metadata and controls
135 lines (119 loc) · 3.37 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
package xtragpt
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
type InitializeRequest struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params struct {
ProtocolVersion string `json:"protocolVersion"`
Capabilities map[string]interface{} `json:"capabilities"`
ClientInfo struct {
Name string `json:"name"`
Version string `json:"version"`
} `json:"clientInfo"`
} `json:"params"`
ID int `json:"id"`
}
type NotificationRequest struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params map[string]interface{} `json:"params"`
}
type ToolsRequest struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
ID int `json:"id"`
}
func MCPNotificationsInitialized(url string, sessionId string) {
notifyReq := NotificationRequest{
JSONRPC: "2.0",
Method: "notifications/initialized",
Params: make(map[string]interface{}),
}
// Marshal notification to JSON
notifyJSON, err := json.Marshal(notifyReq)
if err != nil {
fmt.Printf("Error marshaling notification JSON: %v\n", err)
return
}
// Create HTTP client and request for notification
client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(notifyJSON))
if err != nil {
fmt.Printf("Error creating request: %v\n", err)
return
}
// Set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json, text/event-stream")
req.Header.Set("mcp-session-id", sessionId)
// Make the notification request
notifyResp, err := client.Do(req)
if err != nil {
fmt.Printf("Error making notification request: %v\n", err)
return
}
defer notifyResp.Body.Close()
}
func MCPInitialize(url string) (string, error) {
initReq := InitializeRequest{
JSONRPC: "2.0",
Method: "initialize",
ID: 1,
}
initReq.Params.ProtocolVersion = "2024-11-05"
initReq.Params.Capabilities = make(map[string]interface{})
initReq.Params.ClientInfo.Name = "test-client"
initReq.Params.ClientInfo.Version = "1.0.0"
// Marshal to JSON
jsonData, err := json.Marshal(initReq)
if err != nil {
fmt.Printf("Error marshaling JSON: %v\n", err)
return "", err
}
// Make the first request
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("Error making request: %v\n", err)
return "", err
}
defer resp.Body.Close()
fmt.Println("Initialize response", resp.Body, resp.Header)
// Get session ID from headers
sessionID := resp.Header.Get("mcp-session-id")
fmt.Printf("Session ID: %s\n", sessionID)
MCPNotificationsInitialized(url, sessionID)
fmt.Println("Initialized")
return sessionID, nil
}
func MCPListTools(url string) ([]string, error) {
toolsReq := ToolsRequest{
JSONRPC: "2.0",
Method: "tools/list",
ID: 1,
}
jsonData, err := json.Marshal(toolsReq)
if err != nil {
fmt.Printf("Error marshaling JSON: %v\n", err)
return nil, err
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("Error making request: %v\n", err)
return nil, err
}
defer resp.Body.Close()
fmt.Println("List tools response", resp.Body, resp.Header)
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response: %v\n", err)
return nil, err
}
fmt.Println("List tools response", string(body))
return nil, nil
}