Cao Wei (220–265) was one of the three major states that competed for supremacy over China in the Three Kingdoms period (220–280). With its capital at Luoyang, the state was established by Cao Pi in 220, based upon the foundations laid by his father, Cao Cao, towards the end of the Eastern Han dynasty. The name "Wei" first became associated with Cao Cao when he was named the Duke of Wei by the Eastern Han government in 213, and became the name of the state when Cao Pi proclaimed himself emperor in 220. Historians often add the prefix "Cao" to distinguish it from other Chinese states known as "Wei", such as Wei of the Warring States period and Northern Wei of the Southern and Northern Dynasties. The authority of the ruling Cao family gradually weakened after the death of the second Wei emperor, Cao Rui, and eventually fell into the hands of Sima Yi, a Wei regent, and his family, in 249. Cao Rui's successors remained as puppet rulers under the control of the Simas until Sima Yi's grandson, Sima Yan, forced the last Wei ruler, Cao Huan, to abdicate the throne and established the Jin dynasty.
Towards the end of the Eastern Han dynasty, northern China came under the control of Cao Cao, the chancellor to the last Han ruler, Emperor Xian. In 213, Emperor Xian granted Cao Cao the title of "Duke of Wei" (魏公) and gave him ten cities as his dukedom. The area was named "Wei". At that time, the southern part of China was divided into two areas controlled by two other warlords, Liu Bei and Sun Quan. In 216, Emperor Xian promoted Cao Cao to the status of a vassal king — "King of Wei (魏王)" — and granted him more territories.
Cao Cao died on 15 March 220 and his vassal king title was inherited by his son Cao Pi. Later that year, on 11 December, Cao Pi forced Emperor Xian to abdicate in his favour and took over the throne, establishing the state of Wei. However, Liu Bei immediately contested Cao Pi's claim to the Han throne and declared himself "Emperor of Shu Han" a year later. Sun Quan was nominally a vassal king under Wei, but he declared independence in 222 and eventually proclaimed himself "Emperor of Wu" in 229.
Cao Pi ruled for six years until his death in 226 and was succeeded by his son, Cao Rui, who ruled until his death in 239. Throughout the reigns of Cao Pi and Cao Rui, Wei had been fighting numerous wars with its two rival states — Shu and Wu.
Between 228 and 234, Zhuge Liang, the Shu chancellor and regent, led a series of five military campaigns to attack Wei's western borders (within present-day Gansu and Shaanxi), with the aim of conquering Chang'an, a strategic city which lay on the road to the Wei capital, Luoyang. The Shu invasions were repelled by the Wei armies led by the generals Cao Zhen, Sima Yi, Zhang He and others; Shu did not make any significant gains in the expeditions.
On its southern and eastern borders, Wei engaged Wu in a series of armed conflicts throughout the 220s and 230s, including the battles of Dongkou (222-223), Jiangling (223) and Shiting (228). However, most of the battles resulted in stalemate and neither side managed to significantly expand its territory.
Around that time, as the Korean kingdom Goguryeo consolidated its power, it proceeded to act to conquer the territories on the Korean peninsula which were under Chinese rule. Goguryeo initiated the Goguryeo–Wei Wars in 242, trying to cut off Chinese access to its territories in Korea by attempting to take a Chinese fort. However, Wei responded by invading and defeated Goguryeo. Hwando was destroyed in revenge by Wei forces in 244.
In 249, during the reign of Cao Rui's successor, Cao Fang, the regent Sima Yi seized state power from his co-regent, Cao Shuang, in a coup. This event marked the collapse of imperial authority in Wei, as Cao Fang's role had been reduced to a puppet ruler while Sima Yi wielded state power firmly in his hands. Sima Yi died in 251 and passed on his authority to his eldest son, Sima Shi, who continued ruling as regent. Sima Shi deposed Cao Fang in 254 and replaced him with Cao Mao. After Sima Shi died in the following year, his younger brother, Sima Zhao, inherited his power and status as regent. In 260, Cao Mao attempted to seize back state power from Sima Zhao in a coup, but was killed by Sima's subordinate, Cheng Ji (成濟).
After Cao Mao's death, Cao Huan was enthroned as the fifth ruler of Wei. However, Cao Huan was also a figurehead under Sima Zhao's control much like his predecessor. In 263, Wei armies led by Zhong Hui and Deng Ai conquered Shu. Two years later, Sima Zhao's son, Sima Yan, forced Cao Huan to abdicate in his favour, replacing Wei with the Jin dynasty.
The system of government in Wei inherited many aspects from that of the Eastern Han dynasty. During his reign, Cao Pi established two separate government bodies - the Central Inspectorate (中書監) and the Mobile Imperial Secretariat (行尚書臺) — to reduce the authority of the Imperial Secretariat (尚書臺) and consolidate the power of the central government.
During this time, the minister Chen Qun developed the nine-rank system for civil service nomination, which was adopted by later dynasties until it was superseded by the imperial examination system in the Sui dynasty.
Cao Pi felt that the Han dynasty collapsed because the Governors (州牧) of the various provinces wielded too much power and fell out of the control of the central government. He reduced the role of a Governor to that of an Inspector (刺史), and permitted the Inspectors to administer only civil affairs in their respective provinces, while military affairs were handled by military personnel based in regional offices or in the capital.
Sometime between the late Eastern Han dynasty and the Cao Wei dynasty, kaishu, a style of Chinese calligraphy, appeared, with its first known master being Zhong Yao, who also served as a politician in Wei.
According to the Book of Wei, the Cao family descended from the Yellow Emperor through his grandson Zhuanxu. They were of the same lineage as Emperor Shun. Another account says that the Cao family descended from Emperor Shun. This account was attacked by Chiang Chi, who claimed that those with the family name "Tian" descended from Shun, but not those surnamed "Cao". He also claimed that "Gui" (媯) was Emperor Shun's family name.