@@ -44,12 +44,12 @@ async def main() -> None:
4444 # Get MCP server configuration from environment variables
4545 mcp_server_url = os .environ .get ("MCP_SERVER_URL" , "https://gitmcp.io/Azure/azure-rest-api-specs" )
4646 mcp_server_label = os .environ .get ("MCP_SERVER_LABEL" , "github" )
47-
47+
4848 project_client = AIProjectClient (
4949 endpoint = os .environ ["PROJECT_ENDPOINT" ],
5050 credential = DefaultAzureCredential (),
5151 )
52-
52+
5353 # Initialize agent MCP tool
5454 mcp_tool = McpTool (
5555 server_label = mcp_server_label ,
@@ -60,11 +60,11 @@ async def main() -> None:
6060 search_api_code = "search_azure_rest_api_code"
6161 mcp_tool .allow_tool (search_api_code )
6262 print (f"Allowed tools: { mcp_tool .allowed_tools } " )
63-
63+
6464 # Create agent with MCP tool and process agent run
6565 async with project_client :
6666 agents_client = project_client .agents
67-
67+
6868 # Create a new agent.
6969 # NOTE: To reuse existing agent, fetch it with get_agent(agent_id)
7070 agent = await agents_client .create_agent (
@@ -73,39 +73,39 @@ async def main() -> None:
7373 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." ,
7474 tools = mcp_tool .definitions ,
7575 )
76-
76+
7777 print (f"Created agent, ID: { agent .id } " )
7878 print (f"MCP Server: { mcp_tool .server_label } at { mcp_tool .server_url } " )
79-
79+
8080 # Create thread for communication
8181 thread = await agents_client .threads .create ()
8282 print (f"Created thread, ID: { thread .id } " )
83-
83+
8484 # Create message to thread
8585 message = await agents_client .messages .create (
8686 thread_id = thread .id ,
8787 role = "user" ,
8888 content = "Please summarize the Azure REST API specifications Readme" ,
8989 )
9090 print (f"Created message, ID: { message .id } " )
91-
91+
9292 # Create and process agent run in thread with MCP tools
9393 mcp_tool .update_headers ("SuperSecret" , "123456" )
9494 # mcp_tool.set_approval_mode("never") # Uncomment to disable approval requirement
9595 run = await agents_client .runs .create (thread_id = thread .id , agent_id = agent .id , tool_resources = mcp_tool .resources )
9696 print (f"Created run, ID: { run .id } " )
97-
97+
9898 while run .status in ["queued" , "in_progress" , "requires_action" ]:
9999 await asyncio .sleep (1 )
100100 run = await agents_client .runs .get (thread_id = thread .id , run_id = run .id )
101-
101+
102102 if run .status == "requires_action" and isinstance (run .required_action , SubmitToolApprovalAction ):
103103 tool_calls = run .required_action .submit_tool_approval .tool_calls
104104 if not tool_calls :
105105 print ("No tool calls provided - cancelling run" )
106106 await agents_client .runs .cancel (thread_id = thread .id , run_id = run .id )
107107 break
108-
108+
109109 tool_approvals = []
110110 for tool_call in tool_calls :
111111 if isinstance (tool_call , RequiredMcpToolCall ):
@@ -120,36 +120,36 @@ async def main() -> None:
120120 )
121121 except Exception as e :
122122 print (f"Error approving tool_call { tool_call .id } : { e } " )
123-
123+
124124 print (f"tool_approvals: { tool_approvals } " )
125125 if tool_approvals :
126126 await agents_client .runs .submit_tool_outputs (
127127 thread_id = thread .id , run_id = run .id , tool_approvals = tool_approvals
128128 )
129-
129+
130130 print (f"Current run status: { run .status } " )
131-
131+
132132 print (f"Run completed with status: { run .status } " )
133133 if run .status == "failed" :
134134 print (f"Run failed: { run .last_error } " )
135-
135+
136136 # Display run steps and tool calls
137137 run_steps = agents_client .run_steps .list (thread_id = thread .id , run_id = run .id )
138-
138+
139139 # Loop through each step
140140 async for step in run_steps :
141141 print (f"Step { step ['id' ]} status: { step ['status' ]} " )
142-
142+
143143 # Check if there are tool calls in the step details
144144 step_details = step .get ("step_details" , {})
145145 tool_calls = step_details .get ("tool_calls" , [])
146-
146+
147147 if tool_calls :
148148 print (" MCP Tool calls:" )
149149 for call in tool_calls :
150150 print (f" Tool Call ID: { call .get ('id' )} " )
151151 print (f" Type: { call .get ('type' )} " )
152-
152+
153153 if isinstance (step_details , RunStepActivityDetails ):
154154 for activity in step_details .activities :
155155 for function_name , function_definition in activity .tools .items ():
@@ -164,9 +164,9 @@ async def main() -> None:
164164 print (f" Description: { func_argument .description } " )
165165 else :
166166 print ("This function has no parameters" )
167-
167+
168168 print () # add an extra newline between steps
169-
169+
170170 # Fetch and log all messages
171171 messages = agents_client .messages .list (thread_id = thread .id , order = ListSortOrder .ASCENDING )
172172 print ("\n Conversation:" )
@@ -176,22 +176,23 @@ async def main() -> None:
176176 last_text = msg .text_messages [- 1 ]
177177 print (f"{ msg .role .upper ()} : { last_text .text .value } " )
178178 print ("-" * 50 )
179-
179+
180180 # Example of dynamic tool management
181181 print (f"\n Demonstrating dynamic tool management:" )
182182 print (f"Current allowed tools: { mcp_tool .allowed_tools } " )
183-
183+
184184 # Remove a tool
185185 try :
186186 mcp_tool .disallow_tool (search_api_code )
187187 print (f"After removing { search_api_code } : { mcp_tool .allowed_tools } " )
188188 except ValueError as e :
189189 print (f"Error removing tool: { e } " )
190-
190+
191191 # Clean-up and delete the agent once the run is finished.
192192 # NOTE: Comment out this line if you plan to reuse the agent later.
193193 await agents_client .delete_agent (agent .id )
194194 print ("Deleted agent" )
195195
196+
196197if __name__ == "__main__" :
197198 asyncio .run (main ())
0 commit comments