Skip to content

Commit 580a74d

Browse files
committed
feat: 增加HTTP下载服务与文档更新
1. 新增HTTP图片下载服务器 2. 更新文档:英文README与中文版保持一致 3. 添加版本号(0.4.0)测试用例 4. 更新项目依赖配置
1 parent 65fd129 commit 580a74d

File tree

5 files changed

+195
-63
lines changed

5 files changed

+195
-63
lines changed

README.en.md

+102-51
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# OpenAI MCP Server
22

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.
44

5-
## Architecture
5+
## Architecture Design
66

77
### Overall Architecture
8+
89
```
910
+-----------------+
1011
| |
@@ -47,45 +48,56 @@ MCP protocol integration that enables direct invocation of OpenAI models from Cl
4748
### Core Components
4849

4950
1. **Server Core**
51+
5052
- Implements the MCP protocol
5153
- Handles request routing and lifecycle management
5254
- Provides configuration management and error handling
53-
5455
2. **Request Handler**
56+
5557
- Processes specific request types
5658
- Implements request parameter validation and transformation
5759
- Manages request timeouts and retry logic
58-
5960
3. **Notification Manager**
61+
6062
- Manages notification lifecycle
6163
- Implements reliable notification delivery
6264
- Handles notification cancellation and cleanup
63-
6465
4. **OpenAI Client**
66+
6567
- Encapsulates OpenAI API calls
6668
- Handles response transformation and error handling
6769
- Implements API rate limiting and retry strategies
6870

6971
## Features
7072

7173
### 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
7578

7679
### 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)
7991
- Configurable timeout and retry mechanisms
80-
- Multiple image sizes and quality options
81-
- Batch image generation capability
8292

8393
## Technical Implementation
8494

8595
### Asynchronous Processing
96+
8697
The project uses `anyio` as the async runtime, supporting both asyncio and trio. Key async processing includes:
8798

8899
1. Request Handling
100+
89101
```python
90102
async def handle_request(self, request: Request) -> Response:
91103
async with anyio.create_task_group() as tg:
@@ -95,6 +107,7 @@ async def handle_request(self, request: Request) -> Response:
95107
```
96108

97109
2. Notification Management
110+
98111
```python
99112
class NotificationManager:
100113
async def __aenter__(self):
@@ -106,6 +119,7 @@ class NotificationManager:
106119
```
107120

108121
3. Timeout Control
122+
109123
```python
110124
async def with_timeout(timeout: float):
111125
async with anyio.move_on_after(timeout) as scope:
@@ -115,6 +129,7 @@ async def with_timeout(timeout: float):
115129
### Error Handling Strategy
116130

117131
1. Request Layer Error Handling
132+
118133
```python
119134
try:
120135
response = await self.process_request(request)
@@ -125,6 +140,7 @@ except Exception as e:
125140
```
126141

127142
2. API Call Retry Mechanism
143+
128144
```python
129145
async def call_api_with_retry(self, func, *args, **kwargs):
130146
for attempt in range(self.max_retries):
@@ -136,19 +152,21 @@ async def call_api_with_retry(self, func, *args, **kwargs):
136152
await self.wait_before_retry(attempt)
137153
```
138154

139-
## Setup Guide
155+
## Setup
156+
157+
### Environment
140158

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).
143160

144161
### Configuration Steps
145162

146-
1. Clone repository and set up environment:
163+
1. Clone and setup:
164+
147165
```bash
148166
git clone https://github.com/donghao1393/mcp-openai
149167
cd mcp-openai
150168

151-
# Create and activate virtual environment using uv
169+
# Create and activate venv with uv
152170
uv venv
153171
source .venv/bin/activate # Linux/macOS
154172
# or
@@ -158,7 +176,8 @@ source .venv/bin/activate # Linux/macOS
158176
uv pip install -e .
159177
```
160178

161-
2. Add service configuration to `claude_desktop_config.json`:
179+
2. Add server config to `claude_desktop_config.json`:
180+
162181
```json
163182
{
164183
"mcpServers": {
@@ -181,13 +200,16 @@ uv pip install -e .
181200
## Available Tools
182201

183202
### 1. ask-openai
184-
This tool provides interaction with OpenAI language models:
203+
204+
OpenAI language model interaction:
205+
185206
- `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)
189210

190211
Example:
212+
191213
```python
192214
response = await client.ask_openai(
193215
query="Explain quantum computing",
@@ -198,85 +220,114 @@ response = await client.ask_openai(
198220
```
199221

200222
### 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)
207233

208234
Example:
235+
209236
```python
237+
# Landscape image
210238
images = await client.create_image(
211239
prompt="A wolf running under moonlight",
212240
model="dall-e-3",
213-
size="1024x1024",
241+
size="1792x1024",
214242
quality="hd",
215243
n=1
216244
)
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+
)
217254
```
218255

219256
## Development Guide
220257

221258
### Code Standards
259+
222260
1. **Python Code Style**
261+
223262
- Follow PEP 8 guidelines
224263
- Use black for code formatting
225264
- Use pylint for code quality checking
226-
227265
2. **Async Programming Standards**
266+
228267
- Use async/await syntax
229268
- Properly handle async context managers
230269
- Appropriate use of task groups and cancel scopes
231270

232271
### Recommended Development Tools
272+
233273
- VS Code or PyCharm as IDE
234274
- `pylint` and `black` for code quality checking and formatting
235275
- `pytest` for unit testing
236276

237277
## Troubleshooting
238278

239279
### Common Issues
240-
1. Service Startup Issues
280+
281+
1. Startup Issues
282+
241283
- Check virtual environment activation
242-
- Verify all dependencies installation
284+
- Verify dependency installation
243285
- Confirm PYTHONPATH setting
244286
- Validate OpenAI API key
245-
246287
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
249291
- Startup failure: Check Python version (>=3.10)
250292

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
255299
- 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:
257302

258-
2. Batch Task Processing:
259-
- Allow at least 60 seconds processing time per image
303+
- Allow 60 seconds per image
260304
- Use appropriate concurrency control
261-
- Implement request queuing and rate limiting
305+
- Implement request queuing
262306

263307
## Version History
264308

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
266315
- Added uv package manager support
267-
- Optimized project structure
268316
- Improved async operation stability
269-
- Enhanced error handling mechanisms
317+
- Enhanced error handling
270318

271319
### V0.3.1
272-
- Added configurable timeout and retry mechanisms
320+
321+
- Added configurable timeout and retry
273322
- Optimized image generation error handling
274-
- Enhanced user feedback information detail
323+
- Enhanced user feedback detail
275324

276325
### V0.3.0
277-
- Implemented direct image display in conversations
326+
327+
- Implemented in-chat image display
278328
- Optimized error handling and response format
279-
- Introduced anyio-based async processing framework
329+
- Introduced anyio-based async framework
280330

281331
## License
282-
MIT License
332+
333+
MIT License

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,13 @@ images = await client.create_image(
300300

301301
## 版本历史
302302

303+
### V0.4.0 (当前版本)
304+
305+
- 重构了图像生成实现,使用 OpenAI 原生 URL
306+
- 为生成图片添加了原图下载链接
307+
- 优化了图像压缩显示流程
308+
- 支持 DALL·E 3 的横竖屏尺寸
309+
303310
### V0.3.2 (当前版本)
304311

305312
- 添加了 uv 包管理器支持

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mcp-openai"
3-
version = "0.1.0"
3+
version = "0.4.0"
44
description = "MCP server for OpenAI API integration"
55
requires-python = ">=3.10"
66
dependencies = [
@@ -10,6 +10,7 @@ dependencies = [
1010
"pytest-asyncio",
1111
"tzdata>=2024.2",
1212
"pillow>=10.0.0", # 用于图像处理
13+
"aiohttp>=3.9.0", # 用于下载图片
1314
]
1415

1516
[build-system]

0 commit comments

Comments
 (0)