Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 8, 2026

Implementation Plan: Chat History API for MCP Platform

Based on the .NET SDK implementation in PR #148.

Changes Required:

  • Create ChatHistoryMessage model class
  • Create ChatMessageRequest model class
  • Create OperationResult and OperationError classes
  • Add get_chat_history_endpoint() utility function
  • Add send_chat_history() method to McpToolServerConfigurationService
  • Update model exports in __init__.py files
  • Create unit tests for new models and functionality
  • Add comprehensive unit tests for send_chat_history method
  • Address PR feedback:
    • Updated copyright headers to # Copyright (c) Microsoft Corporation. and # Licensed under the MIT License.
    • Removed examples/send_chat_history_example.py
    • Updated send_chat_history to use TurnContext parameter instead of conversation_id, message_id, user_message
    • Removed auth_token parameter (no authentication required for this endpoint)
    • Added type annotation for turn_context parameter as TurnContext
    • Removed TYPE_CHECKING conditional import and always import TurnContext
    • Added microsoft-agents-hosting-core as a dependency in pyproject.toml
    • Ran uv run --frozen ruff format . to format code correctly
    • Updated datetime import style for consistency across test files

Summary:

✅ Successfully implemented generic API to send chat history to MCP platform
✅ All tests passing (39 unit tests total: 27 for models + 12 for send_chat_history method)
✅ Code follows repository conventions and passes linting
✅ All PR feedback addressed
✅ TurnContext properly imported with dependency added
✅ Comprehensive test coverage including validation, error handling, and success scenarios
✅ Code properly formatted with ruff
✅ Consistent datetime import style across all test files

Original prompt

This section details on the original issue you should resolve

<issue_title>Implementation of API to send chat history to MCP platform.</issue_title>
<issue_description>Implement the generic API to send chat history to MCP platform. Follow the changes that were made for the .NET version of the SDK: microsoft/Agent365-dotnet#148</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Implement API to send chat history to MCP platform Add API to send chat history to MCP platform for threat protection Jan 8, 2026
Copilot AI requested a review from pontemonti January 8, 2026 00:46
@pontemonti
Copy link
Contributor

@copilot -

  1. Update the copyright headers in the added and updated files:
    # Copyright (c) Microsoft Corporation.
    # Licensed under the MIT License.
    
  2. Remove examples/send_chat_history_example.py
  3. In send_chat_history, do not include an auth_token parameter. This request does not require authentication.
  4. In send_chat_history, include TurnContext (from Agents SDK) as parameter instead of conversation_id, message_id and user_message. Those values can be retrieved from TurnContext.

…se TurnContext, remove auth

Co-authored-by: pontemonti <[email protected]>
Copy link
Contributor Author

Copilot AI commented Jan 8, 2026

@copilot -

  1. Update the copyright headers in the added and updated files:

...

All feedback addressed in commit 6c49d71:

  1. ✅ Updated copyright headers to # Copyright (c) Microsoft Corporation. and # Licensed under the MIT License. in all added and updated files
  2. ✅ Removed examples/send_chat_history_example.py
  3. ✅ Removed auth_token parameter from send_chat_history - no authentication headers are now sent
  4. ✅ Updated send_chat_history to accept turn_context parameter - extracts conversation_id, message_id, and user_message from turn_context.activity

…nts-hosting-core dependency

Co-authored-by: pontemonti <[email protected]>
@pontemonti
Copy link
Contributor

@copilot - add tests for send_chat_history

Copy link
Contributor Author

Copilot AI commented Jan 8, 2026

@copilot - add tests for send_chat_history

Added comprehensive unit tests in commit 4443b7d. Created 12 tests covering:

  • Successful requests
  • HTTP error responses
  • Custom options
  • Parameter validation (turn_context, chat_history_messages, activity, conversation_id, message_id, user_message)
  • Error handling (ClientError, TimeoutError)
  • Correct payload generation

All 39 tests (27 existing + 12 new) passing.

@pontemonti
Copy link
Contributor

@copilot - run uv run --frozen ruff format . to format the code correctly

Copy link
Contributor Author

Copilot AI commented Jan 8, 2026

@copilot - run uv run --frozen ruff format . to format the code correctly

Code formatted in commit 1b6f196. Ran uv run --frozen ruff format . which reformatted 2 files (service and test file) with proper whitespace and line length adjustments.

Copilot AI requested a review from pontemonti January 8, 2026 04:36
@pontemonti pontemonti marked this pull request as ready for review January 8, 2026 04:41
@pontemonti pontemonti requested a review from a team as a code owner January 8, 2026 04:41
Copilot AI review requested due to automatic review settings January 8, 2026 04:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements a generic API to send chat history to the MCP (Model Context Protocol) platform for real-time threat protection, following the pattern established in the .NET SDK implementation (PR #148).

Key Changes:

  • Added new model classes (ChatHistoryMessage, ChatMessageRequest) for structuring chat history data
  • Added operation result classes (OperationResult, OperationError) for standardized error handling
  • Implemented send_chat_history() method in McpToolServerConfigurationService that extracts conversation data from TurnContext and sends it to the MCP platform

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
libraries/microsoft-agents-a365-tooling/microsoft_agents_a365/tooling/models/chat_history_message.py New model class representing a single chat message with validation
libraries/microsoft-agents-a365-tooling/microsoft_agents_a365/tooling/models/chat_message_request.py New model class representing the complete request payload for threat protection
libraries/microsoft-agents-a365-runtime/microsoft_agents_a365/runtime/operation_result.py New class for representing operation success/failure states
libraries/microsoft-agents-a365-runtime/microsoft_agents_a365/runtime/operation_error.py New class for wrapping exceptions in operation results
libraries/microsoft-agents-a365-tooling/microsoft_agents_a365/tooling/services/mcp_tool_server_configuration_service.py Added send_chat_history() method with comprehensive validation and error handling
libraries/microsoft-agents-a365-tooling/microsoft_agents_a365/tooling/utils/utility.py Added get_chat_history_endpoint() utility function for endpoint URL construction
libraries/microsoft-agents-a365-tooling/microsoft_agents_a365/tooling/models/init.py Updated exports to include new model classes
libraries/microsoft-agents-a365-runtime/microsoft_agents_a365/runtime/init.py Updated exports to include new runtime classes
libraries/microsoft-agents-a365-tooling/pyproject.toml Added microsoft-agents-hosting-core dependency for TurnContext
tests/tooling/models/test_chat_history_message.py Comprehensive unit tests for ChatHistoryMessage model (7 test cases)
tests/tooling/models/test_chat_message_request.py Comprehensive unit tests for ChatMessageRequest model (6 test cases)
tests/runtime/test_operation_result.py Comprehensive unit tests for OperationResult class (9 test cases)
tests/runtime/test_operation_error.py Comprehensive unit tests for OperationError class (5 test cases)
tests/tooling/services/test_send_chat_history.py Comprehensive unit tests for send_chat_history method (12 test cases covering success, error handling, and validation)
tests/tooling/init.py New test package initialization with correct copyright header
tests/tooling/models/init.py New test package initialization with correct copyright header
tests/tooling/services/init.py New test package initialization with correct copyright header

@pontemonti pontemonti enabled auto-merge (squash) January 8, 2026 05:10
pontemonti and others added 6 commits January 17, 2026 13:42
* Fix inconsistent string representation for failed result with no errors

Improves the __str__ method of OperationResult to return "Failed" instead of
"Failed : " when there are no errors. Also removes the extra space before
the colon for consistency.

Addresses code review Comment 1.

Co-Authored-By: Claude <[email protected]>

* Return defensive copy of errors list to protect singleton

The errors property now returns a copy of the internal errors list to
protect the singleton instance returned by success() from accidental
modification. Updated docstring to document this behavior.

Addresses code review Comment 2.

Co-Authored-By: Claude <[email protected]>

* Use explicit None check for timestamp validation

Changed timestamp validation from falsy check (if not self.timestamp)
to explicit None check (if self.timestamp is None) for safer and more
intentional validation behavior. Updated error message and test accordingly.

Addresses code review Comment 3.

Co-Authored-By: Claude <[email protected]>

* Move local imports to top of file

Moved OperationError, OperationResult, ChatMessageRequest, and
get_chat_history_endpoint imports from inside the send_chat_history
method to the top of the file with the other imports. Removed the
misleading comment about circular dependencies as there is no cycle
in the import graph.

Addresses code review Comment 4.

Co-Authored-By: Claude <[email protected]>

* Change endpoint URL log level from INFO to DEBUG

Detailed operational information like endpoint URLs should be logged
at DEBUG level rather than INFO level. INFO level is reserved for
higher-level operation status messages.

Addresses code review Comment 5.

Co-Authored-By: Claude <[email protected]>

* Use consistent async test pattern for validation tests

Converted validation tests from synchronous methods using asyncio.run()
to async methods with @pytest.mark.asyncio decorator for consistency
with the other tests in the test suite.

Addresses code review Comment 7.

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Johan Broberg <[email protected]>
Co-authored-by: Claude <[email protected]>
Add .strip() checks to string validation in ChatHistoryMessage and
ChatMessageRequest to reject whitespace-only values (CRM-001, CRM-002).
Add missing return type annotations on to_dict() methods (CRM-005, CRM-006).
Improve __post_init__ docstrings with validation details (CRM-015).

Co-Authored-By: Claude Opus 4.5 <[email protected]>
- Add explicit None checks for turn_context and chat_history_messages (CRM-003)
- Add return type annotation OperationResult to send_chat_history (CRM-004)
- Use explicit None and .strip() checks for field validation (CRM-007)
- Add 30 second HTTP timeout to prevent indefinite hangs (CRM-008)
- Log only URL path to avoid potential PII exposure (CRM-009)
- Use aiohttp.ClientResponseError for consistent error handling (CRM-010)
- Enhance docstring with detailed validation requirements (CRM-013)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Extract hardcoded endpoint path to CHAT_HISTORY_ENDPOINT_PATH constant
for better maintainability and discoverability (CRM-017).

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add tests for whitespace-only string validation in ChatHistoryMessage and
ChatMessageRequest (CRM-012). Add test for empty chat_history list
validation in send_chat_history (CRM-011). Update HTTP error test to
match new aiohttp.ClientResponseError format.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
* fix(runtime): implement thread-safe singleton with eager initialization

Convert OperationResult.success() singleton from lazy to eager initialization
at module level. Python's import lock ensures thread-safe initialization,
eliminating the race condition in the previous check-and-create pattern.

Addresses CRM-001 from code review.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* fix(tooling): include error response body in HTTP error log message

Add truncated error_text (max 500 chars) to the log message when HTTP
errors occur in send_chat_history. This improves debugging by showing
the actual error response from the MCP platform.

Addresses CRM-002 from code review.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* fix(tooling): reorder exception handlers for proper timeout handling

Move asyncio.TimeoutError handler before aiohttp.ClientError to ensure
timeouts are caught correctly. Since aiohttp.ServerTimeoutError inherits
from both exceptions, the previous order could misclassify timeouts.

Addresses CRM-003 from code review.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* refactor(tooling): add type hints for local variables in send_chat_history

Add Optional[str] type annotations for conversation_id, message_id, and
user_message variables to improve code clarity and IDE support.

Addresses CRM-004 from code review.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* docs(tooling): improve validation error messages in ChatHistoryMessage

Update error messages to say "cannot be empty or whitespace-only" instead
of just "cannot be empty" for clearer feedback when whitespace validation
fails.

Addresses CRM-005 from code review.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* refactor(tooling): extract timeout and HTTP status code to constants

Add DEFAULT_REQUEST_TIMEOUT_SECONDS and HTTP_STATUS_OK module-level
constants to replace magic values. Improves maintainability and makes
configuration easier to modify.

Addresses CRM-006 from code review.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* docs(runtime): enhance defensive copy docstring in OperationResult.errors

Move the defensive copy rationale into a prominent Note section in the
docstring to make it more visible to developers. This clarifies why the
property returns a copy rather than the internal list.

Addresses CRM-007 from code review.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* test(tooling): use Mock(spec=TurnContext) for stricter interface validation

Import TurnContext and use it as a spec for the mock fixture. This ensures
the mock matches the actual TurnContext interface, catching potential
issues if the API changes.

Addresses CRM-008 from code review.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* docs(tooling): add usage example to send_chat_history docstring

Add an Example section showing how to create ChatHistoryMessage objects
and call send_chat_history with proper error handling. This helps
developers understand the intended usage pattern.

Addresses CRM-009 from code review.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

---------

Co-authored-by: Johan Broberg <[email protected]>
Co-authored-by: Claude Opus 4.5 <[email protected]>
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.

Implementation of API to send chat history to MCP platform.

2 participants