From fc4c7ef6a2b62d7b7915979f83d239b5cd8cee55 Mon Sep 17 00:00:00 2001 From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Date: Mon, 2 Jun 2025 20:30:23 -0400 Subject: [PATCH] Add gemini tutorial --- docs/docs.json | 1 + docs/integrations/anthropic.mdx | 8 ++- docs/integrations/claude-desktop.mdx | 2 +- docs/integrations/gemini.mdx | 94 ++++++++++++++++++++++++++++ docs/integrations/openai.mdx | 8 ++- server.py | 8 ++- 6 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 docs/integrations/gemini.mdx diff --git a/docs/docs.json b/docs/docs.json index d730d3254..58e2aef19 100644 --- a/docs/docs.json +++ b/docs/docs.json @@ -106,6 +106,7 @@ "integrations/anthropic", "integrations/claude-desktop", "integrations/openai", + "integrations/gemini", "integrations/contrib" ] }, diff --git a/docs/integrations/anthropic.mdx b/docs/integrations/anthropic.mdx index b01f01ede..0920afce2 100644 --- a/docs/integrations/anthropic.mdx +++ b/docs/integrations/anthropic.mdx @@ -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} diff --git a/docs/integrations/claude-desktop.mdx b/docs/integrations/claude-desktop.mdx index 9212418f1..7edafa68b 100644 --- a/docs/integrations/claude-desktop.mdx +++ b/docs/integrations/claude-desktop.mdx @@ -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 --- diff --git a/docs/integrations/gemini.mdx b/docs/integrations/gemini.mdx new file mode 100644 index 000000000..02ff590e3 --- /dev/null +++ b/docs/integrations/gemini.mdx @@ -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. + + +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. + +```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. + + diff --git a/docs/integrations/openai.mdx b/docs/integrations/openai.mdx index 2db6fdbcd..bc45c908d 100644 --- a/docs/integrations/openai.mdx +++ b/docs/integrations/openai.mdx @@ -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} diff --git a/server.py b/server.py index 0ddc68740..f695a6be7 100644 --- a/server.py +++ b/server.py @@ -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()