Jeremiah Lowin commited on
Commit
249e11b
·
unverified ·
2 Parent(s): 4885a278bd3f6a

Merge pull request #680 from jlowin/gemini

Browse files
docs/docs.json CHANGED
@@ -106,6 +106,7 @@
106
  "integrations/anthropic",
107
  "integrations/claude-desktop",
108
  "integrations/openai",
 
109
  "integrations/contrib"
110
  ]
111
  },
 
106
  "integrations/anthropic",
107
  "integrations/claude-desktop",
108
  "integrations/openai",
109
+ "integrations/gemini",
110
  "integrations/contrib"
111
  ]
112
  },
docs/integrations/anthropic.mdx CHANGED
@@ -1,7 +1,7 @@
1
  ---
2
  title: Anthropic
3
  sidebarTitle: Anthropic
4
- description: Access FastMCP servers from the Anthropic Messages API
5
  icon: message-smile
6
  ---
7
 
@@ -66,6 +66,12 @@ To use the Messages API with MCP servers, you'll need to install the Anthropic P
66
  pip install anthropic
67
  ```
68
 
 
 
 
 
 
 
69
  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.**
70
 
71
  ```python {5, 13-22}
 
1
  ---
2
  title: Anthropic
3
  sidebarTitle: Anthropic
4
+ description: Call FastMCP servers from the Anthropic API
5
  icon: message-smile
6
  ---
7
 
 
66
  pip install anthropic
67
  ```
68
 
69
+ 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.
70
+
71
+ ```bash
72
+ export ANTHROPIC_API_KEY="your-api-key"
73
+ ```
74
+
75
  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.**
76
 
77
  ```python {5, 13-22}
docs/integrations/claude-desktop.mdx CHANGED
@@ -1,7 +1,7 @@
1
  ---
2
  title: Claude Desktop
3
  sidebarTitle: Claude Desktop
4
- description: Integrate FastMCP servers with Claude Desktop
5
  icon: desktop
6
  ---
7
 
 
1
  ---
2
  title: Claude Desktop
3
  sidebarTitle: Claude Desktop
4
+ description: Call FastMCP servers from Claude Desktop
5
  icon: desktop
6
  ---
7
 
docs/integrations/gemini.mdx ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Gemini SDK
3
+ sidebarTitle: Gemini SDK
4
+ description: Call FastMCP servers from the Google Gemini SDK
5
+ icon: message-smile
6
+ ---
7
+
8
+ import { VersionBadge } from "/snippets/version-badge.mdx"
9
+
10
+ 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.
11
+
12
+ ## Gemini Python SDK
13
+
14
+ Google's [Gemini Python SDK](https://ai.google.dev/gemini-api/docs) can use FastMCP clients directly.
15
+
16
+ <Note>
17
+ 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.
18
+ </Note>
19
+
20
+ <Tip>
21
+ 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.
22
+ </Tip>
23
+
24
+ ### Create a Server
25
+
26
+ 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.
27
+
28
+ ```python server.py
29
+ import random
30
+ from fastmcp import FastMCP
31
+
32
+ mcp = FastMCP(name="Dice Roller")
33
+
34
+ @mcp.tool()
35
+ def roll_dice(n_dice: int) -> list[int]:
36
+ """Roll `n_dice` 6-sided dice and return the results."""
37
+ return [random.randint(1, 6) for _ in range(n_dice)]
38
+
39
+ if __name__ == "__main__":
40
+ mcp.run()
41
+ ```
42
+
43
+ ### Call the Server
44
+
45
+
46
+ To use the Gemini API with MCP, you'll need to install the Google Generative AI SDK:
47
+
48
+ ```bash
49
+ pip install google-genai
50
+ ```
51
+
52
+ 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.
53
+
54
+ ```bash
55
+ export GEMINI_API_KEY="your-api-key"
56
+ ```
57
+
58
+ 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.
59
+
60
+ ```python {5, 9, 15}
61
+ from fastmcp import Client
62
+ from google import genai
63
+ import asyncio
64
+
65
+ mcp_client = Client("server.py")
66
+ gemini_client = genai.Client()
67
+
68
+ async def main():
69
+ async with client:
70
+ response = await gemini_client.aio.models.generate_content(
71
+ model="gemini-2.0-flash",
72
+ contents="Roll 3 dice!",
73
+ config=genai.types.GenerateContentConfig(
74
+ temperature=0,
75
+ tools=[mcp_client.session], # Pass the FastMCP client session
76
+ ),
77
+ )
78
+ print(response.text)
79
+
80
+ if __name__ == "__main__":
81
+ asyncio.run(main())
82
+ ```
83
+
84
+ If you run this code, you'll see output like:
85
+
86
+ ```text
87
+ Okay, I rolled 3 dice and got a 5, 4, and 1.
88
+ ```
89
+
90
+ ### Remote & Authenticated Servers
91
+
92
+ 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.
93
+
94
+ For example, to connect to a remote, authenticated server, you can use the following client:
95
+
96
+ ```python
97
+ from fastmcp import Client
98
+ from fastmcp.client.auth import BearerAuth
99
+
100
+ client = Client(
101
+ "https://my-server.com/sse",
102
+ auth=BearerAuth("<your-token>"),
103
+ )
104
+ ```
105
+
106
+ The rest of the code remains the same.
107
+
108
+
docs/integrations/openai.mdx CHANGED
@@ -1,7 +1,7 @@
1
  ---
2
  title: OpenAI
3
  sidebarTitle: OpenAI
4
- description: Access FastMCP servers from the OpenAI API
5
  icon: message-smile
6
  ---
7
 
@@ -9,7 +9,7 @@ import { VersionBadge } from "/snippets/version-badge.mdx"
9
 
10
  OpenAI recently announced support for MCP servers in the Responses API. Note that at this time, MCP is not supported in ChatGPT.
11
 
12
- ## MCP in the Responses API
13
 
14
  OpenAI's [Responses API](https://platform.openai.com/docs/api-reference/responses) supports [MCP servers](https://platform.openai.com/docs/guides/tools-remote-mcp) as remote tool sources, allowing you to extend AI capabilities with custom functions.
15
 
@@ -71,6 +71,12 @@ To use the Responses API, you'll need to install the OpenAI Python SDK (not incl
71
  pip install openai
72
  ```
73
 
 
 
 
 
 
 
74
  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.
75
 
76
  ```python {4, 11-16}
 
1
  ---
2
  title: OpenAI
3
  sidebarTitle: OpenAI
4
+ description: Call FastMCP servers from the OpenAI API
5
  icon: message-smile
6
  ---
7
 
 
9
 
10
  OpenAI recently announced support for MCP servers in the Responses API. Note that at this time, MCP is not supported in ChatGPT.
11
 
12
+ ## Responses API
13
 
14
  OpenAI's [Responses API](https://platform.openai.com/docs/api-reference/responses) supports [MCP servers](https://platform.openai.com/docs/guides/tools-remote-mcp) as remote tool sources, allowing you to extend AI capabilities with custom functions.
15
 
 
71
  pip install openai
72
  ```
73
 
74
+ 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.
75
+
76
+ ```bash
77
+ export OPENAI_API_KEY="your-api-key"
78
+ ```
79
+
80
  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.
81
 
82
  ```python {4, 11-16}
server.py CHANGED
@@ -12,7 +12,10 @@ auth = BearerAuthProvider(
12
  audience="dice-server",
13
  )
14
 
15
- mcp = FastMCP(name="Dice Roller", auth=auth)
 
 
 
16
 
17
 
18
  @mcp.tool()
@@ -23,4 +26,5 @@ def roll_dice(n_dice: int) -> list[int]:
23
 
24
  if __name__ == "__main__":
25
  print(f"\n---\n\n🔑 Dice Roller access token:\n\n{access_token}\n\n---\n")
26
- mcp.run(transport="sse", port=8000)
 
 
12
  audience="dice-server",
13
  )
14
 
15
+ mcp = FastMCP(
16
+ name="Dice Roller",
17
+ # auth=auth,
18
+ )
19
 
20
 
21
  @mcp.tool()
 
26
 
27
  if __name__ == "__main__":
28
  print(f"\n---\n\n🔑 Dice Roller access token:\n\n{access_token}\n\n---\n")
29
+ # mcp.run(transport="sse", port=8000)
30
+ mcp.run()