feat: align receive messages response contract
This commit is contained in:
@@ -2,6 +2,9 @@ package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
@@ -27,29 +30,124 @@ func RegisterISphereReadTools(server *mcp.Server, source ReceiveMessagesSource)
|
||||
Name: ToolNameReceiveMessages,
|
||||
Description: "Return iSphere messages from the configured read-only log-backed source.",
|
||||
}, func(ctx context.Context, _ *mcp.CallToolRequest, input ReceiveMessagesArgs) (*mcp.CallToolResult, map[string]any, error) {
|
||||
started := time.Now().UTC()
|
||||
result, err := source.ReceiveMessages(ctx, isphere.ReceiveMessagesQuery{Limit: input.Limit})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, receiveMessagesResultToMap(result), nil
|
||||
return nil, receiveMessagesResultToMap(result, started, time.Now().UTC()), nil
|
||||
})
|
||||
}
|
||||
|
||||
func receiveMessagesResultToMap(result isphere.ReceiveMessagesResult) map[string]any {
|
||||
func receiveMessagesResultToMap(result isphere.ReceiveMessagesResult, started time.Time, finished time.Time) map[string]any {
|
||||
messages := make([]map[string]any, 0, len(result.Messages))
|
||||
for _, message := range result.Messages {
|
||||
attachments := receiveMessageAttachments(message)
|
||||
messages = append(messages, map[string]any{
|
||||
"id": message.ID,
|
||||
"message_id": message.ID,
|
||||
"conversation_id": message.ConversationID,
|
||||
"conversation_type": message.ConversationType,
|
||||
"sender_id": message.SenderID,
|
||||
"sender_name": nil,
|
||||
"receiver_id": message.ReceiverID,
|
||||
"text": message.Text,
|
||||
"content_type": receiveMessageContentType(message, attachments),
|
||||
"content_text": message.Text,
|
||||
"attachments": attachments,
|
||||
"timestamp": message.Timestamp,
|
||||
"created_at": packetTimestampToRFC3339(message.Timestamp),
|
||||
"received_at": nil,
|
||||
"subject": message.Subject,
|
||||
"receipt_id": message.ReceiptID,
|
||||
"read": message.Read,
|
||||
"source": "local_readonly",
|
||||
"raw_ref": "packetlog_message",
|
||||
})
|
||||
}
|
||||
return map[string]any{"messages": messages}
|
||||
return map[string]any{
|
||||
"ok": true,
|
||||
"conversation": receiveMessagesConversation(result.Messages),
|
||||
"messages": messages,
|
||||
"next_cursor": nil,
|
||||
"audit": map[string]any{
|
||||
"tool": ToolNameReceiveMessages,
|
||||
"source": "local_readonly",
|
||||
"execution_mode": "read",
|
||||
"started_at": started.Format(time.RFC3339Nano),
|
||||
"finished_at": finished.Format(time.RFC3339Nano),
|
||||
"result": "ok",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func receiveMessagesConversation(messages []isphere.Message) any {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
first := messages[0]
|
||||
return map[string]any{
|
||||
"conversation_id": first.ConversationID,
|
||||
"conversation_type": contractConversationType(first.ConversationType),
|
||||
"display_name": first.ConversationID,
|
||||
}
|
||||
}
|
||||
|
||||
func contractConversationType(value string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "groupchat", "group":
|
||||
return "group"
|
||||
case "chat", "direct":
|
||||
return "direct"
|
||||
case "system":
|
||||
return "system"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func receiveMessageAttachments(message isphere.Message) []map[string]any {
|
||||
files := isphere.ListFilesFromMessages([]isphere.Message{message}, isphere.ListFilesQuery{})
|
||||
attachments := make([]map[string]any, 0, len(files.Files))
|
||||
for _, file := range files.Files {
|
||||
attachments = append(attachments, map[string]any{
|
||||
"file_id": file.FileID,
|
||||
"file_name": file.FileName,
|
||||
"size_bytes": file.SizeBytes,
|
||||
"download_ref": nullableString(file.DownloadRef),
|
||||
})
|
||||
}
|
||||
return attachments
|
||||
}
|
||||
|
||||
func receiveMessageContentType(message isphere.Message, attachments []map[string]any) string {
|
||||
if len(attachments) > 0 {
|
||||
return "file"
|
||||
}
|
||||
if strings.TrimSpace(message.Text) != "" {
|
||||
return "text"
|
||||
}
|
||||
if strings.TrimSpace(message.Subject) != "" {
|
||||
return "notify"
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func packetTimestampToRFC3339(value string) any {
|
||||
trimmed := strings.TrimSpace(value)
|
||||
if trimmed == "" {
|
||||
return nil
|
||||
}
|
||||
epoch, err := strconv.ParseInt(trimmed, 10, 64)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var ts time.Time
|
||||
if epoch > 1_000_000_000_000 {
|
||||
ts = time.UnixMilli(epoch)
|
||||
} else {
|
||||
ts = time.Unix(epoch, 0)
|
||||
}
|
||||
formatted := ts.UTC().Format(time.RFC3339)
|
||||
return formatted
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ func TestISphereReceiveMessagesToolReturnsSourceMessages(t *testing.T) {
|
||||
ConversationType: "chat",
|
||||
SenderID: "sender@imopenfire1-lanzhou",
|
||||
ReceiverID: "receiver@imopenfire1-lanzhou",
|
||||
Text: "redacted",
|
||||
Text: "redacted-report.docx",
|
||||
Timestamp: "1783423807000",
|
||||
Subject: "FILE_TRANSFER_CANCEL",
|
||||
ReceiptID: "receipt-1",
|
||||
@@ -55,18 +55,46 @@ func TestISphereReceiveMessagesToolReturnsSourceMessages(t *testing.T) {
|
||||
}
|
||||
|
||||
var decoded struct {
|
||||
Messages []struct {
|
||||
ID string `json:"id"`
|
||||
OK bool `json:"ok"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
Conversation *struct {
|
||||
ConversationID string `json:"conversation_id"`
|
||||
ConversationType string `json:"conversation_type"`
|
||||
SenderID string `json:"sender_id"`
|
||||
ReceiverID string `json:"receiver_id"`
|
||||
Text string `json:"text"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
Subject string `json:"subject"`
|
||||
ReceiptID string `json:"receipt_id"`
|
||||
Read bool `json:"read"`
|
||||
DisplayName string `json:"display_name"`
|
||||
} `json:"conversation"`
|
||||
Messages []struct {
|
||||
ID string `json:"id"`
|
||||
MessageID string `json:"message_id"`
|
||||
ConversationID string `json:"conversation_id"`
|
||||
ConversationType string `json:"conversation_type"`
|
||||
SenderID string `json:"sender_id"`
|
||||
SenderName *string `json:"sender_name"`
|
||||
ReceiverID string `json:"receiver_id"`
|
||||
Text string `json:"text"`
|
||||
ContentType string `json:"content_type"`
|
||||
ContentText string `json:"content_text"`
|
||||
Attachments []struct {
|
||||
FileID string `json:"file_id"`
|
||||
FileName string `json:"file_name"`
|
||||
DownloadRef any `json:"download_ref"`
|
||||
} `json:"attachments"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
CreatedAt *string `json:"created_at"`
|
||||
ReceivedAt any `json:"received_at"`
|
||||
Subject string `json:"subject"`
|
||||
ReceiptID string `json:"receipt_id"`
|
||||
Read bool `json:"read"`
|
||||
Source string `json:"source"`
|
||||
RawRef string `json:"raw_ref"`
|
||||
} `json:"messages"`
|
||||
Audit struct {
|
||||
Tool string `json:"tool"`
|
||||
Source string `json:"source"`
|
||||
ExecutionMode string `json:"execution_mode"`
|
||||
Result string `json:"result"`
|
||||
StartedAt string `json:"started_at"`
|
||||
FinishedAt string `json:"finished_at"`
|
||||
} `json:"audit"`
|
||||
}
|
||||
payload, err := json.Marshal(callResult.StructuredContent)
|
||||
if err != nil {
|
||||
@@ -78,19 +106,40 @@ func TestISphereReceiveMessagesToolReturnsSourceMessages(t *testing.T) {
|
||||
if len(decoded.Messages) != 1 {
|
||||
t.Fatalf("messages = %+v, want one", decoded.Messages)
|
||||
}
|
||||
if !decoded.OK || decoded.NextCursor != nil {
|
||||
t.Fatalf("ok/next_cursor = %v/%v", decoded.OK, decoded.NextCursor)
|
||||
}
|
||||
if decoded.Conversation == nil {
|
||||
t.Fatalf("conversation missing")
|
||||
}
|
||||
if decoded.Conversation.ConversationID != "sender@imopenfire1-lanzhou|receiver@imopenfire1-lanzhou" || decoded.Conversation.DisplayName == "" {
|
||||
t.Fatalf("unexpected conversation: %+v", decoded.Conversation)
|
||||
}
|
||||
msg := decoded.Messages[0]
|
||||
if msg.ID != "msg-1" || msg.Text != "redacted" || msg.Subject != "FILE_TRANSFER_CANCEL" {
|
||||
if msg.ID != "msg-1" || msg.MessageID != "msg-1" || msg.Text != "redacted-report.docx" || msg.ContentText != "redacted-report.docx" || msg.Subject != "FILE_TRANSFER_CANCEL" {
|
||||
t.Fatalf("unexpected message content: %+v", msg)
|
||||
}
|
||||
if msg.ContentType != "file" {
|
||||
t.Fatalf("content_type = %q, want file", msg.ContentType)
|
||||
}
|
||||
if len(msg.Attachments) != 1 || msg.Attachments[0].FileID != "msg-1:redacted-report.docx" || msg.Attachments[0].FileName != "redacted-report.docx" || msg.Attachments[0].DownloadRef != nil {
|
||||
t.Fatalf("attachments = %+v", msg.Attachments)
|
||||
}
|
||||
if msg.ConversationID != "sender@imopenfire1-lanzhou|receiver@imopenfire1-lanzhou" {
|
||||
t.Fatalf("conversation_id = %q", msg.ConversationID)
|
||||
}
|
||||
if msg.SenderID != "sender@imopenfire1-lanzhou" || msg.ReceiverID != "receiver@imopenfire1-lanzhou" {
|
||||
t.Fatalf("sender/receiver = %q/%q", msg.SenderID, msg.ReceiverID)
|
||||
}
|
||||
if msg.Timestamp != "1783423807000" || msg.ReceiptID != "receipt-1" || !msg.Read {
|
||||
if msg.Timestamp != "1783423807000" || msg.CreatedAt == nil || *msg.CreatedAt != "2026-07-07T11:30:07Z" || msg.ReceiptID != "receipt-1" || !msg.Read {
|
||||
t.Fatalf("metadata = %+v", msg)
|
||||
}
|
||||
if msg.SenderName != nil || msg.ReceivedAt != nil || msg.Source != "local_readonly" || msg.RawRef != "packetlog_message" {
|
||||
t.Fatalf("contract metadata = %+v", msg)
|
||||
}
|
||||
if decoded.Audit.Tool != ToolNameReceiveMessages || decoded.Audit.Source != "local_readonly" || decoded.Audit.ExecutionMode != "read" || decoded.Audit.Result != "ok" || decoded.Audit.StartedAt == "" || decoded.Audit.FinishedAt == "" {
|
||||
t.Fatalf("unexpected audit: %+v", decoded.Audit)
|
||||
}
|
||||
}
|
||||
|
||||
type fakeReceiveMessagesSource struct {
|
||||
|
||||
Reference in New Issue
Block a user