Gemini SDK
Call FastMCP servers from the Google Gemini SDK
Google’s Gemini API includes built-in support for MCP servers in their Python and JavaScript SDKs, allowing you to connect directly to MCP servers and use their tools seamlessly with Gemini models.
Gemini Python SDK
Google’s Gemini Python SDK can use FastMCP clients directly.
Google’s MCP integration is currently experimental and available in the Python and JavaScript SDKs. The API automatically calls MCP tools when needed and can connect to both local and remote MCP servers.
Currently, Gemini’s MCP support only accesses tools from MCP servers—it queries the list_tools
endpoint and exposes those functions to the AI. Other MCP features like resources and prompts are not currently supported.
Create a Server
First, create a FastMCP server with the tools you want to expose. For this example, we’ll create a server with a single tool that rolls dice.
Call the Server
To use the Gemini API with MCP, you’ll need to install the Google Generative AI SDK:
You’ll also need to authenticate with Google. You can do this by setting the GEMINI_API_KEY
environment variable. Consult the Gemini SDK documentation for more information.
Gemini’s SDK interacts directly with the MCP client session. To call the server, you’ll need to instantiate a FastMCP client, enter its connection context, and pass the client session to the Gemini SDK.
If you run this code, you’ll see output like:
Remote & Authenticated Servers
In the above example, we connected to our local server using stdio
transport. Because we’re using a FastMCP client, you can also connect to any local or remote MCP server, using any transport or auth method supported by FastMCP, simply by changing the client configuration.
For example, to connect to a remote, authenticated server, you can use the following client:
The rest of the code remains the same.