Skip to content

Commit dd10bfb

Browse files
authored
Howie/auto func sample 2 (#41901)
* add auto function call sample (#41885) * update * Update sample_agents_auto_function_call.py * added async sample and use project client * update * fix spelling * update change log * Update CHANGELOG.md * update version * update * update version * Fix OpenApi Tool * resolved comment * Update * fix code to pass test * update * update * update * update * update
1 parent fd786c2 commit dd10bfb

14 files changed

+333
-74
lines changed

sdk/ai/azure-ai-agents/CHANGELOG.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
## 1.1.0b4 (Unreleased)
66

7-
### Features Added
8-
9-
- Added support for MCP tool. For more information, see https://learn.microsoft.com/azure/ai-foundry/agents/how-to/tools/model-context-protocol
10-
117
### Sample updates
128

13-
- Added sample showing usage of MCP tool.
9+
- Added a sample showing usage of MCP tool, [`sample_agents_mcp.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_mcp.py).
10+
- Added a sample showing auto function call for a synchronous client, [`sample_agents_auto_function_call.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_auto_function_call.py)
11+
- Added a sample showing auto function call for an asynchronous client, [`sample_agents_auto_function_call_async.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_async/sample_agents_auto_function_call_async.py).
12+
1413

1514
## 1.1.0b3 (2025-06-30)
1615

sdk/ai/azure-ai-agents/README.md

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -422,33 +422,29 @@ Here is an example to integrate Azure AI Search:
422422
<!-- SNIPPET:sample_agents_azure_ai_search.create_agent_with_azure_ai_search_tool -->
423423

424424
```python
425-
with AIProjectClient(
426-
endpoint=os.environ["PROJECT_ENDPOINT"],
427-
credential=DefaultAzureCredential(),
428-
) as project_client:
429-
conn_id = project_client.connections.get_default(ConnectionType.AZURE_AI_SEARCH).id
430-
431-
print(conn_id)
432-
433-
# Initialize agent AI search tool and add the search index connection id
434-
ai_search = AzureAISearchTool(
435-
index_connection_id=conn_id,
436-
index_name="sample_index",
437-
query_type=AzureAISearchQueryType.SIMPLE,
438-
top_k=3,
439-
filter="",
440-
)
425+
conn_id = project_client.connections.get_default(ConnectionType.AZURE_AI_SEARCH).id
441426

442-
# Create agent with AI search tool and process agent run
443-
agents_client = project_client.agents
427+
print(conn_id)
444428

445-
agent = agents_client.create_agent(
446-
model=os.environ["MODEL_DEPLOYMENT_NAME"],
447-
name="my-agent",
448-
instructions="You are a helpful agent",
449-
tools=ai_search.definitions,
450-
tool_resources=ai_search.resources,
451-
)
429+
# Initialize agent AI search tool and add the search index connection id
430+
ai_search = AzureAISearchTool(
431+
index_connection_id=conn_id,
432+
index_name="sample_index",
433+
query_type=AzureAISearchQueryType.SIMPLE,
434+
top_k=3,
435+
filter="",
436+
)
437+
438+
# Create agent with AI search tool and process agent run
439+
agents_client = project_client.agents
440+
441+
agent = agents_client.create_agent(
442+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
443+
name="my-agent",
444+
instructions="You are a helpful agent",
445+
tools=ai_search.definitions,
446+
tool_resources=ai_search.resources,
447+
)
452448
```
453449

454450
<!-- END SNIPPET -->
@@ -531,8 +527,12 @@ agent = await agents_client.create_agent(
531527

532528
<!-- END SNIPPET -->
533529

534-
Notice that if `enable_auto_function_calls` is called, the SDK will invoke the functions automatically during `create_and_process` or streaming. If you prefer to execute them manually, refer to [`sample_agents_stream_eventhandler_with_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_streaming/sample_agents_stream_eventhandler_with_functions.py) or
535-
[`sample_agents_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_functions.py)
530+
When `enable_auto_function_calls` is called, the SDK will automatically invoke functions during both `create_and_process` and streaming workflows. This simplifies agent logic by handling function execution internally. Furthermore, although function tools and definitions are preserved in Agent service, their function implements are not. Therefore, if your code queries earlier created agents through `update_agents` or `get_agents` function, you MUST also provide the function implementations through `enable_auto_function_calls` to complete auto function callings.
531+
532+
- For examples of automatic function calls in action, refer to [`sample_agents_auto_function_call.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_auto_function_call.py) or [`sample_agents_auto_function_call_async.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_async/sample_agents_auto_function_call_async.py).
533+
- If you prefer to manage function execution manually, refer to [`sample_agents_stream_eventhandler_with_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_streaming/sample_agents_stream_eventhandler_with_functions.py) or
534+
[`sample_agents_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_functions.py).
535+
536536

537537
### Create Agent With Azure Function Call
538538

@@ -696,13 +696,6 @@ Below is an example of how to create an Azure Logic App utility tool and registe
696696
<!-- SNIPPET:sample_agents_logic_apps.register_logic_app -->
697697

698698
```python
699-
700-
# Create the agents client
701-
project_client = AIProjectClient(
702-
endpoint=os.environ["PROJECT_ENDPOINT"],
703-
credential=DefaultAzureCredential(),
704-
)
705-
706699
# Extract subscription and resource group from the project scope
707700
subscription_id = os.environ["SUBSCRIPTION_ID"]
708701
resource_group = os.environ["resource_group_name"]
@@ -952,9 +945,11 @@ To understand what calls were made by the main agent to the connected ones, we w
952945
for run_step in agents_client.run_steps.list(thread_id=thread.id, run_id=run.id, order=ListSortOrder.ASCENDING):
953946
if isinstance(run_step.step_details, RunStepToolCallDetails):
954947
for tool_call in run_step.step_details.tool_calls:
955-
print(f"\tAgent: {tool_call._data['connected_agent']['name']} "
956-
f"query: {tool_call._data['connected_agent']['arguments']} ",
957-
f"output: {tool_call._data['connected_agent']['output']}")
948+
print(
949+
f"\tAgent: {tool_call._data['connected_agent']['name']} "
950+
f"query: {tool_call._data['connected_agent']['arguments']} ",
951+
f"output: {tool_call._data['connected_agent']['output']}",
952+
)
958953
```
959954

960955
<!-- END SNIPPET -->
@@ -969,7 +964,7 @@ messages = agents_client.messages.list(thread_id=thread.id, order=ListSortOrder.
969964
for msg in messages:
970965
if msg.text_messages:
971966
last_text = msg.text_messages[-1]
972-
text = last_text.text.value.replace('\u3010', '[').replace('\u3011', ']')
967+
text = last_text.text.value.replace("\u3010", "[").replace("\u3011", "]")
973968
print(f"{msg.role}: {text}")
974969
```
975970

sdk/ai/azure-ai-agents/azure/ai/agents/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
77
# --------------------------------------------------------------------------
88

9-
VERSION = "1.1.0b3"
9+
VERSION = "1.1.0b4"

sdk/ai/azure-ai-agents/azure/ai/agents/aio/operations/_patch.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,8 +1043,13 @@ async def submit_tool_outputs( # pylint: disable=arguments-differ
10431043
:type thread_id: str
10441044
:param run_id: Required.
10451045
:type run_id: str
1046+
:keyword tool_outputs: A list of tools for which the outputs are being submitted. Default value
1047+
is None.
10461048
:paramtype tool_outputs: list[~azure.ai.agents.models.ToolOutput]
1049+
:keyword tool_approvals: A list of tool approvals allowing data to be sent to tools. Default
1050+
value is None.
10471051
:paramtype tool_approvals: list[~azure.ai.agents.models.ToolApproval]
1052+
10481053
:keyword content_type: Body Parameter content-type. Content type parameter for JSON body.
10491054
Default value is "application/json".
10501055
:paramtype content_type: str
@@ -1118,8 +1123,13 @@ async def submit_tool_outputs(
11181123
:type run_id: str
11191124
:param body: Is either a JSON type or a IO[bytes] type. Required.
11201125
:type body: JSON or IO[bytes]
1126+
:keyword tool_outputs: A list of tools for which the outputs are being submitted. Default value
1127+
is None.
11211128
:paramtype tool_outputs: list[~azure.ai.agents.models.ToolOutput]
1129+
:keyword tool_approvals: A list of tool approvals allowing data to be sent to tools. Default
1130+
value is None.
11221131
:paramtype tool_approvals: list[~azure.ai.agents.models.ToolApproval]
1132+
11231133
:return: ThreadRun. The ThreadRun is compatible with MutableMapping
11241134
:rtype: ~azure.ai.agents.models.ThreadRun
11251135
:raises ~azure.core.exceptions.HttpResponseError:
@@ -1198,8 +1208,13 @@ async def submit_tool_outputs_stream(
11981208
:type thread_id: str
11991209
:param run_id: Required.
12001210
:type run_id: str
1211+
:keyword tool_outputs: A list of tools for which the outputs are being submitted. Default value
1212+
is None.
12011213
:paramtype tool_outputs: list[~azure.ai.agents.models.ToolOutput]
1214+
:keyword tool_approvals: A list of tool approvals allowing data to be sent to tools. Default
1215+
value is None.
12021216
:paramtype tool_approvals: list[~azure.ai.agents.models.ToolApproval]
1217+
12031218
:keyword content_type: Body Parameter content-type. Content type parameter for JSON body.
12041219
Default value is "application/json".
12051220
:paramtype content_type: str
@@ -1230,8 +1245,13 @@ async def submit_tool_outputs_stream( # pyright: ignore[reportInconsistentOverl
12301245
:type run_id: str
12311246
:param body: Is either a JSON type or a IO[bytes] type. Required.
12321247
:type body: JSON or IO[bytes]
1248+
:keyword tool_outputs: A list of tools for which the outputs are being submitted. Default value
1249+
is None.
12331250
:paramtype tool_outputs: list[~azure.ai.agents.models.ToolOutput]
1251+
:keyword tool_approvals: A list of tool approvals allowing data to be sent to tools. Default
1252+
value is None.
12341253
:paramtype tool_approvals: list[~azure.ai.agents.models.ToolApproval]
1254+
12351255
:keyword event_handler: The event handler to use for processing events during the run.
12361256
:paramtype event_handler: ~azure.ai.agents.models.AsyncAgentEventHandler
12371257
:raises ~azure.core.exceptions.HttpResponseError:

sdk/ai/azure-ai-agents/azure/ai/agents/models/_patch.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,13 @@ def resources(self) -> ToolResources:
790790
"""
791791
return ToolResources()
792792

793+
def execute(self, tool_call: Any) -> None:
794+
"""
795+
OpenApiTool does not execute client-side.
796+
:param Any tool_call: The tool call to execute.
797+
:type tool_call: Any
798+
"""
799+
793800

794801
class McpTool(Tool[MCPToolDefinition]):
795802
"""
@@ -901,26 +908,46 @@ def update_headers(self, key: str, value: str) -> None:
901908
server_label=self._server_label, headers=self._headers, require_approval=self._require_approval
902909
)
903910
else:
904-
raise ValueError(f"Header key cannot be empty.")
911+
raise ValueError("Header key cannot be empty.")
905912

906913
@property
907914
def server_label(self) -> str:
908-
"""Get the server label."""
915+
"""
916+
Get the server label for the MCP tool.
917+
918+
:return: The label identifying the MCP server.
919+
:rtype: str
920+
"""
909921
return self._server_label
910922

911923
@property
912924
def server_url(self) -> str:
913-
"""Get the server URL."""
925+
"""
926+
Get the server URL for the MCP tool.
927+
928+
:return: The endpoint URL for the MCP server.
929+
:rtype: str
930+
"""
914931
return self._server_url
915932

916933
@property
917934
def allowed_tools(self) -> List[str]:
918-
"""Get the list of allowed tools."""
935+
"""
936+
Get the list of allowed tools for the MCP server.
937+
938+
:return: A copy of the list of tool names that are allowed to be executed on this MCP server.
939+
:rtype: List[str]
940+
"""
919941
return self._allowed_tools.copy()
920942

921943
@property
922944
def headers(self) -> Dict[str, str]:
923-
"""Get the headers for the MCP tool."""
945+
"""
946+
Get the headers for the MCP tool.
947+
948+
:return: Dictionary of HTTP headers to be sent with MCP server requests.
949+
:rtype: Dict[str, str]
950+
"""
924951
return self._resource.headers
925952

926953
@property

sdk/ai/azure-ai-agents/azure/ai/agents/operations/_patch.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,11 @@ def submit_tool_outputs( # pylint: disable=arguments-differ
10651065
:type thread_id: str
10661066
:param run_id: Required.
10671067
:type run_id: str
1068+
:keyword tool_outputs: A list of tools for which the outputs are being submitted. Default value
1069+
is None.
10681070
:paramtype tool_outputs: list[~azure.ai.agents.models.ToolOutput]
1071+
:keyword tool_approvals: A list of tool approvals allowing data to be sent to tools. Default
1072+
value is None.
10691073
:paramtype tool_approvals: list[~azure.ai.agents.models.ToolApproval]
10701074
:keyword content_type: Body Parameter content-type. Content type parameter for JSON body.
10711075
Default value is "application/json".
@@ -1143,7 +1147,11 @@ def submit_tool_outputs(
11431147
:type run_id: str
11441148
:param body: Is either a JSON type or a IO[bytes] type. Required.
11451149
:type body: JSON or IO[bytes]
1150+
:keyword tool_outputs: A list of tools for which the outputs are being submitted. Default value
1151+
is None.
11461152
:paramtype tool_outputs: list[~azure.ai.agents.models.ToolOutput]
1153+
:keyword tool_approvals: A list of tool approvals allowing data to be sent to tools. Default
1154+
value is None.
11471155
:paramtype tool_approvals: list[~azure.ai.agents.models.ToolApproval]
11481156
:return: ThreadRun. The ThreadRun is compatible with MutableMapping
11491157
:rtype: ~azure.ai.agents.models.ThreadRun
@@ -1232,7 +1240,11 @@ def submit_tool_outputs_stream(
12321240
:type thread_id: str
12331241
:param run_id: Required.
12341242
:type run_id: str
1243+
:keyword tool_outputs: A list of tools for which the outputs are being submitted. Default value
1244+
is None.
12351245
:paramtype tool_outputs: list[~azure.ai.agents.models.ToolOutput]
1246+
:keyword tool_approvals: A list of tool approvals allowing data to be sent to tools. Default
1247+
value is None.
12361248
:paramtype tool_approvals: list[~azure.ai.agents.models.ToolApproval]
12371249
:keyword content_type: Body Parameter content-type. Content type parameter for JSON body.
12381250
Default value is "application/json".
@@ -1249,8 +1261,8 @@ def submit_tool_outputs_stream( # pyright: ignore[reportInconsistentOverload]
12491261
run_id: str,
12501262
body: Union[JSON, IO[bytes]] = _Unset,
12511263
*,
1252-
tool_outputs: Optional[List[_models.ToolOutput]] = None,
1253-
tool_approvals: Optional[List[_models.ToolApproval]] = None,
1264+
tool_outputs: Optional[List[_models.ToolOutput]] = _Unset,
1265+
tool_approvals: Optional[List[_models.ToolApproval]] = _Unset,
12541266
event_handler: _models.BaseAgentEventHandler,
12551267
**kwargs: Any,
12561268
) -> None:
@@ -1264,7 +1276,11 @@ def submit_tool_outputs_stream( # pyright: ignore[reportInconsistentOverload]
12641276
:type run_id: str
12651277
:param body: Is either a JSON type or a IO[bytes] type. Required.
12661278
:type body: JSON or IO[bytes]
1279+
:keyword tool_outputs: A list of tools for which the outputs are being submitted. Default value
1280+
is None.
12671281
:paramtype tool_outputs: list[~azure.ai.agents.models.ToolOutput]
1282+
:keyword tool_approvals: A list of tool approvals allowing data to be sent to tools. Default
1283+
value is None.
12681284
:paramtype tool_approvals: list[~azure.ai.agents.models.ToolApproval]
12691285
:keyword event_handler: The event handler to use for processing events during the run.
12701286
:paramtype event_handler: ~azure.ai.agents.models.BaseAgentEventHandler

0 commit comments

Comments
 (0)