Gratuity is given by an employer to employees for the service rendered by the employee to his / her employer for a continuous period of 5 years ( minimum 5 year and count further).
In your case, the employee in question worked with Agency A for 3 years and Agency B for 2 years, this employee never worked with his any of the employers for a "Continuous period of 5 years". Employer in simple terms with whom the employee has an employment contract, i.e. here on the payrolls of Agency A for 3 years and B for 2 years.
Principal Employer is liable to pay certain payments like salary and other statutory payments, only in case the contractor fails to meet commitments to his employee, not gratuity as it is an award for long term service.
In short, in your case company X is not liable to pay gratuity.
From India, Pune
I am Jitendra,
I am working on payroll of india's famous Chennai based staffing company ******HR solution pvt ltd for (Deputed by) MNC Company as a creative desk from last 6 year without any break.
So in case eligible for any gratuity benefint or not................... PLEASE ASKE D ME AS PER NEW LABOUR LAW 2021
From India, Manda