Skip to content

Commit fdbb05f

Browse files
authored
Merge pull request #5947 from ShawnJackson/azure-ai-foundry-agent-service-mcp-documentation
[AQ] edit pass: Azure AI Foundry Agent Service MCP documentation
2 parents 5280492 + 3d2eb4d commit fdbb05f

File tree

2 files changed

+103
-79
lines changed

2 files changed

+103
-79
lines changed

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

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: 'MCP tool code samples'
2+
title: Code Samples for the Model Context Protocol Tool (Preview)
33
titleSuffix: Azure AI Foundry
4-
description: Find code samples to connect Foundry Agent service with MCP.
4+
description: Find code samples to connect Azure AI Foundry Agent Service with MCP servers.
55
services: cognitive-services
66
manager: nitinme
77
ms.service: azure-ai-agent-service
@@ -13,13 +13,14 @@ zone_pivot_groups: selection-mcp-code
1313
ms.custom: azure-ai-agents-code
1414
---
1515

16-
# How to use the Model Context Protocol (MCP) tool
16+
# Code samples for the Model Context Protocol tool (preview)
1717

18-
Use this article to find step-by-step instructions and code samples for connecting Foundry Agent service with MCP.
18+
Use this article to find code samples for connecting Azure AI Foundry Agent Service with Model Context Protocol (MCP) servers.
1919

2020
:::zone pivot="python"
2121

22-
## Initialization
22+
## Initialize the client
23+
2324
The code begins by setting up the necessary imports, getting the relevant MCP server configuration, and initializing the AI Project client:
2425

2526
```python
@@ -29,7 +30,7 @@ from azure.ai.projects import AIProjectClient
2930
from azure.identity import DefaultAzureCredential
3031
from azure.ai.agents.models import McpTool, RequiredMcpToolCall, SubmitToolApprovalAction, ToolApproval
3132

32-
# Get MCP server configuration from environment variables
33+
# Get the MCP server configuration from environment variables
3334
mcp_server_url = os.environ.get("MCP_SERVER_URL", "https://gitmcp.io/Azure/azure-rest-api-specs")
3435
mcp_server_label = os.environ.get("MCP_SERVER_LABEL", "github")
3536

@@ -39,8 +40,9 @@ project_client = AIProjectClient(
3940
)
4041
```
4142

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.
43+
## Set up the tool
44+
45+
To add the MCP server to the agent, use the following example, which takes the MCP server label and URL from the previous step. You can also add or remove allowed tools dynamically through the `allow_tool` parameter.
4446

4547
```python
4648
mcp_tool = McpTool(
@@ -55,17 +57,18 @@ mcp_tool.allow_tool(search_api_code)
5557
print(f"Allowed tools: {mcp_tool.allowed_tools}")
5658
```
5759

58-
## Agent creation
59-
An agent is created using the `project_client.agents.create_agent` method.
60+
## Create an agent
61+
62+
You create an agent by using the `project_client.agents.create_agent` method:
6063

6164
```python
6265
# Create a new agent.
63-
# NOTE: To reuse existing agent, fetch it with get_agent(agent_id)
66+
# NOTE: To reuse an existing agent, fetch it with get_agent(agent_id)
6467
with project_client:
6568
agents_client = project_client.agents
6669

6770
# Create a new agent.
68-
# NOTE: To reuse existing agent, fetch it with get_agent(agent_id)
71+
# NOTE: To reuse an existing agent, fetch it with get_agent(agent_id)
6972
agent = agents_client.create_agent(
7073
model=os.environ["MODEL_DEPLOYMENT_NAME"],
7174
name="my-mcp-agent",
@@ -74,15 +77,16 @@ with project_client:
7477
)
7578
```
7679

77-
## Thread management
78-
Create the thread and add the initial user message.
80+
## Create a thread
81+
82+
Create the thread and add the initial user message:
7983

8084
```python
81-
# Create thread for communication
85+
# Create a thread for communication
8286
thread = agents_client.threads.create()
8387
print(f"Created thread, ID: {thread.id}")
8488

85-
# Create message to thread
89+
# Create a message for the thread
8690
message = agents_client.messages.create(
8791
thread_id=thread.id,
8892
role="user",
@@ -91,20 +95,20 @@ message = agents_client.messages.create(
9195
print(f"Created message, ID: {message.id}")
9296
```
9397

94-
## Handle tool approvals and create a run
98+
## Handle tool approvals
9599

96-
Set the MCP server update headers and optionally disable tool approval requirements.
100+
Set the MCP server update headers and optionally disable tool approval requirements:
97101

98102
```python
99103
mcp_tool.update_headers("SuperSecret", "123456")
100-
# mcp_tool.set_approval_mode("never") # Uncomment to disable approval requirement
104+
# mcp_tool.set_approval_mode("never") # Uncomment to disable approval requirements
101105
run = agents_client.runs.create(thread_id=thread.id, agent_id=agent.id, tool_resources=mcp_tool.resources)
102106
print(f"Created run, ID: {run.id}")
103107
```
104108

105-
106109
## Create a run and check the output
107-
Create the run, check the output, and examine what tools were called during the run.
110+
111+
Create the run, check the output, and examine what tools were called during the run:
108112

109113
```python
110114
# Create and automatically process the run, handling tool calls internally
@@ -133,9 +137,9 @@ Create the run, check the output, and examine what tools were called during the
133137
print()
134138
```
135139

140+
## Perform cleanup
136141

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.
142+
After the interaction is complete, the script performs cleanup by deleting the created agent resource via `agents_client.delete_agent()` to avoid leaving unused resources. It also fetches and prints the entire message history from the thread by using `agents_client.list_messages()` for review or logging.
139143

140144
```python
141145
# Delete the agent resource to clean up
@@ -152,24 +156,24 @@ After the interaction is complete, the script performs cleanup by deleting the c
152156

153157
:::zone pivot="rest"
154158

155-
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`.
159+
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`.
156160

161+
## Create an agent with the MCP tool enabled
157162

158-
## Create an Agent with the MCP tool enabled
163+
To make the MCP tool available to your agent, initialize a tool with the server endpoint, server label, and more:
159164

160-
To make the MCP tool available to your agent, initialize a tool with the server endpoint, server label and more
161165
```bash
162166
curl --request POST \
163167
--url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/assistants?api-version=$API_VERSION \
164168
-H "Authorization: Bearer $AGENT_TOKEN" \
165169
-H "Content-Type: application/json" \
166170
-d "{
167-
"instructions": "You are a customer support chatbot. Use the tools provided and your knowledge base to best respond to customer queries.",
168-
"tools": [
171+
"instructions": "You are a customer support chatbot. Use the tools provided and your knowledge base to best respond to customer queries.",
172+
"tools": [
169173
{
170174
"type": "mcp",
171175
"server_label": "<unique name for your MCP server>",
172-
"server_url": "<your MCP server url>",
176+
"server_url": "<your MCP server URL>",
173177
"allowed_tools": ["<tool_name>"], # optional
174178
}
175179
],
@@ -203,26 +207,27 @@ curl --request POST \
203207

204208
## Create a run and check the output
205209

206-
Create a run to pass headers for the tool and observe that the model uses the Grounding with Bing Search tool to provide a response to the user's question.
207-
`require_approval` parameter is optional. If not provided, `always` is the default value, meaning each time developer needs to approve before calling. Supported values:
210+
Create a run to pass headers for the tool. Observe that the model uses the Grounding with Bing Search tool to provide a response to the user's question.
211+
212+
The `require_approval` parameter is optional. Supported values are:
208213

209-
- `always` by default
210-
- `never` meaning no approval is required
211-
- `{"never":[<tool_name_1>, <tool_name_2>]}` you can also provide a list of tools without required approval
212-
- `{"always":[<tool_name_1>, <tool_name_2>]}` you can provide a list of tools with required approval
214+
- `always`: A developer needs to provide approval for every call. If you don't provide a value, this one is the default.
215+
- `never`: No approval is required.
216+
- `{"never":[<tool_name_1>, <tool_name_2>]}`: You provide a list of tools that don't require approval.
217+
- `{"always":[<tool_name_1>, <tool_name_2>]}`: You provide a list of tools that require approval.
213218

214219
```bash
215220
curl --request POST \
216221
--url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads/thread_abc123/runs?api-version=$API_VERSION \
217222
-H "Authorization: Bearer $AGENT_TOKEN" \
218223
-H "Content-Type: application/json" \
219224
-d '{
220-
"assistant_id": "<agent_id>",
221-
"tool_resources": {
225+
"assistant_id": "<agent_id>",
226+
"tool_resources": {
222227
"mcp": [
223228
{
224229
"server_label": "<the same unique name you provided during agent creation>",
225-
"require_approval": "always" #always by default
230+
"require_approval": "always" #always by default
226231
"headers": {
227232
"Authorization": "Bearer <token>",
228233
}
@@ -232,6 +237,7 @@ curl --request POST \
232237
},
233238
}'
234239
```
240+
235241
## Retrieve the status of the run
236242

237243
```bash
@@ -240,7 +246,8 @@ curl --request GET \
240246
-H "Authorization: Bearer $AGENT_TOKEN"
241247
```
242248

243-
If the model is trying to invoke a tool in your MCP server with approval required, you get a run with `require_action` status.
249+
If the model tries to invoke a tool in your MCP server with approval required, you get a run with `requires_action` status:
250+
244251
```bash
245252
{
246253
"id": "run_123",
@@ -274,17 +281,20 @@ If the model is trying to invoke a tool in your MCP server with approval require
274281
...
275282
}
276283
```
277-
Make sure you carefully reviewed the tool and argument(s) to be passed and make an informed decision for approval.
284+
285+
Carefully review the tool and arguments to be passed so that you can make an informed decision for approval.
278286

279287
## Submit your approval
280-
If you decide to approve, you need to set the `approve` parameter to be `true` with the `id` for the preceding tool calls.
288+
289+
If you decide to approve, set the `approve` parameter to `true` with the `id` value for the preceding tool calls:
290+
281291
```bash
282292
curl --request POST \
283293
--url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads/thread_abc123/runs/run_abc123/submit_tool_outputs?api-version=$API_VERSION \
284294
-H "Authorization: Bearer $AGENT_TOKEN" \
285295
-H "Content-Type: application/json" \
286296
-d '{
287-
"tool_approvals": [
297+
"tool_approvals": [
288298
{
289299
"tool_call_id": "call_abc123",
290300
"approve": true,
@@ -295,7 +305,6 @@ curl --request POST \
295305
}
296306
```
297307
298-
299308
## Retrieve the agent response
300309
301310
```bash
@@ -305,4 +314,3 @@ curl --request GET \
305314
```
306315
307316
:::zone-end
308-

0 commit comments

Comments
 (0)