-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_test.go
More file actions
166 lines (146 loc) · 4.31 KB
/
mcp_test.go
File metadata and controls
166 lines (146 loc) · 4.31 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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func setupMCPServer(t *testing.T) *Server {
t.Helper()
dir := t.TempDir()
cfg := &Config{Port: 8822, AllowedDirs: []string{dir}, ClaudePath: "echo", DataDir: dir}
auth := NewAuth(cfg.SecretPath())
auth.GenerateSecret()
s := NewServer(cfg, auth)
s.registerRoutes()
return s
}
func mcpPost(t *testing.T, s *Server, body string) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest(http.MethodPost, "/mcp", strings.NewReader(body))
req.RemoteAddr = "127.0.0.1:12345"
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.mux.ServeHTTP(w, req)
return w
}
func TestMCPInitialize(t *testing.T) {
s := setupMCPServer(t)
body := `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}`
w := mcpPost(t, s, body)
if w.Code != 200 {
t.Fatalf("want 200, got %d", w.Code)
}
var resp jsonrpcResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("unmarshal: %v", err)
}
result, ok := resp.Result.(map[string]interface{})
if !ok {
t.Fatal("result not a map")
}
if _, ok := result["capabilities"]; !ok {
t.Error("missing capabilities in response")
}
info, ok := result["serverInfo"].(map[string]interface{})
if !ok {
t.Fatal("missing serverInfo")
}
if info["name"] != "claude-remote" {
t.Errorf("want name=claude-remote, got %v", info["name"])
}
}
func TestMCPToolsList(t *testing.T) {
s := setupMCPServer(t)
body := `{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}`
w := mcpPost(t, s, body)
if w.Code != 200 {
t.Fatalf("want 200, got %d", w.Code)
}
var resp jsonrpcResponse
json.Unmarshal(w.Body.Bytes(), &resp)
result, _ := resp.Result.(map[string]interface{})
tools, ok := result["tools"].([]interface{})
if !ok {
t.Fatal("tools not an array")
}
names := map[string]bool{}
for _, tool := range tools {
m, _ := tool.(map[string]interface{})
if name, ok := m["name"].(string); ok {
names[name] = true
}
}
if !names["handoff"] {
t.Error("missing handoff tool")
}
if !names["status"] {
t.Error("missing status tool")
}
}
func TestMCPToolCallStatus(t *testing.T) {
s := setupMCPServer(t)
body := `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"status","arguments":{}}}`
w := mcpPost(t, s, body)
if w.Code != 200 {
t.Fatalf("want 200, got %d", w.Code)
}
var resp jsonrpcResponse
json.Unmarshal(w.Body.Bytes(), &resp)
result, _ := resp.Result.(map[string]interface{})
content, ok := result["content"].([]interface{})
if !ok || len(content) == 0 {
t.Fatal("missing content in result")
}
first, _ := content[0].(map[string]interface{})
text, _ := first["text"].(string)
if !strings.Contains(text, "claude-remote") {
t.Errorf("status text should contain 'claude-remote', got: %s", text)
}
}
func TestMCPToolCallHandoff(t *testing.T) {
s := setupMCPServer(t)
body := `{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"handoff","arguments":{"dir":"/tmp/test"}}}`
w := mcpPost(t, s, body)
if w.Code != 200 {
t.Fatalf("want 200, got %d", w.Code)
}
var resp jsonrpcResponse
json.Unmarshal(w.Body.Bytes(), &resp)
result, _ := resp.Result.(map[string]interface{})
content, ok := result["content"].([]interface{})
if !ok || len(content) == 0 {
t.Fatal("missing content in result")
}
first, _ := content[0].(map[string]interface{})
text, _ := first["text"].(string)
if !strings.Contains(text, "/handoff?") {
t.Errorf("handoff text should contain '/handoff?', got: %s", text)
}
if !strings.Contains(text, "token=") {
t.Errorf("handoff text should contain 'token=', got: %s", text)
}
}
func TestMCPRejectsNonLocalhost(t *testing.T) {
s := setupMCPServer(t)
body := `{"jsonrpc":"2.0","id":5,"method":"initialize","params":{}}`
req := httptest.NewRequest(http.MethodPost, "/mcp", strings.NewReader(body))
req.RemoteAddr = "192.168.1.100:12345"
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.mux.ServeHTTP(w, req)
if w.Code != 403 {
t.Errorf("want 403, got %d", w.Code)
}
}
func TestMCPRejectsGET(t *testing.T) {
s := setupMCPServer(t)
req := httptest.NewRequest(http.MethodGet, "/mcp", nil)
req.RemoteAddr = "127.0.0.1:12345"
w := httptest.NewRecorder()
s.mux.ServeHTTP(w, req)
if w.Code != 405 {
t.Errorf("want 405, got %d", w.Code)
}
}