From 1306e5bce9a817f4bce10ea16d2d621a3fcefc00 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 16 Mar 2026 21:38:01 +0000 Subject: [PATCH] Address automated review comments: span error for ToolExecutionException, README prereqs, and AsyncClient lifecycle Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> --- python/packages/core/agent_framework/_mcp.py | 7 ++++++- python/samples/02-agents/mcp/README.md | 3 +-- .../02-agents/mcp/mcp_github_pat_azure_chat.py | 15 +++++++-------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/python/packages/core/agent_framework/_mcp.py b/python/packages/core/agent_framework/_mcp.py index 484fe9fc33..9eba9cfc6a 100644 --- a/python/packages/core/agent_framework/_mcp.py +++ b/python/packages/core/agent_framework/_mcp.py @@ -950,6 +950,7 @@ class MCPTool: with get_mcp_call_span(span_attributes) as span: # Try the operation, reconnecting once if the connection is closed + _span_error_set = False for attempt in range(2): try: # Capture the JSON-RPC request ID before the call is made. @@ -975,9 +976,13 @@ class MCPTool: error_msg = text or str(parsed) span.set_attribute(OtelAttr.ERROR_TYPE, "ToolError") span.set_status(trace.StatusCode.ERROR, error_msg) + _span_error_set = True raise ToolExecutionException(error_msg) return parser(result) - except ToolExecutionException: + except ToolExecutionException as ex: + if not _span_error_set: + span.set_attribute(OtelAttr.ERROR_TYPE, type(ex).__name__) + span.set_status(trace.StatusCode.ERROR, str(ex)) raise except ClosedResourceError as cl_ex: if attempt == 0: diff --git a/python/samples/02-agents/mcp/README.md b/python/samples/02-agents/mcp/README.md index 32f846c7ea..cd5899cfed 100644 --- a/python/samples/02-agents/mcp/README.md +++ b/python/samples/02-agents/mcp/README.md @@ -17,8 +17,7 @@ The Model Context Protocol (MCP) is an open standard for connecting AI agents to ## Prerequisites -- `OPENAI_API_KEY` environment variable -- `OPENAI_RESPONSES_MODEL_ID` environment variable +Each sample requires its own set of environment variables. See below for details. For `mcp_github_pat_openai_responses.py`: - `GITHUB_PAT` - Your GitHub Personal Access Token (create at https://github.com/settings/tokens) diff --git a/python/samples/02-agents/mcp/mcp_github_pat_azure_chat.py b/python/samples/02-agents/mcp/mcp_github_pat_azure_chat.py index c90d23a478..d50168fc23 100644 --- a/python/samples/02-agents/mcp/mcp_github_pat_azure_chat.py +++ b/python/samples/02-agents/mcp/mcp_github_pat_azure_chat.py @@ -46,24 +46,23 @@ async def github_mcp_example() -> None: "Authorization": f"Bearer {github_pat}", } - # 4. Create HTTP client with authentication headers - http_client = AsyncClient(headers=auth_headers) - - # 5. Create Azure OpenAI Chat Client + # 4. Create Azure OpenAI Chat Client # For authentication, run `az login` command in terminal or replace AzureCliCredential # with your preferred authentication option (e.g., api_key parameter) client = AzureOpenAIChatClient(credential=AzureCliCredential()) - # 6. Create MCP tool for GitHub with PAT authentication - # The MCPStreamableHTTPTool manages the connection to the MCP server and makes its tools available - async with MCPStreamableHTTPTool( + # 5. Create HTTP client with authentication headers and MCP tool for GitHub with PAT authentication. + # The MCPStreamableHTTPTool manages the connection to the MCP server and makes its tools available. + # The HTTP client is used to pass the authentication headers to the MCP server and is closed when + # the context manager exits. + async with AsyncClient(headers=auth_headers) as http_client, MCPStreamableHTTPTool( name="GitHub", description="GitHub MCP server for interacting with GitHub repositories, issues, and more", url="https://api.githubcopilot.com/mcp/", http_client=http_client, # Pass HTTP client with authentication headers approval_mode="never_require", # For sample brevity; use "always_require" in production ) as github_mcp_tool: - # 7. Create agent with the GitHub MCP tool + # 6. Create agent with the GitHub MCP tool agent = client.as_agent( instructions=( "You are a helpful assistant that can help users interact with GitHub. "