|
| 1 | +# pylint: disable=line-too-long,useless-suppression |
| 2 | +# ------------------------------------ |
| 3 | +# Copyright (c) Microsoft Corporation. |
| 4 | +# Licensed under the MIT License. |
| 5 | +# ------------------------------------ |
| 6 | + |
| 7 | +""" |
| 8 | +DESCRIPTION: |
| 9 | + This sample demonstrates how to use agent operations with the |
| 10 | + Model Context Protocol (MCP) tool from the Azure Agents service, and |
| 11 | + iteration in streaming. It uses a synchronous client. |
| 12 | + To learn more about Model Context Protocol, visit https://modelcontextprotocol.io/ |
| 13 | +
|
| 14 | +USAGE: |
| 15 | + python sample_agents_stream_iteration_with_mcp.py |
| 16 | +
|
| 17 | + Before running the sample: |
| 18 | +
|
| 19 | + pip install azure-ai-projects azure-ai-agents azure-identity |
| 20 | +
|
| 21 | + Set these environment variables with your own values: |
| 22 | + 1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview |
| 23 | + page of your Azure AI Foundry portal. |
| 24 | + 2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in |
| 25 | + the "Models + endpoints" tab in your Azure AI Foundry project. |
| 26 | + 3) MCP_SERVER_URL - The URL of your MCP server endpoint. |
| 27 | + 4) MCP_SERVER_LABEL - A label for your MCP server. |
| 28 | +""" |
| 29 | + |
| 30 | +import os |
| 31 | +from azure.ai.projects import AIProjectClient |
| 32 | +from azure.ai.agents.models import AgentStreamEvent, RunStepDeltaChunk |
| 33 | +from azure.ai.agents.models import ( |
| 34 | + MessageDeltaChunk, |
| 35 | + RunStep, |
| 36 | + ThreadMessage, |
| 37 | + ThreadRun, |
| 38 | + McpTool, |
| 39 | + MessageRole, |
| 40 | + MessageDeltaTextContent, |
| 41 | + MessageDeltaTextUrlCitationAnnotation, |
| 42 | + RequiredMcpToolCall, |
| 43 | + SubmitToolApprovalAction, |
| 44 | + ToolApproval |
| 45 | +) |
| 46 | +from azure.identity import DefaultAzureCredential |
| 47 | + |
| 48 | +project_client = AIProjectClient( |
| 49 | + endpoint=os.environ["PROJECT_ENDPOINT"], |
| 50 | + credential=DefaultAzureCredential(), |
| 51 | +) |
| 52 | + |
| 53 | +with project_client: |
| 54 | + agents_client = project_client.agents |
| 55 | + |
| 56 | + # Get MCP server configuration from environment variables |
| 57 | + mcp_server_url = os.environ.get("MCP_SERVER_URL", "https://gitmcp.io/Azure/azure-rest-api-specs") |
| 58 | + mcp_server_label = os.environ.get("MCP_SERVER_LABEL", "github") |
| 59 | + |
| 60 | + # Initialize agent MCP tool |
| 61 | + mcp_tool = McpTool( |
| 62 | + server_label=mcp_server_label, |
| 63 | + server_url=mcp_server_url, |
| 64 | + allowed_tools=[], # Optional: specify allowed tools |
| 65 | + ) |
| 66 | + |
| 67 | + # You can also add or remove allowed tools dynamically |
| 68 | + search_api_code = "search_azure_rest_api_code" |
| 69 | + mcp_tool.allow_tool(search_api_code) |
| 70 | + print(f"Allowed tools: {mcp_tool.allowed_tools}") |
| 71 | + |
| 72 | + # Create a new agent. |
| 73 | + # NOTE: To reuse existing agent, fetch it with get_agent(agent.id) |
| 74 | + agent = agents_client.create_agent( |
| 75 | + model=os.environ["MODEL_DEPLOYMENT_NAME"], |
| 76 | + name="my-agent", |
| 77 | + 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.", |
| 78 | + tools=mcp_tool.definitions, |
| 79 | + ) |
| 80 | + print(f"Created agent, agent ID: {agent.id}") |
| 81 | + |
| 82 | + thread = agents_client.threads.create() |
| 83 | + print(f"Created thread, thread ID {thread.id}") |
| 84 | + |
| 85 | + message = agents_client.messages.create( |
| 86 | + thread_id=thread.id, |
| 87 | + role=MessageRole.USER, |
| 88 | + content="Please summarize the Azure REST API specifications Readme" |
| 89 | + ) |
| 90 | + print(f"Created message, message ID {message.id}") |
| 91 | + |
| 92 | + # Process Agent run and stream events back to the client. It may take a few minutes for the agent to complete the run. |
| 93 | + mcp_tool.update_headers("SuperSecret", "123456") |
| 94 | + # mcp_tool.set_approval_mode("never") # Uncomment to disable approval requirement |
| 95 | + with agents_client.runs.stream(thread_id=thread.id, agent_id=agent.id, tool_resources=mcp_tool.resources) as stream: |
| 96 | + |
| 97 | + for event_type, event_data, _ in stream: |
| 98 | + |
| 99 | + if isinstance(event_data, MessageDeltaChunk): |
| 100 | + print(f"Text delta received: {event_data.text}") |
| 101 | + if event_data.delta.content and isinstance(event_data.delta.content[0], MessageDeltaTextContent): |
| 102 | + delta_text_content = event_data.delta.content[0] |
| 103 | + if delta_text_content.text and delta_text_content.text.annotations: |
| 104 | + for delta_annotation in delta_text_content.text.annotations: |
| 105 | + if isinstance(delta_annotation, MessageDeltaTextUrlCitationAnnotation): |
| 106 | + print( |
| 107 | + f"URL citation delta received: [{delta_annotation.url_citation.title}]({delta_annotation.url_citation.url})" |
| 108 | + ) |
| 109 | + |
| 110 | + elif isinstance(event_data, RunStepDeltaChunk): |
| 111 | + print(f"RunStepDeltaChunk received. ID: {event_data.id}.") |
| 112 | + |
| 113 | + elif isinstance(event_data, ThreadMessage): |
| 114 | + print(f"ThreadMessage created. ID: {event_data.id}, Status: {event_data.status}") |
| 115 | + |
| 116 | + elif isinstance(event_data, ThreadRun): |
| 117 | + print(f"ThreadRun status: {event_data.status}") |
| 118 | + |
| 119 | + if event_data.status == "failed": |
| 120 | + print(f"Run failed. Error: {event_data.last_error}") |
| 121 | + |
| 122 | + if event_data.status == "requires_action" and isinstance( |
| 123 | + event_data.required_action, SubmitToolApprovalAction |
| 124 | + ): |
| 125 | + tool_calls = event_data.required_action.submit_tool_approval.tool_calls |
| 126 | + if not tool_calls: |
| 127 | + print("No tool calls provided - cancelling run") |
| 128 | + agents_client.runs.cancel(thread_id=event_data.thread_id, run_id=event_data.id) |
| 129 | + break |
| 130 | + |
| 131 | + tool_approvals = [] |
| 132 | + for tool_call in tool_calls: |
| 133 | + if isinstance(tool_call, RequiredMcpToolCall): |
| 134 | + try: |
| 135 | + print(f"Approving tool call: {tool_call}") |
| 136 | + tool_approvals.append( |
| 137 | + ToolApproval( |
| 138 | + tool_call_id=tool_call.id, |
| 139 | + approve=True, |
| 140 | + headers=mcp_tool.headers, |
| 141 | + ) |
| 142 | + ) |
| 143 | + except Exception as e: |
| 144 | + print(f"Error approving tool_call {tool_call.id}: {e}") |
| 145 | + |
| 146 | + print(f"tool_approvals: {tool_approvals}") |
| 147 | + if tool_approvals: |
| 148 | + # Once we receive 'requires_action' status, the next event will be DONE. |
| 149 | + # Here we associate our existing event handler to the next stream. |
| 150 | + agents_client.runs.submit_tool_outputs_stream( |
| 151 | + thread_id=event_data.thread_id, run_id=event_data.id, tool_approvals=tool_approvals, event_handler=stream |
| 152 | + ) |
| 153 | + |
| 154 | + elif isinstance(event_data, RunStep): |
| 155 | + print(f"RunStep type: {event_data.type}, Status: {event_data.status}") |
| 156 | + |
| 157 | + elif event_type == AgentStreamEvent.ERROR: |
| 158 | + print(f"An error occurred. Data: {event_data}") |
| 159 | + |
| 160 | + elif event_type == AgentStreamEvent.DONE: |
| 161 | + print("Stream completed.") |
| 162 | + |
| 163 | + else: |
| 164 | + print(f"Unhandled Event Type: {event_type}, Data: {event_data}") |
| 165 | + |
| 166 | + # Clean-up and delete the agent once the run is finished. |
| 167 | + # NOTE: Comment out this line if you plan to reuse the agent later. |
| 168 | + agents_client.delete_agent(agent.id) |
| 169 | + print("Deleted agent") |
| 170 | + |
| 171 | + response_message = agents_client.messages.get_last_message_by_role(thread_id=thread.id, role=MessageRole.AGENT) |
| 172 | + if response_message: |
| 173 | + for text_message in response_message.text_messages: |
| 174 | + print(f"Agent response: {text_message.text.value}") |
| 175 | + for annotation in response_message.url_citation_annotations: |
| 176 | + print(f"URL Citation: [{annotation.url_citation.title}]({annotation.url_citation.url})") |
0 commit comments