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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
package integration
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
// JSONRPCRequest represents a JSON-RPC 2.0 request
type JSONRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
}
// JSONRPCResponse represents a JSON-RPC 2.0 response
type JSONRPCResponse struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Result json.RawMessage `json:"result,omitempty"`
Error *JSONRPCError `json:"error,omitempty"`
}
// JSONRPCError represents a JSON-RPC 2.0 error
type JSONRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
}
// InitializeParams represents initialization parameters
type InitializeParams struct {
ProtocolVersion string `json:"protocolVersion"`
Capabilities interface{} `json:"capabilities"`
ClientInfo ClientInfo `json:"clientInfo"`
}
// ClientInfo represents client information
type ClientInfo struct {
Name string `json:"name"`
Version string `json:"version"`
}
// InitializeResult represents initialization result
type InitializeResult struct {
ProtocolVersion string `json:"protocolVersion"`
Capabilities interface{} `json:"capabilities"`
ServerInfo ServerInfo `json:"serverInfo"`
}
// ServerInfo represents server information
type ServerInfo struct {
Name string `json:"name"`
Version string `json:"version"`
}
// ListResourcesResult represents resources/list result
type ListResourcesResult struct {
Resources []Resource `json:"resources"`
}
// Resource represents an MCP resource
type Resource struct {
URI string `json:"uri"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
MimeType string `json:"mimeType,omitempty"`
}
// ListToolsResult represents tools/list result
type ListToolsResult struct {
Tools []Tool `json:"tools"`
}
// Tool represents an MCP tool
type Tool struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputSchema interface{} `json:"inputSchema,omitempty"`
}
// MCPServer represents a spawned MCP server for testing
type MCPServer struct {
cmd *exec.Cmd
stdin io.WriteCloser
stdout io.ReadCloser
name string
}
// NewMCPServer creates and starts a new MCP server for testing
func NewMCPServer(binaryPath string, args ...string) (*MCPServer, error) {
cmd := exec.Command(binaryPath, args...)
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, fmt.Errorf("failed to create stdin pipe: %w", err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
stdin.Close()
return nil, fmt.Errorf("failed to create stdout pipe: %w", err)
}
if err := cmd.Start(); err != nil {
stdin.Close()
stdout.Close()
return nil, fmt.Errorf("failed to start server: %w", err)
}
return &MCPServer{
cmd: cmd,
stdin: stdin,
stdout: stdout,
name: filepath.Base(binaryPath),
}, nil
}
// SendRequest sends a JSON-RPC request and returns the response
func (s *MCPServer) SendRequest(req JSONRPCRequest) (*JSONRPCResponse, error) {
reqBytes, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
// Send request
_, err = s.stdin.Write(append(reqBytes, '\n'))
if err != nil {
return nil, fmt.Errorf("failed to write request: %w", err)
}
// Read response with timeout
scanner := bufio.NewScanner(s.stdout)
responseChan := make(chan string, 1)
errorChan := make(chan error, 1)
go func() {
if scanner.Scan() {
responseChan <- scanner.Text()
} else if err := scanner.Err(); err != nil {
errorChan <- err
} else {
errorChan <- fmt.Errorf("EOF")
}
}()
select {
case responseText := <-responseChan:
var resp JSONRPCResponse
if err := json.Unmarshal([]byte(responseText), &resp); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
return &resp, nil
case err := <-errorChan:
return nil, fmt.Errorf("failed to read response: %w", err)
case <-time.After(5 * time.Second):
return nil, fmt.Errorf("timeout waiting for response")
}
}
// Close shuts down the MCP server
func (s *MCPServer) Close() error {
// Send shutdown request
shutdownReq := JSONRPCRequest{
JSONRPC: "2.0",
ID: 999,
Method: "shutdown",
}
s.SendRequest(shutdownReq)
s.stdin.Close()
s.stdout.Close()
// Wait for process to exit or kill it
done := make(chan error, 1)
go func() {
done <- s.cmd.Wait()
}()
select {
case <-done:
return nil
case <-time.After(2 * time.Second):
return s.cmd.Process.Kill()
}
}
// ServerTestConfig represents configuration for testing a server
type ServerTestConfig struct {
BinaryName string
Args []string
ExpectedTools []string
ExpectedServers string
MinResources int
}
// getProjectRoot returns the project root directory
func getProjectRoot() string {
wd, _ := os.Getwd()
// Go up from test/integration to project root
return filepath.Join(wd, "../..")
}
// getBinaryPath returns the path to a binary in the bin directory
func getBinaryPath(binaryName string) string {
return filepath.Join(getProjectRoot(), "bin", binaryName)
}
// TestAllServers tests all MCP servers
func TestAllServers(t *testing.T) {
// Create temp directories for testing
tempDir := t.TempDir()
servers := []ServerTestConfig{
{
BinaryName: "mcp-filesystem",
Args: []string{tempDir},
ExpectedTools: []string{"read_file", "write_file"},
ExpectedServers: "filesystem",
MinResources: 1, // Should have at least the temp directory
},
{
BinaryName: "mcp-git",
Args: []string{getProjectRoot()},
ExpectedTools: []string{"git_status", "git_diff", "git_commit"},
ExpectedServers: "mcp-git",
MinResources: 1, // Should have git repository resources
},
{
BinaryName: "mcp-memory",
Args: []string{},
ExpectedTools: []string{"create_entities", "create_relations", "read_graph"},
ExpectedServers: "mcp-memory",
MinResources: 1, // Should have knowledge graph resource
},
{
BinaryName: "mcp-fetch",
Args: []string{},
ExpectedTools: []string{"fetch"},
ExpectedServers: "mcp-fetch",
MinResources: 0, // No static resources
},
{
BinaryName: "mcp-time",
Args: []string{},
ExpectedTools: []string{"get_current_time", "convert_time"},
ExpectedServers: "mcp-time",
MinResources: 0, // No static resources
},
{
BinaryName: "mcp-sequential-thinking",
Args: []string{},
ExpectedTools: []string{"sequentialthinking"},
ExpectedServers: "mcp-sequential-thinking",
MinResources: 0, // No static resources
},
{
BinaryName: "mcp-maildir",
Args: []string{tempDir},
ExpectedTools: []string{"maildir_scan_folders", "maildir_list_messages"},
ExpectedServers: "maildir-server",
MinResources: 1, // Should have maildir resources
},
{
BinaryName: "mcp-imap",
Args: []string{"--server", "example.com", "--username", "test", "--password", "test"},
ExpectedTools: []string{"imap_list_folders", "imap_list_messages", "imap_get_connection_info", "imap_delete_message", "imap_move_to_trash"},
ExpectedServers: "imap",
MinResources: 0, // No static resources (connection fails gracefully)
},
{
BinaryName: "mcp-bash",
Args: []string{},
ExpectedTools: []string{"exec"},
ExpectedServers: "bash",
MinResources: 90, // Bash server has bash builtins and coreutils resources
},
{
BinaryName: "mcp-semantic",
Args: []string{"--project-root", "."},
ExpectedTools: []string{"semantic_find_symbol", "semantic_get_overview", "semantic_get_definition", "semantic_get_references", "semantic_get_call_hierarchy", "semantic_analyze_dependencies"},
ExpectedServers: "mcp-semantic",
MinResources: 0, // No static resources (discovers projects dynamically)
},
}
for _, config := range servers {
t.Run(config.BinaryName, func(t *testing.T) {
testServer(t, config)
})
}
}
// testServer tests a single MCP server
func testServer(t *testing.T, config ServerTestConfig) {
binaryPath := getBinaryPath(config.BinaryName)
// Check if binary exists
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
t.Fatalf("Binary not found: %s (run 'make build' first)", binaryPath)
}
// Start server
server, err := NewMCPServer(binaryPath, config.Args...)
if err != nil {
t.Fatalf("Failed to start server: %v", err)
}
defer server.Close()
// Test initialization
initReq := JSONRPCRequest{
JSONRPC: "2.0",
ID: 1,
Method: "initialize",
Params: InitializeParams{
ProtocolVersion: "2025-06-18",
Capabilities: map[string]interface{}{},
ClientInfo: ClientInfo{
Name: "test-client",
Version: "1.0.0",
},
},
}
resp, err := server.SendRequest(initReq)
if err != nil {
t.Fatalf("Failed to send initialize request: %v", err)
}
if resp.Error != nil {
t.Fatalf("Initialize request failed: %v", resp.Error)
}
var initResult InitializeResult
if err := json.Unmarshal(resp.Result, &initResult); err != nil {
t.Fatalf("Failed to parse initialize response: %v", err)
}
if initResult.ProtocolVersion != "2025-06-18" {
t.Errorf("Expected protocol version 2025-06-18, got %s", initResult.ProtocolVersion)
}
if initResult.ServerInfo.Name != config.ExpectedServers {
t.Errorf("Expected server name %s, got %s", config.ExpectedServers, initResult.ServerInfo.Name)
}
// Test tools/list
toolsReq := JSONRPCRequest{
JSONRPC: "2.0",
ID: 2,
Method: "tools/list",
}
resp, err = server.SendRequest(toolsReq)
if err != nil {
t.Fatalf("Failed to send tools/list request: %v", err)
}
if resp.Error != nil {
t.Fatalf("Tools/list request failed: %v", resp.Error)
}
var toolsResult ListToolsResult
if err := json.Unmarshal(resp.Result, &toolsResult); err != nil {
t.Fatalf("Failed to parse tools/list response: %v", err)
}
// Check that expected tools are present
toolNames := make(map[string]bool)
for _, tool := range toolsResult.Tools {
toolNames[tool.Name] = true
}
for _, expectedTool := range config.ExpectedTools {
if !toolNames[expectedTool] {
t.Errorf("Expected tool %s not found in tools list", expectedTool)
}
}
// Test resources/list
resourcesReq := JSONRPCRequest{
JSONRPC: "2.0",
ID: 3,
Method: "resources/list",
}
resp, err = server.SendRequest(resourcesReq)
if err != nil {
t.Fatalf("Failed to send resources/list request: %v", err)
}
if resp.Error != nil {
t.Fatalf("Resources/list request failed: %v", resp.Error)
}
var resourcesResult ListResourcesResult
if err := json.Unmarshal(resp.Result, &resourcesResult); err != nil {
t.Fatalf("Failed to parse resources/list response: %v", err)
}
if len(resourcesResult.Resources) < config.MinResources {
t.Errorf("Expected at least %d resources, got %d", config.MinResources, len(resourcesResult.Resources))
}
// Validate that resources have required fields
for _, resource := range resourcesResult.Resources {
if resource.URI == "" {
t.Error("Resource missing URI")
}
if resource.Name == "" {
t.Error("Resource missing Name")
}
if !strings.Contains(resource.URI, "://") {
t.Errorf("Resource URI should contain scheme: %s", resource.URI)
}
}
t.Logf("✅ %s: %d tools, %d resources", config.BinaryName, len(toolsResult.Tools), len(resourcesResult.Resources))
}
// TestServerStartupPerformance tests that servers start quickly
func TestServerStartupPerformance(t *testing.T) {
tempDir := t.TempDir()
servers := []string{
"mcp-filesystem",
"mcp-git",
"mcp-memory",
"mcp-fetch",
"mcp-time",
"mcp-sequential-thinking",
"mcp-maildir",
"mcp-imap",
"mcp-bash",
"mcp-semantic",
}
for _, serverName := range servers {
t.Run(serverName, func(t *testing.T) {
binaryPath := getBinaryPath(serverName)
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
t.Skip("Binary not found")
}
start := time.Now()
var args []string
switch serverName {
case "mcp-filesystem":
args = []string{tempDir}
case "mcp-git":
args = []string{getProjectRoot()}
case "mcp-maildir":
args = []string{tempDir}
case "mcp-imap":
args = []string{"--server", "example.com", "--username", "test", "--password", "test"}
case "mcp-semantic":
args = []string{"--project-root", "."}
}
server, err := NewMCPServer(binaryPath, args...)
if err != nil {
t.Fatalf("Failed to start server: %v", err)
}
defer server.Close()
// Send initialize request to confirm server is ready
initReq := JSONRPCRequest{
JSONRPC: "2.0",
ID: 1,
Method: "initialize",
Params: InitializeParams{
ProtocolVersion: "2025-06-18",
Capabilities: map[string]interface{}{},
ClientInfo: ClientInfo{Name: "test", Version: "1.0.0"},
},
}
_, err = server.SendRequest(initReq)
if err != nil {
t.Fatalf("Server not responding: %v", err)
}
duration := time.Since(start)
// Servers should start and respond within 500ms for good performance
if duration > 500*time.Millisecond {
t.Errorf("Server %s took %v to start, expected < 500ms", serverName, duration)
} else {
t.Logf("✅ %s started in %v", serverName, duration)
}
})
}
}
|