With China’s pandemic restrictions dismantled and its leaders wooing executives flying into the country again, this was supposed to be a springtime of renewed investor confidence in the world’s second-biggest economy.

But a drumbeat of government security measures, including a broadening of counterespionage laws, and unannounced visits by investigators to the Chinese offices of several foreign firms have sent a shiver of worry that under Xi Jinping, economic pragmatism could again give way to a heightened focus on state control.

International consulting and advisory firms are among those that have faced questioning from Chinese security officers in recent weeks, adding to fears among foreign investors that the authorities may be trying to choke off their access to unvarnished information about markets, competitors and potential deals in China. The scrutiny has left some companies questioning whether their China operations are at risk.

Officers visited the Shanghai office of Bain & Company, a major American consulting firm, and questioned employees, Bain said in late April. And the Mintz Group, an American company specializing in corporate investigations, said in late March that officials visited its Beijing office and detained five Chinese employees.

“The business community is nervous given the number of visits that foreign companies are getting without any explanation as to why,” said Michael Hart, the president of the American Chamber of Commerce in China.

Broadly, they appear to reflect the demands from Mr. Xi and other senior leaders to fortify national security and, in particular, seal off the flow of potentially sensitive information to foreign governments and investors. Since coming to power in 2012, Mr. Xi has urged vigilance against the United States and Western influence. His wariness has redoubled as relations with Washington have deteriorated sharply under the Trump and Biden administrations.

“The Chinese Communist Party is taking a multipronged approach to achieving supremacy in the control of data, and, through that, broader societal and geopolitical control,” Matthew Johnson, a visiting fellow at the Hoover Institution, wrote in a new report on China’s quest for data dominance.

Mr. Xi and other senior officials have said the United States is pursuing a long-term offensive to stymie China’s rise by cutting off its access to technology for cutting-edge industries and military innovations. The Biden administration’s stringent restrictions on sales of advanced semiconductors and chip-making equipment to China, ushered in last year, appear to have heightened Mr. Xi’s conviction that Washington is engaged in “all-around containment, encirclement and suppression of China.”

American consulting firms, in particular, seem to be attracting the attention of China’s Ministry of State Security, with officials showing up to ask questions about the firms’ work and their contacts with embassies.

It is part of an increasingly perilous environment for foreign companies. Beijing ordered a cybersecurity review of the U.S. chip maker Micron Technology in a move seen as retaliation for the semiconductor trade restrictions. A Japanese pharmaceutical executive was detained on suspicion of espionage. China has also intensified its use of exit bans to prevent people such as business executives from leaving the country.

While China has come down on foreign companies in the past, the targets were often big consumer brands, and the shaming played out in the media. The latest crackdowns are different. Mintz and Bain are cogs in the global business machinery, but they are not household names, and China has said little about the visits.

Official Chinese media reports have suggested that Beijing’s concerns about security vulnerabilities could be driving the activity.

An official report, issued to mark China’s annual National Security Education Day on April 15, said that an unnamed consultancy in southern China had delivered research to a foreign group about labor in Xinjiang, which the United States has put under heavy trade restrictions over allegations of forced labor by Uyghurs, a largely Muslim ethnic group.

The Chinese government denies that forced labor has occurred in Xinjiang’s cotton, textiles, mining and other industries. The consulting company’s work on Xinjiang amounted to a violation of anti-espionage laws and rules and “brought severe risks and hazards to national security and interests,” the Chinese report said.

To fortify China and the Communist Party, Mr. Xi has pushed a sweeping program for “comprehensive” national security, extending new security measures throughout the economy, businesses and data management.

“We must effectively ensure national data security,” Mr. Xi said in March at a meeting of the Politburo, the Communist Party’s two dozen most senior officials, that discussed China’s “big data” strategy.

Mr. Xi’s security demands have rippled throughout the Chinese government.

Last year, the Cyberspace Administration of China introduced new rules for data that is transferred abroad, requiring that it be assessed by the agency for its “degree of sensitivity” and possible risks to “national security.”

Since March, China National Knowledge Infrastructure, or CNKI, a company that distributes Chinese research papers, dissertations and statistics to foreign universities and other users abroad, has severely restricted access to them. CNKI cited the need to suspend such services while it figures out how to enforce the data export rules introduced last year.

Since a Communist Party congress in October, Mr. Xi has elevated a cohort of security officials who appear eager to continue this drive for tighter controls.

“Along with the profound changes in the international and domestic environment, the scope of national security is constantly expanding,” Chen Yixin, the minister for state security, wrote in an article for the party’s chief ideological journal last month.

The United States ambassador to China, R. Nicholas Burns, criticized the espionage law on Tuesday.

“This is a law that potentially could make illegal in China the kind of mundane activities that a business would have to do to seek due diligence before you agree to a major investment deal,” he said.

It appears that European firms have not attracted the attention of China’s security establishment, as Europe’s national leaders have generally taken a more accommodating stance toward China than the Biden administration. But European companies also say it is critical to maintain public access to information about markets and companies.

“There is a strong need for clarification on which information is sensitive and which is not,” said Joerg Wuttke, the president of the European Union Chamber of Commerce in China.

Dan Harris, a Seattle-based lawyer who works with foreign companies in China, said that in the past week he had heard from at least two American companies looking to leave the country, having seen the signs that the Chinese Communist Party appears to be sending with the recent scrutiny.

“The message is: ‘We don’t care that much about the economy. What we care about is keeping you in line,’” Mr. Harris said. “‘And if you don’t do what we want you to do, we will come after you.’”

Ana Swanson and Edward Wong contributed reporting.