From 7d10a8e847a5c3593cec3d9eb55fb68736be9f7b Mon Sep 17 00:00:00 2001 From: Tao Chen Date: Thu, 30 Apr 2026 16:11:47 -0700 Subject: [PATCH] Address comments --- .../responses/01_basic/agent.yaml | 5 +- .../responses/02_tools/agent.yaml | 5 +- .../responses/03_mcp/agent.yaml | 2 + .../responses/04_foundry_toolbox/agent.yaml | 7 ++- .../responses/05_workflows/agent.yaml | 5 +- .../responses/06_files/README.md | 10 ++-- .../responses/06_files/agent.yaml | 5 +- .../responses/06_files/main.py | 50 +++++++++---------- 8 files changed, 55 insertions(+), 34 deletions(-) diff --git a/python/samples/04-hosting/foundry-hosted-agents/responses/01_basic/agent.yaml b/python/samples/04-hosting/foundry-hosted-agents/responses/01_basic/agent.yaml index eee9883579..ee9e862958 100644 --- a/python/samples/04-hosting/foundry-hosted-agents/responses/01_basic/agent.yaml +++ b/python/samples/04-hosting/foundry-hosted-agents/responses/01_basic/agent.yaml @@ -6,4 +6,7 @@ protocols: version: 1.0.0 resources: cpu: '0.25' - memory: '0.5Gi' \ No newline at end of file + memory: '0.5Gi' +environment_variables: + - name: AZURE_AI_MODEL_DEPLOYMENT_NAME + value: ${AZURE_AI_MODEL_DEPLOYMENT_NAME} \ No newline at end of file diff --git a/python/samples/04-hosting/foundry-hosted-agents/responses/02_tools/agent.yaml b/python/samples/04-hosting/foundry-hosted-agents/responses/02_tools/agent.yaml index 6cabe7b799..9a82bc7fcb 100644 --- a/python/samples/04-hosting/foundry-hosted-agents/responses/02_tools/agent.yaml +++ b/python/samples/04-hosting/foundry-hosted-agents/responses/02_tools/agent.yaml @@ -5,4 +5,7 @@ protocols: version: 1.0.0 resources: cpu: "0.25" - memory: 0.5Gi \ No newline at end of file + memory: 0.5Gi +environment_variables: + - name: AZURE_AI_MODEL_DEPLOYMENT_NAME + value: ${AZURE_AI_MODEL_DEPLOYMENT_NAME} \ No newline at end of file diff --git a/python/samples/04-hosting/foundry-hosted-agents/responses/03_mcp/agent.yaml b/python/samples/04-hosting/foundry-hosted-agents/responses/03_mcp/agent.yaml index a1edfa8c71..2dcd14778b 100644 --- a/python/samples/04-hosting/foundry-hosted-agents/responses/03_mcp/agent.yaml +++ b/python/samples/04-hosting/foundry-hosted-agents/responses/03_mcp/agent.yaml @@ -7,5 +7,7 @@ resources: cpu: "0.25" memory: 0.5Gi environment_variables: + - name: AZURE_AI_MODEL_DEPLOYMENT_NAME + value: ${AZURE_AI_MODEL_DEPLOYMENT_NAME} - name: GITHUB_PAT value: ${GITHUB_PAT} \ No newline at end of file diff --git a/python/samples/04-hosting/foundry-hosted-agents/responses/04_foundry_toolbox/agent.yaml b/python/samples/04-hosting/foundry-hosted-agents/responses/04_foundry_toolbox/agent.yaml index f4a3bd9a8c..826ab39192 100644 --- a/python/samples/04-hosting/foundry-hosted-agents/responses/04_foundry_toolbox/agent.yaml +++ b/python/samples/04-hosting/foundry-hosted-agents/responses/04_foundry_toolbox/agent.yaml @@ -5,4 +5,9 @@ protocols: version: 1.0.0 resources: cpu: "0.25" - memory: 0.5Gi \ No newline at end of file + memory: 0.5Gi +environment_variables: + - name: AZURE_AI_MODEL_DEPLOYMENT_NAME + value: ${AZURE_AI_MODEL_DEPLOYMENT_NAME} + - name: TOOLBOX_NAME + value: "agent-tools" \ No newline at end of file diff --git a/python/samples/04-hosting/foundry-hosted-agents/responses/05_workflows/agent.yaml b/python/samples/04-hosting/foundry-hosted-agents/responses/05_workflows/agent.yaml index a58893ddf8..0dd24a9995 100644 --- a/python/samples/04-hosting/foundry-hosted-agents/responses/05_workflows/agent.yaml +++ b/python/samples/04-hosting/foundry-hosted-agents/responses/05_workflows/agent.yaml @@ -5,4 +5,7 @@ protocols: version: 1.0.0 resources: cpu: "0.25" - memory: 0.5Gi \ No newline at end of file + memory: 0.5Gi +environment_variables: + - name: AZURE_AI_MODEL_DEPLOYMENT_NAME + value: ${AZURE_AI_MODEL_DEPLOYMENT_NAME} \ No newline at end of file diff --git a/python/samples/04-hosting/foundry-hosted-agents/responses/06_files/README.md b/python/samples/04-hosting/foundry-hosted-agents/responses/06_files/README.md index c813009b8d..9e10f2f9c1 100644 --- a/python/samples/04-hosting/foundry-hosted-agents/responses/06_files/README.md +++ b/python/samples/04-hosting/foundry-hosted-agents/responses/06_files/README.md @@ -16,12 +16,14 @@ The agent is hosted using the [Agent Framework](https://github.com/microsoft/age ### Tools -This agent uses two tools: +This agent uses four tools: -1. **Local Shell Tool (`run_bash`)** – Executes shell commands locally to perform filesystem operations such as listing files, reading file contents, or creating directories. The tool captures stdout, stderr, and the exit code of the command and returns them as a string. -2. **Code Interpreter Tool (`code_interpreter`)** – Allows the agent to execute Python code in a safe, sandboxed environment. This tool can be used to read and manipulate file contents, perform calculations, or run other Python logic as part of the agent's responses. +1. **Get Current Working Directory Tool (`get_cwd`)** – Returns the current working directory of the agent host process. +2. **List Files Tool (`list_files`)** – Lists the files in a specified directory. +3. **Read File Tool (`read_file`)** – Reads the contents of a specified file. +4. **Code Interpreter Tool (`code_interpreter`)** – Allows the agent to execute Python code in a safe. -> In this sample, the local shell tool is a function tool defined in Python using the `@tool` decorator from the Agent Framework. The code interpreter tool is a managed tool provided by [Foundry Toolbox](https://learn.microsoft.com/en-us/azure/foundry/agents/how-to/tools/toolbox). Learn more about foundry toolbox integration with hosted agents with this [sample](../04_foundry_toolbox/). +> In this sample, the filesystem tools are function tools defined in Python using the `@tool` decorator from the Agent Framework. The code interpreter tool is a managed tool provided by [Foundry Toolbox](https://learn.microsoft.com/en-us/azure/foundry/agents/how-to/tools/toolbox). Learn more about foundry toolbox integration with hosted agents with this [sample](../04_foundry_toolbox/). ## Running the Agent Host diff --git a/python/samples/04-hosting/foundry-hosted-agents/responses/06_files/agent.yaml b/python/samples/04-hosting/foundry-hosted-agents/responses/06_files/agent.yaml index 5c95360fa3..7b3b1f01d1 100644 --- a/python/samples/04-hosting/foundry-hosted-agents/responses/06_files/agent.yaml +++ b/python/samples/04-hosting/foundry-hosted-agents/responses/06_files/agent.yaml @@ -6,4 +6,7 @@ protocols: version: 1.0.0 resources: cpu: '0.25' - memory: '0.5Gi' \ No newline at end of file + memory: '0.5Gi' +environment_variables: + - name: AZURE_AI_MODEL_DEPLOYMENT_NAME + value: ${AZURE_AI_MODEL_DEPLOYMENT_NAME} \ No newline at end of file diff --git a/python/samples/04-hosting/foundry-hosted-agents/responses/06_files/main.py b/python/samples/04-hosting/foundry-hosted-agents/responses/06_files/main.py index f136b49ff7..2bd40055e9 100644 --- a/python/samples/04-hosting/foundry-hosted-agents/responses/06_files/main.py +++ b/python/samples/04-hosting/foundry-hosted-agents/responses/06_files/main.py @@ -2,7 +2,6 @@ import asyncio import os -import subprocess from agent_framework import Agent, tool from agent_framework.foundry import FoundryChatClient @@ -15,31 +14,32 @@ from dotenv import load_dotenv load_dotenv() -@tool( - description="Execute a shell command for filesystem operations.", - approval_mode="never_require", -) -def run_bash(command: str) -> str: - """Execute a shell command locally and return stdout, stderr, and exit code.""" +@tool(description="Get the current working directory.", approval_mode="never_require") +def get_cwd() -> str: + """Get the current working directory.""" try: - result = subprocess.run( - command, - shell=True, - capture_output=True, - text=True, - timeout=30, - ) - parts: list[str] = [] - if result.stdout: - parts.append(result.stdout) - if result.stderr: - parts.append(f"stderr: {result.stderr}") - parts.append(f"exit_code: {result.returncode}") - return "\n".join(parts) - except subprocess.TimeoutExpired: - return "Command timed out after 30 seconds" + return os.getcwd() except Exception as e: - return f"Error executing command: {e}" + return f"Error getting current working directory: {e}" + + +@tool(description="List files in a directory.", approval_mode="never_require") +def list_files(directory: str) -> list[str]: + """List files in a directory.""" + try: + return os.listdir(directory) + except Exception as e: + return [f"Error listing files in {directory}: {e}"] + + +@tool(description="Read the contents of a file.", approval_mode="never_require") +def read_file(file_path: str) -> str: + """Read the contents of a file.""" + try: + with open(file_path) as f: + return f.read() + except Exception as e: + return f"Error reading file {file_path}: {e}" async def main(): @@ -68,7 +68,7 @@ async def main(): "Make sure all mathematical calculations are performed using the code interpreter " "instead of mental arithmetic." ), - tools=[run_bash] + selected_tools, + tools=[get_cwd, list_files, read_file] + selected_tools, # 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