mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-12-06 14:24:14 +00:00
Also sets a write deadline on MCP responses so a client that stops reading cannot hold up change notices, and checks that an ID exists before the find tool reports it.
120 lines
4.5 KiB
Go
120 lines
4.5 KiB
Go
// Copyright 2026 Beacon Contributors
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package router
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/MeshCore-Beacon/beacon-server/internal/api"
|
|
"github.com/MeshCore-Beacon/beacon-server/internal/config"
|
|
"github.com/MeshCore-Beacon/beacon-server/internal/guards"
|
|
"github.com/MeshCore-Beacon/beacon-server/internal/mcp"
|
|
)
|
|
|
|
// Every route MCP can reach must be in the reviewed catalog, and the other way round. A new public
|
|
// read fails here until someone decides whether assistants should see it.
|
|
func TestPublicReadsAreClassified(t *testing.T) {
|
|
param := regexp.MustCompile(`\{[^}]+\}`)
|
|
norm := func(p string) string {
|
|
p = strings.TrimSuffix(strings.TrimPrefix(p, "/api/v1"), "/")
|
|
return param.ReplaceAllString(p, "{}")
|
|
}
|
|
catalog := map[string]string{}
|
|
for id, path := range mcp.Catalog {
|
|
catalog[norm(path)] = id
|
|
}
|
|
routed := map[string]bool{}
|
|
err := chi.Walk(publicReads(nil, nil, Options{}, api.Info{}), func(method, route string, _ http.Handler, _ ...func(http.Handler) http.Handler) error {
|
|
route = norm(strings.ReplaceAll(route, "/*/", "/"))
|
|
if method != http.MethodGet {
|
|
t.Errorf("MCP can reach a non-GET route: %s %s", method, route)
|
|
}
|
|
routed[route] = true
|
|
if _, ok := catalog[route]; !ok {
|
|
t.Errorf("public read %s is not in mcp.Catalog; add it there on purpose or keep it out of mountPublicReads", route)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for route, id := range catalog {
|
|
if !routed[route] {
|
|
t.Errorf("mcp.Catalog operation %s (%s) is not a mounted public read", id, route)
|
|
}
|
|
}
|
|
for route := range routed {
|
|
for _, private := range []string{"/admin", "/me", "/auth", "/ws-ticket"} {
|
|
if route == private || strings.HasPrefix(route, private+"/") {
|
|
t.Errorf("MCP can reach %s", route)
|
|
}
|
|
}
|
|
}
|
|
if len(routed) != len(mcp.Catalog) {
|
|
t.Errorf("%d routes, %d catalog entries", len(routed), len(mcp.Catalog))
|
|
}
|
|
}
|
|
|
|
func mcpCall(t *testing.T, h http.Handler, body string) (int, string) {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodPost, "/mcp", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Accept", "application/json, text/event-stream")
|
|
req.RemoteAddr = "203.0.113.9:4000"
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
return rec.Code, rec.Body.String()
|
|
}
|
|
|
|
// A tool call goes through the real router: the same handlers, parameter limits and guards as REST.
|
|
func TestMCPCallsTheRealHandlers(t *testing.T) {
|
|
resolved, err := config.MCPConfig{Enabled: true}.Resolve()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g := guards.New(guards.Options{Budget: "shadow"})
|
|
h := New(nil, nil, nil, Options{MCP: resolved, Guards: g, ServerVersion: "9.9.9"})
|
|
|
|
code, body := mcpCall(t, h, `{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"beacon_get_server_info","arguments":{}}}`)
|
|
if code != http.StatusOK || !strings.Contains(body, `\"serverVersion\":\"9.9.9\"`) {
|
|
t.Fatalf("server info through /mcp: %d %s", code, body)
|
|
}
|
|
// The stub has no reader, so this read panics inside the handler; the call must fail cleanly.
|
|
code, body = mcpCall(t, h, `{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"beacon_list_areas","arguments":{}}}`)
|
|
if code != http.StatusOK || !strings.Contains(body, "INTERNAL") || !strings.Contains(body, `"isError":true`) {
|
|
t.Errorf("a panicking read: %d %s", code, body)
|
|
}
|
|
req := httptest.NewRequest(http.MethodGet, "/mcp", nil)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusMethodNotAllowed {
|
|
t.Errorf("GET /mcp: want 405, got %d", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestMCPIsOffByDefault(t *testing.T) {
|
|
h := New(nil, nil, nil, Options{})
|
|
if code, _ := mcpCall(t, h, `{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}`); code != http.StatusNotFound {
|
|
t.Errorf("want 404 with MCP off, got %d", code)
|
|
}
|
|
}
|
|
|
|
// MCP reads go through the same list-parameter limit as REST, before any handler runs.
|
|
func TestPublicReadsKeepTheListParameterLimit(t *testing.T) {
|
|
big := strings.TrimSuffix(strings.Repeat("AAA,", 1001), ",")
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/nodes?iatas="+big, nil)
|
|
rec := httptest.NewRecorder()
|
|
// A nil reader would panic if the request reached the handler.
|
|
publicReads(nil, nil, Options{}, api.Info{}).ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "at most 1000 values") {
|
|
t.Errorf("want 400 from the list limit, got %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|