forked from mark3labs/mcp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsse_test.go
234 lines (195 loc) · 6.11 KB
/
sse_test.go
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
227
228
229
230
231
232
233
234
package client
import (
"context"
"testing"
"time"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
func TestSSEMCPClient(t *testing.T) {
// Create MCP server with capabilities
mcpServer := server.NewMCPServer(
"test-server",
"1.0.0",
server.WithResourceCapabilities(true, true),
server.WithPromptCapabilities(true),
server.WithToolCapabilities(true),
)
// Add a test tool
mcpServer.AddTool(mcp.NewTool(
"test-tool",
mcp.WithDescription("Test tool"),
mcp.WithString("parameter-1", mcp.Description("A string tool parameter")),
), func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: "Input parameter: " + request.Params.Arguments["parameter-1"].(string),
},
},
}, nil
})
// Initialize
testServer := server.NewTestServer(mcpServer)
defer testServer.Close()
t.Run("Can create client", func(t *testing.T) {
client, err := NewSSEMCPClient(testServer.URL + "/sse")
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
defer client.Close()
if client.baseURL == nil {
t.Error("Base URL should not be nil")
}
})
t.Run("Can initialize and make requests", func(t *testing.T) {
client, err := NewSSEMCPClient(testServer.URL + "/sse")
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Start the client
if err := client.Start(ctx); err != nil {
t.Fatalf("Failed to start client: %v", err)
}
// Initialize
initRequest := mcp.InitializeRequest{}
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
initRequest.Params.ClientInfo = mcp.Implementation{
Name: "test-client",
Version: "1.0.0",
}
result, err := client.Initialize(ctx, initRequest)
if err != nil {
t.Fatalf("Failed to initialize: %v", err)
}
if result.ServerInfo.Name != "test-server" {
t.Errorf(
"Expected server name 'test-server', got '%s'",
result.ServerInfo.Name,
)
}
// Test Ping
if err := client.Ping(ctx); err != nil {
t.Errorf("Ping failed: %v", err)
}
// Test ListTools
toolsRequest := mcp.ListToolsRequest{}
_, err = client.ListTools(ctx, toolsRequest)
if err != nil {
t.Errorf("ListTools failed: %v", err)
}
})
// t.Run("Can handle notifications", func(t *testing.T) {
// client, err := NewSSEMCPClient(testServer.URL + "/sse")
// if err != nil {
// t.Fatalf("Failed to create client: %v", err)
// }
// defer client.Close()
// notificationReceived := make(chan mcp.JSONRPCNotification, 1)
// client.OnNotification(func(notification mcp.JSONRPCNotification) {
// notificationReceived <- notification
// })
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
// if err := client.Start(ctx); err != nil {
// t.Fatalf("Failed to start client: %v", err)
// }
// // Initialize first
// initRequest := mcp.InitializeRequest{}
// initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
// initRequest.Params.ClientInfo = mcp.Implementation{
// Name: "test-client",
// Version: "1.0.0",
// }
// _, err = client.Initialize(ctx, initRequest)
// if err != nil {
// t.Fatalf("Failed to initialize: %v", err)
// }
// // Subscribe to a resource to test notifications
// subRequest := mcp.SubscribeRequest{}
// subRequest.Params.URI = "test://resource"
// if err := client.Subscribe(ctx, subRequest); err != nil {
// t.Fatalf("Failed to subscribe: %v", err)
// }
// select {
// case <-notificationReceived:
// // Success
// case <-time.After(time.Second):
// t.Error("Timeout waiting for notification")
// }
// })
t.Run("Handles errors properly", func(t *testing.T) {
client, err := NewSSEMCPClient(testServer.URL + "/sse")
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := client.Start(ctx); err != nil {
t.Fatalf("Failed to start client: %v", err)
}
// Try to make a request without initializing
toolsRequest := mcp.ListToolsRequest{}
_, err = client.ListTools(ctx, toolsRequest)
if err == nil {
t.Error("Expected error when making request before initialization")
}
})
// t.Run("Handles context cancellation", func(t *testing.T) {
// client, err := NewSSEMCPClient(testServer.URL + "/sse")
// if err != nil {
// t.Fatalf("Failed to create client: %v", err)
// }
// defer client.Close()
// if err := client.Start(context.Background()); err != nil {
// t.Fatalf("Failed to start client: %v", err)
// }
// ctx, cancel := context.WithCancel(context.Background())
// cancel() // Cancel immediately
// toolsRequest := mcp.ListToolsRequest{}
// _, err = client.ListTools(ctx, toolsRequest)
// if err == nil {
// t.Error("Expected error when context is cancelled")
// }
// })
t.Run("CallTool", func(t *testing.T) {
client, err := NewSSEMCPClient(testServer.URL + "/sse")
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := client.Start(ctx); err != nil {
t.Fatalf("Failed to start client: %v", err)
}
// Initialize
initRequest := mcp.InitializeRequest{}
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
initRequest.Params.ClientInfo = mcp.Implementation{
Name: "test-client",
Version: "1.0.0",
}
_, err = client.Initialize(ctx, initRequest)
if err != nil {
t.Fatalf("Failed to initialize: %v", err)
}
request := mcp.CallToolRequest{}
request.Params.Name = "test-tool"
request.Params.Arguments = map[string]interface{}{
"parameter-1": "value1",
}
result, err := client.CallTool(ctx, request)
if err != nil {
t.Fatalf("CallTool failed: %v", err)
}
if len(result.Content) != 1 {
t.Errorf("Expected 1 content item, got %d", len(result.Content))
}
})
}