36
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

たった38行で作る!初めてのGoogle検索MCPサーバー

Posted at

Gradioに、「MCPサーバーになる」機能が追加されました。

これとLangChainを組み合わせて、「お手軽Google検索MCPサーバー」を作りたいと思います。

手順

  1. uvでプロジェクトを作成します

    uv init
    
  2. ライブラリーを追加します

    • Gradio(With MCPライブラリー)
    • LangChainのGoogle Communityライブラリー
    uv add gradio[mcp] langchain-google-community
    

    2025/5/5時点で実行時にエラーが出ました。
    mcpのバージョンが最新の1.7.1だとうまく動かないようで、1.7.0を指定して追加でインストールしました。

    uv add mcp==1.7.0
    
  3. app.pyに実装を行います

    以下のドキュメントを参考に、関数を作ります

    https://python.langchain.com/docs/integrations/tools/google_search/

    def perform_web_search(query: str, num_results: int = 10):
        """
        Performs a web search using the Google Search API, ideal for general queries, news, articles, and online content.
        Use this for broad information gathering, recent events, or when you need diverse web sources.
        Maximum 20 results per request, with offset for pagination.
    
        Args:
            query (str): Search query (max 400 chars, 50 words)
            num_results (int): Number of results (1-20, default 10)
    
        Returns:
            str: Search results
        """
    
        search = GoogleSearchAPIWrapper()
    
        return json.dumps(
            search.results(query=query, num_results=num_results),
            indent=2,
            ensure_ascii=False,
        )
    

    docstringはBrave Search MCP Serverから拝借しました。MCPツールの説明として利用されるのでしっかり書きましょう。

    GradioのInterfaceを作成します

    demo.launchのパラメーターとしてmcp_server=Trueをセットすることで、MCPサーバー機能が有効になります。 これだけ です!

    demo = gr.Interface(
        fn=perform_web_search, inputs=[gr.Text(), gr.Number(value=10)], outputs=gr.Textbox
    )
    
    if __name__ == "__main__":
        demo.launch(mcp_server=True)
    

    これで完成です!

app.py全体(たった38行!)
import json

import gradio as gr
from dotenv import load_dotenv
from langchain_google_community import GoogleSearchAPIWrapper

load_dotenv()


def perform_web_search(query: str, num_results: int = 10):
    """
    Performs a web search using the Google Search API, ideal for general queries, news, articles, and online content.
    Use this for broad information gathering, recent events, or when you need diverse web sources.
    Maximum 20 results per request, with offset for pagination.

    Args:
        query (str): Search query (max 400 chars, 50 words)
        num_results (int): Number of results (1-20, default 10)

    Returns:
        str: Search results
    """

    search = GoogleSearchAPIWrapper()

    return json.dumps(
        search.results(query=query, num_results=num_results),
        indent=2,
        ensure_ascii=False,
    )


demo = gr.Interface(
    fn=perform_web_search, inputs=[gr.Text(), gr.Number(value=10)], outputs=gr.Textbox
)

if __name__ == "__main__":
    demo.launch(mcp_server=True)

動かす

.envに環境変数をセットします。

GOOGLE_CSE_ID=
GOOGLE_API_KEY=

GOOGLE_API_KEYはGoogle Cloudで発行、GOOGLE_CSE_IDはプログラム可能検索エンジンで発行します。

ここに簡単な手順を書いてたので良かったらどうぞ。
https://qiita.com/moritalous/items/99a7cdf1620e79d77290#google-search

Python仮想環境を有効にします。

source .venv/bin/activate

Gradioを起動します。

python app.py 

こんな感じで出力されれば起動完了です。

* Running on local URL:  http://127.0.0.1:7860
* To create a public link, set `share=True` in `launch()`.

🔨 MCP server (using SSE) running at: http://127.0.0.1:7860/gradio_api/mcp/sse

ClineのMCP設定に追加します。

{
  "mcpServers": {
    "google-search": {
      "url": "http://127.0.0.1:7860/gradio_api/mcp/sse"
    }
  }
}

追加に成功すると、こうなります。

image.png

image.png

まだ動作が不安定な気もしますが、、簡単にMCPサーバーがつくれていいんじゃないでしょうか!

36
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?