Add gemini tutorial

This commit is contained in:
Jeremiah Lowin 2025-06-02 20:30:23 -04:00
commit fc4c7ef6a2
6 changed files with 116 additions and 5 deletions

View file

@ -106,6 +106,7 @@
"integrations/anthropic",
"integrations/claude-desktop",
"integrations/openai",
"integrations/gemini",
"integrations/contrib"
]
},

View file

@ -1,7 +1,7 @@
---
title: Anthropic
sidebarTitle: Anthropic
description: Access FastMCP servers from the Anthropic Messages API
description: Call FastMCP servers from the Anthropic API
icon: message-smile
---
@ -66,6 +66,12 @@ To use the Messages API with MCP servers, you'll need to install the Anthropic P
pip install anthropic
```
You'll also need to authenticate with Anthropic. You can do this by setting the `ANTHROPIC_API_KEY` environment variable. Consult the Anthropic SDK documentation for more information.
```bash
export ANTHROPIC_API_KEY="your-api-key"
```
Here is an example of how to call your server from Python. Note that you'll need to replace `https://your-server-url.com` with the actual URL of your server. In addition, we use `/sse` as the endpoint because we deployed an SSE server with the default path; you may need to use a different endpoint if you customized your server's deployment. **At this time you must also include the `extra_headers` parameter with the `anthropic-beta` header.**
```python {5, 13-22}

View file

@ -1,7 +1,7 @@
---
title: Claude Desktop
sidebarTitle: Claude Desktop
description: Integrate FastMCP servers with Claude Desktop
description: Call FastMCP servers from Claude Desktop
icon: desktop
---

View file

@ -0,0 +1,94 @@
---
title: Gemini SDK
sidebarTitle: Gemini SDK
description: Call FastMCP servers from the Google Gemini SDK
icon: message-smile
---
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 API with MCP
Google's [Gemini API](https://ai.google.dev/gemini-api/docs) supports MCP servers through built-in integration in their SDKs. Unlike other providers that require deploying servers to public URLs, Gemini can connect directly to local MCP servers, making development much simpler.
<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 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) method supported by FastMCP, simply by changing the client configuration.

View file

@ -1,7 +1,7 @@
---
title: OpenAI
sidebarTitle: OpenAI
description: Access FastMCP servers from the OpenAI API
description: Call FastMCP servers from the OpenAI API
icon: message-smile
---
@ -71,6 +71,12 @@ To use the Responses API, you'll need to install the OpenAI Python SDK (not incl
pip install openai
```
You'll also need to authenticate with OpenAI. You can do this by setting the `OPENAI_API_KEY` environment variable. Consult the OpenAI SDK documentation for more information.
```bash
export OPENAI_API_KEY="your-api-key"
```
Here is an example of how to call your server from Python. Note that you'll need to replace `https://your-server-url.com` with the actual URL of your server. In addition, we use `/sse` as the endpoint because we deployed an SSE server with the default path; you may need to use a different endpoint if you customized your server's deployment.
```python {4, 11-16}

View file

@ -12,7 +12,10 @@ auth = BearerAuthProvider(
audience="dice-server",
)
mcp = FastMCP(name="Dice Roller", auth=auth)
mcp = FastMCP(
name="Dice Roller",
# auth=auth,
)
@mcp.tool()
@ -23,4 +26,5 @@ def roll_dice(n_dice: int) -> list[int]:
if __name__ == "__main__":
print(f"\n---\n\n🔑 Dice Roller access token:\n\n{access_token}\n\n---\n")
mcp.run(transport="sse", port=8000)
# mcp.run(transport="sse", port=8000)
mcp.run()