Skip to content

Commit 08231a2

Browse files
authored
Add support for custom middleware in the correct order. (#2026)
* Add support for custom middleware in the correct order. * Switch this up to be more clear on what it's doing
1 parent 851030c commit 08231a2

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

pkg/github/server.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type MCPServerConfig struct {
7373

7474
type MCPServerOption func(*mcp.ServerOptions)
7575

76-
func NewMCPServer(ctx context.Context, cfg *MCPServerConfig, deps ToolDependencies, inv *inventory.Inventory) (*mcp.Server, error) {
76+
func NewMCPServer(ctx context.Context, cfg *MCPServerConfig, deps ToolDependencies, inv *inventory.Inventory, middleware ...mcp.Middleware) (*mcp.Server, error) {
7777
// Create the MCP server
7878
serverOpts := &mcp.ServerOptions{
7979
Instructions: inv.Instructions(),
@@ -98,9 +98,11 @@ func NewMCPServer(ctx context.Context, cfg *MCPServerConfig, deps ToolDependenci
9898

9999
ghServer := NewServer(cfg.Version, serverOpts)
100100

101-
// Add middlewares
102-
ghServer.AddReceivingMiddleware(addGitHubAPIErrorToContext)
101+
// 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,
102+
// and any middleware that needs to read or modify the context should be before it.
103+
ghServer.AddReceivingMiddleware(middleware...)
103104
ghServer.AddReceivingMiddleware(InjectDepsMiddleware(deps))
105+
ghServer.AddReceivingMiddleware(addGitHubAPIErrorToContext)
104106

105107
if unrecognized := inv.UnrecognizedToolsets(); len(unrecognized) > 0 {
106108
cfg.Logger.Warn("Warning: unrecognized toolsets ignored", "toolsets", strings.Join(unrecognized, ", "))

pkg/http/handler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import (
1919
)
2020

2121
type InventoryFactoryFunc func(r *http.Request) (*inventory.Inventory, error)
22+
23+
// GitHubMCPServerFactoryFunc is a function type for creating a new MCP Server instance.
24+
// middleware are applied AFTER the default GitHub MCP Server middlewares (like error context injection)
2225
type GitHubMCPServerFactoryFunc func(r *http.Request, deps github.ToolDependencies, inventory *inventory.Inventory, cfg *github.MCPServerConfig) (*mcp.Server, error)
2326

2427
type Handler struct {

0 commit comments

Comments
 (0)