Skip to content

Commit 80583bc

Browse files
Update tool registrations during reload
1 parent a1cb7e5 commit 80583bc

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

pkg/gateway/dynamic_mcps.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,14 +673,54 @@ func (g *Gateway) createMcpAddTool(clientConfig *clientConfig) *ToolRegistration
673673
log.Log("Warning: Failed to persist configuration:", err)
674674
}
675675

676+
// Get the list of tools that were just added from this server
677+
var addedTools []*mcp.Tool
678+
g.capabilitiesMu.RLock()
679+
if serverCaps := g.serverCapabilities[serverName]; serverCaps != nil {
680+
for _, toolName := range serverCaps.ToolNames {
681+
if toolReg, exists := g.toolRegistrations[toolName]; exists {
682+
addedTools = append(addedTools, toolReg.Tool)
683+
}
684+
}
685+
}
686+
g.capabilitiesMu.RUnlock()
687+
688+
// Build the response text
689+
responseText := fmt.Sprintf("Successfully added %d tools in server '%s'. Assume that it is fully configured and ready to use.", len(addedTools), serverName)
690+
691+
// Include the JSON representation of the newly added tools
692+
// This is useful when the client session does not support tool change notifications
693+
if len(addedTools) > 0 {
694+
// Create a tools list response matching the format from tools/list
695+
toolsList := make([]map[string]any, 0, len(addedTools))
696+
for _, tool := range addedTools {
697+
toolMap := map[string]any{
698+
"name": tool.Name,
699+
"description": tool.Description,
700+
}
701+
if tool.InputSchema != nil {
702+
toolMap["inputSchema"] = tool.InputSchema
703+
}
704+
toolsList = append(toolsList, toolMap)
705+
}
706+
707+
// Convert to JSON
708+
toolsJSON, err := json.MarshalIndent(map[string]any{
709+
"tools": toolsList,
710+
}, "", " ")
711+
if err == nil {
712+
responseText += "\n\nNewly added tools:\n```json\n" + string(toolsJSON) + "\n```"
713+
}
714+
}
715+
676716
// Register DCR client and start OAuth provider if this is a remote OAuth server
677717
if g.McpOAuthDcrEnabled && serverConfig != nil && serverConfig.Spec.IsRemoteOAuthServer() {
678718
return g.addRemoteOAuthServer(ctx, serverName, req)
679719
}
680720

681721
return &mcp.CallToolResult{
682722
Content: []mcp.Content{&mcp.TextContent{
683-
Text: fmt.Sprintf("Successfully added server '%s'. Assume that it is fully configured and ready to use.", serverName),
723+
Text: responseText,
684724
}},
685725
}, nil
686726
}

pkg/gateway/reload.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ func (g *Gateway) reloadServerConfiguration(ctx context.Context, serverName stri
268268
// Remove old capabilities that are no longer present
269269
if len(removedTools) > 0 {
270270
g.mcpServer.RemoveTools(removedTools...)
271+
// Remove from tool registrations tracking
272+
for _, toolName := range removedTools {
273+
delete(g.toolRegistrations, toolName)
274+
}
271275
log.Log(" - Removed", len(removedTools), "tools for", serverName)
272276
}
273277

@@ -290,6 +294,8 @@ func (g *Gateway) reloadServerConfiguration(ctx context.Context, serverName stri
290294
for _, tool := range addedTools {
291295
if registration, err := newServerCaps.getToolByName(tool); err == nil {
292296
g.mcpServer.AddTool(registration.Tool, registration.Handler)
297+
// Track tool registration for mcp-exec and mcp-add
298+
g.toolRegistrations[registration.Tool.Name] = registration
293299
}
294300
}
295301
if len(addedTools) > 0 {

0 commit comments

Comments
 (0)