Skip to content

Commit c25b743

Browse files
committed
Remove duplication
Signed-off-by: David Gageot <[email protected]>
1 parent e2cac2d commit c25b743

File tree

1 file changed

+28
-18
lines changed
  • cmd/docker-mcp/internal/gateway

1 file changed

+28
-18
lines changed

cmd/docker-mcp/internal/gateway/run.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,36 +90,25 @@ func (g *Gateway) Run(ctx context.Context) error {
9090
}
9191
toolCallbacks := interceptors.Callbacks(g.LogCalls, g.BlockSecrets, customInterceptors)
9292

93-
// Which servers are enabled in the registry.yaml?
94-
serverNames := configuration.ServerNames()
95-
if len(serverNames) == 0 {
96-
log("- No server is enabled")
97-
} else {
98-
log("- Those servers are enabled:", strings.Join(serverNames, ", "))
99-
}
100-
101-
// List all the available tools.
102-
startList := time.Now()
103-
log("- Listing MCP tools...")
104-
capabilities, err := g.listCapabilities(ctx, configuration, serverNames)
105-
if err != nil {
106-
return fmt.Errorf("listing resources: %w", err)
107-
}
108-
log(">", len(capabilities.Tools), "tools listed in", time.Since(startList))
109-
11093
// TODO: cleanup stopped servers. That happens in stdio over TCP mode.
11194
var (
11295
lock sync.Mutex
11396
changeListeners []func(*Capabilities)
11497
)
11598

99+
capabilities, err := g.listServersAndCapabilities(ctx, configuration)
100+
if err != nil {
101+
return fmt.Errorf("listing capabilities: %w", err)
102+
}
103+
116104
newMCPServer := func() *server.MCPServer {
117105
mcpServer := server.NewMCPServer(
118106
"Docker AI MCP Gateway",
119107
"2.0.1",
120108
server.WithToolHandlerMiddleware(toolCallbacks),
121109
)
122110

111+
// TODO: This will create a new server instance with an outdated vision of the capabilities.
123112
refreshCapabilities(mcpServer, capabilities)
124113

125114
lock.Lock()
@@ -148,7 +137,7 @@ func (g *Gateway) Run(ctx context.Context) error {
148137
continue
149138
}
150139

151-
capabilities, err := g.listCapabilities(ctx, configuration, configuration.ServerNames())
140+
capabilities, err := g.listServersAndCapabilities(ctx, configuration)
152141
if err != nil {
153142
logf("> Unable to list capabilities: %s", err)
154143
continue
@@ -197,6 +186,27 @@ func (g *Gateway) Run(ctx context.Context) error {
197186
}
198187
}
199188

189+
func (g *Gateway) listServersAndCapabilities(ctx context.Context, configuration Configuration) (*Capabilities, error) {
190+
// Which servers are enabled in the registry.yaml?
191+
serverNames := configuration.ServerNames()
192+
if len(serverNames) == 0 {
193+
log("- No server is enabled")
194+
} else {
195+
log("- Those servers are enabled:", strings.Join(serverNames, ", "))
196+
}
197+
198+
// List all the available tools.
199+
startList := time.Now()
200+
log("- Listing MCP tools...")
201+
capabilities, err := g.listCapabilities(ctx, configuration, serverNames)
202+
if err != nil {
203+
return nil, fmt.Errorf("listing resources: %w", err)
204+
}
205+
log(">", len(capabilities.Tools), "tools listed in", time.Since(startList))
206+
207+
return capabilities, nil
208+
}
209+
200210
func refreshCapabilities(s *server.MCPServer, c *Capabilities) {
201211
s.SetTools(c.Tools...)
202212
s.SetPrompts(c.Prompts...)

0 commit comments

Comments
 (0)