Skip to content

Commit c34424d

Browse files
committed
init project
0 parents  commit c34424d

File tree

7 files changed

+186
-0
lines changed

7 files changed

+186
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.10

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# OpenAI WebSearch MCP
2+
3+
一个使用FastMCP框架的模型控制协议(MCP)服务器,旨在将OpenAI的网络搜索功能集成到Claude等不具备网络搜索能力的AI模型中。
4+
5+
## 项目概述
6+
7+
OpenAI WebSearch MCP利用OpenAI的`web_search_preview`工具,使其他AI模型(如Claude)能够执行网络搜索并在回答中利用最新的网络信息。该项目通过FastMCP框架实现,提供了一个简单的API,可以轻松集成到现有的AI工作流程中。
8+
9+
## 功能特点
10+
11+
- 通过MCP协议为Claude等AI模型提供网络搜索能力
12+
- 利用OpenAI的网络搜索API获取实时网络信息
13+
- 支持自定义搜索参数和用户位置设置
14+
- 基于FastMCP框架,易于扩展和维护
15+
16+
## 安装
17+
18+
本项目使用`uv`进行包管理,确保您已安装了Python和uv。
19+
20+
1. 克隆仓库:
21+
```bash
22+
git clone https://github.com/yourusername/openai-websearch-mcp.git
23+
cd openai-websearch-mcp
24+
```
25+
26+
2. 安装依赖:
27+
```bash
28+
uv sync --frozen
29+
```
30+
31+
## 配置
32+
33+
1. 设置OpenAI API密钥:
34+
```bash
35+
export OPENAI_API_KEY='your-api-key-here'
36+
```
37+
38+
## 使用方法
39+
40+
### 启动MCP服务器测试
41+
42+
```bash
43+
fastmcp run main.py
44+
45+
# Or run with inspector
46+
fastmcp dev main.py
47+
```
48+
49+
服务器默认在`localhost:5173`启动。
50+
51+
### 在客户端中使用
52+
53+
在与Claude或其他AI模型的交互中使用MCP客户端调用网络搜索功能:
54+
55+
```python
56+
fastmcp install server.py -e API_KEY=your-api-key
57+
```
58+
59+
## API参考
60+
61+
### 网络搜索工具属性
62+
63+
- `type`: 搜索工具类型,始终为`web_search_preview`
64+
- `search_context_size`: 搜索上下文窗口大小,可选值为"low"、"medium"、"high",默认为"medium"
65+
- `user_location`: 用户位置参数(可选)
66+
- `type`: 位置近似类型,始终为"approximate"
67+
- `city`: 用户所在城市,例如"Shanghai"
68+
- `country`: 用户所在国家的两字母ISO代码,例如"CN"
69+
- `region`: 用户所在地区,例如"Shanghai"
70+
- `timezone`: 用户IANA时区,例如"Asia/Shanghai"
71+
72+
73+
## 许可证
74+
75+
[MIT License](LICENSE)
76+
77+
## 贡献
78+
79+
欢迎贡献代码、报告问题或提出改进建议!请fork本仓库并提交拉取请求。

docs/openai-websearch-tool.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
# Web search
3+
This tool searches the web for relevant results to use in a response. Learn more about the web search tool.
4+
5+
6+
## properties
7+
`type` string
8+
9+
> Required
10+
> The type of the web search tool. One of:
11+
> web_search_preview
12+
> web_search_preview_2025_03_11
13+
14+
15+
`search_context_size` string
16+
17+
> Optional
18+
> Defaults to medium
19+
> High level guidance for the amount of context window space to use for the search. One of low, medium, or high. medium is the default.
20+
21+
`user_location` object or null
22+
23+
> Optional
24+
> Approximate location parameters > for the search.
25+
26+
27+
properties of `user_location`
28+
`type` string
29+
30+
> Required
31+
> The type of location > approximation. Always approximate.
32+
33+
`city` string
34+
35+
> Optional
36+
> Free text input for the city of the user, e.g. San Francisco.
37+
38+
`country` string
39+
40+
> Optional
41+
> The two-letter ISO country code of the user, e.g. US.
42+
43+
`region` string
44+
45+
> Optional
46+
> Free text input for the region of the user, e.g. California.
47+
48+
`timezone` string
49+
50+
> Optional
51+
> The IANA timezone of the user, e.g. America/Los_Angeles.

main.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pydantic import BaseModel
2+
from typing import Literal
3+
from mcp.server.fastmcp import FastMCP
4+
from openai import OpenAI
5+
from pydantic_extra_types.timezone_name import TimeZoneName
6+
from pydantic import BaseModel
7+
8+
mcp = FastMCP("OpenAI Web Search")
9+
client = OpenAI()
10+
11+
12+
class UserLocation(BaseModel):
13+
type: Literal["approximate"] = "approximate"
14+
city: str
15+
country: str = None
16+
region: str = None
17+
timezone: TimeZoneName
18+
19+
20+
@mcp.tool()
21+
def web_search(
22+
input: str,
23+
model: Literal["gpt-4o", "gpt-4o-mini"] = "gpt-4o-mini",
24+
type: Literal["web_search_preview", "web_search_preview_2025_03_11"] = "web_search_preview",
25+
search_context_size: Literal["low", "medium", "high"] = "medium",
26+
user_location: UserLocation = None,
27+
) -> list[str]:
28+
response = client.responses.create(
29+
model=model,
30+
tools=[
31+
{
32+
"type": type,
33+
"search_context_size": search_context_size,
34+
"user_location": user_location.model_dump() if user_location else None,
35+
}
36+
],
37+
input=input,
38+
)
39+
return response.output_text
40+

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
name = "openai-websearch-mcp"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.10"
7+
dependencies = []

uv.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)