3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Agent Engine 内で直接 Node.js の MCP サーバーを動かす方法

Last updated at Posted at 2025-08-26

はじめに

Google Cloud の Vertex AI Agent Engine は、AI エージェントをデプロイ、管理、スケーリングできるようにするマネージドサービスです。
AI エージェントは MCP(Model Context Protocol) と連携することで様々なツールを活用することが可能となっています。

MCP クライアントとサーバー間の通信では以下の2つがあります。

  1. stdio
    1. 標準入力と標準出力を介した通信
  2. Streamable HTTP

Streamable HTTP に対応している MCP サーバーの場合、Cloud Run などにデプロイすることで MCP クライアントと通信することが可能です。
しかし、stdio にしか対応していない MCP サーバーの場合、Agent Engine 内で起動させる必要があります。

多くの MCP サーバーは Node.js パッケージとして配布されています。
今回は、Node.js パッケージの MCP サーバーを Agent Engine 内で動作させる方法を紹介します。

方法

Agent Engine では、コンテナイメージをビルドする際に実行するインストールスクリプトを指定することが可能となっています。

以下のシェルスクリプトを配置します。

.
├── ...
└── installation_scripts/
    └── install.sh
install.sh
#!/bin/bash

# Exit immediately if a command exits with a non-zero status.
set -e

echo "--- Installing System-Wide Node.js v20.x ---"

# 1. Install prerequisites
apt-get update
apt-get install -y ca-certificates curl gnupg

# 2. Add the NodeSource repository GPG key
mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

# 3. Add the NodeSource repository for Node.js v20
NODE_MAJOR=20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list

# 4. Update package lists again and install Node.js
apt-get update
apt-get install nodejs -y

# MCP install
npm install @modelcontextprotocol/server-everything

こちらの MCP サーバーを使用します。

Agent Engine をデプロイする際に、extra_packagesbuild_optionsにシェルスクリプトのパスを指定します。

import vertexai
from vertexai import agent_engines

remote_agent = agent_engines.create(
    ...
    extra_packages=["installation_scripts/install.sh"],
    build_options = {"installation_scripts": ["installation_scripts/install.sh"]}
)
Agent Engine デプロイ用 Python コード
deploy.py
import os

import vertexai
from agents.agent import root_agent
from vertexai import agent_engines
from vertexai.preview import reasoning_engines

vertexai.init(
    project="{PROJECT_ID}",
    location="us-central1",
    staging_bucket="gs://{BUCKET}",
)

directory = "agents"

adk_app = reasoning_engines.AdkApp(
    agent=root_agent,
    enable_tracing=True,
)

remote_agent = agent_engines.create(
    adk_app,
    display_name=root_agent.name,
    requirements=[
        "google-adk==1.10.0",
        "google-cloud-aiplatform[agent_engines]",
        "google-genai",
        "pydantic",
    ],
    extra_packages=[directory, "installation_scripts/install.sh"],
    gcs_dir_name=directory,
    service_account={SERVICE_ACCOUNT},
    build_options = {"installation_scripts": ["installation_scripts/install.sh"]}
)

AI エージェントの用意

エージェントには、Agent Developent Kit を使用します。

.
├── agents/
│   ├── __init__.py
│   ├── .env
│   └── agent.py
├── installation_scripts/
│   └── install.sh
└── deploy.py
agent.py
from google.adk.agents import LlmAgent
from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset, StdioConnectionParams, StdioServerParameters


MODEL = "gemini-2.5-flash"

root_agent = LlmAgent(
    model=MODEL,
    name='everything_agent',
    instruction='ユーザーの質問に MCP を使用して回答してください。',
    tools=[
        MCPToolset(
            connection_params=StdioConnectionParams(
                server_params = StdioServerParameters(
                    command='npx',
                    args=[
                        "-y",
                        "@modelcontextprotocol/server-everything",
                    ],
                ),
                timeout=20.0,
            ),
            errlog=None,
        )
    ],
)

デプロイ

python deploy.py

検証

import vertexai
from vertexai import agent_engines

vertexai.init(project={PROJECT_ID}, location="us-central1")

remote_app = agent_engines.get({AGENT_ENGINE_ID})
remote_session = remote_app.create_session(user_id="user")

for event in remote_app.stream_query(
    user_id="user",
    session_id=remote_session["id"],
    message="structuredContent を使って、東京",
):
    print(event)

structuredContent ツール

structuredContent ツールは、固定の天気データのオブジェクトが返される仕様になっています。

  • temperature: 22.5(摂氏温度)
  • conditions: "Partly cloudy"(天気の状況)
  • humidity: 65(湿度パーセンテージ)
function レスポンス
{
  "content": {
    "parts": [
      {
        "function_response": {
          "name": "structuredContent",
          "response": {
            "result": {
              "content": [
                {
                  "type": "text",
                  "text": '{
             "temperature" : 22.5,
             "conditions" : "Partly cloudy",
             "humidity" : 65
           }'
                }
              ],
              "structuredContent": {
                "temperature": 22.5,
                "conditions": "Partly cloudy",
                "humidity": 65
              },
              "isError": False
            }
          }
        }
      }
    ],
    "role": "user"
  },
  ...
}
レスポンス
{
  "content": {
    "parts": [
      {
        "text": "東京の天候は一部曇りで、気温は22.5度、湿度は65%です。"
      }
    ],
    "role": "model"
  },
  ...
}

Agent Engine 上で、Node.js の stdio の MCP サーバーが起動していて、ツールを使って回答が得られることが確認できました!

まとめ

Vertex AI Agent Engine 上に Node.js パッケージの MCP サーバーをインストールして、そのツールが使われていることを確認しました。
当初、stdio 接続のツールをどうやって Agent Engine と連携させるか迷いましたが、stdio にしか対応していない MCP サーバーでも実行できる環境であることが分かりました。

Vertex AI Agent Engine が多様なツールエコシステムと連携できる高い拡張性を持っていることを再確認しました。
AI エージェント開発の自由度が高くなりそうだと感じました!

3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?