Google Search MCP Server

Google 検索 MCP サーバー

Google Custom Search API を通じてウェブ検索と画像検索機能を提供するモデルコンテキストプロトコル(MCP)サーバー。このサーバーは MCP 仕様に準拠しており、Claude やその他の AI アシスタントと統合できます。

私たちが構築するもの

多くのAIアシスタントは最新情報やウェブ検索機能を備えていません。このMCPサーバーは、以下の2つのツールを提供することでこの問題を解決します。

  • google_web_search : 最新情報をウェブで検索
  • google_image_search : クエリに関連する画像を検索

MCP 対応クライアント (Claude in Cursor、VSCode、Claude Desktop など) に接続すると、AI アシスタントは検索を実行し、現在の情報にアクセスできるようになります。

MCPコアコンセプト

MCPサーバーはAIアシスタントに機能を提供します。このサーバーは以下の機能を実装しています。

  • ツール:AIが呼び出せる機能(ユーザーの承認が必要)
  • 構造化コミュニケーション:MCPプロトコルによる標準化されたメッセージング形式
  • トランスポート層: 標準入出力を介した通信

前提条件

  • Node.js (v18以上) と npm
  • Google Cloud Platform アカウント
  • Google カスタム検索 API キーと検索エンジン ID
  • MCP 互換クライアント (Claude for Desktop、Cursor、VSCode with Claude など)

クイックスタート(このリポジトリをクローンする)

このサーバーを最初から構築せずに使用したい場合は、次の手順に従ってください。

# Clone the repository git clone https://github.com/yourusername/google-search-mcp-server.git cd google-search-mcp-server # Install dependencies npm install # Set up your environment variables # Setup .env file in the root folder of the project # On macOS/Linux touch .env # On Windows new-item .env # Edit .env file to add your Google API credentials # Use any text editor you prefer (VS Code, Notepad, nano, vim, etc.) # Add these to your newly created .env GOOGLE_API_KEY=your_api_key_here GOOGLE_CSE_ID=your_search_engine_id_here # Build the server npm run build # Test the server (optional) # On macOS/Linux echo '{"jsonrpc":"2.0","method":"listTools","id":1}' | node dist/index.js # On Windows PowerShell echo '{"jsonrpc":"2.0","method":"listTools","id":1}' | node dist/index.js # On Windows CMD echo {"jsonrpc":"2.0","method":"listTools","id":1} | node dist/index.js

ビルド後、 「MCP クライアントへの接続」セクションに従って、サーバーを優先クライアントに接続します。

環境を設定する(ゼロから構築)

自分でサーバーを最初から構築したい場合は、次の手順に従ってください。

プロジェクト構造を作成する

macOS/Linux

# Create a new directory for our project mkdir google-search-mcp cd google-search-mcp # Initialize a new npm project npm init -y # Install dependencies npm install @modelcontextprotocol/sdk dotenv zod npm install -D @types/node typescript # Create our files mkdir src touch src/index.ts

ウィンドウズ

# Create a new directory for our project md google-search-mcp cd google-search-mcp # Initialize a new npm project npm init -y # Install dependencies npm install @modelcontextprotocol/sdk dotenv zod npm install -D @types/node typescript # Create our files md src new-item src\index.ts

TypeScriptの設定

ルート ディレクトリにtsconfig.jsonを作成します。

{ "compilerOptions": { "target": "ES2022", "module": "Node16", "moduleResolution": "Node16", "outDir": "./dist", "rootDir": "./src", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true }, "include": ["src/**/*"], "exclude": ["node_modules"] }

package.json を更新する

package.jsonに以下が含まれていることを確認します。

{ "name": "google_search_mcp", "version": "0.1.0", "description": "MCP server for Google Custom Search API integration", "license": "MIT", "type": "module", "bin": { "google_search": "./dist/index.js" }, "files": [ "dist" ], "scripts": { "build": "tsc", "build:unix": "tsc && chmod 755 dist/index.js", "prepare": "npm run build", "watch": "tsc --watch", "start": "node dist/index.js" } }

Google API のセットアップ

Google Cloud Platform を設定し、API 認証情報を取得する必要があります。

Google Cloud Platform のセットアップ

  1. Google Cloud Consoleに移動
  2. 新しいプロジェクトを作成する
  3. カスタム検索 API を有効にします。
    Navigate to "APIs & Services" → "Library" Search for "Custom Search API" Click on "Custom Search API" → "Enable"
  4. API 資格情報を作成します。
    Navigate to "APIs & Services" → "Credentials" Click "Create Credentials" → "API key" Copy your API key

カスタム検索エンジンの設定

  1. プログラム可能な検索エンジン
  2. 「追加」をクリックして新しい検索エンジンを作成します
  3. 「ウェブ全体を検索」を選択し、検索エンジンに名前を付けます
  4. コントロールパネルから検索エンジンID(cx値)を取得します

環境設定

ルート ディレクトリに.envファイルを作成します。

GOOGLE_API_KEY=your_api_key_here GOOGLE_CSE_ID=your_search_engine_id_here

資格情報を保護するために、 .gitignoreファイルに.envを追加します。

echo ".env" >> .gitignore

サーバーの構築

サーバー実装を作成する

src/index.tsにサーバー実装を作成します。

import dotenv from "dotenv" import { Server } from "@modelcontextprotocol/sdk/server/index.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { CallToolRequestSchema, ListToolsRequestSchema, Tool, } from "@modelcontextprotocol/sdk/types.js"; dotenv.config(); // Define your tools const WEB_SEARCH_TOOL: Tool = { name: "google_web_search", description: "Performs a web search using Google's Custom Search API...", inputSchema: { // Schema details here }, }; const IMAGE_SEARCH_TOOL: Tool = { name: "google_image_search", description: "Searches for images using Google's Custom Search API...", inputSchema: { // Schema details here } }; // Server implementation const server = new Server( { name: "google-search", version: "0.1.0", }, { capabilities: { tools: {}, }, }, ); // Check for API key and Search Engine ID const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY!; const GOOGLE_CSE_ID = process.env.GOOGLE_CSE_ID!; if (!GOOGLE_API_KEY || !GOOGLE_CSE_ID) { console.error("Error: Missing environment variables"); process.exit(1); } // Tool handlers server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [WEB_SEARCH_TOOL, IMAGE_SEARCH_TOOL], })); server.setRequestHandler(CallToolRequestSchema, async (request) => { // Implement tool handlers }); // Run the server async function runServer() { const transport = new StdioServerTransport(); await server.connect(transport); console.error("Google Search MCP Server running on stdio"); } runServer().catch((error) => { console.error("Fatal error running server:", error); process.exit(1); });

完全な実装の詳細については、リポジトリ ファイルを参照してください。

サーバーの構築

実装が完了したら、サーバーを構築します。

npm run build

これにより、TypeScript コードがdistディレクトリ内の JavaScript にコンパイルされます。

MCPクライアントへの接続

MCPサーバーは様々なクライアントに接続できます。一般的なクライアントの設定手順は以下のとおりです。

デスクトップ版クロード

macOS/Linux

  1. 設定ファイルを開きます:
code ~/Library/Application\ Support/Claude/claude_desktop_config.json
  1. サーバー構成を追加します。
{ "mcpServers": { "google_search": { "command": "node", "args": [ "/absolute/path/to/google-search-mcp/dist/index.js" ], "env": { "GOOGLE_API_KEY": "your_api_key_here", "GOOGLE_CSE_ID": "your_search_engine_id_here" } } } }

ウィンドウズ

  1. 設定ファイルを開きます:
code $env:AppData\Claude\claude_desktop_config.json
  1. サーバー構成を追加します。
{ "mcpServers": { "google_search": { "command": "node", "args": [ "C:\\absolute\\path\\to\\google-search-mcp\\dist\\index.js" ], "env": { "GOOGLE_API_KEY": "your_api_key_here", "GOOGLE_CSE_ID": "your_search_engine_id_here" } } } }
  1. デスクトップ版のClaudeを再起動
  2. インターフェースのツールアイコンをクリックしてツールが表示されていることを確認します

クロードとVSCode

macOS/Linux および Windows

  1. VSCode用のMCP拡張機能をインストールする
  2. ワークスペースで.vscode/settings.jsonを作成または編集します。

macOS/Linuxの場合:

{ "mcp.servers": { "google_search": { "command": "node", "args": [ "/absolute/path/to/google-search-mcp/dist/index.js" ], "env": { "GOOGLE_API_KEY": "your_api_key_here", "GOOGLE_CSE_ID": "your_search_engine_id_here" } } } }

Windowsの場合:

{ "mcp.servers": { "google_search": { "command": "node", "args": [ "C:\\absolute\\path\\to\\google-search-mcp\\dist\\index.js" ], "env": { "GOOGLE_API_KEY": "your_api_key_here", "GOOGLE_CSE_ID": "your_search_engine_id_here" } } } }
  1. VSCodeを再起動します
  2. これらのツールはVSCodeでClaudeが利用できるようになります。

カーソル

  1. カーソル設定を開く(歯車アイコン)
  2. 「MCP」を検索し、MCP設定を開きます
  3. 「新しいMCPサーバーを追加」をクリックします
  4. 上記と同様の設定で構成します。

macOS/Linuxの場合:

{ "mcpServers": { "google_search": { "command": "node", "args": [ "/absolute/path/to/google-search-mcp/dist/index.js" ], "env": { "GOOGLE_API_KEY": "your_api_key_here", "GOOGLE_CSE_ID": "your_search_engine_id_here" } } } }

Windowsの場合:

{ "mcpServers": { "google_search": { "command": "node", "args": [ "C:\\absolute\\path\\to\\google-search-mcp\\dist\\index.js" ], "env": { "GOOGLE_API_KEY": "your_api_key_here", "GOOGLE_CSE_ID": "your_search_engine_id_here" } } } }
  1. カーソルを再開

サーバーのテスト

クロードと一緒に使う

接続したら、次のような質問をして Claude にツールをテストできます。

  • 「再生可能エネルギーに関する最新ニュースを検索」
  • 「電気自動車の画像を探す」
  • 「日本で最も人気のある観光地はどこですか?」

Claude は必要に応じて適切な検索ツールを自動的に使用します。

手動テスト

サーバーを直接テストすることもできます。

# Test web search echo '{ "jsonrpc": "2.0", "method": "callTool", "params": { "name": "google_web_search", "arguments": { "query": "test query", "count": 2 } }, "id": 1 }' | node dist/index.js

ボンネットの下で何が起こっているのか

質問するときは:

  1. クライアントがあなたの質問をクロードに送ります
  2. クロードは利用可能なツールを分析し、どれを使用するかを決定します
  3. クライアントはMCPサーバーを通じて選択したツールを実行します。
  4. 結果はクロードに送り返される
  5. クロードは検索結果に基づいて自然言語応答を作成します
  6. 応答が表示されます

トラブルシューティング

よくある問題

環境変数

Error: GOOGLE_API_KEY environment variable is required :

# Check your .env file cat .env # Try setting environment variables directly: export GOOGLE_API_KEY=your_key_here export GOOGLE_CSE_ID=your_id_here

APIエラー

API エラーが発生した場合:

# Test your API credentials directly curl "https://www.googleapis.com/customsearch/v1?key=YOUR_API_KEY&cx=YOUR_CX_ID&q=test"

接続の問題

クライアントがサーバーに接続できない場合:

# Verify the server runs correctly on its own node dist/index.js # Check file permissions chmod 755 dist/index.js # Ensure you're using absolute paths in your configuration

APIリファレンス

Google のカスタム検索 API を使用して Web 検索を実行します。

パラメータ:

  • query (文字列、必須): 検索クエリ
  • count (数値、オプション): 結果の数 (1-10、デフォルトは5)
  • start (数値、オプション):ページネーションの開始インデックス(デフォルトは1)
  • site (文字列、オプション):検索を特定のサイトに制限します(例:'example.com')

Google のカスタム検索 API を使用して画像を検索します。

パラメータ:

  • query (文字列、必須): 画像検索クエリ
  • count (数値、オプション): 結果の数 (1-10、デフォルトは5)
  • start (数値、オプション):ページネーションの開始インデックス(デフォルトは1)

制限事項

  • Google カスタム検索 API の無料枠: 1 日あたり 100 クエリ
  • サーバーによるレート制限: 1 秒あたり 5 リクエスト
  • クエリごとに最大 10 件の結果 (Google API の制限)

ライセンス

このプロジェクトは MIT ライセンスに基づいてライセンスされています - 詳細については LICENSE ファイルを参照してください。

ID: 5pgk82sfi7