谷歌搜索MCP服务

一个利用Google自定义搜索(CSE)从互联网检索信息的模型上下文协议(MCP)服务器,使大型语言模型(LLM)能够执行实时网络搜索。

  • by ShogoOkamoto
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 ShogoOkamoto/google-search-mcp

使用说明

谷歌搜索mcp

一种模型上下文协议(MCP)服务器,利用谷歌自定义搜索(CSE)从互联网检索信息。通过利用此MCP,大型语言模型(LLM)能够执行实时Web搜索。

先决条件

需要Google自定义搜索API密钥和自定义搜索引擎ID:

  1. 从获取Google API密钥 谷歌云控制台
  2. 在以下位置创建自定义搜索引擎 可编程搜索引擎

安装

pip install google-search-mcp

或者从源代码安装:

pip install .

配置

创建一个 .env 项目根目录中的文件:

cp .env.example .env

然后编辑 .env 文件并添加您的凭据:

GOOGLE_API_KEY=your-api-key
GOOGLE_CSE_ID=your-custom-search-engine-id

或者,您可以直接设置环境变量:

export GOOGLE_API_KEY="your-api-key"
export GOOGLE_CSE_ID="your-custom-search-engine-id"

用法

作为CLI

google-search-mcp

使用克劳德桌面

将以下内容添加到您的Claude Desktop配置文件中:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json 视窗: %APPDATA%\Claude\claude_desktop_config.json

对于macOS/Linux:

{
  "mcpServers": {
    "google-search": {
      "command": "python",
      "args": ["-m", "google_search_mcp"],
      "env": {
        "GOOGLE_API_KEY": "your-api-key",
        "GOOGLE_CSE_ID": "your-custom-search-engine-id"
      }
    }
  }
}

对于Windows:

如果 python 是否在您的路径中(请与 where python),您可以使用与macOS/Linux相同的配置:

{
  "mcpServers": {
    "google-search": {
      "command": "python",
      "args": ["-m", "google_search_mcp"],
      "env": {
        "GOOGLE_API_KEY": "your-api-key",
        "GOOGLE_CSE_ID": "your-custom-search-engine-id"
      }
    }
  }
}

如果你需要使用特定的Python安装,请使用以下内容找到完整路径:

python -c "import sys; print(sys.executable)"

然后在配置中使用完整路径:

{
  "mcpServers": {
    "google-search": {
      "command": "C:\\Users\\YOUR_USERNAME\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\\python.exe",
      "args": ["-m", "google_search_mcp"],
      "env": {
        "GOOGLE_API_KEY": "your-api-key",
        "GOOGLE_CSE_ID": "your-custom-search-engine-id"
      }
    }
  }
}

注: 必须在Claude Desktop配置中设置环境变量 env 服务器访问它们的部分。

工具

搜索

使用谷歌自定义搜索搜索网络。

参数:

  • query (string,必填):搜索查询字符串
  • num_results (整数,可选):要返回的结果数(1-10,默认值10)

发展

运行测试

安装开发依赖项:

pip install -e ".[dev]"

运行测试套件:

pytest

运行具有详细输出的测试:

pytest -v

运行覆盖率测试:

pytest --cov=google_search_mcp --cov-report=html

测试覆盖率

该测试套件包括以下方面的全面测试:

  • 成功的搜索请求
  • 环境变量验证
  • API错误处理
  • JSON解析错误
  • 空搜索结果
  • 输入参数验证(num_results夹紧)
  • 结果格式化
  • 请求参数验证

许可证

MIT许可证-请参阅 许可证 了解详情。