📖

【Microsoft Agent Framework】- hosted agentをデプロイしてみる

に公開

https://zenn.dev/headwaters/articles/e81d19893d220d

の続き

執筆日

2026/5/10

前提

hosted agent のデプロイ方法は、2つ。

  1. azd コマンド
  2. VSCode の拡張機能

今回は、azd コマンドでデプロイを実施します。

始める前に

  • azd コマンドのインストールが必要です。

  • Microsoft Foundry ProjectAzure Container RegistryApplication Insights の3つのリソースをデプロイしておく。(同一サブスクリプションで)

  • Microsoft Foundry Project でモデルのデプロイをする。

  • Microsoft Foundry Project には、ACR Pull の権限が必要です。

  • 共同作成者ロール所有者 or ユーザー アクセス管理者

サンプルをデプロイしてみる

  1. 以下のコマンドで Azure テナントにログインする
az login -t <tenant id>
  1. 以下のコマンドを実行してプロジェクトを作成する
# 1. Azure Developer CLI の ai agent 拡張機能をインストールします。
azd ext install azure.ai.agents
# 2. 拡張機能がインストールされていることを確認するには、次を実行します。
azd ext list
# 3. 空のディレクトリで新しいホステッドエージェントプロジェクトを初期化します。
azd ai agent init
  1. ローカルでテストする
# エージェントをローカル起動。localhost:8088 で起動
azd ai agent run
# 別ターミナルでテスト
azd ai agent invoke --local "テストメッセージ"


4. Azure へデプロイする

azd deploy
  1. 以下のコマンドを実行して、Status が Active になっていることを確認する

  2. Microsoft Foundry Project を開き、デプロイされていることを確認する

  3. 動作確認
    チャットしてみる

    トレースを見ると
    ※セッションの有効期限もある。

    モニター
    ※メトリックを確認できる

    評価
    Evaluation を作成すると評価も行える

hosted agent をコードから実行する方法

main.py
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import VersionRefIndicator

endpoint = <"project endpoint">
agent_name = <"Agent Name">
agent_version = "1" # バージョンを指定できる
isolation_key = <"isolation key name。任意の名前を指定">

print(f"Agent: {agent_name}, version: {agent_version}")

with (
    DefaultAzureCredential() as credential,
    AIProjectClient(
        endpoint=endpoint,
        credential=credential,
        allow_preview=True,
    ) as project_client,
):
    # Create a session for conversation state (preview feature)
    session = project_client.beta.agents.create_session(
        agent_name=agent_name,
        isolation_key=isolation_key,
        version_indicator=VersionRefIndicator(agent_version=agent_version),
    )
    print(f"Created session: {session.agent_session_id}")

    try:
        # Create an OpenAI client bound to the agent endpoint
        openai_client = project_client.get_openai_client(agent_name=agent_name)

        # Call Responses API with the session for state continuity
        response = openai_client.responses.create(
            input="Hello, what can you help me with?",
            extra_body={
                "agent_session_id": session.agent_session_id,
            },
        )
        print(f"Response: {response.output_text}")
    finally:
        # Clean up the session when done
        project_client.beta.agents.delete_session(
            agent_name=agent_name,
            session_id=session.agent_session_id,
            isolation_key=isolation_key,
        )

バージョンをアップデートしてみる

instructions を変更して再度デプロイする。

main.py
# Copyright (c) Microsoft. All rights reserved.

import os

from agent_framework import Agent
from agent_framework.foundry import FoundryChatClient
from agent_framework_foundry_hosting import ResponsesHostServer
from azure.identity import DefaultAzureCredential
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()


def main():
    client = FoundryChatClient(
        project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
        model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
        credential=DefaultAzureCredential(),
    )

    agent = Agent(
        client=client,
        instructions="あなたはモノボケをするのが好きなAIアシスタントです。ユーザーからのお題に対して、面白いモノボケを考えてください。",
        # History will be managed by the hosting infrastructure, thus there
        # is no need to store history by the service. Learn more at:
        # https://developers.openai.com/api/reference/resources/responses/methods/create
        default_options={"store": False},
    )

    server = ResponsesHostServer(agent)
    server.run()

if __name__ == "__main__":
    main()

バージョンが 2 になっていることを確認する

バージョンの管理も可能。
```bash:出力結果
Response: こんにちは!僕はモノボケAIです。例えば、身の回りのものを使って面白いボケを作るのが得意です。何かお題を教えてもらえたら、それにぴったりのモノボケを考えますよ!何かありますか?

ツールをエージェントにアタッチすることも可能

https://zenn.dev/headwaters/articles/f08b68bceb4f0d#2.-エージェントに-toolboxes-をアタッチする

ヘッドウォータース

Discussion