227 lines
5.7 KiB
Go
227 lines
5.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
"inou/lib"
|
|
)
|
|
|
|
// MCP Tool Implementations
|
|
// Data queries go through lib directly with RBAC enforcement.
|
|
// Image rendering goes through the API (which also enforces RBAC via lib).
|
|
|
|
const apiBaseURL = "http://localhost:8082" // Internal API server (images only)
|
|
|
|
// mcpAPIGet calls the internal API with Bearer auth.
|
|
func mcpAPIGet(accessToken, path string, params map[string]string) ([]byte, error) {
|
|
v := url.Values{}
|
|
for k, val := range params {
|
|
if val != "" {
|
|
v.Set(k, val)
|
|
}
|
|
}
|
|
u := apiBaseURL + path
|
|
if len(v) > 0 {
|
|
u += "?" + v.Encode()
|
|
}
|
|
req, err := http.NewRequest("GET", u, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
return body, nil
|
|
}
|
|
|
|
// --- Data query tools: all go through lib with RBAC ---
|
|
|
|
func mcpListDossiers(accessorID string) (string, error) {
|
|
rows, err := lib.DossierQuery(accessorID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Group by dossier_id
|
|
type dossierWithCategories struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Email string `json:"email,omitempty"`
|
|
DOB string `json:"date_of_birth,omitempty"`
|
|
Sex string `json:"sex,omitempty"`
|
|
Categories []string `json:"categories"`
|
|
}
|
|
|
|
dossierMap := make(map[string]*dossierWithCategories)
|
|
for _, r := range rows {
|
|
if _, exists := dossierMap[r.DossierID]; !exists {
|
|
dossierMap[r.DossierID] = &dossierWithCategories{
|
|
ID: r.DossierID,
|
|
Name: r.Name,
|
|
Email: r.Email,
|
|
DOB: r.DateOfBirth,
|
|
Sex: lib.SexTranslate(r.Sex, "en"),
|
|
Categories: []string{},
|
|
}
|
|
}
|
|
|
|
if r.EntryCount > 0 {
|
|
dossierMap[r.DossierID].Categories = append(dossierMap[r.DossierID].Categories, lib.CategoryName(r.Category))
|
|
}
|
|
}
|
|
|
|
// Convert map to array
|
|
var result []*dossierWithCategories
|
|
for _, d := range dossierMap {
|
|
result = append(result, d)
|
|
}
|
|
|
|
pretty, _ := json.MarshalIndent(result, "", " ")
|
|
return string(pretty), nil
|
|
}
|
|
|
|
func mcpQueryEntries(accessorID, dossier, category, typ, searchKey, parent, from, to string, limit int) (string, error) {
|
|
cat := 0
|
|
if category != "" {
|
|
cat = lib.CategoryFromString[category]
|
|
}
|
|
filter := &lib.EntryFilter{DossierID: dossier}
|
|
if typ != "" {
|
|
filter.Type = typ
|
|
}
|
|
if searchKey != "" {
|
|
filter.SearchKey = searchKey
|
|
}
|
|
if from != "" {
|
|
filter.FromDate, _ = strconv.ParseInt(from, 10, 64)
|
|
}
|
|
if to != "" {
|
|
filter.ToDate, _ = strconv.ParseInt(to, 10, 64)
|
|
}
|
|
if limit > 0 {
|
|
filter.Limit = limit
|
|
}
|
|
|
|
entries, err := lib.EntryList(accessorID, parent, cat, filter)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return formatEntries(entries), nil
|
|
}
|
|
|
|
func mcpGetCategories(dossier, accessorID string) (string, error) {
|
|
ctx := &lib.AccessContext{AccessorID: accessorID}
|
|
result, err := lib.EntryCategoryCounts(ctx, dossier)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
pretty, _ := json.MarshalIndent(result, "", " ")
|
|
return string(pretty), nil
|
|
}
|
|
|
|
// formatEntries converts entries to the standard MCP response format.
|
|
func formatEntries(entries []*lib.Entry) string {
|
|
var result []map[string]any
|
|
for _, e := range entries {
|
|
entry := map[string]any{
|
|
"id": e.EntryID,
|
|
"parent_id": e.ParentID,
|
|
"category": lib.CategoryName(e.Category),
|
|
"type": e.Type,
|
|
"summary": e.Summary,
|
|
"ordinal": e.Ordinal,
|
|
"timestamp": e.Timestamp,
|
|
}
|
|
result = append(result, entry)
|
|
}
|
|
pretty, _ := json.MarshalIndent(result, "", " ")
|
|
return string(pretty)
|
|
}
|
|
|
|
// --- Image tools: RBAC via lib, then API for rendering ---
|
|
|
|
func mcpFetchImage(accessToken, dossier, slice string, wc, ww float64) (map[string]interface{}, error) {
|
|
params := map[string]string{}
|
|
if wc != 0 {
|
|
params["wc"] = strconv.FormatFloat(wc, 'f', 0, 64)
|
|
}
|
|
if ww != 0 {
|
|
params["ww"] = strconv.FormatFloat(ww, 'f', 0, 64)
|
|
}
|
|
|
|
body, err := mcpAPIGet(accessToken, "/image/"+slice, params)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
b64 := base64.StdEncoding.EncodeToString(body)
|
|
return mcpImageContent(b64, "image/webp", fmt.Sprintf("Slice %s (%d bytes)", slice[:8], len(body))), nil
|
|
}
|
|
|
|
func mcpFetchContactSheet(accessToken, dossier, series string, wc, ww float64) (map[string]interface{}, error) {
|
|
params := map[string]string{}
|
|
if wc != 0 {
|
|
params["wc"] = strconv.FormatFloat(wc, 'f', 0, 64)
|
|
}
|
|
if ww != 0 {
|
|
params["ww"] = strconv.FormatFloat(ww, 'f', 0, 64)
|
|
}
|
|
|
|
body, err := mcpAPIGet(accessToken, "/contact-sheet.webp/"+series, params)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
b64 := base64.StdEncoding.EncodeToString(body)
|
|
return mcpImageContent(b64, "image/webp", fmt.Sprintf("Contact sheet %s (%d bytes)", series[:8], len(body))), nil
|
|
}
|
|
|
|
// --- Document fetch: returns extracted text + metadata from Data field ---
|
|
|
|
func mcpFetchDocument(accessorID, dossier, entryID string) (string, error) {
|
|
entries, err := lib.EntryRead(accessorID, dossier, &lib.Filter{EntryID: entryID})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if len(entries) == 0 {
|
|
return "", fmt.Errorf("document not found")
|
|
}
|
|
e := entries[0]
|
|
|
|
result := map[string]any{
|
|
"id": e.EntryID,
|
|
"type": e.Type,
|
|
"summary": e.Summary,
|
|
"timestamp": e.Timestamp,
|
|
}
|
|
|
|
// Merge Data fields (extracted text, findings, etc.) into result
|
|
if e.Data != "" {
|
|
var data map[string]interface{}
|
|
if json.Unmarshal([]byte(e.Data), &data) == nil {
|
|
for k, v := range data {
|
|
result[k] = v
|
|
}
|
|
}
|
|
}
|
|
|
|
pretty, _ := json.MarshalIndent(result, "", " ")
|
|
return string(pretty), nil
|
|
}
|