| 1 | package agentaccess |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/modelcontextprotocol/go-sdk/mcp" |
| 13 | ) |
| 14 | |
| 15 | func TestLargeIDsSurviveHTTPAndMCP(t *testing.T) { |
| 16 | const id = int64(9007199254740993) |
| 17 | r, err := NewRegistry(Operation{Name: "notes_echo", Description: "Echo an exact identifier", Scope: "notes:write", |
| 18 | Input: json.RawMessage(`{"type":"object","required":["team","id"],"properties":{"team":{"type":"string"},"id":{"type":"integer"}}}`), |
| 19 | Output: json.RawMessage(`{"type":"object","required":["id"],"properties":{"id":{"type":"integer"}}}`), |
| 20 | Invoke: func(_ context.Context, _ Principal, raw json.RawMessage) (any, error) { |
| 21 | var in struct { |
| 22 | ID int64 `json:"id"` |
| 23 | } |
| 24 | if json.Unmarshal(raw, &in) != nil || in.ID != id { |
| 25 | t.Error("input ID rounded") |
| 26 | } |
| 27 | return map[string]int64{"id": in.ID}, nil |
| 28 | }}) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | v := VerifyFunc(func(context.Context, string) (Principal, error) { return principal(), nil }) |
| 33 | mux := http.NewServeMux() |
| 34 | mux.Handle("/api/", r.HTTP(v)) |
| 35 | mux.Handle("/mcp", r.MCP(v)) |
| 36 | s := httptest.NewServer(mux) |
| 37 | defer s.Close() |
| 38 | request, _ := http.NewRequest("POST", s.URL+"/api/v1/operations/notes_echo", strings.NewReader(`{"team":"acme","id":9007199254740993}`)) |
| 39 | request.Header.Set("Authorization", "Bearer test") |
| 40 | request.Header.Set("Content-Type", "application/json") |
| 41 | response, err := s.Client().Do(request) |
| 42 | if err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | body, _ := io.ReadAll(response.Body) |
| 46 | response.Body.Close() |
| 47 | if response.StatusCode != 200 || !strings.Contains(string(body), "9007199254740993") { |
| 48 | t.Fatalf("HTTP rounded ID: %s", body) |
| 49 | } |
| 50 | c := mcp.NewClient(&mcp.Implementation{Name: "test", Version: "1"}, nil) |
| 51 | session, err := c.Connect(t.Context(), &mcp.StreamableClientTransport{Endpoint: s.URL + "/mcp", HTTPClient: &http.Client{Transport: headerTransport{http.DefaultTransport}}}, nil) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | defer session.Close() |
| 56 | result, err := session.CallTool(t.Context(), &mcp.CallToolParams{Name: "notes_echo", Arguments: json.RawMessage(`{"team":"acme","id":9007199254740993}`)}) |
| 57 | if err != nil || result.IsError { |
| 58 | t.Fatal(result, err) |
| 59 | } |
| 60 | if len(result.Content) != 1 || !strings.Contains(result.Content[0].(*mcp.TextContent).Text, "9007199254740993") { |
| 61 | t.Fatal("MCP text rounded ID") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestFractionalSchemaBoundsAreExact(t *testing.T) { |
| 66 | calls := 0 |
| 67 | r, err := NewRegistry(Operation{Name: "notes_number", Description: "Validate a decimal", Scope: "notes:write", |
| 68 | Input: json.RawMessage(`{"type":"object","required":["team","value"],"properties":{"team":{"type":"string"},"value":{"type":"number","maximum":0.1}}}`), |
| 69 | Output: json.RawMessage(`{"type":"object"}`), Invoke: func(context.Context, Principal, json.RawMessage) (any, error) { calls++; return map[string]any{}, nil }}) |
| 70 | if err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | for _, number := range []string{"0.100000000000000005", "1e999999999", "1e-999999999"} { |
| 74 | if _, err = r.Call(t.Context(), principal(), "notes_number", []byte(`{"team":"acme","value":`+number+`}`)); err == nil { |
| 75 | t.Fatal("accepted out-of-bounds number", number) |
| 76 | } |
| 77 | } |
| 78 | if calls != 0 { |
| 79 | t.Fatal("invalid input reached handler") |
| 80 | } |
| 81 | if _, err = r.Call(t.Context(), principal(), "notes_number", []byte(`{"team":"acme","value":0.1}`)); err != nil || calls != 1 { |
| 82 | t.Fatal("valid decimal rejected", err) |
| 83 | } |
| 84 | } |
| 85 | |