内容
# Brave Search MCP Server
[](https://smithery.ai/server/@thomasvan/mcp-brave-search)
该项目实现了一个用于 Brave Search 的模型上下文协议(Model Context Protocol,MCP)服务器,允许与像 Claude 这样的 AI 助手集成。
## 先决条件
- Python 3.11+
- [uv](https://github.com/astral-sh/uv) - 一个快速的 Python 包安装和解析器
## 安装
### 通过 Smithery 安装
要通过 [Smithery](https://smithery.ai/server/@thomasvan/mcp-brave-search) 自动安装 Brave Search MCP 服务器以供 Claude Desktop 使用,请运行:
```bash
npx -y @smithery/cli install @thomasvan/mcp-brave-search --client claude
```
### 手动安装
1. 克隆代码库:
```
git clone https://github.com/thomasvan/mcp-brave-search.git
cd mcp-brave-search
```
2. 创建虚拟环境并使用 uv 安装依赖:
```
uv venv
source .venv/bin/activate # 在 Windows 上使用: .venv\Scripts\activate
uv pip install -r requirements.txt
```
3. 设置你的 Brave Search API 密钥:
```
export BRAVE_API_KEY=your_api_key_here
```
在 Windows 上使用: `set BRAVE_API_KEY=your_api_key_here`
## 使用
1. 配置你的 MCP 设置文件(例如,`claude_desktop_config.json`),以包含 Brave Search MCP 服务器:
```json
{
"mcpServers": {
"brave-search": {
"command": "uv",
"args": [
"--directory",
"path-to\\mcp-python\\mcp-brave-search\\src",
"run",
"server.py"
],
"env": {
"BRAVE_API_KEY": "YOUR_BRAVE_API_KEY_HERE"
}
}
}
}
```
将 `YOUR_BRAVE_API_KEY_HERE` 替换为你的实际 Brave API 密钥。
2. 通过运行更新后的配置启动 Brave Search MCP 服务器与兼容的 AI 助手。
3. 服务器现在将运行并准备接受来自 MCP 客户端的请求。
4. 你现在可以在你的 MCP 兼容 AI 助手(如 Claude)中使用 Brave Search 功能,通过调用可用工具。
## 可用工具
服务器提供两个主要工具:
1. `brave_web_search`:使用 Brave Search API 执行网页搜索。
2. `brave_local_search`:搜索本地商家和地点。
有关详细使用信息,请参阅 `src/server.py` 中的工具文档字符串。
## 开发
要对项目进行更改:
1. 根据需要修改 `src` 目录中的代码。
2. 如果添加或删除依赖项,请更新 `requirements.txt` 文件:
```
uv pip freeze > requirements.txt
```
3. 重启服务器以应用更改。
## 测试
该项目包括单元测试和集成测试:
### 安装测试依赖
```bash
uv pip install pytest pytest-asyncio pytest-cov
```
### 运行单元测试
单元测试可以在没有 API 密钥的情况下运行,并使用模拟来模拟 API 响应:
```bash
# 运行所有单元测试
python -m pytest tests/unit/
# 以详细输出运行
python -m pytest tests/unit/ -v
```
### 运行集成测试
集成测试需要有效的 Brave API 密钥并进行真实的 API 调用:
```bash
# 使用你的 API 密钥运行集成测试
BRAVE_API_KEY_INTEGRATION="your_api_key_here" python -m pytest tests/integration/ -v
```
### 测试覆盖率
要检查测试覆盖率:
```bash
python -m pytest --cov=src/mcp_brave_search
```
## 故障排除
如果遇到任何问题:
1. 确保你的 Brave API 密钥设置正确。
2. 检查所有依赖项是否已安装。
3. 验证你使用的 Python 版本是否兼容。
4. 如果你对代码进行了更改,请确保重启服务器。
## 许可证
该项目根据 MIT 许可证授权 - 详细信息请参见 [LICENSE](LICENSE) 文件。