Skip to content

Commit 4776cdf

Browse files
Refactor: Prepare gateway for dynamic server configuration updates (#191)
* Refactor: Update mcpServer handler functions to use serverName Changed the three mcpServer handler functions to take `serverName` string parameter instead of `*catalog.ServerConfig`: - mcpServerToolHandler - mcpServerPromptHandler - mcpServerResourceHandler Each function now looks up the ServerConfig dynamically using `g.configuration.Find(serverName)` at the start of the handler closure. Benefits: - Reduces coupling between handler registration and server configuration - Handlers automatically use the latest configuration without re-registration - Consistent pattern across all three handler types - Better error handling with explicit server lookup failures All call sites in capabilitites.go updated to pass serverConfig.Name instead of serverConfig. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Refactor: Use slices.Contains instead of manual loop Replace manual loop to check if serverName exists in serverNames slice with slices.Contains for cleaner, more idiomatic Go code. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Refactor: Remove configuration parameter from listCapabilities Changed listCapabilities to use g.configuration directly instead of accepting it as a parameter. This simplifies the function signature and ensures it always uses the gateway's current configuration. Changes: - Updated function signature to remove Configuration parameter - Changed configuration.Find() to g.configuration.Find() - Updated isToolEnabled() calls to use g.configuration - Updated both call sites in reload.go Benefits: - Simpler function signature - Eliminates possibility of passing stale configuration - Consistent with other methods that access g.configuration directly - Reduces parameter passing overhead 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent fe07398 commit 4776cdf

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

pkg/gateway/capabilitites.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (caps *Capabilities) getResourceTemplateByURITemplate(resource string) (Res
110110
return ResourceTemplateRegistration{}, fmt.Errorf("unable to find resource template")
111111
}
112112

113-
func (g *Gateway) listCapabilities(ctx context.Context, configuration Configuration, serverNames []string, clientConfig *clientConfig) (*Capabilities, error) {
113+
func (g *Gateway) listCapabilities(ctx context.Context, serverNames []string, clientConfig *clientConfig) (*Capabilities, error) {
114114
var (
115115
lock sync.Mutex
116116
allCapabilities []Capabilities
@@ -119,7 +119,7 @@ func (g *Gateway) listCapabilities(ctx context.Context, configuration Configurat
119119
errs, ctx := errgroup.WithContext(ctx)
120120
errs.SetLimit(runtime.NumCPU())
121121
for _, serverName := range serverNames {
122-
serverConfig, toolGroup, found := configuration.Find(serverName)
122+
serverConfig, toolGroup, found := g.configuration.Find(serverName)
123123

124124
switch {
125125
case !found:
@@ -148,7 +148,7 @@ func (g *Gateway) listCapabilities(ctx context.Context, configuration Configurat
148148
prefix := g.getToolNamePrefix(serverConfig)
149149

150150
for _, tool := range tools.Tools {
151-
if !isToolEnabled(configuration, serverConfig.Name, serverConfig.Spec.Image, tool.Name, g.ToolNames) {
151+
if !isToolEnabled(g.configuration, serverConfig.Name, serverConfig.Spec.Image, tool.Name, g.ToolNames) {
152152
continue
153153
}
154154

@@ -159,7 +159,7 @@ func (g *Gateway) listCapabilities(ctx context.Context, configuration Configurat
159159
capabilities.Tools = append(capabilities.Tools, ToolRegistration{
160160
ServerName: serverConfig.Name,
161161
Tool: &prefixedTool,
162-
Handler: g.mcpServerToolHandler(serverConfig, g.mcpServer, tool.Annotations),
162+
Handler: g.mcpServerToolHandler(serverConfig.Name, g.mcpServer, tool.Annotations),
163163
})
164164
}
165165
}
@@ -173,7 +173,7 @@ func (g *Gateway) listCapabilities(ctx context.Context, configuration Configurat
173173
capabilities.Prompts = append(capabilities.Prompts, PromptRegistration{
174174
ServerName: serverConfig.Name,
175175
Prompt: prompt,
176-
Handler: g.mcpServerPromptHandler(serverConfig, g.mcpServer),
176+
Handler: g.mcpServerPromptHandler(serverConfig.Name, g.mcpServer),
177177
})
178178
}
179179
}
@@ -187,7 +187,7 @@ func (g *Gateway) listCapabilities(ctx context.Context, configuration Configurat
187187
capabilities.Resources = append(capabilities.Resources, ResourceRegistration{
188188
ServerName: serverConfig.Name,
189189
Resource: resource,
190-
Handler: g.mcpServerResourceHandler(serverConfig, g.mcpServer),
190+
Handler: g.mcpServerResourceHandler(serverConfig.Name, g.mcpServer),
191191
})
192192
}
193193
}
@@ -201,7 +201,7 @@ func (g *Gateway) listCapabilities(ctx context.Context, configuration Configurat
201201
capabilities.ResourceTemplates = append(capabilities.ResourceTemplates, ResourceTemplateRegistration{
202202
ServerName: serverConfig.Name,
203203
ResourceTemplate: *resourceTemplate,
204-
Handler: g.mcpServerResourceHandler(serverConfig, g.mcpServer),
204+
Handler: g.mcpServerResourceHandler(serverConfig.Name, g.mcpServer),
205205
})
206206
}
207207
}
@@ -241,7 +241,7 @@ func (g *Gateway) listCapabilities(ctx context.Context, configuration Configurat
241241
}
242242

243243
for _, tool := range *toolGroup {
244-
if !isToolEnabled(configuration, serverName, "", tool.Name, g.ToolNames) {
244+
if !isToolEnabled(g.configuration, serverName, "", tool.Name, g.ToolNames) {
245245
continue
246246
}
247247

pkg/gateway/dynamic_mcps.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,7 @@ func (g *Gateway) createMcpAddTool(clientConfig *clientConfig) *ToolRegistration
497497
}
498498

499499
// Append the new server to the current serverNames if not already present
500-
found = false
501-
for _, existing := range g.configuration.serverNames {
502-
if existing == serverName {
503-
found = true
504-
break
505-
}
506-
}
500+
found = slices.Contains(g.configuration.serverNames, serverName)
507501
if !found {
508502
g.configuration.serverNames = append(g.configuration.serverNames, serverName)
509503
}

pkg/gateway/handlers.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ func (g *Gateway) mcpToolHandler(tool catalog.Tool) mcp.ToolHandler {
5757
}
5858
}
5959

60-
func (g *Gateway) mcpServerToolHandler(serverConfig *catalog.ServerConfig, server *mcp.Server, annotations *mcp.ToolAnnotations) mcp.ToolHandler {
60+
func (g *Gateway) mcpServerToolHandler(serverName string, server *mcp.Server, annotations *mcp.ToolAnnotations) mcp.ToolHandler {
6161
return func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
62+
// Look up server configuration
63+
serverConfig, _, ok := g.configuration.Find(serverName)
64+
if !ok {
65+
return nil, fmt.Errorf("server %q not found in configuration", serverName)
66+
}
67+
6268
// Debug logging to stderr
6369
if os.Getenv("DOCKER_MCP_TELEMETRY_DEBUG") != "" {
6470
fmt.Fprintf(os.Stderr, "[MCP-HANDLER] Tool call received: %s from server: %s\n", req.Params.Name, serverConfig.Name)
@@ -152,8 +158,14 @@ func (g *Gateway) mcpServerToolHandler(serverConfig *catalog.ServerConfig, serve
152158
}
153159
}
154160

155-
func (g *Gateway) mcpServerPromptHandler(serverConfig *catalog.ServerConfig, server *mcp.Server) mcp.PromptHandler {
161+
func (g *Gateway) mcpServerPromptHandler(serverName string, server *mcp.Server) mcp.PromptHandler {
156162
return func(ctx context.Context, req *mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
163+
// Look up server configuration
164+
serverConfig, _, ok := g.configuration.Find(serverName)
165+
if !ok {
166+
return nil, fmt.Errorf("server %q not found in configuration", serverName)
167+
}
168+
157169
// Debug logging to stderr
158170
if os.Getenv("DOCKER_MCP_TELEMETRY_DEBUG") != "" {
159171
fmt.Fprintf(os.Stderr, "[MCP-HANDLER] Prompt get received: %s from server: %s\n", req.Params.Name, serverConfig.Name)
@@ -212,8 +224,14 @@ func (g *Gateway) mcpServerPromptHandler(serverConfig *catalog.ServerConfig, ser
212224
}
213225
}
214226

215-
func (g *Gateway) mcpServerResourceHandler(serverConfig *catalog.ServerConfig, server *mcp.Server) mcp.ResourceHandler {
227+
func (g *Gateway) mcpServerResourceHandler(serverName string, server *mcp.Server) mcp.ResourceHandler {
216228
return func(ctx context.Context, req *mcp.ReadResourceRequest) (*mcp.ReadResourceResult, error) {
229+
// Look up server configuration
230+
serverConfig, _, ok := g.configuration.Find(serverName)
231+
if !ok {
232+
return nil, fmt.Errorf("server %q not found in configuration", serverName)
233+
}
234+
217235
// Debug logging to stderr
218236
if os.Getenv("DOCKER_MCP_TELEMETRY_DEBUG") != "" {
219237
fmt.Fprintf(os.Stderr, "[MCP-HANDLER] Resource read received: %s from server: %s\n", req.Params.URI, serverConfig.Name)

pkg/gateway/reload.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (g *Gateway) reloadConfiguration(ctx context.Context, configuration Configu
2626
// List all the available tools.
2727
startList := time.Now()
2828
log.Log("- Listing MCP tools...")
29-
capabilities, err := g.listCapabilities(ctx, configuration, serverNames, clientConfig)
29+
capabilities, err := g.listCapabilities(ctx, serverNames, clientConfig)
3030
if err != nil {
3131
return fmt.Errorf("listing resources: %w", err)
3232
}
@@ -201,7 +201,7 @@ func (g *Gateway) reloadServerConfiguration(ctx context.Context, serverName stri
201201
}
202202

203203
// Get current newServerCaps from the server (this reflects the server's current state after it notified us of changes)
204-
newServerCaps, err := g.listCapabilities(ctx, g.configuration, []string{serverName}, clientConfig)
204+
newServerCaps, err := g.listCapabilities(ctx, []string{serverName}, clientConfig)
205205
if err != nil {
206206
return fmt.Errorf("failed to list capabilities for %s: %w", serverName, err)
207207
}

0 commit comments

Comments
 (0)