-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(group): add unified Group type deriving toolset and promptset views #3488
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
Open
twishabansal
wants to merge
5
commits into
main
Choose a base branch
from
feat/groups-pr1-group-package
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+276
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
caa1d3f
feat(group): add unified Group type deriving toolset and promptset views
twishabansal 47e8679
refactor(group): store toolset and promptset to preserve O(1) lookups
twishabansal 847f22b
Merge branch 'main' into feat/groups-pr1-group-package
twishabansal 3ecb078
Merge branch 'main' into feat/groups-pr1-group-package
twishabansal a97762a
Merge branch 'main' into feat/groups-pr1-group-package
twishabansal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // 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 group | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/googleapis/mcp-toolbox/internal/prompts" | ||
| "github.com/googleapis/mcp-toolbox/internal/tools" | ||
| ) | ||
|
|
||
| // GroupConfig is the parsed configuration for a group: a single named collection | ||
| // that holds both tools and prompts. Its description doubles as the MCP server | ||
| // instructions for clients connected to the group. | ||
| type GroupConfig struct { | ||
| Name string `yaml:"name"` | ||
| Description string `yaml:"description"` | ||
| ToolNames []string `yaml:"tools"` | ||
| PromptNames []string `yaml:"prompts"` | ||
| } | ||
|
|
||
| // Group is an initialized group and the source of truth from which the legacy | ||
| // toolset and promptset views are derived (see ToToolset and ToPromptset). It | ||
| // holds the fully-initialized toolset and promptset so the derived views retain | ||
| // their O(1) lookup sets instead of being rebuilt on every projection. | ||
| type Group struct { | ||
| GroupConfig | ||
| toolset tools.Toolset | ||
| promptset prompts.Promptset | ||
| } | ||
|
|
||
| // Initialize validates the declared tools and prompts against the provided maps | ||
| // and builds the derived toolset and promptset. It delegates to | ||
| // tools.ToolsetConfig.Initialize and prompts.PromptsetConfig.Initialize so that | ||
| // validation and manifest-building stay identical to the legacy types. | ||
| func (gc GroupConfig) Initialize(serverVersion string, toolsMap map[string]tools.Tool, promptsMap map[string]prompts.Prompt) (Group, error) { | ||
| if !tools.IsValidName(gc.Name) { | ||
| return Group{}, fmt.Errorf("invalid group name: %s", gc.Name) | ||
| } | ||
|
|
||
| toolset, err := tools.ToolsetConfig{Name: gc.Name, ToolNames: gc.ToolNames}.Initialize(serverVersion, toolsMap) | ||
| if err != nil { | ||
| return Group{}, err | ||
| } | ||
| promptset, err := prompts.PromptsetConfig{Name: gc.Name, PromptNames: gc.PromptNames}.Initialize(serverVersion, promptsMap) | ||
| if err != nil { | ||
| return Group{}, err | ||
| } | ||
|
|
||
| return Group{GroupConfig: gc, toolset: toolset, promptset: promptset}, nil | ||
| } | ||
|
|
||
| // ToToolset returns the derived toolset view, keyed by the group's name, so that | ||
| // existing toolset call sites keep working unchanged. | ||
| func (g Group) ToToolset() tools.Toolset { | ||
| return g.toolset | ||
| } | ||
|
|
||
| // ToPromptset returns the derived promptset view, keyed by the group's name, so | ||
| // that prompts scope to the connected group. | ||
| func (g Group) ToPromptset() prompts.Promptset { | ||
| return g.promptset | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| // 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 group_test | ||
|
|
||
| import ( | ||
| "slices" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/googleapis/mcp-toolbox/internal/group" | ||
| "github.com/googleapis/mcp-toolbox/internal/prompts" | ||
| "github.com/googleapis/mcp-toolbox/internal/testutils" | ||
| "github.com/googleapis/mcp-toolbox/internal/tools" | ||
| "github.com/googleapis/mcp-toolbox/internal/util/parameters" | ||
| ) | ||
|
|
||
| const serverVersion = "test-version" | ||
|
|
||
| func testFixtures() (map[string]tools.Tool, map[string]prompts.Prompt) { | ||
| toolsMap := map[string]tools.Tool{ | ||
| "tool1": testutils.NewMockTool("tool1", "first tool", []parameters.Parameter{}, false, false), | ||
| "tool2": testutils.NewMockTool("tool2", "second tool", []parameters.Parameter{}, false, false), | ||
| } | ||
| promptsMap := map[string]prompts.Prompt{ | ||
| "prompt1": testutils.NewMockPrompt("prompt1", "first prompt", prompts.Arguments{}), | ||
| "prompt2": testutils.NewMockPrompt("prompt2", "second prompt", prompts.Arguments{}), | ||
| } | ||
| return toolsMap, promptsMap | ||
| } | ||
|
|
||
| func TestGroupConfig_Initialize(t *testing.T) { | ||
| t.Parallel() | ||
| toolsMap, promptsMap := testFixtures() | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| config group.GroupConfig | ||
| wantTools []string | ||
| wantPrompts []string | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "tools and prompts", | ||
| config: group.GroupConfig{ | ||
| Name: "mygroup", | ||
| Description: "a group", | ||
| ToolNames: []string{"tool1", "tool2"}, | ||
| PromptNames: []string{"prompt1", "prompt2"}, | ||
| }, | ||
| wantTools: []string{"tool1", "tool2"}, | ||
| wantPrompts: []string{"prompt1", "prompt2"}, | ||
| }, | ||
| { | ||
| name: "tools only", | ||
| config: group.GroupConfig{ | ||
| Name: "toolsonly", | ||
| ToolNames: []string{"tool1"}, | ||
| }, | ||
| wantTools: []string{"tool1"}, | ||
| wantPrompts: []string{}, | ||
| }, | ||
| { | ||
| name: "prompts only", | ||
| config: group.GroupConfig{ | ||
| Name: "promptsonly", | ||
| PromptNames: []string{"prompt1"}, | ||
| }, | ||
| wantTools: []string{}, | ||
| wantPrompts: []string{"prompt1"}, | ||
| }, | ||
| { | ||
| name: "default nameless group", | ||
| config: group.GroupConfig{ | ||
| Name: "", | ||
| ToolNames: []string{"tool1"}, | ||
| PromptNames: []string{"prompt1"}, | ||
| }, | ||
| wantTools: []string{"tool1"}, | ||
| wantPrompts: []string{"prompt1"}, | ||
| }, | ||
| { | ||
| name: "invalid group name", | ||
| config: group.GroupConfig{ | ||
| Name: "bad name!", | ||
| ToolNames: []string{"tool1"}, | ||
| }, | ||
| wantErr: "invalid group name", | ||
| }, | ||
| { | ||
| name: "missing tool", | ||
| config: group.GroupConfig{ | ||
| Name: "g", | ||
| ToolNames: []string{"nope"}, | ||
| }, | ||
| wantErr: "tool does not exist: nope", | ||
| }, | ||
| { | ||
| name: "missing prompt", | ||
| config: group.GroupConfig{ | ||
| Name: "g", | ||
| PromptNames: []string{"nope"}, | ||
| }, | ||
| wantErr: "prompt does not exist: nope", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| g, err := tc.config.Initialize(serverVersion, toolsMap, promptsMap) | ||
| if tc.wantErr != "" { | ||
| if err == nil { | ||
| t.Fatalf("expected error containing %q, got nil", tc.wantErr) | ||
| } | ||
| if !strings.Contains(err.Error(), tc.wantErr) { | ||
| t.Fatalf("error = %q, want it to contain %q", err.Error(), tc.wantErr) | ||
| } | ||
| return | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| ts := g.ToToolset() | ||
| if got := toolNames(ts.Tools); !slices.Equal(got, tc.wantTools) { | ||
| t.Errorf("tools = %v, want %v", got, tc.wantTools) | ||
| } | ||
| ps := g.ToPromptset() | ||
| if len(ps.Prompts) != len(tc.wantPrompts) { | ||
| t.Errorf("got %d prompts, want %d", len(ps.Prompts), len(tc.wantPrompts)) | ||
| } | ||
| for _, name := range tc.wantPrompts { | ||
| if !ps.ContainsPrompt(name) { | ||
| t.Errorf("derived promptset missing prompt %q", name) | ||
| } | ||
| } | ||
| if ts.Manifest.ServerVersion != serverVersion { | ||
| t.Errorf("tools manifest server version = %q, want %q", ts.Manifest.ServerVersion, serverVersion) | ||
| } | ||
| if ps.Manifest.ServerVersion != serverVersion { | ||
| t.Errorf("prompts manifest server version = %q, want %q", ps.Manifest.ServerVersion, serverVersion) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGroup_Projections(t *testing.T) { | ||
| t.Parallel() | ||
| toolsMap, promptsMap := testFixtures() | ||
|
|
||
| g, err := group.GroupConfig{ | ||
| Name: "mygroup", | ||
| Description: "a group", | ||
| ToolNames: []string{"tool1", "tool2"}, | ||
| PromptNames: []string{"prompt1", "prompt2"}, | ||
| }.Initialize(serverVersion, toolsMap, promptsMap) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| ts := g.ToToolset() | ||
| if ts.Name != "mygroup" { | ||
| t.Errorf("toolset name = %q, want %q", ts.Name, "mygroup") | ||
| } | ||
| if !ts.ContainsTool("tool1") || !ts.ContainsTool("tool2") { | ||
| t.Errorf("derived toolset missing expected tools") | ||
| } | ||
| if ts.ContainsTool("tool3") { | ||
| t.Errorf("derived toolset reports an absent tool") | ||
| } | ||
|
|
||
| ps := g.ToPromptset() | ||
| if ps.Name != "mygroup" { | ||
| t.Errorf("promptset name = %q, want %q", ps.Name, "mygroup") | ||
| } | ||
| if !ps.ContainsPrompt("prompt1") || !ps.ContainsPrompt("prompt2") { | ||
| t.Errorf("derived promptset missing expected prompts") | ||
| } | ||
| if ps.ContainsPrompt("prompt3") { | ||
| t.Errorf("derived promptset reports an absent prompt") | ||
| } | ||
| } | ||
|
|
||
| func toolNames(ts []*tools.Tool) []string { | ||
| names := make([]string, 0, len(ts)) | ||
| for _, t := range ts { | ||
| names = append(names, (*t).GetName()) | ||
| } | ||
| return names | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.