Skip to content

Commit f35e3af

Browse files
committed
fix: return records array correctly from list_dns_records tool
1 parent 40c3123 commit f35e3af

2 files changed

Lines changed: 63 additions & 7 deletions

File tree

cmd/acme-dns-mcp/mcp_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,31 @@ func TestToolListTools(t *testing.T) {
8181
}
8282
}
8383
}
84+
85+
func TestToolListRecords(t *testing.T) {
86+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
87+
if r.URL.Path == "/admin/records" {
88+
w.Header().Set("Content-Type", "application/json")
89+
w.WriteHeader(http.StatusOK)
90+
w.Write([]byte(`[{"id":"test-id","name":"example.com","type":"A","value":"1.2.3.4","ttl":300,"created":0}]`))
91+
}
92+
}))
93+
defer srv.Close()
94+
95+
cfg := mcpConfig{BaseURL: srv.URL, AdminToken: "test-token"}
96+
result, err := callTool(cfg, "list_dns_records", map[string]interface{}{})
97+
if err != nil {
98+
t.Fatalf("list_dns_records failed: %v", err)
99+
}
100+
records, ok := result["records"]
101+
if !ok {
102+
t.Fatalf("expected 'records' key in result, got %v", result)
103+
}
104+
arr, ok := records.([]interface{})
105+
if !ok {
106+
t.Fatalf("expected records to be array, got %T", records)
107+
}
108+
if len(arr) != 1 {
109+
t.Fatalf("expected 1 record, got %d", len(arr))
110+
}
111+
}

cmd/acme-dns-mcp/tools.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func callTool(cfg mcpConfig, name string, args map[string]interface{}) (map[stri
7777
return nil, fmt.Errorf("unknown tool: %s", name)
7878
}
7979

80-
func doRequest(cfg mcpConfig, method, path string, body interface{}, headers map[string]string) (map[string]interface{}, int, error) {
80+
func doRequest(cfg mcpConfig, method, path string, body interface{}, headers map[string]string) (interface{}, int, error) {
8181
var bodyReader io.Reader
8282
if body != nil {
8383
b, err := json.Marshal(body)
@@ -101,7 +101,7 @@ func doRequest(cfg mcpConfig, method, path string, body interface{}, headers map
101101
return nil, 0, err
102102
}
103103
defer resp.Body.Close()
104-
var result map[string]interface{}
104+
var result interface{}
105105
_ = json.NewDecoder(resp.Body).Decode(&result)
106106
if result == nil {
107107
result = map[string]interface{}{}
@@ -122,7 +122,13 @@ func toolHealthCheck(cfg mcpConfig) (map[string]interface{}, error) {
122122

123123
func toolRegister(cfg mcpConfig, args map[string]interface{}) (map[string]interface{}, error) {
124124
result, _, err := doRequest(cfg, "POST", "/register", args, nil)
125-
return result, err
125+
if err != nil {
126+
return nil, err
127+
}
128+
if m, ok := result.(map[string]interface{}); ok {
129+
return m, nil
130+
}
131+
return map[string]interface{}{"result": result}, nil
126132
}
127133

128134
func toolUpdateTXT(cfg mcpConfig, args map[string]interface{}) (map[string]interface{}, error) {
@@ -134,7 +140,13 @@ func toolUpdateTXT(cfg mcpConfig, args map[string]interface{}) (map[string]inter
134140
"X-Api-Key": cfg.Password,
135141
}
136142
result, _, err := doRequest(cfg, "POST", "/update", args, headers)
137-
return result, err
143+
if err != nil {
144+
return nil, err
145+
}
146+
if m, ok := result.(map[string]interface{}); ok {
147+
return m, nil
148+
}
149+
return map[string]interface{}{"result": result}, nil
138150
}
139151

140152
func toolListRecords(cfg mcpConfig, args map[string]interface{}) (map[string]interface{}, error) {
@@ -154,7 +166,11 @@ func toolListRecords(cfg mcpConfig, args map[string]interface{}) (map[string]int
154166
}
155167
headers := map[string]string{"Authorization": "Bearer " + cfg.AdminToken}
156168
result, _, err := doRequest(cfg, "GET", path, nil, headers)
157-
return result, err
169+
if err != nil {
170+
return nil, err
171+
}
172+
// GET /admin/records returns a JSON array — wrap it for uniform map return
173+
return map[string]interface{}{"records": result}, nil
158174
}
159175

160176
func toolCreateRecord(cfg mcpConfig, args map[string]interface{}) (map[string]interface{}, error) {
@@ -163,7 +179,13 @@ func toolCreateRecord(cfg mcpConfig, args map[string]interface{}) (map[string]in
163179
}
164180
headers := map[string]string{"Authorization": "Bearer " + cfg.AdminToken}
165181
result, _, err := doRequest(cfg, "POST", "/admin/records", args, headers)
166-
return result, err
182+
if err != nil {
183+
return nil, err
184+
}
185+
if m, ok := result.(map[string]interface{}); ok {
186+
return m, nil
187+
}
188+
return map[string]interface{}{"result": result}, nil
167189
}
168190

169191
func toolUpdateRecord(cfg mcpConfig, args map[string]interface{}) (map[string]interface{}, error) {
@@ -176,7 +198,13 @@ func toolUpdateRecord(cfg mcpConfig, args map[string]interface{}) (map[string]in
176198
}
177199
headers := map[string]string{"Authorization": "Bearer " + cfg.AdminToken}
178200
result, _, err := doRequest(cfg, "PUT", "/admin/records/"+id, args, headers)
179-
return result, err
201+
if err != nil {
202+
return nil, err
203+
}
204+
if m, ok := result.(map[string]interface{}); ok {
205+
return m, nil
206+
}
207+
return map[string]interface{}{"result": result}, nil
180208
}
181209

182210
func toolDeleteRecord(cfg mcpConfig, args map[string]interface{}) (map[string]interface{}, error) {

0 commit comments

Comments
 (0)