以下の公式のポストなどなど、情報が出ていた件です。
https://x.com/Google/status/1924876712749203804
Google I/O 2025 で発表された内容の 1つ、「Gemini API & SDK の MCP 対応」を試します。
試していく
Node.js を使ってお試しをやってみます。
公式ドキュメントを見てみる
公式ドキュメントで、詳細を確認してみます。
「Gemini API を使用した関数呼び出し」のページの「Model Context Protocol(MCP)」という部分を見ると、サンプルコードなどを確認できます。
サンプルコード
コードは Python か JavaScript のサンプルを見ることができ、自分は JavaScript のほうを見てみます。
掲載されているコードは、以下のとおりです。
import { GoogleGenAI, FunctionCallingConfigMode , mcpToTool} from '@google/genai';
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
// Create server parameters for stdio connection
const serverParams = new StdioClientTransport({
command: "npx", // Executable
args: ["-y", "@philschmid/weather-mcp"] // MCP Server
});
const client = new Client(
{
name: "example-client",
version: "1.0.0"
}
);
// Configure the client
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
// Initialize the connection between client and server
await client.connect(serverParams);
// Send request to the model with MCP tools
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: `What is the weather in London in ${new Date().toLocaleDateString()}?`,
config: {
tools: [mcpToTool(client)], // uses the session, will automatically call the tool
// Uncomment if you **don't** want the sdk to automatically call the tool
// automaticFunctionCalling: {
// disable: true,
// },
},
});
console.log(response.text)
// Close the connection
await client.close();
下準備とコードの実行
下準備
また、先ほどのサンプルコードの冒頭を見ると、2つのパッケージをインストールすれば良さそうです(具体的には以下のとおり)。
npm install @google/genai @modelcontextprotocol/sdk
それと合わせて、下準備として Gemini の APIキーを、環境変数 GEMINI_API_KEY という名前で設定しておきます。
MCPサーバーを使った処理の実行
先ほどのサンプルコードを書きかえて、MCPサーバーを使った処理の実装などを行います。
MCPサーバーは Filesystem MCP Server を使う形に変えてみて、そして他の部分も少し書きかえています。コードは以下のとおりです。
import { GoogleGenAI, mcpToTool } from "@google/genai";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
async function main() {
const serverParams = new StdioClientTransport({
command: "npx",
args: [
"-y",
"@modelcontextprotocol/server-filesystem",
"【処理を許可するフォルダへのフルパス】",
],
});
const client = new Client({ name: "test-client", version: "1.0.0" });
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
await client.connect(serverParams);
const tool = mcpToTool(client);
const listResp = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: "利用許可されているディレクトリのパスを教えてください。",
config: {
tools: [tool],
},
});
console.log(JSON.stringify(listResp, null, 2));
await client.close();
}
main().catch((err) => {
console.error(err);
process.exit(1);
});
上記を実行したところ、MCPサーバーに関する記載をした部分の「【処理を許可するフォルダへのフルパス】」に該当するパスの情報が、Gemini からのレスポンスとして返ってきたのを確認できました。
余談
今回の内容をやっていく中で、公式ドキュメント左メニューで実験的なマークがついてたこれらも気になりました。
他、新しく出た以下のモデルも軽く API から試してみたいところです(以下は Google AI Studio の画面)。