1+ import os
2+ from dotenv import load_dotenv
3+
4+ # Add references
5+ from azure .identity import DefaultAzureCredential
6+ from azure .ai .agents import AgentsClient
7+ from azure .ai .agents .models import McpTool
8+
9+ # Load environment variables from .env file
10+ load_dotenv ()
11+ project_endpoint = "[PROJECT_ENDPOINT]"
12+ model_deployment = "gpt-4o"
13+
14+ # Connect to the agents client
15+ agents_client = AgentsClient (
16+ endpoint = project_endpoint ,
17+ credential = DefaultAzureCredential (
18+ exclude_environment_credential = True ,
19+ exclude_managed_identity_credential = True
20+ )
21+ )
22+
23+ # MCP server configuration
24+ mcp_server_url = "[MCP_SERVER_URL]/mcp/stream"
25+ mcp_server_label = "todolist"
26+
27+ # Initialize agent MCP tool
28+ mcp_tool = McpTool (
29+ server_label = mcp_server_label ,
30+ server_url = mcp_server_url ,
31+ )
32+
33+ # Create agent with MCP tool and process agent run
34+ with agents_client :
35+
36+ # Create a new agent with the mcp tool definitions
37+ agent = agents_client .create_agent (
38+ model = model_deployment ,
39+ name = "my-mcp-agent" ,
40+ instructions = """
41+ You are a helpful agent that can use MCP tools to assist users.
42+ Use the available MCP tools to answer questions and perform tasks.""" ,
43+ tools = mcp_tool .definitions ,
44+ )
45+
46+ # Log info
47+ print (f"Created agent, ID: { agent .id } " )
48+ print (f"MCP Server: { mcp_tool .server_label } at { mcp_tool .server_url } " )
49+
50+ # Create thread for communication
51+ thread = agents_client .threads .create ()
52+ print (f"Created thread, ID: { thread .id } " )
53+
54+ # Create a message on the thread
55+ message = agents_client .messages .create (
56+ thread_id = thread .id ,
57+ role = "user" ,
58+ content = "Create a high priority todo to buy groceries." ,
59+ )
60+ print (f"Created message, ID: { message .id } " )
61+
62+ # Set approval mode
63+ mcp_tool .set_approval_mode ("never" )
64+
65+ # Create and process agent run in thread with MCP tools
66+ run = agents_client .runs .create_and_process (thread_id = thread .id , agent_id = agent .id , tool_resources = mcp_tool .resources )
67+ print (f"Created run, ID: { run .id } " )
68+
69+ # Check run status
70+ print (f"Run completed with status: { run .status } " )
71+ if run .status == "failed" :
72+ print (f"Run failed: { run .last_error } " )
73+
74+ # Display run steps and tool calls
75+ run_steps = agents_client .run_steps .list (thread_id = thread .id , run_id = run .id )
76+ for step in run_steps :
77+ print (f"Step { step ['id' ]} status: { step ['status' ]} " )
78+
79+ # Check if there are tool calls in the step details
80+ step_details = step .get ("step_details" , {})
81+ tool_calls = step_details .get ("tool_calls" , [])
82+
83+ if tool_calls :
84+ # Display the MCP tool call details
85+ print (" MCP Tool calls:" )
86+ for call in tool_calls :
87+ print (f" Tool Call ID: { call .get ('id' )} " )
88+ print (f" Type: { call .get ('type' )} " )
89+ print (f" Type: { call .get ('name' )} " )
90+
91+ print () # add an extra newline between steps
92+
93+ # Fetch and log all messages
94+ messages = agents_client .messages .list (thread_id = thread .id )
95+ print ("\n Conversation:" )
96+ print ("-" * 50 )
97+ for msg in messages :
98+ if msg .text_messages :
99+ last_text = msg .text_messages [- 1 ]
100+ print (f"{ msg .role .upper ()} : { last_text .text .value } " )
101+ print ("-" * 50 )
102+
103+ # Clean-up and delete the agent once the run is finished.
104+ agents_client .delete_agent (agent .id )
105+ print ("Deleted agent" )
0 commit comments