WASHINGTON — Senator John McCain, the sometimes cantankerous, often charming and eternally irrepressible Republican from Arizona, has never minced words. But in the twilight of a long and storied career, as he fights a virulent form of brain cancer, the 81-year-old senator has found a new voice.

In twin speeches — one in July, where he issued a call to bipartisanship in the Senate, and another in Philadelphia this past week, where he railed against “half-baked, spurious nationalism” — Mr. McCain has taken on both his colleagues and President Trump. In the process, his friends and fellow senators say, he has carved out a new role for himself on Capitol Hill: elder statesman and truth-teller.

“Even if John were not ill, with his experience and age, there is a part of you that I think begins to focus on your legacy,” said former Vice President Joseph R. Biden Jr., a close friend of Mr. McCain’s. But with cancer, Mr. Biden said, “he’s in the fight of his life, and he knows it.”

Having won re-election last year, Mr. McCain was already free to speak his mind. Were he to run again in 2022, he would be 86, and friends say that his 2016 campaign was almost certainly his last.