|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +""" |
| 4 | +Example usage of the chat history API to send chat history to the MCP platform. |
| 5 | +
|
| 6 | +This example demonstrates how to use the send_chat_history method to send |
| 7 | +chat conversation history to the MCP platform for real-time threat protection analysis. |
| 8 | +""" |
| 9 | + |
| 10 | +import asyncio |
| 11 | +from datetime import datetime, timezone |
| 12 | + |
| 13 | +from microsoft_agents_a365.tooling.models import ChatHistoryMessage |
| 14 | +from microsoft_agents_a365.tooling.services import McpToolServerConfigurationService |
| 15 | + |
| 16 | + |
| 17 | +async def main(): |
| 18 | + """Example of sending chat history to MCP platform.""" |
| 19 | + |
| 20 | + # Create the service |
| 21 | + service = McpToolServerConfigurationService() |
| 22 | + |
| 23 | + # Create chat history messages |
| 24 | + messages = [ |
| 25 | + ChatHistoryMessage( |
| 26 | + id="msg-1", |
| 27 | + role="user", |
| 28 | + content="Hello, I need help with my account", |
| 29 | + timestamp=datetime.now(timezone.utc), |
| 30 | + ), |
| 31 | + ChatHistoryMessage( |
| 32 | + id="msg-2", |
| 33 | + role="assistant", |
| 34 | + content="I'd be happy to help you with your account. What do you need assistance with?", |
| 35 | + timestamp=datetime.now(timezone.utc), |
| 36 | + ), |
| 37 | + ChatHistoryMessage( |
| 38 | + id="msg-3", |
| 39 | + role="user", |
| 40 | + content="I forgot my password", |
| 41 | + timestamp=datetime.now(timezone.utc), |
| 42 | + ), |
| 43 | + ] |
| 44 | + |
| 45 | + # Send chat history to MCP platform |
| 46 | + result = await service.send_chat_history( |
| 47 | + conversation_id="conv-123456", |
| 48 | + message_id="msg-4", |
| 49 | + user_message="Can you help me reset it?", |
| 50 | + chat_history_messages=messages, |
| 51 | + auth_token="your-auth-token-here", |
| 52 | + ) |
| 53 | + |
| 54 | + # Check the result |
| 55 | + if result.succeeded: |
| 56 | + print("✅ Chat history sent successfully!") |
| 57 | + else: |
| 58 | + print(f"❌ Failed to send chat history: {result}") |
| 59 | + for error in result.errors: |
| 60 | + print(f" - {error.message}") |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + asyncio.run(main()) |
0 commit comments