Jeremiah Lowin commited on
Commit
42f934d
·
1 Parent(s): 3642d21

Add OpenAI integration docs

Browse files
docs/docs.json CHANGED
@@ -68,6 +68,7 @@
68
  "servers/composition",
69
  {
70
  "group": "Deployment",
 
71
  "pages": [
72
  "deployment/running-server",
73
  "deployment/asgi",
@@ -92,18 +93,20 @@
92
  "clients/advanced-features"
93
  ]
94
  },
 
 
 
 
 
 
 
95
  {
96
  "group": "Patterns",
97
  "pages": [
98
  "patterns/decorating-methods",
99
  "patterns/http-requests",
100
- "patterns/contrib",
101
  "patterns/testing"
102
  ]
103
- },
104
- {
105
- "group": "Deployment",
106
- "pages": []
107
  }
108
  ]
109
  },
 
68
  "servers/composition",
69
  {
70
  "group": "Deployment",
71
+ "icon": "network-wired",
72
  "pages": [
73
  "deployment/running-server",
74
  "deployment/asgi",
 
93
  "clients/advanced-features"
94
  ]
95
  },
96
+ {
97
+ "group": "Integrations",
98
+ "pages": [
99
+ "integrations/openai",
100
+ "integrations/contrib"
101
+ ]
102
+ },
103
  {
104
  "group": "Patterns",
105
  "pages": [
106
  "patterns/decorating-methods",
107
  "patterns/http-requests",
 
108
  "patterns/testing"
109
  ]
 
 
 
 
110
  }
111
  ]
112
  },
docs/{patterns → integrations}/contrib.mdx RENAMED
File without changes
docs/integrations/openai.mdx ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: OpenAI
3
+ sidebarTitle: OpenAI
4
+ description: Integrate FastMCP servers with the OpenAI API
5
+ icon: "); -webkit-mask-image: url('https://upload.wikimedia.org/wikipedia/commons/6/66/OpenAI_logo_2025_%28symbol%29.svg');/*"
6
+ ---
7
+
8
+ import { VersionBadge } from "/snippets/version-badge.mdx"
9
+
10
+
11
+ OpenAI recently announced support for MCP servers in the Responses API. Note that at this time, MCP is not supported in ChatGPT.
12
+
13
+ ## MCP in the Responses API
14
+
15
+ 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.
16
+
17
+ <Note>
18
+ The Responses API is a distinct API from OpenAI's Completions API, Assistants API, or ChatGPT. At this time, only the Responses API supports MCP.
19
+ </Note>
20
+
21
+ <Tip>
22
+ Currently, the Responses API only accesses **tools** from MCP servers—it queries the `list_tools` endpoint and exposes those functions to the AI agent. Other MCP features like resources and prompts are not currently supported.
23
+ </Tip>
24
+
25
+
26
+ ### Create a Server
27
+
28
+ 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.
29
+
30
+ ```python server.py
31
+ import random
32
+ from fastmcp import FastMCP
33
+
34
+ mcp = FastMCP(name="Dice Roller")
35
+
36
+ @mcp.tool()
37
+ def roll_dice(n_dice: int) -> list[int]:
38
+ """Roll `n_dice` 6-sided dice and return the results."""
39
+ return [random.randint(1, 6) for _ in range(n_dice)]
40
+
41
+ if __name__ == "__main__":
42
+ mcp.run(transport="sse", port=8000)
43
+ ```
44
+
45
+ ### Deploy the Server
46
+
47
+ Your server must be deployed to a public URL in order for OpenAI to access it.
48
+
49
+ For development, you can use tools like `ngrok` to temporarily expose a locally-running server to the internet. We'll do that for this example (you may need to install `ngrok` and create a free account), but you can use any other method to deploy your server.
50
+
51
+ Assuming you saved the above code as `server.py`, you can run the following two commands in two separate terminals to deploy your server and expose it to the internet:
52
+
53
+ <CodeGroup>
54
+ ```bash FastMCP server
55
+ python server.py
56
+ ```
57
+
58
+ ```bash ngrok
59
+ ngrok http 8000
60
+ ```
61
+ </CodeGroup>
62
+
63
+ <Warning>
64
+ This exposes your unauthenticated server to the internet. Only run this command in a safe environment if you understand the risks.
65
+ </Warning>
66
+
67
+ ### Call the Server
68
+
69
+ To use the Responses API, you'll need to install the OpenAI Python SDK (not included with FastMCP):
70
+
71
+ ```bash
72
+ pip install openai
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.
76
+
77
+ ```python {4, 11-16}
78
+ from openai import OpenAI
79
+
80
+ # Your server URL (replace with your actual URL)
81
+ url = 'https://your-server-url.com'
82
+
83
+ client = OpenAI()
84
+
85
+ resp = client.responses.create(
86
+ model="gpt-4.1",
87
+ tools=[
88
+ {
89
+ "type": "mcp",
90
+ "server_label": "dice_server",
91
+ "server_url": f"{url}/sse",
92
+ "require_approval": "never",
93
+ },
94
+ ],
95
+ input="Roll a few dice!",
96
+ )
97
+
98
+ print(resp.output_text)
99
+ ```
100
+ If you run this code, you'll see something like the following output:
101
+
102
+ ```text
103
+ You rolled 3 dice and got the following results: 6, 4, and 2!
104
+ ```
105
+
106
+ ### Authentication
107
+
108
+ <VersionBadge version="2.6.0" />
109
+
110
+ The Responses API can include headers to authenticate the request, which means you don't have to worry about your server being publicly accessible.
111
+
112
+ #### Server Authentication
113
+
114
+ The simplest way to add authentication to the server is to use a bearer token scheme.
115
+
116
+ For this example, we'll quickly generate our own tokens with FastMCP's `RSAKeyPair` utility, but this may not be appropriate for production use. For more details, see the complete server-side [Bearer Auth](/servers/auth/bearer) documentation.
117
+
118
+ We'll start by creating an RSA key pair to sign and verify tokens.
119
+
120
+ ```python
121
+ from fastmcp.server.auth.providers.bearer import RSAKeyPair
122
+
123
+ key_pair = RSAKeyPair.generate()
124
+ access_token = key_pair.create_token(audience="dice-server")
125
+ ```
126
+
127
+ This will generate a new RSA key pair and a corresponding access token.
128
+
129
+ Next, we'll create a `BearerAuthProvider` to authenticate the server.
130
+
131
+ ```python
132
+ from fastmcp import FastMCP
133
+ from fastmcp.server.auth import BearerAuthProvider
134
+
135
+ auth = BearerAuthProvider(
136
+ public_key=key_pair.public_key,
137
+ audience="dice-server",
138
+ )
139
+
140
+ mcp = FastMCP(name="Dice Roller", auth=auth)
141
+ ```
142
+
143
+ Here is a complete example that you can copy/paste. For simplicity, it will print the token to the console - **do NOT do this in production!**
144
+
145
+ ```python server.py [expandable]
146
+ from fastmcp import FastMCP
147
+ from fastmcp.server.auth import BearerAuthProvider
148
+ from fastmcp.server.auth.providers.bearer import RSAKeyPair
149
+ import random
150
+
151
+ key_pair = RSAKeyPair.generate()
152
+ access_token = key_pair.create_token(audience="dice-server")
153
+
154
+ auth = BearerAuthProvider(
155
+ public_key=key_pair.public_key,
156
+ audience="dice-server",
157
+ )
158
+
159
+ mcp = FastMCP(name="Dice Roller", auth=auth)
160
+
161
+ @mcp.tool()
162
+ def roll_dice(n_dice: int) -> list[int]:
163
+ """Roll `n_dice` 6-sided dice and return the results."""
164
+ return [random.randint(1, 6) for _ in range(n_dice)]
165
+
166
+ if __name__ == "__main__":
167
+ print(f"\n---\n\n🔑 Dice Roller access token:\n\n{access_token}\n\n---\n")
168
+ mcp.run(transport="sse", port=8000)
169
+ ```
170
+
171
+ #### Client Authentication
172
+
173
+ If you try to call the authenticated server with the same OpenAI code we wrote earlier, you'll get an error like this:
174
+
175
+ ```python
176
+ pythonAPIStatusError: Error code: 424 - {
177
+ "error": {
178
+ "message": "Error retrieving tool list from MCP server: 'dice_server'. Http status code: 401 (Unauthorized)",
179
+ "type": "external_connector_error",
180
+ "param": "tools",
181
+ "code": "http_error"
182
+ }
183
+ }
184
+ ```
185
+
186
+ As expected, the server is rejecting the request because it's not authenticated.
187
+
188
+ To authenticate the client, you can pass the token in the `Authorization` header with the `Bearer` scheme:
189
+
190
+
191
+ ```python {4, 7, 19-21} [expandable]
192
+ from openai import OpenAI
193
+
194
+ # Your server URL (replace with your actual URL)
195
+ url = 'https://your-server-url.com'
196
+
197
+ # Your access token (replace with your actual token)
198
+ access_token = 'your-access-token'
199
+
200
+ client = OpenAI()
201
+
202
+ resp = client.responses.create(
203
+ model="gpt-4.1",
204
+ tools=[
205
+ {
206
+ "type": "mcp",
207
+ "server_label": "dice_server",
208
+ "server_url": f"{url}/sse",
209
+ "require_approval": "never",
210
+ "headers": {
211
+ "Authorization": f"Bearer {access_token}"
212
+ }
213
+ },
214
+ ],
215
+ input="Roll a few dice!",
216
+ )
217
+
218
+ print(resp.output_text)
219
+ ```
220
+
221
+ You should now see the dice roll results in the output.
docs/patterns/fastapi.mdx DELETED
@@ -1,47 +0,0 @@
1
- ---
2
- title: FastAPI Integration
3
- sidebarTitle: FastAPI
4
- description: Generate MCP servers from FastAPI apps
5
- icon: square-bolt
6
- ---
7
- import { VersionBadge } from '/snippets/version-badge.mdx'
8
-
9
- <VersionBadge version="2.0.0" />
10
-
11
- <Note>
12
- **Documentation Moved**: The comprehensive FastAPI integration documentation has been moved to the [OpenAPI Integration](/patterns/openapi#fastapi-integration) page, where it's covered alongside all other OpenAPI features including route mapping and tags support.
13
- </Note>
14
-
15
- ## Quick Start
16
-
17
- FastMCP can automatically convert FastAPI applications into MCP servers:
18
-
19
- ```python
20
- from fastapi import FastAPI
21
- from fastmcp import FastMCP
22
-
23
- # A FastAPI app
24
- app = FastAPI()
25
-
26
- @app.get("/items")
27
- def list_items():
28
- return [{"id": 1, "name": "Item 1"}, {"id": 2, "name": "Item 2"}]
29
-
30
- @app.get("/items/{item_id}")
31
- def get_item(item_id: int):
32
- return {"id": item_id, "name": f"Item {item_id}"}
33
-
34
- @app.post("/items")
35
- def create_item(name: str):
36
- return {"id": 3, "name": name}
37
-
38
- # Create an MCP server from your FastAPI app
39
- mcp = FastMCP.from_fastapi(app=app)
40
-
41
- if __name__ == "__main__":
42
- mcp.run() # Start the MCP server
43
- ```
44
-
45
- <Tip>
46
- For complete documentation including tag-based routing, route mapping configuration, timeout settings, authentication examples, and advanced configuration options, see the comprehensive [OpenAPI Integration documentation](/patterns/openapi#fastapi-integration).
47
- </Tip>