Skip to content

Commit 810ec95

Browse files
Refactor to build inventory once before authentication fork
- Move inventory building outside OnAuthenticated callback in NewUnauthenticatedMCPServer - Build inventory once before creating auth tools, eliminating duplication - Reuse the same inventory instance in OnAuthenticated via closure - Tool filters are now applied only once, preventing stale auth tools - Reduces code duplication without changing architecture - All tests pass, 0 lint issues Co-authored-by: SamMorrowDrums <4811358+SamMorrowDrums@users.noreply.github.com>
1 parent b99bcea commit 810ec95

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

internal/ghmcp/server.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,20 @@ func NewUnauthenticatedMCPServer(cfg MCPServerConfig) (*UnauthenticatedServerRes
316316
// Create auth manager
317317
authManager := github.NewAuthManager(oauthHost, cfg.OAuthClientID, cfg.OAuthClientSecret, cfg.OAuthScopes)
318318

319+
// Build the tool inventory once before forking - this ensures tool filters are applied once
320+
enabledToolsets := resolveEnabledToolsets(cfg)
321+
inventory := github.NewInventory(cfg.Translator).
322+
WithDeprecatedAliases(github.DeprecatedToolAliases).
323+
WithReadOnly(cfg.ReadOnly).
324+
WithToolsets(enabledToolsets).
325+
WithTools(github.CleanTools(cfg.EnabledTools)).
326+
WithFeatureChecker(createFeatureChecker(cfg.EnabledFeatures)).
327+
Build()
328+
329+
if unrecognized := inventory.UnrecognizedToolsets(); len(unrecognized) > 0 {
330+
fmt.Fprintf(os.Stderr, "Warning: unrecognized toolsets ignored: %s\n", strings.Join(unrecognized, ", "))
331+
}
332+
319333
// Create the MCP server with capabilities advertised for dynamic tool registration
320334
serverOpts := &mcp.ServerOptions{
321335
Instructions: "GitHub MCP Server - Authentication Required\n\nYou are not currently authenticated with GitHub. Use the auth_login tool to complete authentication. This is a single, blocking call that will guide you through the entire device authorization flow and return once authentication has finished.",
@@ -377,33 +391,16 @@ func NewUnauthenticatedMCPServer(cfg MCPServerConfig) (*UnauthenticatedServerRes
377391
}
378392
})
379393

380-
// Resolve enabled toolsets
381-
enabledToolsets := resolveEnabledToolsets(authenticatedCfg)
382-
383-
// Build and register the tool/resource/prompt inventory
384-
inv := github.NewInventory(cfg.Translator).
385-
WithDeprecatedAliases(github.DeprecatedToolAliases).
386-
WithReadOnly(cfg.ReadOnly).
387-
WithToolsets(enabledToolsets).
388-
WithTools(github.CleanTools(cfg.EnabledTools)).
389-
WithFeatureChecker(createFeatureChecker(cfg.EnabledFeatures)).
390-
Build()
391-
392-
// Log how many tools we're about to register
393-
availableTools := inv.AvailableTools(ctx)
394-
if cfg.Logger != nil {
395-
cfg.Logger.Info("registering tools after authentication", "count", len(availableTools))
396-
}
397-
398-
// Register all GitHub tools/resources/prompts
399-
inv.RegisterAll(ctx, ghServer, deps)
394+
// Register all GitHub tools/resources/prompts using the pre-built inventory
395+
inventory.RegisterAll(ctx, ghServer, deps)
400396

401397
// Register dynamic toolset management tools if enabled
402398
if cfg.DynamicToolsets {
403-
registerDynamicTools(ghServer, inv, deps, cfg.Translator)
399+
registerDynamicTools(ghServer, inventory, deps, cfg.Translator)
404400
}
405401

406402
if cfg.Logger != nil {
403+
availableTools := inventory.AvailableTools(ctx)
407404
cfg.Logger.Info("authentication complete, tools registered", "toolCount", len(availableTools))
408405
}
409406

0 commit comments

Comments
 (0)