Skip to content

Commit 5fe0132

Browse files
committed
update README and TypeSpec commit
1 parent 1d1918c commit 5fe0132

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

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

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ To report an issue with the client library, or request additional features, plea
3939
- [Fabric data](#create-an-agent-with-fabric)
4040
- [Connected agents](#create-an-agent-using-another-agents)
4141
- [Deep Research](#create-agent-with-deep-research)
42+
- [MCP](#create-agent-with-mcp)
4243
- [Create thread](#create-thread) with
4344
- [Tool resource](#create-thread-with-tool-resource)
4445
- [Create message](#create-message) with:
@@ -374,7 +375,7 @@ with project_client:
374375

375376
### Create Agent with Deep Research
376377

377-
To enable your Agent to do a detailed research of a topic, use the `DeepResearchTool` along with a connection to a Bing Grounding resource.
378+
To enable your Agent to do detailed research of a topic, use the `DeepResearchTool` along with a connection to a Bing Grounding resource.
378379
This scenarios requires you to specify two model deployments. One is the generic chat model that does arbitration, and is
379380
specified as usual when you call the `create_agent` method. The other is the Deep Research model, which is specified
380381
when you define the `DeepResearchTool`.
@@ -413,6 +414,92 @@ with project_client:
413414
> **Limitation**: The Deep Research tool is currently recommended **only** in non-streaming scenarios.
414415
> Using it with streaming can work, but it may occasionally time-out and is therefore not yet recommended.
415416
417+
### Create Agent with MCP
418+
419+
To enable your Agent to connect to a MCP server, use the `McpTool` along with a server URI to a MCP server and a label for that server.
420+
Note that approval to send data to that server is required by default (but can be set to not required for each run).
421+
422+
Here is an example:
423+
424+
<!-- SNIPPET:sample_agents_mcp.create_agent_with_mcp_tool -->
425+
426+
```python
427+
# Initialize agent MCP tool
428+
mcp_tool = McpTool(
429+
server_label=mcp_server_label,
430+
server_url=mcp_server_url,
431+
allowed_tools=[], # Optional: specify allowed tools
432+
)
433+
434+
# You can also add or remove allowed tools dynamically
435+
search_api_code = "search_azure_rest_api_code"
436+
mcp_tool.allow_tool(search_api_code)
437+
print(f"Allowed tools: {mcp_tool.allowed_tools}")
438+
439+
# Create agent with MCP tool and process agent run
440+
with project_client:
441+
agents_client = project_client.agents
442+
443+
# Create a new agent.
444+
# NOTE: To reuse existing agent, fetch it with get_agent(agent_id)
445+
agent = agents_client.create_agent(
446+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
447+
name="my-mcp-agent",
448+
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.",
449+
tools=mcp_tool.definitions,
450+
)
451+
```
452+
453+
<!-- END SNIPPET -->
454+
455+
The tool approval flow looks like this:
456+
457+
<!-- SNIPPET:sample_agents_mcp.handle_tool_approvals -->
458+
459+
```python
460+
# Create and process agent run in thread with MCP tools
461+
mcp_tool.update_headers("SuperSecret", "123456")
462+
# mcp_tool.set_approval_mode("never") # Uncomment to disable approval requirement
463+
run = agents_client.runs.create(thread_id=thread.id, agent_id=agent.id, tool_resources=mcp_tool.resources)
464+
print(f"Created run, ID: {run.id}")
465+
466+
while run.status in ["queued", "in_progress", "requires_action"]:
467+
time.sleep(1)
468+
run = agents_client.runs.get(thread_id=thread.id, run_id=run.id)
469+
470+
if run.status == "requires_action" and isinstance(run.required_action, SubmitToolApprovalAction):
471+
tool_calls = run.required_action.submit_tool_approval.tool_calls
472+
if not tool_calls:
473+
print("No tool calls provided - cancelling run")
474+
agents_client.runs.cancel(thread_id=thread.id, run_id=run.id)
475+
break
476+
477+
tool_approvals = []
478+
for tool_call in tool_calls:
479+
if isinstance(tool_call, RequiredMcpToolCall):
480+
try:
481+
print(f"Approving tool call: {tool_call}")
482+
tool_approvals.append(
483+
ToolApproval(
484+
tool_call_id=tool_call.id,
485+
approve=True,
486+
headers=mcp_tool.headers,
487+
)
488+
)
489+
except Exception as e:
490+
print(f"Error approving tool_call {tool_call.id}: {e}")
491+
492+
print(f"tool_approvals: {tool_approvals}")
493+
if tool_approvals:
494+
agents_client.runs.submit_tool_outputs(
495+
thread_id=thread.id, run_id=run.id, tool_approvals=tool_approvals
496+
)
497+
498+
print(f"Current run status: {run.status}")
499+
```
500+
501+
<!-- END SNIPPET -->
502+
416503
### Create Agent with Azure AI Search
417504

418505
Azure AI Search is an enterprise search system for high-performance applications. It integrates with Azure OpenAI Service and Azure Machine Learning, offering advanced search technologies like vector search and full-text search. Ideal for knowledge base insights, information discovery, and automation. Creating an Agent with Azure AI Search requires an existing Azure AI Search Index. For more information and setup guides, see [Azure AI Search Tool Guide](https://learn.microsoft.com/azure/ai-services/agents/how-to/tools/azure-ai-search?tabs=azurecli%2Cpython&pivots=overview-azure-ai-search).

sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_mcp.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
credential=DefaultAzureCredential(),
4141
)
4242

43-
# [START create_agent_with_mcp]
43+
# [START create_agent_with_mcp_tool]
4444
# Initialize agent MCP tool
4545
mcp_tool = McpTool(
4646
server_label=mcp_server_label,
@@ -65,7 +65,7 @@
6565
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.",
6666
tools=mcp_tool.definitions,
6767
)
68-
# [END create_agent_with_mcp]
68+
# [END create_agent_with_mcp_tool]
6969

7070
print(f"Created agent, ID: {agent.id}")
7171
print(f"MCP Server: {mcp_tool.server_label} at {mcp_tool.server_url}")
@@ -82,6 +82,7 @@
8282
)
8383
print(f"Created message, ID: {message.id}")
8484

85+
# [START handle_tool_approvals]
8586
# Create and process agent run in thread with MCP tools
8687
mcp_tool.update_headers("SuperSecret", "123456")
8788
# mcp_tool.set_approval_mode("never") # Uncomment to disable approval requirement
@@ -121,6 +122,7 @@
121122
)
122123

123124
print(f"Current run status: {run.status}")
125+
# [END handle_tool_approvals]
124126

125127
print(f"Run completed with status: {run.status}")
126128
if run.status == "failed":
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
directory: specification/ai/Azure.AI.Agents
2-
commit: 7b710e9f3b2418d197c085dc3b598b18e22bd032
2+
commit: 325a30f626267b74387d08ba7536b4bd6519efee
33
repo: Azure/azure-rest-api-specs
44
additionalDirectories:

0 commit comments

Comments
 (0)