Skip to content

Ensure headers get set when rest clients receive an aiohttp.ClientSession instance - #618

Draft
Rodrigo Brandão (rodrigobr-msft) wants to merge 1 commit into
mainfrom
users/robrandao/user-agent
Draft

Rodrigo Brandão (rodrigobr-msft) wants to merge 1 commit into
mainfrom
users/robrandao/user-agent

Conversation

@rodrigobr-msft

Copy link
Copy Markdown
Contributor

This pull request ensures that all core connector clients in the Microsoft Agents Hosting Core library consistently set and send a standardized User-Agent header, as returned by get_product_info(), with every outgoing HTTP request. It also adds comprehensive tests to verify this behavior for ConnectorClient, TeamsConnectorClient, UserTokenClient, and MCSConnectorClient.

User-Agent header standardization:

Testing improvements:

  • Added new tests to verify that ConnectorClient, TeamsConnectorClient, and UserTokenClient set and send the correct User-Agent header on all requests. (test_connector_client.py [1] test_user_token_client.py [2]
  • Added new tests to verify that MCSConnectorClient sets and sends the correct User-Agent header (noting that these tests currently fail if the header is not set). (test_mcs_connector_client.py tests/hosting_core/connector/test_mcs_connector_client.pyR1-R55)

These changes improve observability and consistency across all connector clients by ensuring a uniform User-Agent is present in all outgoing requests.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

Review effort: Lite
Findings: 1 High severity · 3 Medium severity

Open (4)
What changed in this PR

This PR standardizes outbound HTTP User-Agent headers across core connector clients by using get_product_info() and adds tests to verify the header is set and sent on requests.

Changes:

  • Updated ConnectorClient, UserTokenClient, and MCSConnectorClient initialization to apply standardized headers (including User-Agent) to aiohttp.ClientSession.
  • Added tests validating User-Agent is present on the session and transmitted on requests for Connector/UserToken/MCS clients.
  • Expanded connector test suite coverage to include TeamsConnectorClient User-Agent behavior.
File Description
tests/​hosting_core/​connector/​test_user_token_client.py Adds assertions that UserTokenClient sets/sends standardized User-Agent.
tests/​hosting_core/​connector/​test_mcs_connector_client.py Introduces new tests verifying MCSConnectorClient User-Agent behavior.
tests/​hosting_core/​connector/​test_connector_client.py Adds User-Agent tests for ConnectorClient and TeamsConnectorClient.
libraries/​microsoft-agents-hosting-core/​microsoft_agents/​hosting/​core/​connector/​mcs/​mcs_connector_client.py Ensures MCSConnectorClient session includes standardized default headers including User-Agent.
libraries/​microsoft-agents-hosting-core/​microsoft_agents/​hosting/​core/​connector/​client/​user_token_client.py Applies headers to session after creation to ensure consistency even with provided sessions.
libraries/​microsoft-agents-hosting-core/​microsoft_agents/​hosting/​core/​connector/​client/​connector_client.py Applies headers to session after creation to ensure consistency even with provided sessions.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +981 to +983
finally:
await server.close()
await client.client.close()
Comment on lines +792 to +793
session = session or ClientSession(base_url=endpoint)
session.headers.update(headers)
Comment on lines +228 to +233
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"User-Agent": get_product_info(),
}
self._client.headers.update(headers)
Comment on lines +16 to +22
"""Ensures MCSConnectorClient sends a User-Agent header.

NOTE: Unlike ConnectorClient/TeamsConnectorClient and UserTokenClient,
MCSConnectorClient does not currently set a custom "agents-sdk-py" User-Agent
header on its session or outgoing requests, so these tests currently fail.
"""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants