-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: add emulator mode for mocked tool responses #3062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,160 @@ | ||||||||||||||
| // Copyright 2026 Google LLC | ||||||||||||||
| // | ||||||||||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||
| // you may not use this file except in compliance with the License. | ||||||||||||||
| // You may obtain a copy of the License at | ||||||||||||||
| // | ||||||||||||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||
| // | ||||||||||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||||||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||
| // See the License for the specific language governing permissions and | ||||||||||||||
| // limitations under the License. | ||||||||||||||
|
|
||||||||||||||
| package server | ||||||||||||||
|
|
||||||||||||||
| import ( | ||||||||||||||
| "context" | ||||||||||||||
| "encoding/json" | ||||||||||||||
| "fmt" | ||||||||||||||
| "os" | ||||||||||||||
| "reflect" | ||||||||||||||
|
|
||||||||||||||
| "github.com/googleapis/mcp-toolbox/internal/embeddingmodels" | ||||||||||||||
| "github.com/googleapis/mcp-toolbox/internal/tools" | ||||||||||||||
| "github.com/googleapis/mcp-toolbox/internal/util" | ||||||||||||||
| "github.com/googleapis/mcp-toolbox/internal/util/parameters" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| type emulatorMockFile struct { | ||||||||||||||
| Mocks []emulatorMock `json:"mocks"` | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| type emulatorMock struct { | ||||||||||||||
| ToolName string `json:"tool_name"` | ||||||||||||||
| Description string `json:"description,omitempty"` | ||||||||||||||
| Parameters map[string]any `json:"parameters"` | ||||||||||||||
| Response any `json:"response"` | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| type emulatorTool struct { | ||||||||||||||
| name string | ||||||||||||||
| base tools.Tool | ||||||||||||||
| mocks []emulatorMock | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func loadEmulatorMocks(path string) (map[string][]emulatorMock, error) { | ||||||||||||||
| raw, err := os.ReadFile(path) | ||||||||||||||
| if err != nil { | ||||||||||||||
| return nil, fmt.Errorf("unable to read emulator mocks file %q: %w", path, err) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| var file emulatorMockFile | ||||||||||||||
| if err := json.Unmarshal(raw, &file); err != nil { | ||||||||||||||
| return nil, fmt.Errorf("unable to parse emulator mocks file %q: %w", path, err) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| mocks := make(map[string][]emulatorMock) | ||||||||||||||
| for i := range file.Mocks { | ||||||||||||||
| m := &file.Mocks[i] | ||||||||||||||
| if m.ToolName == "" { | ||||||||||||||
| return nil, fmt.Errorf("invalid emulator mock at index %d: tool_name is required", i) | ||||||||||||||
| } | ||||||||||||||
| if m.Parameters == nil { | ||||||||||||||
| m.Parameters = map[string]any{} | ||||||||||||||
| } | ||||||||||||||
| normalized, err := normalizeJSONValue(m.Parameters) | ||||||||||||||
| if err != nil { | ||||||||||||||
| return nil, fmt.Errorf("failed to normalize parameters for mock at index %d: %w", i, err) | ||||||||||||||
| } | ||||||||||||||
| normalizedMap, ok := normalized.(map[string]any) | ||||||||||||||
| if !ok { | ||||||||||||||
| return nil, fmt.Errorf("failed to normalize parameters for mock at index %d: expected object parameters", i) | ||||||||||||||
| } | ||||||||||||||
| m.Parameters = normalizedMap | ||||||||||||||
| mocks[m.ToolName] = append(mocks[m.ToolName], *m) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return mocks, nil | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func wrapToolsForEmulator(toolMap map[string]tools.Tool, mocksByTool map[string][]emulatorMock) map[string]tools.Tool { | ||||||||||||||
| wrapped := make(map[string]tools.Tool, len(toolMap)) | ||||||||||||||
| for toolName, t := range toolMap { | ||||||||||||||
| wrapped[toolName] = emulatorTool{ | ||||||||||||||
| name: toolName, | ||||||||||||||
| base: t, | ||||||||||||||
| mocks: mocksByTool[toolName], | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| return wrapped | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+82
to
+92
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation wraps every single tool in the server when emulator mode is enabled. This means that any tool without a defined mock will return an error upon invocation, effectively breaking all non-mocked tools. Consider only wrapping tools that actually have mocks defined in the func wrapToolsForEmulator(toolMap map[string]tools.Tool, mocksByTool map[string][]emulatorMock) map[string]tools.Tool {
wrapped := make(map[string]tools.Tool, len(toolMap))
for toolName, t := range toolMap {
if mocks, ok := mocksByTool[toolName]; ok && len(mocks) > 0 {
wrapped[toolName] = emulatorTool{
name: toolName,
base: t,
mocks: mocks,
}
} else {
wrapped[toolName] = t
}
}
return wrapped
} |
||||||||||||||
|
|
||||||||||||||
| func normalizeJSONValue(v any) (any, error) { | ||||||||||||||
| buf, err := json.Marshal(v) | ||||||||||||||
| if err != nil { | ||||||||||||||
| return nil, err | ||||||||||||||
| } | ||||||||||||||
| var normalized any | ||||||||||||||
| if err := json.Unmarshal(buf, &normalized); err != nil { | ||||||||||||||
| return nil, err | ||||||||||||||
| } | ||||||||||||||
| return normalized, nil | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (e emulatorTool) findMock(inMap map[string]any) (any, bool) { | ||||||||||||||
| for _, mock := range e.mocks { | ||||||||||||||
| if reflect.DeepEqual(inMap, mock.Parameters) { | ||||||||||||||
| return mock.Response, true | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| return nil, false | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (e emulatorTool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) { | ||||||||||||||
| in, err := normalizeJSONValue(params.AsMap()) | ||||||||||||||
| if err != nil { | ||||||||||||||
| return nil, util.NewAgentError("emulator mode: failed to normalize input parameters", err) | ||||||||||||||
| } | ||||||||||||||
| inMap, ok := in.(map[string]any) | ||||||||||||||
| if !ok { | ||||||||||||||
| return nil, util.NewAgentError("emulator mode: failed to normalize input parameters", nil) | ||||||||||||||
| } | ||||||||||||||
| if response, found := e.findMock(inMap); found { | ||||||||||||||
| return response, nil | ||||||||||||||
| } | ||||||||||||||
| return nil, util.NewAgentError(fmt.Sprintf("emulator mode: no mock matched for tool %q with parameters %v", e.name, inMap), nil) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (e emulatorTool) EmbedParams(ctx context.Context, params parameters.ParamValues, embeddingModels map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) { | ||||||||||||||
| return e.base.EmbedParams(ctx, params, embeddingModels) | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+130
to
+132
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By delegating It is recommended to make
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| func (e emulatorTool) Manifest() tools.Manifest { | ||||||||||||||
| return e.base.Manifest() | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (e emulatorTool) McpManifest() tools.McpManifest { | ||||||||||||||
| return e.base.McpManifest() | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (e emulatorTool) Authorized(verifiedAuthServices []string) bool { | ||||||||||||||
| return e.base.Authorized(verifiedAuthServices) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (e emulatorTool) RequiresClientAuthorization(resourceMgr tools.SourceProvider) (bool, error) { | ||||||||||||||
| return e.base.RequiresClientAuthorization(resourceMgr) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (e emulatorTool) ToConfig() tools.ToolConfig { | ||||||||||||||
| return e.base.ToConfig() | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (e emulatorTool) GetAuthTokenHeaderName(resourceMgr tools.SourceProvider) (string, error) { | ||||||||||||||
| return e.base.GetAuthTokenHeaderName(resourceMgr) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (e emulatorTool) GetParameters() parameters.Parameters { | ||||||||||||||
| return e.base.GetParameters() | ||||||||||||||
| } | ||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.