コンテンツにスキップ

Google Cloud ツール

Google Cloudツールを使用すると、エージェントをGoogle Cloudの製品やサービスに簡単に接続できます。わずか数行のコードでこれらのツールを使用して、エージェントを以下に接続できます。

  • 開発者がApigeeでホストする任意のカスタムAPI
  • Salesforce、Workday、SAPなどのエンタープライズシステムへの100以上事前構築済みコネクタ
  • Application Integrationを使用して構築された自動化ワークフロー
  • データベース向けMCP Toolboxを使用して、Spanner、AlloyDB、Postgresなどのデータベース

Google Cloud ツール

Apigee API Hub ツール

ADKでサポートPython v0.1.0

ApiHubToolsetを使用すると、Apigee API Hubからドキュメント化された任意のAPIを、わずか数行のコードでツールに変換できます。このセクションでは、APIへの安全な接続のための認証設定を含む、ステップバイステップの手順を説明します。

前提条件

  1. ADKをインストールする
  2. Google Cloud CLIをインストールする。
  3. ドキュメント化された(つまりOpenAPI仕様を持つ)APIを含む Apigee API Hub インスタンス
  4. プロジェクト構造を設定し、必要なファイルを作成する
project_root_folder
 |
 `-- my_agent
     |-- .env
     |-- __init__.py
     |-- agent.py
     `__ tool.py

API Hubツールセットの作成

注:このチュートリアルにはエージェントの作成が含まれています。すでにエージェントをお持ちの場合は、これらの手順の一部のみを従う必要があります。

  1. アクセストークンを取得し、APIHubToolsetがAPI Hub APIから仕様を取得できるようにします。 ターミナルで次のコマンドを実行します。

    gcloud auth print-access-token
    # 'ya29....' のようなアクセストークンが出力されます
    
  2. 使用するアカウントに必要な権限があることを確認します。 事前定義されたロールroles/apihub.viewerを使用するか、以下の権限を割り当てることができます。

    1. apihub.specs.get (必須)
    2. apihub.apis.get (任意)
    3. apihub.apis.list (任意)
    4. apihub.versions.get (任意)
    5. apihub.versions.list (任意)
    6. apihub.specs.list (任意)
  3. APIHubToolsetを使用してツールを作成します。以下をtools.pyに追加します。

    APIが認証を必要とする場合は、ツールの認証を設定する必要があります。 以下のコードサンプルは、APIキーを設定する方法を示しています。ADKはトークンベースの認証(APIキー、ベアラートークン)、サービスアカウント、およびOpenID Connectをサポートしています。近日中に、さまざまなOAuth2フローのサポートを追加する予定です。

    from google.adk.tools.openapi_tool.auth.auth_helpers import token_to_scheme_credential
    from google.adk.tools.apihub_tool.apihub_toolset import APIHubToolset
    
    # APIの認証を提供します。APIが認証を必要としない場合は不要です。
    auth_scheme, auth_credential = token_to_scheme_credential(
        "apikey", "query", "apikey", apikey_credential_str
    )
    
    sample_toolset = APIHubToolset(
        name="apihub-sample-tool",
        description="サンプルツール",
        access_token="...",  # ステップ1で生成したアクセストークンをコピー
        apihub_resource_name="...", # API Hubのリソース名
        auth_scheme=auth_scheme,
        auth_credential=auth_credential,
    )
    

    本番環境へのデプロイでは、アクセストークンの代わりにサービスアカウントを使用することをお勧めします。上記のコードスニペットでは、トークンの代わりにservice_account_json=service_account_cred_json_strを使用し、サービスアカウントの認証情報を提供してください。

    apihub_resource_nameについては、APIに使用されているOpenAPI仕様の特定のIDがわかっている場合は、 `projects/my-project-id/locations/us-west1/apis/my-api-id/versions/version-id/specs/spec-id` を使用します。 ツールセットがAPIから利用可能な最初の仕様を自動的に取得するようにしたい場合は、 `projects/my-project-id/locations/us-west1/apis/my-api-id` を使用します。

  4. エージェントファイルAgent.pyを作成し、作成したツールをエージェントの定義に追加します。

    from google.adk.agents.llm_agent import LlmAgent
    from .tools import sample_toolset
    
    root_agent = LlmAgent(
        model='gemini-2.0-flash',
        name='enterprise_assistant',
        instruction='ユーザーを助け、アクセスできるツールを活用してください',
        tools=sample_toolset.get_tools(),
    )
    
  5. __init__.pyを設定してエージェントを公開します。

    from . import agent
    
  6. Google ADK Web UIを起動し、エージェントを試します。

    # `adk web` は project_root_folder から実行してください
    adk web
    

次に、http://localhost:8000にアクセスして、Web UIからエージェントを試します。


Application Integration ツール

ADKでサポートPython v0.1.0Java v0.3.0

ApplicationIntegrationToolsetを使用すると、Integration Connectorの100以上の事前構築済みコネクタ(Salesforce, ServiceNow, JIRA, SAPなど)を使用して、エージェントをエンタープライズアプリケーションにシームレスかつ安全に、管理された方法で接続できます。

オンプレミスとSaaSの両方のアプリケーションをサポートしています。さらに、Application IntegrationのワークフローをADKエージェントにツールとして提供することで、既存のApplication Integrationプロセス自動化をエージェントワークフローに変換できます。

Application Integration内のフェデレーション検索を使用すると、ADKエージェントを使用して複数のエンタープライズアプリケーションとデータソースを同時にクエリできます。

このビデオウォークスルーでApplication IntegrationのADKフェデレーション検索がどのように機能するかをご覧ください

前提条件

1. ADKのインストール

インストールガイドの手順に従って、Agent Development Kitをインストールします。

2. CLIのインストール

Google Cloud CLIをインストールします。 デフォルトの認証情報でツールを使用するには、次のコマンドを実行します。

gcloud config set project <project-id>
gcloud auth application-default login
gcloud auth application-default set-quota-project <project-id>

<project-id>をGoogle Cloudプロジェクトの一意のIDに置き換えます。

3. Application Integrationワークフローのプロビジョニングと接続ツールの公開

エージェントで使用する既存の Application Integration ワークフローまたは Integrations Connector 接続を使用します。新しい Application Integrationワークフロー または 接続を作成することもできます。

テンプレートライブラリから 接続ツールを インポートして公開します。

:Integration Connectorsからコネクタを使用するには、接続と同じリージョンでApplication Integrationをプロビジョニングする必要があります。

4. プロジェクト構造の作成

プロジェクト構造を設定し、必要なファイルを作成します。

project_root_folder
├── .env
└── my_agent
    ├── __init__.py
    ├── agent.py
    └── tools.py

エージェントを実行するときは、project_root_folderからadk webを実行してください。

プロジェクト構造を設定し、必要なファイルを作成します。

  project_root_folder
  └── my_agent
      ├── agent.java
      └── pom.xml

エージェントを実行するときは、project_root_folderからコマンドを実行してください。

5. ロールと権限の設定

ApplicationIntegrationToolsetを設定するために必要な権限を取得するには、 プロジェクトに次のIAMロールが必要です(Integration ConnectorsとApplication Integration Workflowsの両方に共通)。

- roles/integrations.integrationEditor
- roles/connectors.invoker
- roles/secretmanager.secretAccessor

注:デプロイにAgent Engine(AE)を使用する場合、403エラーが発生する可能性があるため、 roles/integrations.integrationInvokerを使用しないでください。代わりに roles/integrations.integrationEditorを使用してください。

Integration Connectorsの使用

Integration Connectorsを使用して、エージェントをエンタープライズアプリケーションに接続します。

開始する前に

注: ExecuteConnection統合は、通常、特定のリージョンでApplication Integrationをプロビ저닝할 때 자동으로 생성됩니다。 ExecuteConnection통합 목록에 없는 경우、다음 단계에 따라 생성해야 합니다。

  1. Integration Connectorsからコネクタを使用するには、「QUICK SETUP」ボタンをクリックして、接続と同じリージョンでApplication Integrationをプロビジョニングする必要があります。

Google Cloud ツール

  1. テンプレートライブラリからConnection Toolテンプレートに移動し、「USE TEMPLATE」ボタンをクリックします。

    Google Cloud ツール

  2. Integration NameにExecuteConnectionと入力し(この統合名のみを使用することが必須です)、接続リージョンと同じリージョンを選択します。「CREATE」をクリックします。

  3. Application Integrationエディタの「PUBLISH」ボタンを使用して統合を公開します。

    Google Cloud ツール

Application Integration Toolsetの作成

Integration Connectors用のApplication Integration Toolsetを作成するには、次の手順に従います。

  1. tools.pyファイル内にApplicationIntegrationToolsetを使用してツールを作成します。

    from google.adk.tools.application_integration_tool.application_integration_toolset import ApplicationIntegrationToolset
    
    connector_tool = ApplicationIntegrationToolset(
        project="test-project", # TODO: 接続のGCPプロジェクトに置き換えてください
        location="us-central1", #TODO: 接続のロケーションに置き換えてください
        connection="test-connection", #TODO: 接続名に置き換えてください
        entity_operations={"Entity_One": ["LIST","CREATE"], "Entity_Two": []},#空のリストはエンティティ上のすべての操作がサポートされていることを意味します。
        actions=["action1"], #TODO: アクションに置き換えてください
        service_account_json='{...}', # 任意。サービスアカウントキーの文字列化されたJSON
        tool_name_prefix="tool_prefix2",
        tool_instructions="..."
    )
    

    注:

    ApplicationIntegrationToolsetは、Integration Connectors向けの動的OAuth2認証のためにauth_schemeauth_credentialの提供もサポートするようになりました。これを使用するには、tools.pyファイル内に次のようなツールを作成します。

    from google.adk.tools.application_integration_tool.application_integration_toolset import ApplicationIntegrationToolset
    from google.adk.tools.openapi_tool.auth.auth_helpers import dict_to_auth_scheme
    from google.adk.auth import AuthCredential
    from google.adk.auth import AuthCredentialTypes
    from google.adk.auth import OAuth2Auth
    
    oauth2_data_google_cloud = {
      "type": "oauth2",
      "flows": {
          "authorizationCode": {
              "authorizationUrl": "https://accounts.google.com/o/oauth2/auth",
              "tokenUrl": "https://oauth2.googleapis.com/token",
              "scopes": {
                  "https://www.googleapis.com/auth/cloud-platform": (
                      "Google Cloud Platformサービス全体のデータを表示および管理します"
                  ),
                  "https://www.googleapis.com/auth/calendar.readonly": "カレンダーを表示します"
              },
          }
      },
    }
    
    oauth_scheme = dict_to_auth_scheme(oauth2_data_google_cloud)
    
    auth_credential = AuthCredential(
      auth_type=AuthCredentialTypes.OAUTH2,
      oauth2=OAuth2Auth(
          client_id="...", #TODO: client_idに置き換えてください
          client_secret="...", #TODO: client_secretに置き換えてください
      ),
    )
    
    connector_tool = ApplicationIntegrationToolset(
        project="test-project", # TODO: 接続のGCPプロジェクトに置き換えてください
        location="us-central1", #TODO: 接続のロケーションに置き換えてください
        connection="test-connection", #TODO: 接続名に置き換えてください
        entity_operations={"Entity_One": ["LIST","CREATE"], "Entity_Two": []},#空のリストはエンティティ上のすべての操作がサポートされていることを意味します。
        actions=["GET_calendars/%7BcalendarId%7D/events"], #TODO: アクションに置き換えてください。これはイベントを一覧表示するためのものです。
        service_account_json='{...}', # 任意。サービスアカウントキーの文字列化されたJSON
        tool_name_prefix="tool_prefix2",
        tool_instructions="...",
        auth_scheme=oauth_scheme,
        auth_credential=auth_credential
    )
    
  2. agent.pyファイルを更新し、ツールをエージェントに追加します。

    from google.adk.agents.llm_agent import LlmAgent
    from .tools import connector_tool
    
    root_agent = LlmAgent(
        model='gemini-2.0-flash',
        name='connector_agent',
        instruction="ユーザーを助け、アクセスできるツールを活用してください",
        tools=[connector_tool],
    )
    
  3. __init__.pyを設定してエージェントを公開します。

    from . import agent
    
  4. Google ADK Web UIを起動し、エージェントを使用します。

    # `adk web` は project_root_folder から実行してください
    adk web
    

上記の手順を完了したら、http://localhost:8000にアクセスし、 my_agentエージェント(エージェントフォルダ名と同じ)を選択します。

App Integrationワークフローの使用

既存の Application Integration ワークフローをエージェントのツールとして使用するか、新しいものを作成します。

1. ツールの作成

tools.pyファイル内にApplicationIntegrationToolsetを使用してツールを作成するには、次のコードを使用します。

    integration_tool = ApplicationIntegrationToolset(
        project="test-project", # TODO: 接続のGCPプロジェクトに置き換えてください
        location="us-central1", #TODO: 接続のロケーションに置き換えてください
        integration="test-integration", #TODO: 統合名に置き換えてください
        triggers=["api_trigger/test_trigger"],#TODO: トリガーIDに置き換えてください。空のリストは統合内のすべてのAPIトリガーが考慮されることを意味します。
        service_account_json='{...}', #任意。サービスアカウントキーの文字列化されたJSON
        tool_name_prefix="tool_prefix1",
        tool_instructions="..."
    )

注:デフォルトの認証情報の代わりに使用するサービスアカウントを提供できます。これを行うには、サービスアカウントキーを生成し、サービスアカウントに適切なApplication IntegrationおよびIntegration Connector IAMロールを付与します。IAMロールの詳細については、前提条件セクションを参照してください。

tools.javaファイル内にApplicationIntegrationToolsetを使用してツールを作成するには、次のコードを使用します。

    import com.google.adk.tools.applicationintegrationtoolset.ApplicationIntegrationToolset;
    import com.google.common.collect.ImmutableList;
    import com.google.common.collect.ImmutableMap;

    public class Tools {
        private static ApplicationIntegrationToolset integrationTool;
        private static ApplicationIntegrationToolset connectionsTool;

        static {
            integrationTool = new ApplicationIntegrationToolset(
                    "test-project",
                    "us-central1",
                    "test-integration",
                    ImmutableList.of("api_trigger/test-api"),
                    null,
                    null,
                    null,
                    "{...}",
                    "tool_prefix1",
                    "...");

            connectionsTool = new ApplicationIntegrationToolset(
                    "test-project",
                    "us-central1",
                    null,
                    null,
                    "test-connection",
                    ImmutableMap.of("Issue", ImmutableList.of("GET")),
                    ImmutableList.of("ExecuteCustomQuery"),
                    "{...}",
                    "tool_prefix",
                    "...");
        }
    }

注:デフォルトの認証情報の代わりに使用するサービスアカウントを提供できます。これを行うには、サービスアカウントキーを生成し、サービスアカウントに適切なApplication IntegrationおよびIntegration Connector IAMロールを付与します。IAMロールの詳細については、前提条件セクションを参照してください。

2. ツールをエージェントに追加する

agent.pyファイルを更新し、ツールをエージェントに追加するには、次のコードを使用します。

    from google.adk.agents.llm_agent import LlmAgent
    from .tools import integration_tool, connector_tool

    root_agent = LlmAgent(
        model='gemini-2.0-flash',
        name='integration_agent',
        instruction="ユーザーを助け、アクセスできるツールを活用してください",
        tools=[integration_tool],
    )

agent.javaファイルを更新し、ツールをエージェントに追加するには、次のコードを使用します。

```java import com.google.adk.agent.LlmAgent; import com.google.adk.tools.BaseTool; import com.google.common.collect.ImmutableList;

    public class MyAgent {
        public static void main(String[] args) {
            // 前のステップで定義されていると仮定したToolsクラス
            ImmutableList<BaseTool> tools = ImmutableList.<BaseTool>builder()
                    .add(Tools.integrationTool)
                    .add(Tools.connectionsTool)
                    .build();

            // 最後に、自動的に生成されたツールでエージェントを作成します。
            LlmAgent rootAgent = LlmAgent.builder()
                    .name("science-teacher")
                    .description("科学教師エージェント")
                    .model("gemini-2.0-flash")
                    .instruction(
                            "ユーザーを助け、アクセスできるツールを活用してください。"
                    )
                    .tools(tools)
                    .build();

            // これでrootAgentを使用してLLMと対話できます
            // たとえば、エージェントとの会話を開始できます。
        }
    }
```

注:接続でサポートされているエンティティとアクションのリストを見つけるには、 これらのコネクタAPIを使用します:listActionslistEntityTypes

3. エージェントを公開する

__init__.pyを設定してエージェントを公開するには、次のコードを使用します。

    from . import agent

4. エージェントを使用する

Google ADK Web UIを起動し、エージェントを使用するには、次のコマンドを使用します。

    # `adk web` は project_root_folder から実行してください
    adk web
上記の手順を完了したら、http://localhost:8000にアクセスし、my_agentエージェント(エージェントフォルダ名と同じ)を選択します。

Google ADK Web UIを起動し、エージェントを使用するには、次のコマンドを使用します。

    mvn install

    mvn exec:java \
        -Dexec.mainClass="com.google.adk.web.AdkWebServer" \
        -Dexec.args="--adk.agents.source-dir=src/main/java" \
        -Dexec.classpathScope="compile"

上記の手順を完了したら、http://localhost:8000にアクセスし、my_agentエージェント(エージェントフォルダ名と同じ)を選択します。