5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GPT 5.6 on Bedrockでサーバーサイドツールを使う with AgentCoreゲートウェイ

5
Posted at

タイトルつけるの難しい。。

BedrockでOpenAIのResponses APIを使う場合に、サーバーサイドツールが使用できます。

クライアントサイドでのツール実行とサーバーサイドでのツール実行の違いはこんな感じ。

アプリの実装が超簡単になります!もうエージェントフレームワークいらないんじゃないかと思うぐらい!

Responses APIで使えるサーバーサイドツール

  • Lambda関数
  • Noteツール(AWS提供)
  • Bedrock AgentCoreゲートウェイのMCPツール

やってみよう

今回はBedrock AgentCoreゲートウェイを指定してみます。GPT5.6 Lunaを使いたかったのでバージニア北部で試しました。

  1. AgentCoreゲートウェイのターゲットとして使用するナレッジベースを作成します

  2. ナレッジベースができたら、AgentCoreゲートウェイを作成します

    InboundAuth typeはIAMを選択します

    image.png

    作成したら、Gateway resource ARNをコピーします

    image.png

  3. Pythonのスクリプトを作成します。

    uv add openai aws-bedrock-token-generator
    
    from aws_bedrock_token_generator import provide_token
    from openai import BedrockOpenAI
    
    client = BedrockOpenAI(
        aws_region="us-east-1",
        bedrock_token_provider=lambda: provide_token(region="us-east-1")
    )
    
    response = client.responses.create(
        model="openai.gpt-5.6-luna",
        tools=[
            {
                "type": "mcp",
                "server_label": "agentcore_tools",
                "connector_id": "{AgentCoreゲートウェイのARN}",
                "server_description": "AgentCore Gateway providing custom tools",
                "require_approval": "never",
            }
        ],
        input="プロンプト",
    )
    
    print(response.output_text)
    

AWSのプロファイルを指定するときは環境変数のAWS_REGIONを使うか、以下のように指定します。(面倒くさいので環境変数がおすすめ)

from botocore.session import Session

class ProfileCredentials:
    def __init__(self, profile):
        self._session = Session(profile=profile)
    def load(self):
        return self._session.get_credentials()

bedrock_token_provider=lambda: provide_token(
    region="us-east-1",
    aws_credentials_provider=ProfileCredentials("dev"),
)
5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?