Skip to content

Commit 01c8cc7

Browse files
authored
Merge pull request #6061 from aahill/july-fixes
adding mcp python sample + other fixes
2 parents 0621c1f + f0f5212 commit 01c8cc7

File tree

6 files changed

+154
-7
lines changed

6 files changed

+154
-7
lines changed

articles/ai-foundry/agents/how-to/tools/logic-apps.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services: cognitive-services
66
manager: nitinme
77
ms.service: azure-ai-agent-service
88
ms.topic: how-to
9-
ms.date: 07/11/2025
9+
ms.date: 07/16/2025
1010
author: aahill
1111
ms.author: aahi
1212
ms.reviewer: umangsehgal
@@ -74,7 +74,7 @@ project_client = AIProjectClient(
7474

7575
## Register the Logic App
7676

77-
Register the Logic App by providing its name and trigger details. You can find code for `AzureLogicAppTool` on[GitHub](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/utils/user_functions.py).
77+
Register the Logic App by providing its name and trigger details. You can find code for `AzureLogicAppTool` on [GitHub](https://github.com/azure-ai-foundry/foundry-samples/blob/main/samples/microsoft/python/getting-started-agents/logic_apps/user_logic_apps.py).
7878

7979
```python
8080
from user_logic_apps import AzureLogicAppTool

articles/ai-foundry/agents/how-to/tools/model-context-protocol-samples.md

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services: cognitive-services
66
manager: nitinme
77
ms.service: azure-ai-agent-service
88
ms.topic: how-to
9-
ms.date: 06/26/2025
9+
ms.date: 07/14/2025
1010
author: aahill
1111
ms.author: aahi
1212
zone_pivot_groups: selection-mcp-code
@@ -17,6 +17,141 @@ ms.custom: azure-ai-agents-code
1717

1818
Use this article to find step-by-step instructions and code samples for connecting Foundry Agent service with MCP.
1919

20+
:::zone pivot="python"
21+
22+
## Initialization
23+
The code begins by setting up the necessary imports, getting the relevant MCP server configuration, and initializing the AI Project client:
24+
25+
```python
26+
# Import necessary libraries
27+
import os, time
28+
from azure.ai.projects import AIProjectClient
29+
from azure.identity import DefaultAzureCredential
30+
from azure.ai.agents.models import McpTool, RequiredMcpToolCall, SubmitToolApprovalAction, ToolApproval
31+
32+
# Get MCP server configuration from environment variables
33+
mcp_server_url = os.environ.get("MCP_SERVER_URL", "https://gitmcp.io/Azure/azure-rest-api-specs")
34+
mcp_server_label = os.environ.get("MCP_SERVER_LABEL", "github")
35+
36+
project_client = AIProjectClient(
37+
endpoint=os.environ["PROJECT_ENDPOINT"],
38+
credential=DefaultAzureCredential(),
39+
)
40+
```
41+
42+
## Tool setup
43+
To add the MCP server to the agent, use the following example, which takes the MCP server label and URL from the last step. You can also add or remove allowed tools dynamically through the `allow_tool` parameter.
44+
45+
```python
46+
mcp_tool = McpTool(
47+
server_label=mcp_server_label,
48+
server_url=mcp_server_url,
49+
allowed_tools=[], # Optional: specify allowed tools
50+
)
51+
52+
# You can also add or remove allowed tools dynamically
53+
search_api_code = "search_azure_rest_api_code"
54+
mcp_tool.allow_tool(search_api_code)
55+
print(f"Allowed tools: {mcp_tool.allowed_tools}")
56+
```
57+
58+
## Agent creation
59+
An agent is created using the `project_client.agents.create_agent` method.
60+
61+
```python
62+
# Create a new agent.
63+
# NOTE: To reuse existing agent, fetch it with get_agent(agent_id)
64+
with project_client:
65+
agents_client = project_client.agents
66+
67+
# Create a new agent.
68+
# NOTE: To reuse existing agent, fetch it with get_agent(agent_id)
69+
agent = agents_client.create_agent(
70+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
71+
name="my-mcp-agent",
72+
instructions="You are a helpful agent that can use MCP tools to assist users. Use the available MCP tools to answer questions and perform tasks.",
73+
tools=mcp_tool.definitions,
74+
)
75+
```
76+
77+
## Thread management
78+
Create the thread and add the initial user message.
79+
80+
```python
81+
# Create thread for communication
82+
thread = agents_client.threads.create()
83+
print(f"Created thread, ID: {thread.id}")
84+
85+
# Create message to thread
86+
message = agents_client.messages.create(
87+
thread_id=thread.id,
88+
role="user",
89+
content="Please summarize the Azure REST API specifications Readme",
90+
)
91+
print(f"Created message, ID: {message.id}")
92+
```
93+
94+
## Handle tool approvals and create a run
95+
96+
Set the MCP server update headers and optionally disable tool approval requirements.
97+
98+
```python
99+
mcp_tool.update_headers("SuperSecret", "123456")
100+
# mcp_tool.set_approval_mode("never") # Uncomment to disable approval requirement
101+
run = agents_client.runs.create(thread_id=thread.id, agent_id=agent.id, tool_resources=mcp_tool.resources)
102+
print(f"Created run, ID: {run.id}")
103+
```
104+
105+
106+
## Create a run and check the output
107+
Create the run, check the output, and examine what tools were called during the run.
108+
109+
```python
110+
# Create and automatically process the run, handling tool calls internally
111+
run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
112+
print(f"Run finished with status: {run.status}")
113+
114+
if run.status == "failed":
115+
print(f"Run failed: {run.last_error}")
116+
117+
# Retrieve the steps taken during the run for analysis
118+
run_steps = project_client.agents.run_steps.list(thread_id=thread.id, run_id=run.id)
119+
120+
# Loop through each step to display information
121+
for step in run_steps:
122+
print(f"Step {step['id']} status: {step['status']}")
123+
124+
tool_calls = step.get("step_details", {}).get("tool_calls", [])
125+
for call in tool_calls:
126+
print(f" Tool Call ID: {call.get('id')}")
127+
print(f" Type: {call.get('type')}")
128+
function_details = call.get("function", {})
129+
if function_details:
130+
print(f" Function name: {function_details.get('name')}")
131+
print(f" function output: {function_details.get('output')}")
132+
133+
print()
134+
```
135+
136+
137+
## Cleanup
138+
After the interaction is complete, the script performs cleanup by deleting the created agent resource using `agents_client.delete_agent()` to avoid leaving unused resources. It also fetches and prints the entire message history from the thread using `agents_client.list_messages()` for review or logging.
139+
140+
```python
141+
# Delete the agent resource to clean up
142+
project_client.agents.delete_agent(agent.id)
143+
print("Deleted agent")
144+
145+
# Fetch and log all messages exchanged during the conversation thread
146+
messages = project_client.agents.messages.list(thread_id=thread.id)
147+
for msg in messages:
148+
print(f"Message ID: {msg.id}, Role: {msg.role}, Content: {msg.content}")
149+
```
150+
151+
:::zone-end
152+
153+
:::zone pivot="rest"
154+
20155
Follow the [REST API Quickstart](../../quickstart.md?pivots=rest-api#api-call-information) to set the right values for the environment variables `AGENT_TOKEN`, `AZURE_AI_FOUNDRY_PROJECT_ENDPOINT`, and `API_VERSION`.
21156

22157

@@ -168,3 +303,6 @@ curl --request GET \
168303
--url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads/thread_abc123/messages?api-version=$API_VERSION \
169304
-H "Authorization: Bearer $AGENT_TOKEN"
170305
```
306+
307+
:::zone-end
308+

articles/ai-foundry/agents/how-to/tools/model-context-protocol.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services: cognitive-services
66
manager: nitinme
77
ms.service: azure-ai-agent-service
88
ms.topic: how-to
9-
ms.date: 06/17/2025
9+
ms.date: 07/16/2025
1010
author: aahill
1111
ms.author: aahi
1212
ms.custom: references_regions
@@ -35,7 +35,7 @@ You can bring multiple remote MCP servers to Foundry Agent service by adding the
3535

3636
|Azure AI foundry support | Python SDK | C# SDK | JavaScript SDK | REST API |Basic agent setup | Standard agent setup |
3737
|:---------:|:---------:|:---------:|:---------:|:---------:|:---------:|---------:|
38-
| - | - | - | - | ✔️ | ✔️ | ✔️ |
38+
| - | ✔️ | - | - | ✔️ | ✔️ | ✔️ |
3939

4040
## Setup
4141
1. Create an Azure AI Foundry Agent by following the steps in the [quickstart](../../quickstart.md).

articles/ai-foundry/agents/includes/quickstart-typescript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: aahill
44
ms.author: aahi
55
ms.service: azure-ai-agent-service
66
ms.topic: include
7-
ms.date: 03/28/2025
7+
ms.date: 07/16/2025
88
ms.custom: devx-track-ts
99
---
1010

articles/ai-foundry/openai/concepts/use-your-data.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.service: azure-ai-openai
77
ms.topic: quickstart
88
author: aahill
99
ms.author: aahi
10-
ms.date: 06/30/2025
10+
ms.date: 07/16/2025
1111
recommendations: false
1212
ms.custom: references_regions, ignite-2024
1313
---
@@ -722,6 +722,7 @@ Each user message can translate to multiple search queries, all of which get sen
722722
> * o1 models
723723
> * o3 models
724724
> * model-router
725+
> * GPT 4.1 models
725726
726727
| Region | `gpt-35-turbo-16k (0613)` | `gpt-35-turbo (1106)` | `gpt-4-32k (0613)` | `gpt-4 (1106-preview)` | `gpt-4 (0125-preview)` | `gpt-4 (0613)` | `gpt-4o`\*\* | `gpt-4 (turbo-2024-04-09)` |
727728
|------|---|---|---|---|---|----|----|----|

zone-pivots/zone-pivot-groups.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,14 @@ groups:
11291129
title: Python
11301130
- id: rest
11311131
title: REST API
1132+
- id: selection-mcp-code
1133+
title: Selections
1134+
prompt: What would you like to see?
1135+
pivots:
1136+
- id: python
1137+
title: Python
1138+
- id: rest
1139+
title: REST API
11321140
- id: selection-agent-sharepoint
11331141
title: Selections
11341142
prompt: What would you like to see?

0 commit comments

Comments
 (0)