@@ -39,6 +39,7 @@ To report an issue with the client library, or request additional features, plea
39
39
- [ Fabric data] ( #create-an-agent-with-fabric )
40
40
- [ Connected agents] ( #create-an-agent-using-another-agents )
41
41
- [ Deep Research] ( #create-agent-with-deep-research )
42
+ - [ MCP] ( #create-agent-with-mcp )
42
43
- [ Create thread] ( #create-thread ) with
43
44
- [ Tool resource] ( #create-thread-with-tool-resource )
44
45
- [ Create message] ( #create-message ) with:
@@ -374,7 +375,7 @@ with project_client:
374
375
375
376
### Create Agent with Deep Research
376
377
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.
378
379
This scenarios requires you to specify two model deployments. One is the generic chat model that does arbitration, and is
379
380
specified as usual when you call the ` create_agent ` method. The other is the Deep Research model, which is specified
380
381
when you define the ` DeepResearchTool ` .
@@ -413,6 +414,92 @@ with project_client:
413
414
> ** Limitation** : The Deep Research tool is currently recommended ** only** in non-streaming scenarios.
414
415
> Using it with streaming can work, but it may occasionally time-out and is therefore not yet recommended.
415
416
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
+
416
503
### Create Agent with Azure AI Search
417
504
418
505
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 ) .
0 commit comments