Add API to send chat history to MCP platform for threat protection#105
Merged
pontemonti merged 21 commits intomainfrom Jan 21, 2026
Merged
Add API to send chat history to MCP platform for threat protection#105pontemonti merged 21 commits intomainfrom
pontemonti merged 21 commits intomainfrom
Conversation
Co-authored-by: pontemonti <7850950+pontemonti@users.noreply.github.com>
Co-authored-by: pontemonti <7850950+pontemonti@users.noreply.github.com>
Co-authored-by: pontemonti <7850950+pontemonti@users.noreply.github.com>
Co-authored-by: pontemonti <7850950+pontemonti@users.noreply.github.com>
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
Contributor
|
@copilot -
|
…se TurnContext, remove auth Co-authored-by: pontemonti <7850950+pontemonti@users.noreply.github.com>
Contributor
Author
... All feedback addressed in commit 6c49d71:
|
pontemonti
reviewed
Jan 8, 2026
...a365-tooling/microsoft_agents_a365/tooling/services/mcp_tool_server_configuration_service.py
Outdated
Show resolved
Hide resolved
Co-authored-by: pontemonti <7850950+pontemonti@users.noreply.github.com>
pontemonti
reviewed
Jan 8, 2026
...a365-tooling/microsoft_agents_a365/tooling/services/mcp_tool_server_configuration_service.py
Outdated
Show resolved
Hide resolved
…nts-hosting-core dependency Co-authored-by: pontemonti <7850950+pontemonti@users.noreply.github.com>
Contributor
|
@copilot - add tests for |
Co-authored-by: pontemonti <7850950+pontemonti@users.noreply.github.com>
Contributor
Author
Added comprehensive unit tests in commit 4443b7d. Created 12 tests covering:
All 39 tests (27 existing + 12 new) passing. |
Contributor
Author
Contributor
There was a problem hiding this comment.
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 inMcpToolServerConfigurationServicethat extracts conversation data fromTurnContextand 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 |
Co-authored-by: pontemonti <7850950+pontemonti@users.noreply.github.com>
pontemonti
approved these changes
Jan 8, 2026
* 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> --------- Co-authored-by: Johan Broberg <johanb@microsoft.com> Co-authored-by: Claude <noreply@anthropic.com>
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 <noreply@anthropic.com>
- 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 <noreply@anthropic.com>
Extract hardcoded endpoint path to CHAT_HISTORY_ENDPOINT_PATH constant for better maintainability and discoverability (CRM-017). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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 <noreply@anthropic.com>
* 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> --------- Co-authored-by: Johan Broberg <johanb@microsoft.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
tmlsousa
reviewed
Jan 19, 2026
...s/microsoft-agents-a365-tooling/microsoft_agents_a365/tooling/models/chat_history_message.py
Outdated
Show resolved
Hide resolved
tmlsousa
previously approved these changes
Jan 19, 2026
...s/microsoft-agents-a365-tooling/microsoft_agents_a365/tooling/models/chat_history_message.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-a365-runtime/microsoft_agents_a365/runtime/__init__.py
Show resolved
Hide resolved
tmlsousa
approved these changes
Jan 20, 2026
rahuldevikar761
approved these changes
Jan 21, 2026
🧹 Deployment Cleanup CompleteThe Azure Container App deployment for this PR has been cleaned up.
Thank you for contributing! 🎉 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implementation Plan: Chat History API for MCP Platform
Based on the .NET SDK implementation in PR #148.
Changes Required:
ChatHistoryMessagemodel classChatMessageRequestmodel classOperationResultandOperationErrorclassesget_chat_history_endpoint()utility functionsend_chat_history()method toMcpToolServerConfigurationService__init__.pyfilessend_chat_historymethod# Copyright (c) Microsoft Corporation.and# Licensed under the MIT License.examples/send_chat_history_example.pysend_chat_historyto useTurnContextparameter instead ofconversation_id,message_id,user_messageauth_tokenparameter (no authentication required for this endpoint)turn_contextparameter asTurnContextTYPE_CHECKINGconditional import and always importTurnContextmicrosoft-agents-hosting-coreas a dependency in pyproject.tomluv run --frozen ruff format .to format code correctlySummary:
✅ 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
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.