60e0ffc959
Upgrade checks / Notify on failure (push) Has been cancelled
Upgrade checks / Close issue on success (push) Has been cancelled
Schema Crash Test / Real-world schema crash test (232K schemas) (push) Has been cancelled
Run static analysis / static_analysis (push) Has been cancelled
Tests / Tests: Python 3.10 on ubuntu-latest (push) Has been cancelled
Tests / Tests: Python 3.13 on ubuntu-latest (push) Has been cancelled
Tests / Tests: Python 3.10 on windows-latest (push) Has been cancelled
Tests / Tests with lowest-direct dependencies (push) Has been cancelled
Tests / MCP conformance tests (push) Has been cancelled
Tests / Integration tests (push) Has been cancelled
Tests / Package install smoke (push) Has been cancelled
Upgrade checks / Static analysis (push) Has been cancelled
Upgrade checks / Tests: Python 3.10 on ubuntu-latest (push) Has been cancelled
Upgrade checks / Tests: Python 3.13 on ubuntu-latest (push) Has been cancelled
Upgrade checks / Tests: Python 3.10 on windows-latest (push) Has been cancelled
Upgrade checks / Integration tests (push) Has been cancelled
Update MCPServerConfig Schema / update-config-schema (push) Has been cancelled
Update SDK Documentation / update-sdk-docs (push) Has been cancelled
109 lines
3.4 KiB
Plaintext
109 lines
3.4 KiB
Plaintext
---
|
|
title: Gemini SDK 🤝 FastMCP
|
|
sidebarTitle: Gemini SDK
|
|
description: Connect FastMCP servers to the Google Gemini SDK
|
|
icon: message-code
|
|
---
|
|
|
|
import { VersionBadge } from "/snippets/version-badge.mdx"
|
|
|
|
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](https://ai.google.dev/gemini-api/docs) can use FastMCP clients directly.
|
|
|
|
<Note>
|
|
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.
|
|
</Note>
|
|
|
|
<Tip>
|
|
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.
|
|
</Tip>
|
|
|
|
### 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.
|
|
|
|
```python server.py
|
|
import random
|
|
from fastmcp import FastMCP
|
|
|
|
mcp = FastMCP(name="Dice Roller")
|
|
|
|
@mcp.tool
|
|
def roll_dice(n_dice: int) -> list[int]:
|
|
"""Roll `n_dice` 6-sided dice and return the results."""
|
|
return [random.randint(1, 6) for _ in range(n_dice)]
|
|
|
|
if __name__ == "__main__":
|
|
mcp.run()
|
|
```
|
|
|
|
### Call the Server
|
|
|
|
|
|
To use the Gemini API with MCP, you'll need to install the Google Generative AI SDK:
|
|
|
|
```bash
|
|
pip install google-genai
|
|
```
|
|
|
|
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.
|
|
|
|
```bash
|
|
export GEMINI_API_KEY="your-api-key"
|
|
```
|
|
|
|
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.
|
|
|
|
```python {5, 9, 15}
|
|
from fastmcp import Client
|
|
from google import genai
|
|
import asyncio
|
|
|
|
mcp_client = Client("server.py")
|
|
gemini_client = genai.Client()
|
|
|
|
async def main():
|
|
async with mcp_client:
|
|
response = await gemini_client.aio.models.generate_content(
|
|
model="gemini-2.0-flash",
|
|
contents="Roll 3 dice!",
|
|
config=genai.types.GenerateContentConfig(
|
|
temperature=0,
|
|
tools=[mcp_client.session], # Pass the FastMCP client session
|
|
),
|
|
)
|
|
print(response.text)
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|
|
```
|
|
|
|
If you run this code, you'll see output like:
|
|
|
|
```text
|
|
Okay, I rolled 3 dice and got a 5, 4, and 1.
|
|
```
|
|
|
|
### 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](/clients/transports) or [auth](/clients/auth/oauth) 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:
|
|
|
|
```python
|
|
from fastmcp import Client
|
|
from fastmcp.client.auth import BearerAuth
|
|
|
|
mcp_client = Client(
|
|
"https://my-server.com/mcp/",
|
|
auth=BearerAuth("<your-token>"),
|
|
)
|
|
```
|
|
|
|
The rest of the code remains the same.
|
|
|
|
|