package app import ( "encoding/json" "fmt" "strings" "testing" "time" "unicode/utf8" ) func TestMCPCharacterLimits(t *testing.T) { s, _ := fixtureService(t) status, body := callHTTP(t, s, "/mcp", `{"jsonrpc":"2.0","id":1,"method":"tools/list"}`) if status != 200 { t.Fatal(status) } var inventory struct { Tools []struct { Name string `json:"name"` InputSchema map[string]any `json:"inputSchema"` } `json:"tools"` } if err := json.Unmarshal(body["result"], &inventory); err != nil { t.Fatal(err) } count := 0 var check func(any) check = func(value any) { switch node := value.(type) { case map[string]any: for key, child := range node { if key == "maxCharacters" || key == "max_chars" { limit, ok := child.(map[string]any) if !ok || limit["type"] != "integer" || limit["minimum"] != float64(0) || limit["maximum"] != float64(10000) { t.Fatalf("%s has mismatched character limits: %v", key, child) } count++ } check(child) } case []any: for _, child := range node { check(child) } } } for _, tool := range inventory.Tools { check(tool.InputSchema) } if count != 7 { t.Fatalf("expected four content limits and three alias limits, got %d", count) } } func TestContentCharacterLimitValidation(t *testing.T) { for _, feature := range []string{"text", "highlights"} { for _, limit := range []int{-1, 0, 1, 10000, 10001, 16000} { t.Run(fmt.Sprintf("%s/%d", feature, limit), func(t *testing.T) { var q ContentsRequest err := strictJSON(fmt.Appendf(nil, `{"urls":["https://example.com"],%q:{"maxCharacters":%d}}`, feature, limit), &q) if err == nil { _, err = q.validate() } valid := limit >= 0 && limit <= 10000 if (err == nil) != valid { t.Fatalf("limit %d valid=%v: %v", limit, valid, err) } }) } } } func TestCharacterLimitsOverHTTPAndMCP(t *testing.T) { s, page := fixtureService(t) text := strings.Repeat("request limits. ", 800) s.fetch.cache.put(page.URL, Document{URL: page.URL, FinalURL: page.URL, Title: "limits", Markdown: text, Fetched: time.Now()}) for _, limit := range []int{0, 1, 10000, 10001, 16000} { for _, transport := range []string{"http", "mcp"} { t.Run(fmt.Sprintf("%d/%s", limit, transport), func(t *testing.T) { args := fmt.Sprintf(`{"urls":[%q],"text":{"maxCharacters":%d}}`, page.URL, limit) path := "/contents" if transport == "mcp" { path = "/mcp" args = fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"web_contents","arguments":%s}}`, args) } status, payload := callHTTP(t, s, path, args) valid := limit <= 10000 if transport == "mcp" { var result struct { IsError bool `json:"isError"` StructuredContent map[string]json.RawMessage `json:"structuredContent"` } if err := json.Unmarshal(payload["result"], &result); err != nil { t.Fatal(err) } if status != 200 || result.IsError == valid { t.Fatalf("unexpected mcp result: %s", payload["result"]) } payload = result.StructuredContent } else if valid && status != 200 || !valid && status != 400 { t.Fatalf("unexpected http status %d: %v", status, payload) } if !valid { return } var results []Result if err := json.Unmarshal(payload["results"], &results); err != nil { t.Fatal(err) } want := utf8.RuneCountInString(text) if limit > 0 { want = min(want, limit) } if len(results) != 1 || results[0].Text == nil || utf8.RuneCountInString(*results[0].Text) != want { t.Fatalf("text was not capped to %d characters: %+v", want, results) } }) } } }