-
Notifications
You must be signed in to change notification settings - Fork 7
Add API to send chat history to MCP platform for threat protection #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: pontemonti <[email protected]>
Co-authored-by: pontemonti <[email protected]>
Co-authored-by: pontemonti <[email protected]>
Co-authored-by: pontemonti <[email protected]>
|
@copilot -
|
…se TurnContext, remove auth Co-authored-by: pontemonti <[email protected]>
... All feedback addressed in commit 6c49d71:
|
...a365-tooling/microsoft_agents_a365/tooling/services/mcp_tool_server_configuration_service.py
Outdated
Show resolved
Hide resolved
Co-authored-by: pontemonti <[email protected]>
...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 <[email protected]>
|
@copilot - add tests for |
Co-authored-by: pontemonti <[email protected]>
Added comprehensive unit tests in commit 4443b7d. Created 12 tests covering:
All 39 tests (27 existing + 12 new) passing. |
|
@copilot - run |
Co-authored-by: pontemonti <[email protected]>
There was a problem hiding this 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 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 <[email protected]>
* 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]>
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.