1
1
# OpenAI MCP Server
2
2
3
- MCP protocol integration that enables direct invocation of OpenAI models from Claude for conversations and image generation. Supports in-chat image display with configurable timeouts and retry mechanisms.
3
+ A MCP server implementation for direct interaction with OpenAI models through Claude. Supports text and image generation, with built-in image display, original image download links, and configurable timeout/ retry mechanisms.
4
4
5
- ## Architecture
5
+ ## Architecture Design
6
6
7
7
### Overall Architecture
8
+
8
9
```
9
10
+-----------------+
10
11
| |
@@ -47,45 +48,56 @@ MCP protocol integration that enables direct invocation of OpenAI models from Cl
47
48
### Core Components
48
49
49
50
1 . ** Server Core**
51
+
50
52
- Implements the MCP protocol
51
53
- Handles request routing and lifecycle management
52
54
- Provides configuration management and error handling
53
-
54
55
2 . ** Request Handler**
56
+
55
57
- Processes specific request types
56
58
- Implements request parameter validation and transformation
57
59
- Manages request timeouts and retry logic
58
-
59
60
3 . ** Notification Manager**
61
+
60
62
- Manages notification lifecycle
61
63
- Implements reliable notification delivery
62
64
- Handles notification cancellation and cleanup
63
-
64
65
4 . ** OpenAI Client**
66
+
65
67
- Encapsulates OpenAI API calls
66
68
- Handles response transformation and error handling
67
69
- Implements API rate limiting and retry strategies
68
70
69
71
## Features
70
72
71
73
### Text Generation
72
- - Supports GPT-4 and GPT-3.5-turbo models
73
- - Configurable temperature and response length parameters
74
- - Stream response support
74
+
75
+ - Support for GPT-4 and GPT-3.5-turbo models
76
+ - Adjustable temperature and response length
77
+ - Streaming response support
75
78
76
79
### Image Generation
77
- - Supports DALL·E 2 and DALL·E 3
78
- - Direct in-chat image display
80
+
81
+ - Support for DALL·E 2 and DALL·E 3
82
+ - Smart image display:
83
+ - Compressed images in chat
84
+ - Original image download links
85
+ - Multiple size options:
86
+ - DALL·E 2/3 common: 1024x1024, 512x512, 256x256
87
+ - DALL·E 3 exclusive: 1792x1024 (landscape), 1024x1792 (portrait)
88
+ - HD quality option (DALL·E 3)
89
+ - Generation progress feedback
90
+ - Batch generation support (up to 10 images)
79
91
- Configurable timeout and retry mechanisms
80
- - Multiple image sizes and quality options
81
- - Batch image generation capability
82
92
83
93
## Technical Implementation
84
94
85
95
### Asynchronous Processing
96
+
86
97
The project uses ` anyio ` as the async runtime, supporting both asyncio and trio. Key async processing includes:
87
98
88
99
1 . Request Handling
100
+
89
101
``` python
90
102
async def handle_request (self , request : Request) -> Response:
91
103
async with anyio.create_task_group() as tg:
@@ -95,6 +107,7 @@ async def handle_request(self, request: Request) -> Response:
95
107
```
96
108
97
109
2 . Notification Management
110
+
98
111
``` python
99
112
class NotificationManager :
100
113
async def __aenter__ (self ):
@@ -106,6 +119,7 @@ class NotificationManager:
106
119
```
107
120
108
121
3 . Timeout Control
122
+
109
123
``` python
110
124
async def with_timeout (timeout : float ):
111
125
async with anyio.move_on_after(timeout) as scope:
@@ -115,6 +129,7 @@ async def with_timeout(timeout: float):
115
129
### Error Handling Strategy
116
130
117
131
1 . Request Layer Error Handling
132
+
118
133
``` python
119
134
try :
120
135
response = await self .process_request(request)
@@ -125,6 +140,7 @@ except Exception as e:
125
140
```
126
141
127
142
2 . API Call Retry Mechanism
143
+
128
144
``` python
129
145
async def call_api_with_retry (self , func , * args , ** kwargs ):
130
146
for attempt in range (self .max_retries):
@@ -136,19 +152,21 @@ async def call_api_with_retry(self, func, *args, **kwargs):
136
152
await self .wait_before_retry(attempt)
137
153
```
138
154
139
- ## Setup Guide
155
+ ## Setup
156
+
157
+ ### Environment
140
158
141
- ### Environment Preparation
142
- We use uv as the dependency management tool, offering faster package installation and dependency resolution. If you haven't installed uv, please refer to the [ official documentation] ( https://github.com/astral-sh/uv ) .
159
+ We use uv for dependency management, offering faster package installation and dependency resolution. If you haven't installed uv, refer to the [ official documentation] ( https://github.com/astral-sh/uv ) .
143
160
144
161
### Configuration Steps
145
162
146
- 1 . Clone repository and set up environment:
163
+ 1 . Clone and setup:
164
+
147
165
``` bash
148
166
git clone https://github.com/donghao1393/mcp-openai
149
167
cd mcp-openai
150
168
151
- # Create and activate virtual environment using uv
169
+ # Create and activate venv with uv
152
170
uv venv
153
171
source .venv/bin/activate # Linux/macOS
154
172
# or
@@ -158,7 +176,8 @@ source .venv/bin/activate # Linux/macOS
158
176
uv pip install -e .
159
177
```
160
178
161
- 2 . Add service configuration to ` claude_desktop_config.json ` :
179
+ 2 . Add server config to ` claude_desktop_config.json ` :
180
+
162
181
``` json
163
182
{
164
183
"mcpServers" : {
@@ -181,13 +200,16 @@ uv pip install -e .
181
200
## Available Tools
182
201
183
202
### 1. ask-openai
184
- This tool provides interaction with OpenAI language models:
203
+
204
+ OpenAI language model interaction:
205
+
185
206
- ` query ` : Your question or prompt
186
- - ` model ` : Choose between "gpt-4" or "gpt-3.5-turbo"
187
- - ` temperature ` : Controls response randomness, range 0-2
188
- - ` max_tokens ` : Maximum response length, range 1-4000
207
+ - ` model ` : "gpt-4" or "gpt-3.5-turbo"
208
+ - ` temperature ` : Randomness control ( 0-2)
209
+ - ` max_tokens ` : Response length ( 1-4000)
189
210
190
211
Example:
212
+
191
213
``` python
192
214
response = await client.ask_openai(
193
215
query = " Explain quantum computing" ,
@@ -198,85 +220,114 @@ response = await client.ask_openai(
198
220
```
199
221
200
222
### 2. create-image
201
- This tool provides DALL·E image generation functionality:
202
- - ` prompt ` : Description of the image you want to generate
203
- - ` model ` : Choose between "dall-e-3" or "dall-e-2"
204
- - ` size ` : Image size, options: "1024x1024", "512x512", or "256x256"
205
- - ` quality ` : Image quality, options: "standard" or "hd"
206
- - ` n ` : Number of images to generate, range 1-10
223
+
224
+ DALL·E image generation with compressed display and original download links:
225
+
226
+ - ` prompt ` : Image description
227
+ - ` model ` : "dall-e-3" or "dall-e-2"
228
+ - ` size ` : Image dimensions:
229
+ - DALL·E 3: "1024x1024" (square), "1792x1024" (landscape), "1024x1792" (portrait)
230
+ - DALL·E 2: "1024x1024", "512x512", "256x256" only
231
+ - ` quality ` : "standard" or "hd" (DALL·E 3 only)
232
+ - ` n ` : Number of images (1-10)
207
233
208
234
Example:
235
+
209
236
``` python
237
+ # Landscape image
210
238
images = await client.create_image(
211
239
prompt = " A wolf running under moonlight" ,
212
240
model = " dall-e-3" ,
213
- size = " 1024x1024 " ,
241
+ size = " 1792x1024 " ,
214
242
quality = " hd" ,
215
243
n = 1
216
244
)
245
+
246
+ # Portrait image
247
+ images = await client.create_image(
248
+ prompt = " An ancient lighthouse" ,
249
+ model = " dall-e-3" ,
250
+ size = " 1024x1792" ,
251
+ quality = " standard" ,
252
+ n = 1
253
+ )
217
254
```
218
255
219
256
## Development Guide
220
257
221
258
### Code Standards
259
+
222
260
1 . ** Python Code Style**
261
+
223
262
- Follow PEP 8 guidelines
224
263
- Use black for code formatting
225
264
- Use pylint for code quality checking
226
-
227
265
2 . ** Async Programming Standards**
266
+
228
267
- Use async/await syntax
229
268
- Properly handle async context managers
230
269
- Appropriate use of task groups and cancel scopes
231
270
232
271
### Recommended Development Tools
272
+
233
273
- VS Code or PyCharm as IDE
234
274
- ` pylint ` and ` black ` for code quality checking and formatting
235
275
- ` pytest ` for unit testing
236
276
237
277
## Troubleshooting
238
278
239
279
### Common Issues
240
- 1 . Service Startup Issues
280
+
281
+ 1 . Startup Issues
282
+
241
283
- Check virtual environment activation
242
- - Verify all dependencies installation
284
+ - Verify dependency installation
243
285
- Confirm PYTHONPATH setting
244
286
- Validate OpenAI API key
245
-
246
287
2 . Runtime Errors
247
- - ModuleNotFoundError: Check PYTHONPATH and dependency installation
248
- - ImportError: Use ` uv pip list ` to verify package installation
288
+
289
+ - ModuleNotFoundError: Check PYTHONPATH and dependencies
290
+ - ImportError: Use ` uv pip list ` to verify packages
249
291
- Startup failure: Check Python version (>=3.10)
250
292
251
- ### Performance Optimization Tips
252
- 1 . For complex image generation tasks:
253
- - Increase timeout parameter appropriately, especially with DALL·E 3
254
- - Adjust max_retries parameter
293
+ ### Performance Tips
294
+
295
+ 1 . For Complex Image Generation:
296
+
297
+ - Increase timeout for DALL·E 3
298
+ - Adjust max_retries
255
299
- Simplify image descriptions
256
- - Consider using DALL·E 2 for faster response times
300
+ - Consider DALL·E 2 for faster response
301
+ 2 . Batch Processing:
257
302
258
- 2 . Batch Task Processing:
259
- - Allow at least 60 seconds processing time per image
303
+ - Allow 60 seconds per image
260
304
- Use appropriate concurrency control
261
- - Implement request queuing and rate limiting
305
+ - Implement request queuing
262
306
263
307
## Version History
264
308
265
- ### V0.3.2 (Current)
309
+ ### V0.4.0 (Current)
310
+
311
+ - Refactored image generation to use OpenAI native URLs
312
+ - Added original image download links
313
+ - Optimized image compression workflow
314
+ - Added DALL·E 3 landscape/portrait sizes
266
315
- Added uv package manager support
267
- - Optimized project structure
268
316
- Improved async operation stability
269
- - Enhanced error handling mechanisms
317
+ - Enhanced error handling
270
318
271
319
### V0.3.1
272
- - Added configurable timeout and retry mechanisms
320
+
321
+ - Added configurable timeout and retry
273
322
- Optimized image generation error handling
274
- - Enhanced user feedback information detail
323
+ - Enhanced user feedback detail
275
324
276
325
### V0.3.0
277
- - Implemented direct image display in conversations
326
+
327
+ - Implemented in-chat image display
278
328
- Optimized error handling and response format
279
- - Introduced anyio-based async processing framework
329
+ - Introduced anyio-based async framework
280
330
281
331
## License
282
- MIT License
332
+
333
+ MIT License
0 commit comments