|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package tests |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "net/http" |
| 22 | + "reflect" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/google/uuid" |
| 27 | + |
| 28 | + "github.com/googleapis/genai-toolbox/internal/server/mcp/jsonrpc" |
| 29 | + v20251125 "github.com/googleapis/genai-toolbox/internal/server/mcp/v20251125" |
| 30 | +) |
| 31 | + |
| 32 | +// NewMCPRequestHeader takes custom headers and appends headers required for MCP. |
| 33 | +func NewMCPRequestHeader(t *testing.T, customHeaders map[string]string) map[string]string { |
| 34 | + headers := make(map[string]string) |
| 35 | + for k, v := range customHeaders { |
| 36 | + headers[k] = v |
| 37 | + } |
| 38 | + headers["Content-Type"] = "application/json" |
| 39 | + headers["MCP-Protocol-Version"] = v20251125.PROTOCOL_VERSION |
| 40 | + return headers |
| 41 | +} |
| 42 | + |
| 43 | +// InvokeMCPTool is a transparent, native JSON-RPC execution harness for tests. |
| 44 | +func InvokeMCPTool(t *testing.T, toolName string, arguments map[string]any, requestHeader map[string]string) (int, *MCPCallToolResponse, error) { |
| 45 | + headers := NewMCPRequestHeader(t, requestHeader) |
| 46 | + |
| 47 | + req := NewMCPCallToolRequest(uuid.New().String(), toolName, arguments) |
| 48 | + reqBody, err := json.Marshal(req) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("error marshalling request body: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + resp, respBody := RunRequest(t, http.MethodPost, "http://127.0.0.1:5000/mcp", bytes.NewBuffer(reqBody), headers) |
| 54 | + |
| 55 | + var mcpResp MCPCallToolResponse |
| 56 | + if err := json.Unmarshal(respBody, &mcpResp); err != nil { |
| 57 | + if resp.StatusCode != http.StatusOK { |
| 58 | + return resp.StatusCode, nil, fmt.Errorf("%s", string(respBody)) |
| 59 | + } |
| 60 | + t.Fatalf("error parsing mcp response body: %v\nraw body: %s", err, string(respBody)) |
| 61 | + } |
| 62 | + |
| 63 | + return resp.StatusCode, &mcpResp, nil |
| 64 | +} |
| 65 | + |
| 66 | +// GetMCPToolsList is a JSON-RPC harness that fetches the tools/list registry. |
| 67 | +func GetMCPToolsList(t *testing.T, requestHeader map[string]string) (int, []any, error) { |
| 68 | + headers := NewMCPRequestHeader(t, requestHeader) |
| 69 | + |
| 70 | + req := MCPListToolsRequest{ |
| 71 | + Jsonrpc: jsonrpc.JSONRPC_VERSION, |
| 72 | + Id: uuid.New().String(), |
| 73 | + Method: v20251125.TOOLS_LIST, |
| 74 | + } |
| 75 | + reqBody, err := json.Marshal(req) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("error marshalling tools/list request body: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + resp, respBody := RunRequest(t, http.MethodPost, "http://127.0.0.1:5000/mcp", bytes.NewBuffer(reqBody), headers) |
| 81 | + |
| 82 | + var mcpResp jsonrpc.JSONRPCResponse |
| 83 | + if err := json.Unmarshal(respBody, &mcpResp); err != nil { |
| 84 | + if resp.StatusCode != http.StatusOK { |
| 85 | + return resp.StatusCode, nil, fmt.Errorf("%s", string(respBody)) |
| 86 | + } |
| 87 | + t.Fatalf("error parsing tools/list response: %v\nraw body: %s", err, string(respBody)) |
| 88 | + } |
| 89 | + |
| 90 | + resultMap, ok := mcpResp.Result.(map[string]any) |
| 91 | + if !ok { |
| 92 | + t.Fatalf("tools/list result is not a map: %v", mcpResp.Result) |
| 93 | + } |
| 94 | + |
| 95 | + toolsList, ok := resultMap["tools"].([]any) |
| 96 | + if !ok { |
| 97 | + t.Fatalf("tools/list did not contain tools array: %v", resultMap) |
| 98 | + } |
| 99 | + |
| 100 | + return resp.StatusCode, toolsList, nil |
| 101 | +} |
| 102 | + |
| 103 | +// AssertMCPError asserts that the response contains an error covering the expected message. |
| 104 | +func AssertMCPError(t *testing.T, mcpResp *MCPCallToolResponse, wantErrMsg string) { |
| 105 | + t.Helper() |
| 106 | + var errText string |
| 107 | + if mcpResp.Error != nil { |
| 108 | + errText = mcpResp.Error.Message |
| 109 | + } else if mcpResp.Result.IsError { |
| 110 | + for _, content := range mcpResp.Result.Content { |
| 111 | + if content.Type == "text" { |
| 112 | + errText += content.Text |
| 113 | + } |
| 114 | + } |
| 115 | + } else { |
| 116 | + t.Fatalf("expected error containing %q, but got success result: %v", wantErrMsg, mcpResp.Result) |
| 117 | + } |
| 118 | + |
| 119 | + if !strings.Contains(errText, wantErrMsg) { |
| 120 | + t.Fatalf("expected error text containing %q, got %q", wantErrMsg, errText) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// RunMCPToolsListMethod calls tools/list and verifies that the returned tools match the expected list. |
| 125 | +func RunMCPToolsListMethod(t *testing.T, expectedOutput []MCPToolManifest) { |
| 126 | + t.Helper() |
| 127 | + statusCodeList, toolsList, errList := GetMCPToolsList(t, nil) |
| 128 | + if errList != nil { |
| 129 | + t.Fatalf("native error executing tools/list: %s", errList) |
| 130 | + } |
| 131 | + if statusCodeList != http.StatusOK { |
| 132 | + t.Fatalf("expected status 200 for tools/list, got %d", statusCodeList) |
| 133 | + } |
| 134 | + |
| 135 | + // Unmarshal toolsList into []MCPToolManifest |
| 136 | + toolsJSON, err := json.Marshal(toolsList) |
| 137 | + if err != nil { |
| 138 | + t.Fatalf("error marshalling tools list: %v", err) |
| 139 | + } |
| 140 | + |
| 141 | + var actualTools []MCPToolManifest |
| 142 | + if err := json.Unmarshal(toolsJSON, &actualTools); err != nil { |
| 143 | + t.Fatalf("error unmarshalling tools into MCPToolManifest: %v", err) |
| 144 | + } |
| 145 | + |
| 146 | + for _, expected := range expectedOutput { |
| 147 | + found := false |
| 148 | + for _, actual := range actualTools { |
| 149 | + if actual.Name == expected.Name { |
| 150 | + found = true |
| 151 | + // Use reflect.DeepEqual to check all fields (description, parameters, etc.) |
| 152 | + if !reflect.DeepEqual(actual, expected) { |
| 153 | + t.Fatalf("tool %s mismatch:\nwant: %+v\ngot: %+v", expected.Name, expected, actual) |
| 154 | + } |
| 155 | + break |
| 156 | + } |
| 157 | + } |
| 158 | + if !found { |
| 159 | + t.Fatalf("tool %s was not found in the tools/list registry", expected.Name) |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +// RunMCPCustomToolCallMethod invokes a tool and compares the result with expected output. |
| 165 | +func RunMCPCustomToolCallMethod(t *testing.T, toolName string, arguments map[string]any, want string) { |
| 166 | + t.Helper() |
| 167 | + statusCode, mcpResp, err := InvokeMCPTool(t, toolName, arguments, nil) |
| 168 | + if err != nil { |
| 169 | + t.Fatalf("native error executing %s: %s", toolName, err) |
| 170 | + } |
| 171 | + if statusCode != http.StatusOK { |
| 172 | + t.Fatalf("expected status 200, got %d", statusCode) |
| 173 | + } |
| 174 | + if mcpResp.Result.IsError { |
| 175 | + t.Fatalf("%s returned error result: %v", toolName, mcpResp.Result) |
| 176 | + } |
| 177 | + if len(mcpResp.Result.Content) == 0 { |
| 178 | + t.Fatalf("%s returned empty content field", toolName) |
| 179 | + } |
| 180 | + got := mcpResp.Result.Content[0].Text |
| 181 | + if !strings.Contains(got, want) { |
| 182 | + t.Fatalf(`expected %q to contain %q`, got, want) |
| 183 | + } |
| 184 | +} |
0 commit comments