Skip to content

Commit 42612fc

Browse files
leggetterclaude
andcommitted
test: every array-typed MCP argument must deliver its values to the API
The input-shape guard (#455) makes every read that treats an argument as a list go through StringList, so a string is never dropped. It cannot see the reverse: a handler that declares an argument as an array and reads it with in.String(), which returns "" for an array and drops it without an error. These tests find every array-typed argument from the live tool schemas on both servers, with writes enabled, and require each to have a valid call in a table. Each call sends a genuine JSON array to a recording server, and every value must reach the request. A new array argument fails until it has an entry; a stale entry fails too. Ten arguments today -- four on Gateway (metrics measures and dimensions, request connection_ids, connection rules) and six on Outpost -- and all ten deliver correctly, so this is a guard, not a fix. The expected strings are the encodings captured from the recording server, not assumed. Verified against the failure: reading connection_ids with in.String() fails the Gateway test -- and shows the retry going out as {}, i.e. retry every connection instead of the two chosen, a silent widening. Reading include with in.String() fails the Outpost test. Removing a table entry fails with a message naming the argument. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XtSQ2kfpcRXXqweskgXkH
1 parent 1b131fc commit 42612fc

2 files changed

Lines changed: 241 additions & 0 deletions

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
16+
// The Outpost half of the Gateway test of the same name: every array-typed
17+
// argument must deliver a genuine array to the API. A handler that declared an
18+
// array but read it with in.String() would drop it without an error. A new
19+
// array argument fails here until it has an entry with a valid call.
20+
//
21+
// The wanted strings are the encoding the client actually sends, captured from
22+
// a recording server rather than assumed.
23+
var outpostArrayArgs = map[string]struct {
24+
args map[string]any
25+
want []string
26+
}{
27+
"outpost_attempts_read.include": {
28+
args: map[string]any{"action": "list", "tenant_id": "t1", "include": []any{"event", "destination"}},
29+
want: []string{"include[0]=event", "include[1]=destination"},
30+
},
31+
"outpost_config_write.unset": {
32+
args: map[string]any{"action": "set", "unset": []any{"MAX_RETRY_LIMIT", "RETRY_INTERVAL_SECONDS"}},
33+
want: []string{`"MAX_RETRY_LIMIT":null`, `"RETRY_INTERVAL_SECONDS":null`},
34+
},
35+
"outpost_destinations_read.topics": {
36+
args: map[string]any{"action": "list", "tenant_id": "t1", "topics": []any{"qa.a", "qa.b"}},
37+
want: []string{"topics[0]=qa.a", "topics[1]=qa.b"},
38+
},
39+
"outpost_destinations_write.topics": {
40+
args: map[string]any{"action": "create", "tenant_id": "t1", "type": "webhook",
41+
"config": map[string]any{"url": "https://example.com/hook"}, "topics": []any{"qa.a", "qa.b"}},
42+
want: []string{`"topics":["qa.a","qa.b"]`},
43+
},
44+
"outpost_metrics_read.measures": {
45+
args: map[string]any{"action": "events", "start": "2025-01-01T00:00:00Z", "end": "2025-01-02T00:00:00Z",
46+
"measures": []any{"count", "successful_count"}},
47+
want: []string{"measures[0]=count", "measures[1]=successful_count"},
48+
},
49+
"outpost_metrics_read.dimensions": {
50+
args: map[string]any{"action": "events", "start": "2025-01-01T00:00:00Z", "end": "2025-01-02T00:00:00Z",
51+
"measures": []any{"count"}, "dimensions": []any{"tenant_id", "topic"}},
52+
want: []string{"dimensions[0]=tenant_id", "dimensions[1]=topic"},
53+
},
54+
}
55+
56+
func TestOutpostArrayArgumentsReachTheAPI(t *testing.T) {
57+
var mu sync.Mutex
58+
var seen []string
59+
api := mockAPI(t, map[string]http.HandlerFunc{
60+
"/2026-09-01/": func(w http.ResponseWriter, r *http.Request) {
61+
body, _ := io.ReadAll(r.Body)
62+
q, _ := url.QueryUnescape(r.URL.RawQuery)
63+
mu.Lock()
64+
seen = append(seen, r.Method+" "+r.URL.Path+"?"+q+" "+string(body))
65+
mu.Unlock()
66+
_, _ = w.Write([]byte(`{"id":"x","models":[],"data":[],"pagination":{}}`))
67+
},
68+
})
69+
requests := func() string { mu.Lock(); defer mu.Unlock(); return strings.Join(seen, "\n") }
70+
session := connect(t, ServerOptions{Client: newTestClient(t, api.URL), WriteEnabled: true, PublishAPIKey: "pk"})
71+
72+
list, err := session.ListTools(context.Background(), nil)
73+
require.NoError(t, err)
74+
found := map[string]bool{}
75+
for _, tool := range list.Tools {
76+
props, _ := tool.InputSchema.(map[string]any)["properties"].(map[string]any)
77+
for name, prop := range props {
78+
if p, _ := prop.(map[string]any); p["type"] == "array" {
79+
found[tool.Name+"."+name] = true
80+
}
81+
}
82+
}
83+
require.NotEmpty(t, found, "no array arguments found; the walk is broken and the guard would pass vacuously")
84+
85+
for key := range found {
86+
call, ok := outpostArrayArgs[key]
87+
if !assert.True(t, ok, "%s is declared as an array but has no entry in outpostArrayArgs. "+
88+
"Add a valid call and check the values reach the API.", key) {
89+
continue
90+
}
91+
t.Run(key, func(t *testing.T) {
92+
before := requests()
93+
callTool(t, session, strings.SplitN(key, ".", 2)[0], call.args)
94+
sent := strings.TrimPrefix(requests(), before)
95+
require.NotEmpty(t, sent, "the call reached no API endpoint")
96+
for _, w := range call.want {
97+
assert.Contains(t, sent, w, "an array value did not reach the API")
98+
}
99+
})
100+
}
101+
for key := range outpostArrayArgs {
102+
assert.True(t, found[key], "%s is in outpostArrayArgs but is no longer an array argument", key)
103+
}
104+
}

0 commit comments

Comments
 (0)