Google Search MCP Server

Google Search MCP Server

Enables users to perform Google Custom Search queries through the Model Context Protocol. Requires Google API credentials and Custom Search Engine configuration for web search functionality.

Category
访问服务器

README

Google Search MCP Server

A Model Context Protocol (MCP) server that provides Google Custom Search functionality.

Configuration

This server uses Dynaconf for configuration management, supporting both .env files and environment variables.

Setup

  1. Copy the example environment file:

    cp .env.example .env
    
  2. Edit .env and add your Google API credentials:

    GOOGLE_API_KEY=your_actual_api_key_here
    GOOGLE_CX=your_custom_search_engine_id_here
    

Environment Variable Override

Environment variables will override .env file values when both are present. This allows for flexible deployment scenarios:

  • Development: Use .env file for local development
  • Production: Use environment variables for production deployment
  • CI/CD: Environment variables can override defaults for testing

Example:

If your .env file contains:

GOOGLE_API_KEY=dev_key_from_file

And you set an environment variable:

export GOOGLE_API_KEY=prod_key_from_env

The server will use prod_key_from_env (environment variable takes precedence).

Required Configuration

  • GOOGLE_API_KEY: Your Google Custom Search API key
  • GOOGLE_CX: Your Custom Search Engine ID

Get these from:

Optional Configuration

  • ALLOW_DOMAINS (GOOGLE_ALLOW_DOMAINS env var): Comma-separated list of allowed domains (e.g., example.com, docs.python.org). When set, results outside these domains are filtered out.

  • Logging:

    • LOG_QUERIES (GOOGLE_LOG_QUERIES): Enable logging of query hash, timing, and key params.
    • LOG_QUERY_TEXT (GOOGLE_LOG_QUERY_TEXT): Also log full query text (off by default).
    • LOG_LEVEL (GOOGLE_LOG_LEVEL): Set logging level (e.g., INFO, DEBUG).
    • LOG_FILE (GOOGLE_LOG_FILE): Optional path to also write logs to a file (stderr remains enabled).

Usage

Run the server:

python server.py

Web-based MCP (SSE)

If you are integrating with a browser or any client that uses MCP over HTTP/SSE, run the web server variant:

# Install HTTP extras
uv sync --extra http

# Start the SSE MCP server (defaults to 127.0.0.1:8000)
uv run google-search-mcp-http

# Or run directly
uv run python -m server_http

This exposes the standard MCP endpoints used by web clients:

  • GET /sse (Server-Sent Events connection)
  • POST /messages (MCP messages)

Notes:

  • CORS is enabled by default (CORS_ORIGINS="*"). Customize with CORS_ORIGINS env var.
  • Same configuration applies as stdio mode (GOOGLE_API_KEY, GOOGLE_CX, logging flags, etc.).

Run via uvx (no activation, from any folder)

The project exposes a console script google-search-mcp so you can run it with uvx without activating a venv:

# Run using the repo as the source
uvx --from /Users/justin/src/google_search_mcp google-search-mcp

# If you need .env loading, either run with cwd set to the repo
(cd /Users/justin/src/google_search_mcp && uvx --from . google-search-mcp)

# Or point Dynaconf directly at the .env
DYNACONF_DOTENV_PATH=/Users/justin/src/google_search_mcp/.env \
uvx --from /Users/justin/src/google_search_mcp google-search-mcp

Run via pipx (isolated, globally available)

You can also install and run the script with pipx:

# Install from local path
pipx install /Users/justin/src/google_search_mcp

# Or from a VCS URL (example)
# pipx install git+https://github.com/jspv/google_search_mcp.git

# Run the server
google-search-mcp

# If you rely on .env, run it from the repo directory or set DYNACONF_DOTENV_PATH
cd /Users/justin/src/google_search_mcp && google-search-mcp
# or
DYNACONF_DOTENV_PATH=/Users/justin/src/google_search_mcp/.env google-search-mcp

Quick start

This project uses httpx with HTTP/2 support enabled for better performance. The dependency is declared as httpx[http2] and will install the h2 package automatically when you sync the environment.

Using uv (recommended)

# Install deps from pyproject/lockfile
uv sync

# Create and fill in your configuration
cp .env.example .env
$EDITOR .env

# Run tests (optional sanity check)
uv run pytest -q

# Start the MCP server
uv run python server.py

Try it quickly (no MCP client required)

You can also call the tool function directly for a quick smoke test (uses your env vars):

uv run python -c 'import asyncio, server; print(asyncio.run(server.search("site:python.org httpx", num=2, safe="off")))'

Note: When using the MCP server with a client, the search tool parameters follow Google CSE semantics. In particular, safe must be one of off or active, and num is clamped to the CSE maximum of 10.

Logging behavior

If LOG_QUERIES is enabled, the server will write a single line per request to stdout containing:

  • q_hash (short, non-reversible hash of the query), dt_ms (latency), num, start, safe, and endpoint (cse/siterestrict)
  • If LOG_QUERY_TEXT is true, it also includes the full q text.

Example log line:

2025-09-27T12:34:56+0000 INFO google_search_mcp: search q_hash=1a2b3c4d dt_ms=123 num=5 start=1 safe=off endpoint=cse q="site:python.org httpx"

When a client spawns the server via uvx, logs go to the server process’s stderr by default (safe for MCP stdio). To persist logs regardless of the client’s stderr handling:

  • Set a file path (absolute recommended):
    GOOGLE_LOG_QUERIES=true
    GOOGLE_LOG_FILE=/var/log/google_search_mcp.log
    
  • Or redirect stderr in the launch command:
    uvx --from /path/to/repo google-search-mcp 2>> /tmp/google_search_mcp.log
    

Streamable HTTP Variant

For clients that use StreamableHttpServerParams (non-SSE MCP over HTTP), run the Streamable HTTP server variant:

# Ensure HTTP extras are installed
uv sync --extra http

# Start the Streamable MCP server
uv run google-search-mcp-stream

# Or run directly
uv run python -m server_http_stream

Client usage example with the official MCP streamable HTTP transport:

import asyncio
from mcp.client.session import ClientSession
from mcp.client.streamable_http import streamable_http_client


async def main():
   async with streamable_http_client("http://127.0.0.1:8000") as (read, write):
      async with ClientSession(read, write) as session:
         await session.initialize()
         tools = await session.list_tools()
         print([t.name for t in tools.tools])


if __name__ == "__main__":
   asyncio.run(main())

Notes:

  • Endpoints are managed by FastMCP; you should not call /sse for this variant.
  • CORS is enabled and configurable with CORS_ORIGINS.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选