Skip to content

Commit d94d715

Browse files
pontemontiJohan Broberg
andauthored
feat: Add chat history API for Semantic Kernel (#149)
Co-authored-by: Johan Broberg <johanb@microsoft.com>
1 parent d64969b commit d94d715

File tree

10 files changed

+3746
-10
lines changed

10 files changed

+3746
-10
lines changed

docs/prd/semantic-kernel-chat-history-api-tasks.md

Lines changed: 555 additions & 0 deletions
Large diffs are not rendered by default.

docs/prd/semantic-kernel-chat-history-api.md

Lines changed: 1306 additions & 0 deletions
Large diffs are not rendered by default.

libraries/microsoft-agents-a365-tooling-extensions-semantickernel/docs/design.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,105 @@ microsoft_agents_a365/tooling/extensions/semantickernel/
5858
└── mcp_tool_registration_service.py
5959
```
6060

61+
## Chat History API
62+
63+
The `McpToolRegistrationService` provides methods to send chat history from Semantic Kernel agents to the MCP platform for real-time threat protection and compliance monitoring.
64+
65+
### send_chat_history
66+
67+
Send chat history from a Semantic Kernel `ChatHistory` object:
68+
69+
```python
70+
from semantic_kernel.contents import ChatHistory
71+
from microsoft_agents_a365.tooling.extensions.semantickernel import McpToolRegistrationService
72+
73+
service = McpToolRegistrationService()
74+
75+
# Create and populate chat history
76+
chat_history = ChatHistory()
77+
chat_history.add_user_message("Hello!")
78+
chat_history.add_assistant_message("Hi there! How can I help you?")
79+
chat_history.add_user_message("Tell me about the weather.")
80+
81+
# Send chat history to MCP platform
82+
result = await service.send_chat_history(
83+
turn_context=turn_context,
84+
chat_history=chat_history,
85+
limit=50, # Optional: send only the most recent 50 messages
86+
)
87+
88+
if result.succeeded:
89+
print("Chat history sent successfully")
90+
else:
91+
print(f"Failed to send chat history: {result.errors}")
92+
```
93+
94+
### send_chat_history_messages
95+
96+
Send a list of `ChatMessageContent` objects directly:
97+
98+
```python
99+
from semantic_kernel.contents import ChatMessageContent, AuthorRole
100+
from microsoft_agents_a365.tooling.extensions.semantickernel import McpToolRegistrationService
101+
102+
service = McpToolRegistrationService()
103+
104+
# Create messages directly
105+
messages = [
106+
ChatMessageContent(role=AuthorRole.USER, content="Hello!"),
107+
ChatMessageContent(role=AuthorRole.ASSISTANT, content="Hi there!"),
108+
]
109+
110+
# Send messages to MCP platform
111+
result = await service.send_chat_history_messages(
112+
turn_context=turn_context,
113+
messages=messages,
114+
)
115+
116+
if result.succeeded:
117+
print("Messages sent successfully")
118+
```
119+
120+
### Message Conversion Flow
121+
122+
```
123+
ChatHistory / List[ChatMessageContent]
124+
125+
126+
McpToolRegistrationService.send_chat_history()
127+
128+
├── Extract messages from ChatHistory
129+
├── Apply limit if specified (most recent N)
130+
131+
132+
McpToolRegistrationService.send_chat_history_messages()
133+
134+
├── Convert ChatMessageContent to ChatHistoryMessage
135+
│ ├── Map AuthorRole to lowercase string (USER → "user")
136+
│ ├── Extract content as string
137+
│ ├── Extract ID from metadata or generate UUID
138+
│ └── Extract timestamp from metadata or use current UTC
139+
140+
├── Filter invalid messages (None, empty content)
141+
142+
143+
McpToolServerConfigurationService.send_chat_history()
144+
145+
146+
MCP Platform (real-time threat protection)
147+
```
148+
149+
### Error Handling
150+
151+
Both methods return an `OperationResult`:
152+
153+
- `result.succeeded` - Boolean indicating success
154+
- `result.errors` - List of errors if the operation failed
155+
156+
Validation errors (e.g., None turn_context) raise `ValueError` immediately.
157+
61158
## Dependencies
62159

63160
- `semantic-kernel` - Semantic Kernel framework
64161
- `microsoft-agents-a365-tooling` - Core tooling service
65-
- `microsoft-agents-a365-runtime` - Utility functions
162+
- `microsoft-agents-a365-runtime` - Utility functions and OperationResult

0 commit comments

Comments
 (0)