木曜日, 5月 8, 2025
No menu items!
ホームニューステックニュースたった38行で作る!初めてのGoogle検索MCPサーバー #GoogleCloud - Qiita

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



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

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

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

手順

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

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

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

    2025/5/5時点で実行時にエラーが出ました。
    mcpのバージョンが最新の1.7.1だとうまく動かないようで、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=

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

source .venv/bin/activate

Gradioを起動します。

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

* 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サーバーがつくれていいんじゃないでしょうか!



フラッグシティパートナーズ海外不動産投資セミナー 【DMM FX】入金

Source link

Views: 0

RELATED ARTICLES

返事を書く

あなたのコメントを入力してください。
ここにあなたの名前を入力してください

- Advertisment -

Most Popular