|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/hookdeck/hookdeck-cli/pkg/hookdeck" |
| 16 | + mcpsdk "github.com/modelcontextprotocol/go-sdk/mcp" |
| 17 | +) |
| 18 | + |
| 19 | +// A tool that declares an argument as an array must deliver a genuine array to |
| 20 | +// the API. The input-shape guard makes every read that treats an argument as a |
| 21 | +// list go through mcpcore.StringList; it cannot see the reverse -- a handler |
| 22 | +// that declares an array but reads it with in.String(), which returns "" for an |
| 23 | +// array and drops it without an error. |
| 24 | +// |
| 25 | +// So every array-typed argument is listed here with a valid call. The test |
| 26 | +// sends a real JSON array and requires each value to reach the API. A new array |
| 27 | +// argument fails until it has an entry. |
| 28 | + |
| 29 | +type arrayArgCall struct { |
| 30 | + args map[string]any // a valid call, with the array argument set |
| 31 | + want []string // strings that must appear in the request the API receives |
| 32 | +} |
| 33 | + |
| 34 | +var gatewayArrayArgs = map[string]arrayArgCall{ |
| 35 | + "gateway_metrics_read.measures": { |
| 36 | + args: map[string]any{"action": "events", "start": "2025-01-01T00:00:00Z", "end": "2025-01-02T00:00:00Z", |
| 37 | + "measures": []any{"count", "successful_count"}}, |
| 38 | + want: []string{"measures[]=count", "measures[]=successful_count"}, |
| 39 | + }, |
| 40 | + "gateway_metrics_read.dimensions": { |
| 41 | + args: map[string]any{"action": "events", "start": "2025-01-01T00:00:00Z", "end": "2025-01-02T00:00:00Z", |
| 42 | + "measures": []any{"count"}, "dimensions": []any{"status", "connection_id"}}, |
| 43 | + // connection_id is sent in the API's spelling (#442). |
| 44 | + want: []string{"dimensions[]=status", "dimensions[]=webhook_id"}, |
| 45 | + }, |
| 46 | + "gateway_request_write.connection_ids": { |
| 47 | + args: map[string]any{"action": "retry", "id": "req_1", "connection_ids": []any{"web_qa_a", "web_qa_b"}}, |
| 48 | + want: []string{"web_qa_a", "web_qa_b"}, |
| 49 | + }, |
| 50 | + "gateway_connections_write.rules": { |
| 51 | + args: map[string]any{"action": "update", "id": "web_1", "rules": []any{ |
| 52 | + map[string]any{"type": "filter", "headers": map[string]any{"x-qa": "qa_marker_a"}}, |
| 53 | + map[string]any{"type": "filter", "body": map[string]any{"qa": "qa_marker_b"}}, |
| 54 | + }}, |
| 55 | + want: []string{"qa_marker_a", "qa_marker_b"}, |
| 56 | + }, |
| 57 | +} |
| 58 | + |
| 59 | +// recordingAPI answers every API request and keeps its query and body. |
| 60 | +func recordingAPI(t *testing.T) (map[string]http.HandlerFunc, func() string) { |
| 61 | + var mu sync.Mutex |
| 62 | + var seen []string |
| 63 | + handler := func(w http.ResponseWriter, r *http.Request) { |
| 64 | + body, _ := io.ReadAll(r.Body) |
| 65 | + q, _ := url.QueryUnescape(r.URL.RawQuery) |
| 66 | + mu.Lock() |
| 67 | + seen = append(seen, r.Method+" "+r.URL.Path+"?"+q+" "+string(body)) |
| 68 | + mu.Unlock() |
| 69 | + w.Header().Set("Content-Type", "application/json") |
| 70 | + _, _ = w.Write([]byte(`{"id":"web_1","models":[],"pagination":{},"data":[]}`)) |
| 71 | + } |
| 72 | + return map[string]http.HandlerFunc{hookdeck.APIPathPrefix + "/": handler}, func() string { |
| 73 | + mu.Lock() |
| 74 | + defer mu.Unlock() |
| 75 | + return strings.Join(seen, "\n") |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func arrayArgumentsOf(t *testing.T, tools []toolSchema) map[string]bool { |
| 80 | + found := map[string]bool{} |
| 81 | + for _, tool := range tools { |
| 82 | + for name, prop := range tool.props { |
| 83 | + if p, _ := prop.(map[string]any); p["type"] == "array" { |
| 84 | + found[tool.name+"."+name] = true |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return found |
| 89 | +} |
| 90 | + |
| 91 | +type toolSchema struct { |
| 92 | + name string |
| 93 | + props map[string]any |
| 94 | +} |
| 95 | + |
| 96 | +func TestGatewayArrayArgumentsReachTheAPI(t *testing.T) { |
| 97 | + handlers, requests := recordingAPI(t) |
| 98 | + session := mockAPIWithClientWriteEnabled(t, handlers) |
| 99 | + |
| 100 | + list, err := session.ListTools(context.Background(), nil) |
| 101 | + require.NoError(t, err) |
| 102 | + var tools []toolSchema |
| 103 | + for _, tool := range list.Tools { |
| 104 | + props, _ := tool.InputSchema.(map[string]any)["properties"].(map[string]any) |
| 105 | + tools = append(tools, toolSchema{tool.Name, props}) |
| 106 | + } |
| 107 | + found := arrayArgumentsOf(t, tools) |
| 108 | + require.NotEmpty(t, found, "no array arguments found; the walk is broken and the guard would pass vacuously") |
| 109 | + |
| 110 | + for key := range found { |
| 111 | + call, ok := gatewayArrayArgs[key] |
| 112 | + if !assert.True(t, ok, "%s is declared as an array but has no entry in gatewayArrayArgs. "+ |
| 113 | + "Add a valid call and check the values reach the API.", key) { |
| 114 | + continue |
| 115 | + } |
| 116 | + t.Run(key, func(t *testing.T) { |
| 117 | + before := requests() |
| 118 | + result := callTool(t, session, strings.SplitN(key, ".", 2)[0], call.args) |
| 119 | + sent := strings.TrimPrefix(requests(), before) |
| 120 | + require.NotEmpty(t, sent, "the call reached no API endpoint: %s", fmtResult(t, result)) |
| 121 | + for _, w := range call.want { |
| 122 | + assert.Contains(t, sent, w, "an array value did not reach the API") |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | + for key := range gatewayArrayArgs { |
| 127 | + assert.True(t, found[key], "%s is in gatewayArrayArgs but is no longer an array argument", key) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func fmtResult(t *testing.T, r *mcpsdk.CallToolResult) string { |
| 132 | + t.Helper() |
| 133 | + if r == nil || len(r.Content) == 0 { |
| 134 | + return "" |
| 135 | + } |
| 136 | + return textContent(t, r) |
| 137 | +} |
0 commit comments