-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmain.go
More file actions
226 lines (197 loc) · 6.62 KB
/
Copy pathmain.go
File metadata and controls
226 lines (197 loc) · 6.62 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Tencent is pleased to support the open source community by making trpc-mcp-go available.
//
// Copyright (C) 2025 Tencent. All rights reserved.
//
// trpc-mcp-go is licensed under the Apache License Version 2.0.
package main
import (
"context"
"fmt"
"log"
"time"
mcp "trpc.group/trpc-go/trpc-mcp-go"
)
func main() {
// Set log level.
log.Printf("Starting Stateful JSON Yes GET SSE mode client...")
// Create context.
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
// Create client info.
clientInfo := mcp.Implementation{
Name: "Stateful-JSON-GETSSE-Client",
Version: "1.0.0",
}
// Create client, connect to server.
mcpClient, err := mcp.NewClient(
"http://localhost:3004/mcp",
clientInfo,
mcp.WithClientGetSSEEnabled(true), // Enable GET SSE
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer mcpClient.Close()
// Register notification handler.
mcpClient.RegisterNotificationHandler("notifications/message", handleNotification)
// Initialize client.
log.Printf("Initializing client...")
initResp, err := mcpClient.Initialize(ctx, &mcp.InitializeRequest{})
if err != nil {
log.Fatalf("Initialization failed: %v", err)
}
log.Printf("Initialization succeeded: Server=%s %s, Protocol=%s.",
initResp.ServerInfo.Name, initResp.ServerInfo.Version, initResp.ProtocolVersion)
log.Printf("Server capabilities: %+v.", initResp.Capabilities)
// Get session info.
sessionID := mcpClient.GetSessionID()
if sessionID == "" {
log.Printf("Warning: No session ID received. Server may not be properly configured for stateful mode.")
} else {
log.Printf("Session established, ID: %s.", sessionID)
}
// Get available tools list.
log.Printf("Listing tools...")
tools, err := mcpClient.ListTools(ctx, &mcp.ListToolsRequest{})
if err != nil {
log.Fatalf("Failed to get tools list: %v", err)
}
log.Printf("Server provides %d tools.", len(tools.Tools))
for _, tool := range tools.Tools {
log.Printf("- Tool: %s (%s).", tool.Name, tool.Description)
}
// Call greet tool.
log.Printf("Calling greet tool...")
callToolReq := mcp.CallToolRequest{}
callToolReq.Params.Name = "greet"
callToolReq.Params.Arguments = map[string]interface{}{"name": "JSON+GETSSE client user"}
callResult, err := mcpClient.CallTool(ctx, &callToolReq)
if err != nil {
log.Fatalf("Failed to call tool: %v", err)
}
// Show call result.
log.Printf("Call result:")
for _, content := range callResult.Content {
if textContent, ok := content.(mcp.TextContent); ok {
log.Printf("- Text: %s.", textContent.Text)
} else {
log.Printf("- Other type content: %+v.", content)
}
}
// Call counter tool, demonstrate session state keeping.
log.Printf("Calling counter tool first time...")
counterResult1, err := mcpClient.CallTool(ctx, &mcp.CallToolRequest{
Params: mcp.CallToolParams{
Name: "counter",
Arguments: map[string]interface{}{
"increment": 1,
},
Meta: &mcp.Meta{
ProgressToken: 123,
},
},
})
if err != nil {
log.Fatalf("Failed to call counter tool: %v", err)
}
// Show counter result.
log.Printf("Counter result (first time):")
for _, content := range counterResult1.Content {
if textContent, ok := content.(mcp.TextContent); ok {
log.Printf("- Text: %s.", textContent.Text)
}
}
// Call counter tool again, verify state keeping.
log.Printf("Calling counter tool second time...")
counterResult2, err := mcpClient.CallTool(ctx, &mcp.CallToolRequest{
Params: mcp.CallToolParams{
Name: "counter",
Arguments: map[string]interface{}{
"increment": 2,
},
},
})
if err != nil {
log.Fatalf("Failed to call counter tool: %v", err)
}
// Show counter result.
log.Printf("Counter result (second time):")
for _, content := range counterResult2.Content {
if textContent, ok := content.(mcp.TextContent); ok {
log.Printf("- Text: %s.", textContent.Text)
}
}
// Call notification tool, demonstrate server push notification feature.
log.Printf("Calling sendNotification tool, you will receive a notification after a delay...")
notifyResult, err := mcpClient.CallTool(ctx, &mcp.CallToolRequest{
Params: mcp.CallToolParams{
Name: "sendNotification",
Arguments: map[string]interface{}{
"message": "This is a test notification message sent from JSON+GETSSE client",
"delay": 2,
},
},
})
if err != nil {
log.Fatalf("Failed to call notification tool: %v", err)
}
// Show call result.
log.Printf("Notification tool call result:")
for _, content := range notifyResult.Content {
if textContent, ok := content.(mcp.TextContent); ok {
log.Printf("- Text: %s.", textContent.Text)
}
}
// Print session info.
log.Printf("\nSession info: ID=%s.", mcpClient.GetSessionID())
log.Printf("Client has enabled GET SSE connection, waiting for notifications...")
log.Printf("Tip: you can use curl http://localhost:3004/sessions to check server session state.")
// Wait a while to receive possible notifications.
log.Printf("Waiting to receive notifications (60 seconds)...")
time.Sleep(60 * time.Second)
log.Printf("Client example finished, exiting soon...")
}
// handler function for server notifications.
func handleNotification(notification *mcp.JSONRPCNotification) error {
paramsMap := notification.Params.AdditionalFields // Use AdditionalFields here.
level, _ := paramsMap["level"].(string)
dataMap, ok := paramsMap["data"].(map[string]interface{})
if !ok {
log.Printf(
"Received notification [%s] (Level: %s), but 'data' field is invalid or missing: %+v.",
notification.Method, level, paramsMap,
)
return fmt.Errorf("'data' field is invalid or missing")
}
notificationType, typeOk := dataMap["type"].(string)
if !typeOk {
log.Printf(
"Received notification [%s] (Level: %s), but 'type' field in data is invalid or missing: %+v",
notification.Method, level, dataMap,
)
return fmt.Errorf("'type' field in notification data is invalid or missing")
}
log.Printf(
"Received notification [%s] (Level: %s, Type: %s): %+v.",
notification.Method, level, notificationType, dataMap,
)
// Handle specific notification types.
switch notificationType {
case "test_notification":
// Handle test notification.
if message, exists := dataMap["message"].(string); exists {
log.Printf(" Test notification content: %s.", message)
}
case "system_status":
// Handle system status notification.
if cpu, exists := dataMap["cpu"].(string); exists {
log.Printf(" System status - CPU: %s.", cpu)
}
if memory, exists := dataMap["memory"].(string); exists {
log.Printf(" System status - Memory: %s.", memory)
}
default:
log.Printf(" Unknown notification data type: %s.", notificationType)
}
return nil
}