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
19 changes: 19 additions & 0 deletions internal/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,22 @@ func handleClose(unifiedServer *UnifiedServer) http.Handler {
}
})
}

// registerCommonEndpoints registers shared HTTP endpoints that are common to both routed and unified modes
// This includes OAuth discovery, health check, and close endpoints
func registerCommonEndpoints(mux *http.ServeMux, unifiedServer *UnifiedServer, apiKey string) {
// OAuth discovery endpoints - return 404 since we don't use OAuth
// Standard path for OAuth discovery (per RFC 8414)
mux.Handle("/.well-known/oauth-authorization-server", withResponseLogging(handleOAuthDiscovery()))
// MCP-prefixed path for backward compatibility
mux.Handle("/mcp/.well-known/oauth-authorization-server", withResponseLogging(handleOAuthDiscovery()))

// Health check (spec 8.1.1)
healthHandler := HandleHealth(unifiedServer)
mux.Handle("/health", withResponseLogging(healthHandler))

// Close endpoint for graceful shutdown (spec 5.1.3)
closeHandler := handleClose(unifiedServer)
finalCloseHandler := applyAuthIfConfigured(apiKey, closeHandler.ServeHTTP)
mux.Handle("/close", withResponseLogging(finalCloseHandler))
}
26 changes: 3 additions & 23 deletions internal/server/routed.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,8 @@ func CreateHTTPServerForRoutedMode(addr string, unifiedServer *UnifiedServer, ap
logRouted.Printf("Creating HTTP server for routed mode: addr=%s", addr)
mux := http.NewServeMux()

// OAuth discovery endpoints - return 404 since we don't use OAuth
// Standard path for OAuth discovery (per RFC 8414)
mux.Handle("/.well-known/oauth-authorization-server", withResponseLogging(handleOAuthDiscovery()))
// MCP-prefixed path for backward compatibility
mux.Handle("/mcp/.well-known/oauth-authorization-server", withResponseLogging(handleOAuthDiscovery()))
// Register common endpoints (OAuth discovery, health, close)
registerCommonEndpoints(mux, unifiedServer, apiKey)

// Create routes for all backends, plus sys only if DIFC is enabled
allBackends := unifiedServer.GetServerIDs()
Expand Down Expand Up @@ -147,31 +144,14 @@ func CreateHTTPServerForRoutedMode(addr string, unifiedServer *UnifiedServer, ap
shutdownHandler := rejectIfShutdown(unifiedServer, loggedHandler, "server:routed")

// Apply auth middleware if API key is configured (spec 7.1)
finalHandler := shutdownHandler
if apiKey != "" {
finalHandler = authMiddleware(apiKey, shutdownHandler.ServeHTTP)
}
finalHandler := applyAuthIfConfigured(apiKey, shutdownHandler.ServeHTTP)

// Mount the handler at both /mcp/<server> and /mcp/<server>/
mux.Handle(route+"/", finalHandler)
mux.Handle(route, finalHandler)
log.Printf("Registered route: %s", route)
}

// Health check (spec 8.1.1)
healthHandler := HandleHealth(unifiedServer)
mux.Handle("/health", withResponseLogging(healthHandler))

// Close endpoint for graceful shutdown (spec 5.1.3)
closeHandler := handleClose(unifiedServer)

// Apply auth middleware if API key is configured (spec 7.1)
finalCloseHandler := closeHandler
if apiKey != "" {
finalCloseHandler = authMiddleware(apiKey, closeHandler.ServeHTTP)
}
mux.Handle("/close", withResponseLogging(finalCloseHandler))

return &http.Server{
Addr: addr,
Handler: mux,
Expand Down
18 changes: 2 additions & 16 deletions internal/server/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,8 @@ func CreateHTTPServerForMCP(addr string, unifiedServer *UnifiedServer, apiKey st
logTransport.Printf("Creating HTTP server for MCP: addr=%s, auth_enabled=%v", addr, apiKey != "")
mux := http.NewServeMux()

// OAuth discovery endpoints - return 404 since we don't use OAuth
// Standard path for OAuth discovery (per RFC 8414)
mux.Handle("/.well-known/oauth-authorization-server", withResponseLogging(handleOAuthDiscovery()))
// MCP-prefixed path for backward compatibility
mux.Handle("/mcp/.well-known/oauth-authorization-server", withResponseLogging(handleOAuthDiscovery()))
// Register common endpoints (OAuth discovery, health, close)
registerCommonEndpoints(mux, unifiedServer, apiKey)

logTransport.Print("Registering streamable HTTP handler for MCP protocol")
// Create StreamableHTTP handler for MCP protocol (supports POST requests)
Expand Down Expand Up @@ -131,17 +128,6 @@ func CreateHTTPServerForMCP(addr string, unifiedServer *UnifiedServer, apiKey st
mux.Handle("/mcp/", finalHandler)
mux.Handle("/mcp", finalHandler)

// Health check (spec 8.1.1)
healthHandler := HandleHealth(unifiedServer)
mux.Handle("/health", withResponseLogging(healthHandler))

// Close endpoint for graceful shutdown (spec 5.1.3)
closeHandler := handleClose(unifiedServer)

// Apply auth middleware if API key is configured (spec 7.1)
finalCloseHandler := applyAuthIfConfigured(apiKey, closeHandler.ServeHTTP)
mux.Handle("/close", withResponseLogging(finalCloseHandler))

return &http.Server{
Addr: addr,
Handler: mux,
Expand Down
Loading