Gaikoku bugy? (?) were the commissioners or "magistrates of foreign affairs" appointed at the end of the Edo era by the Tokugawa shogunate to oversee trade and diplomatic relations with foreign countries. In essence this was the beginning of the creation of a Ministry of Foreign Affairs after Japan's long period of isolationist policy.
The Gaikoku bugy? system began just prior to the negotiations which resulted in the Harris Treaty. First appointed in August 1858, the gaikoku-bugy? were shogunate officials who were charged with advising the government on foreign affairs and who were tasked with conducting negotiations with foreign diplomats both in Japan and abroad. This was a high-ranking office, in status roughly equivalent to that of kanj?-bugy?, or expressed differently, the status of this office ranked slightly below that of daimy?. The number of gaikoku bugy? varied, from five in 1858 to a maximum of 13, with wide variations in the numbers of officials who were appointed across the span of years.
The office was often held concurrently with that of kanj?-bugy? or the office was held concurrently by those serving the shogunate as governor of one of the great ports (Nagasaki bugy? or Kanagawa bugy?).
The genesis of the gaikoku-bugy? pre-dates the actual creation of the office.
The prefix kaib?-gakari meaning "in charge of maritime defense" was used with the titles of some shogunate officials after 1845. This term was used to designate those who bore a special responsibility for overseeing coastal waters, and by implication, for dealing with matters involving foreigners--for example, kaib?-gakari-?metsuke which later came to be superseded by the term gaikoku-gakari.
R?j? Hotta Masayoshi formed an ad hoc committee of shogunate officials with special knowledge of foreign affairs, and he himself headed this working group. In November 1856, he appointed the members and charged them to come up with recommendations about the terms for opening Japanese ports. The results of their deliberations would become the basis for negotiations which ultimately resulted in the Harris Treaty (the Treaty of Amity and Commerce between Japan and the United States).
The numbers of gaikoku bugy? varied throughout the Edo period: