Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/github/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type MCPServerConfig struct {

type MCPServerOption func(*mcp.ServerOptions)

func NewMCPServer(ctx context.Context, cfg *MCPServerConfig, deps ToolDependencies, inv *inventory.Inventory) (*mcp.Server, error) {
func NewMCPServer(ctx context.Context, cfg *MCPServerConfig, deps ToolDependencies, inv *inventory.Inventory, middleware ...mcp.Middleware) (*mcp.Server, error) {
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding test coverage for the new middleware parameter to verify that custom middleware is executed in the correct order relative to the default middlewares (error context and deps injection). A test could verify that custom middleware can successfully read errors from the context that were set by tools/handlers, confirming the middleware chain works as intended.

This issue also appears on line 101 of the same file.

Copilot uses AI. Check for mistakes.
// Create the MCP server
serverOpts := &mcp.ServerOptions{
Instructions: inv.Instructions(),
Expand All @@ -98,9 +98,11 @@ func NewMCPServer(ctx context.Context, cfg *MCPServerConfig, deps ToolDependenci

ghServer := NewServer(cfg.Version, serverOpts)

// Add middlewares
ghServer.AddReceivingMiddleware(addGitHubAPIErrorToContext)
// Add middlewares. Order matters - for example, the error context middleware should be applied last so that it runs FIRST (closest to the handler) to ensure all errors are captured,
// and any middleware that needs to read or modify the context should be before it.
ghServer.AddReceivingMiddleware(middleware...)
ghServer.AddReceivingMiddleware(InjectDepsMiddleware(deps))
ghServer.AddReceivingMiddleware(addGitHubAPIErrorToContext)

if unrecognized := inv.UnrecognizedToolsets(); len(unrecognized) > 0 {
cfg.Logger.Warn("Warning: unrecognized toolsets ignored", "toolsets", strings.Join(unrecognized, ", "))
Expand Down
3 changes: 3 additions & 0 deletions pkg/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
)

type InventoryFactoryFunc func(r *http.Request) (*inventory.Inventory, error)

// GitHubMCPServerFactoryFunc is a function type for creating a new MCP Server instance.
// middleware are applied AFTER the default GitHub MCP Server middlewares (like error context injection)
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "middleware are applied AFTER the default GitHub MCP Server middlewares" is ambiguous. "Applied AFTER" could be interpreted as "added to the stack after" or "executed after". Based on the code, custom middleware is added first (line 103) but executes after the default middlewares have set up the context. Consider clarifying: "middleware will execute after the default middlewares (error context, deps injection) have set up the request context, allowing them to read errors and dependencies set by the defaults".

Suggested change
// middleware are applied AFTER the default GitHub MCP Server middlewares (like error context injection)
// Custom middleware created via this factory will execute after the default GitHub MCP Server
// middlewares (such as error context and dependency injection) have populated the request context,
// allowing the custom middleware to read errors and dependencies set by the defaults.

Copilot uses AI. Check for mistakes.
type GitHubMCPServerFactoryFunc func(r *http.Request, deps github.ToolDependencies, inventory *inventory.Inventory, cfg *github.MCPServerConfig) (*mcp.Server, error)

type Handler struct {
Expand Down