feat: register isphere contact search tool
This commit is contained in:
65
internal/tools/isphere_contacts.go
Normal file
65
internal/tools/isphere_contacts.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"isphere-ai-bridge/internal/isphere"
|
||||
)
|
||||
|
||||
const ToolNameSearchContacts = "isphere_search_contacts"
|
||||
|
||||
type SearchContactsArgs struct {
|
||||
Query string `json:"query" jsonschema:"contact search query; currently matches bare JIDs from local readonly message logs"`
|
||||
Limit int `json:"limit,omitempty" jsonschema:"maximum number of contacts to return; zero or negative returns all matches"`
|
||||
}
|
||||
|
||||
func RegisterISphereContactTools(server *mcp.Server, source ReceiveMessagesSource) {
|
||||
if source == nil {
|
||||
source = isphere.EncryptedPacketLogSource{}
|
||||
}
|
||||
|
||||
mcp.AddTool[SearchContactsArgs, map[string]any](server, &mcp.Tool{
|
||||
Name: ToolNameSearchContacts,
|
||||
Description: "Search iSphere contact candidates from the configured read-only message source.",
|
||||
}, func(ctx context.Context, _ *mcp.CallToolRequest, input SearchContactsArgs) (*mcp.CallToolResult, map[string]any, error) {
|
||||
started := time.Now().UTC()
|
||||
messages, err := source.ReceiveMessages(ctx, isphere.ReceiveMessagesQuery{Limit: 0})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
contacts := isphere.SearchContactsFromMessages(messages.Messages, isphere.SearchContactsQuery{Query: input.Query, Limit: input.Limit})
|
||||
return nil, searchContactsResultToMap(contacts, started, time.Now().UTC()), nil
|
||||
})
|
||||
}
|
||||
|
||||
func searchContactsResultToMap(result isphere.SearchContactsResult, started time.Time, finished time.Time) map[string]any {
|
||||
contacts := make([]map[string]any, 0, len(result.Contacts))
|
||||
for _, contact := range result.Contacts {
|
||||
contacts = append(contacts, map[string]any{
|
||||
"contact_id": contact.ContactID,
|
||||
"display_name": contact.DisplayName,
|
||||
"account": contact.Account,
|
||||
"department": nil,
|
||||
"title": nil,
|
||||
"source": contact.Source,
|
||||
"confidence": contact.Confidence,
|
||||
"raw_ref": contact.RawRef,
|
||||
})
|
||||
}
|
||||
return map[string]any{
|
||||
"ok": true,
|
||||
"contacts": contacts,
|
||||
"next_cursor": nil,
|
||||
"audit": map[string]any{
|
||||
"tool": ToolNameSearchContacts,
|
||||
"source": "local_readonly",
|
||||
"execution_mode": "read",
|
||||
"started_at": started.Format(time.RFC3339Nano),
|
||||
"finished_at": finished.Format(time.RFC3339Nano),
|
||||
"result": "ok",
|
||||
},
|
||||
}
|
||||
}
|
||||
76
internal/tools/isphere_contacts_test.go
Normal file
76
internal/tools/isphere_contacts_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"isphere-ai-bridge/internal/isphere"
|
||||
)
|
||||
|
||||
func TestISphereSearchContactsToolReturnsJIDContacts(t *testing.T) {
|
||||
fake := &fakeReceiveMessagesSource{
|
||||
result: isphere.ReceiveMessagesResult{Messages: []isphere.Message{{
|
||||
ID: "msg-1",
|
||||
SenderID: "alice@imopenfire1-lanzhou",
|
||||
ReceiverID: "bob@imopenfire1-lanzhou",
|
||||
}}},
|
||||
}
|
||||
session, cleanup := connectToolsTestSession(t, func(server *mcp.Server) {
|
||||
RegisterISphereContactTools(server, fake)
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
callResult, err := session.CallTool(context.Background(), &mcp.CallToolParams{
|
||||
Name: ToolNameSearchContacts,
|
||||
Arguments: map[string]any{"query": "alice", "limit": 10},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("call %s: %v", ToolNameSearchContacts, err)
|
||||
}
|
||||
if callResult.IsError {
|
||||
t.Fatalf("call result is error: %+v", callResult)
|
||||
}
|
||||
|
||||
var decoded struct {
|
||||
OK bool `json:"ok"`
|
||||
Contacts []struct {
|
||||
ContactID string `json:"contact_id"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Account string `json:"account"`
|
||||
Source string `json:"source"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
RawRef string `json:"raw_ref"`
|
||||
} `json:"contacts"`
|
||||
NextCursor any `json:"next_cursor"`
|
||||
Audit map[string]any `json:"audit"`
|
||||
}
|
||||
payload, err := json.Marshal(callResult.StructuredContent)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal structured content: %v", err)
|
||||
}
|
||||
if err := json.Unmarshal(payload, &decoded); err != nil {
|
||||
t.Fatalf("decode structured content %s: %v", payload, err)
|
||||
}
|
||||
if !decoded.OK {
|
||||
t.Fatalf("ok = false in %s", payload)
|
||||
}
|
||||
if len(decoded.Contacts) != 1 {
|
||||
t.Fatalf("contacts = %+v, want one", decoded.Contacts)
|
||||
}
|
||||
contact := decoded.Contacts[0]
|
||||
if contact.ContactID != "alice@imopenfire1-lanzhou" || contact.DisplayName != "alice@imopenfire1-lanzhou" || contact.Account != "alice@imopenfire1-lanzhou" {
|
||||
t.Fatalf("unexpected contact identity: %+v", contact)
|
||||
}
|
||||
if contact.Source != "local_readonly" || contact.Confidence != 0.6 || contact.RawRef != "message_jid" {
|
||||
t.Fatalf("unexpected contact metadata: %+v", contact)
|
||||
}
|
||||
if decoded.Audit["tool"] != ToolNameSearchContacts || decoded.Audit["result"] != "ok" {
|
||||
t.Fatalf("audit = %+v", decoded.Audit)
|
||||
}
|
||||
if len(fake.queries) != 1 || fake.queries[0].Limit != 0 {
|
||||
t.Fatalf("source queries = %+v, want one unbounded receive query", fake.queries)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user