-
Notifications
You must be signed in to change notification settings - Fork 168
feet(tools+context): Dynamic tool reg #309
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
ioanarm
wants to merge
6
commits into
main
Choose a base branch
from
dynamic-tool-reg
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.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7d2c8ac
add dynamic tool functionality
ioanarm a54db45
add test file
ioanarm 20f208d
enable tools and dynamic tools enablement
ioanarm 9821b86
add a comment on where the dynamic tool capability is triggered
ioanarm b49f6e9
remove some comments
ioanarm 6853654
update README
ioanarm 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
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,160 @@ | ||
package mcpgrafana | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"sync" | ||
|
||
"github.com/mark3labs/mcp-go/server" | ||
) | ||
|
||
// Toolset represents a category of related tools that can be dynamically enabled or disabled | ||
type Toolset struct { | ||
Name string | ||
Description string | ||
Tools []Tool | ||
AddFunc func(*server.MCPServer) | ||
} | ||
|
||
// DynamicToolManager manages dynamic tool registration and discovery | ||
type DynamicToolManager struct { | ||
server *server.MCPServer | ||
toolsets map[string]*Toolset | ||
enabled map[string]bool | ||
mu sync.RWMutex | ||
} | ||
|
||
// NewDynamicToolManager creates a new dynamic tool manager | ||
func NewDynamicToolManager(srv *server.MCPServer) *DynamicToolManager { | ||
return &DynamicToolManager{ | ||
server: srv, | ||
toolsets: make(map[string]*Toolset), | ||
enabled: make(map[string]bool), | ||
} | ||
} | ||
|
||
// RegisterToolset registers a toolset for dynamic discovery | ||
func (dtm *DynamicToolManager) RegisterToolset(toolset *Toolset) { | ||
dtm.mu.Lock() | ||
defer dtm.mu.Unlock() | ||
dtm.toolsets[toolset.Name] = toolset | ||
slog.Debug("Registered toolset", "name", toolset.Name, "description", toolset.Description) | ||
} | ||
|
||
// EnableToolset enables a specific toolset by name | ||
func (dtm *DynamicToolManager) EnableToolset(ctx context.Context, name string) error { | ||
dtm.mu.Lock() | ||
defer dtm.mu.Unlock() | ||
|
||
toolset, exists := dtm.toolsets[name] | ||
if !exists { | ||
return fmt.Errorf("toolset not found: %s", name) | ||
} | ||
|
||
if dtm.enabled[name] { | ||
slog.Debug("Toolset already enabled", "name", name) | ||
return nil | ||
} | ||
|
||
// Add tools using the toolset's AddFunc | ||
// Note: The mcp-go library automatically sends a tools/list_changed notification | ||
// when AddTool is called (via the Register method), so we don't need to manually | ||
// send notifications here. This happens because WithToolCapabilities(true) was set | ||
// during server initialization. | ||
if toolset.AddFunc != nil { | ||
toolset.AddFunc(dtm.server) | ||
} | ||
|
||
dtm.enabled[name] = true | ||
slog.Info("Enabled toolset", "name", name) | ||
return nil | ||
} | ||
|
||
// DisableToolset disables a specific toolset | ||
// Note: mcp-go doesn't support removing tools at runtime, so this just marks it as disabled | ||
func (dtm *DynamicToolManager) DisableToolset(name string) error { | ||
dtm.mu.Lock() | ||
defer dtm.mu.Unlock() | ||
|
||
if _, exists := dtm.toolsets[name]; !exists { | ||
return fmt.Errorf("toolset not found: %s", name) | ||
} | ||
|
||
dtm.enabled[name] = false | ||
slog.Info("Disabled toolset", "name", name) | ||
return nil | ||
} | ||
|
||
// ListToolsets returns information about all available toolsets | ||
func (dtm *DynamicToolManager) ListToolsets() []ToolsetInfo { | ||
dtm.mu.RLock() | ||
defer dtm.mu.RUnlock() | ||
|
||
toolsets := make([]ToolsetInfo, 0, len(dtm.toolsets)) | ||
for name, toolset := range dtm.toolsets { | ||
toolsets = append(toolsets, ToolsetInfo{ | ||
Name: name, | ||
Description: toolset.Description, | ||
Enabled: dtm.enabled[name], | ||
}) | ||
} | ||
return toolsets | ||
} | ||
|
||
// ToolsetInfo provides information about a toolset | ||
type ToolsetInfo struct { | ||
Name string `json:"name" jsonschema:"required,description=The name of the toolset"` | ||
Description string `json:"description" jsonschema:"description=Description of what the toolset provides"` | ||
Enabled bool `json:"enabled" jsonschema:"description=Whether the toolset is currently enabled"` | ||
} | ||
|
||
// AddDynamicDiscoveryTools adds the list and enable toolset tools to the server | ||
func AddDynamicDiscoveryTools(dtm *DynamicToolManager, srv *server.MCPServer) { | ||
type ListToolsetsRequest struct{} | ||
|
||
listToolsetsHandler := func(ctx context.Context, request ListToolsetsRequest) ([]ToolsetInfo, error) { | ||
return dtm.ListToolsets(), nil | ||
} | ||
|
||
listToolsetsTool := MustTool( | ||
"grafana_list_toolsets", | ||
"List all available Grafana toolsets that can be enabled dynamically. Each toolset provides a category of related functionality.", | ||
listToolsetsHandler, | ||
) | ||
listToolsetsTool.Register(srv) | ||
|
||
// Tool to enable a specific toolset | ||
type EnableToolsetRequest struct { | ||
Toolset string `json:"toolset" jsonschema:"required,description=The name of the toolset to enable (e.g. 'prometheus' 'loki' 'dashboard' 'incident')"` | ||
} | ||
|
||
enableToolsetHandler := func(ctx context.Context, request EnableToolsetRequest) (string, error) { | ||
if err := dtm.EnableToolset(ctx, request.Toolset); err != nil { | ||
return "", err | ||
} | ||
|
||
// Get toolset info to provide better guidance | ||
toolsetInfo := dtm.getToolsetInfo(request.Toolset) | ||
if toolsetInfo == nil { | ||
return fmt.Sprintf("Successfully enabled toolset: %s. The tools are now available for use.", request.Toolset), nil | ||
} | ||
|
||
return fmt.Sprintf("Successfully enabled toolset: %s\n\nDescription: %s\n\nNote: All tools are already registered and available. You can now use the tools from this toolset directly.", | ||
request.Toolset, toolsetInfo.Description), nil | ||
} | ||
|
||
enableToolsetTool := MustTool( | ||
"grafana_enable_toolset", | ||
"Enable a specific Grafana toolset to make its tools available. Use grafana_list_toolsets to see available toolsets.", | ||
enableToolsetHandler, | ||
) | ||
enableToolsetTool.Register(srv) | ||
} | ||
|
||
// getToolsetInfo returns information about a specific toolset | ||
func (dtm *DynamicToolManager) getToolsetInfo(name string) *Toolset { | ||
dtm.mu.RLock() | ||
defer dtm.mu.RUnlock() | ||
return dtm.toolsets[name] | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it would be worth including
ToolNames []string
here, as a more concrete hint of what tools this contains?