177 lines
7.4 KiB
Go
177 lines
7.4 KiB
Go
package cabana
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
|
"git.golem15.com/golem15/summercms/modules/pact"
|
|
"git.golem15.com/golem15/summercms/modules/party"
|
|
)
|
|
|
|
const mediaMCPPluginID = "summercms.media"
|
|
|
|
// MediaMCPPlugin returns the framework MCP pseudo-plugin (ID "summercms.media")
|
|
// exposing the 7 built-in Media Library tools (media_prepare_upload,
|
|
// media_upload, media_list, media_get, media_update, media_delete,
|
|
// media_create_folder) bound to cabana's {adminPrefix}/api/v1/media/* routes
|
|
// under the "backend" guard.
|
|
func MediaMCPPlugin(adminPrefix string) party.Plugin {
|
|
prefix := strings.TrimRight(strings.TrimSpace(adminPrefix), "/")
|
|
if prefix == "" {
|
|
prefix = DefaultAdminPrefix
|
|
}
|
|
return mediaMCPPlugin{apiBase: prefix + adminAPIVersion}
|
|
}
|
|
|
|
type mediaMCPPlugin struct {
|
|
apiBase string
|
|
}
|
|
|
|
var (
|
|
_ party.Plugin = mediaMCPPlugin{}
|
|
_ pact.HasMCP = mediaMCPPlugin{}
|
|
_ pact.HasPermissions = mediaMCPPlugin{}
|
|
_ pact.HasTokenScopes = mediaMCPPlugin{}
|
|
)
|
|
|
|
func (mediaMCPPlugin) ID() string { return mediaMCPPluginID }
|
|
func (mediaMCPPlugin) Requires() []string { return nil }
|
|
func (mediaMCPPlugin) Register(*backpack.App) error { return nil }
|
|
func (mediaMCPPlugin) Boot(*backpack.App) error { return nil }
|
|
|
|
func (mediaMCPPlugin) Permissions() []pact.Permission {
|
|
return mediaPermissions()
|
|
}
|
|
|
|
func (mediaMCPPlugin) TokenScopes() []pact.TokenScope {
|
|
return mediaTokenScopes()
|
|
}
|
|
|
|
type mediaPrepareUploadMCPInput struct {
|
|
Filename string `json:"filename" jsonschema:"Target filename including extension (for example hero.jpg or guide.pdf)"`
|
|
ContentType string `json:"content_type,omitempty" jsonschema:"Optional expected MIME type"`
|
|
FolderID *uint `json:"folder_id,omitempty" jsonschema:"Optional destination virtual folder ID"`
|
|
MaxBytes int64 `json:"max_bytes,omitempty" jsonschema:"Optional byte limit (capped by server media.max_bytes)"`
|
|
}
|
|
|
|
type mediaUploadMCPInput struct {
|
|
Filename string `json:"filename" jsonschema:"Filename including extension (for example photo.png or report.pdf)"`
|
|
ContentBase64 string `json:"content_base64" jsonschema:"Base64-encoded file bytes (decoded size capped at media.base64_max_bytes, default 5 MB)"`
|
|
FolderID *uint `json:"folder_id,omitempty" jsonschema:"Optional destination virtual folder ID"`
|
|
Alt string `json:"alt,omitempty" jsonschema:"Optional alternative text for images"`
|
|
Title string `json:"title,omitempty" jsonschema:"Optional display title"`
|
|
Caption string `json:"caption,omitempty" jsonschema:"Optional caption"`
|
|
}
|
|
|
|
type mediaListMCPInput struct {
|
|
FolderID string `json:"folder_id,omitempty" query:"folder_id" jsonschema:"Filter by folder ID, or 'root' for root-level items"`
|
|
RootOnly string `json:"root_only,omitempty" query:"root_only" jsonschema:"Set to '1' to list only items not in any folder"`
|
|
Query string `json:"q,omitempty" query:"q" jsonschema:"Search filename, title, alt, or slug"`
|
|
Type string `json:"type,omitempty" query:"type" jsonschema:"Filter by media type: 'image' or 'file'"`
|
|
SHA256 string `json:"sha256,omitempty" query:"sha256" jsonschema:"Filter by exact lowercase 64-char SHA-256 hex digest"`
|
|
Page int `json:"page,omitempty" query:"page" jsonschema:"Page number (1-based)"`
|
|
PerPage int `json:"per_page,omitempty" query:"per_page" jsonschema:"Items per page (1-100, default 24)"`
|
|
}
|
|
|
|
type mediaGetMCPInput struct {
|
|
ID uint `json:"id" path:"id" jsonschema:"Media item ID"`
|
|
}
|
|
|
|
type mediaUpdateMCPInput struct {
|
|
ID uint `json:"id" path:"id" jsonschema:"Media item ID"`
|
|
Alt *string `json:"alt,omitempty" jsonschema:"Updated alternative text"`
|
|
Title *string `json:"title,omitempty" jsonschema:"Updated display title"`
|
|
Caption *string `json:"caption,omitempty" jsonschema:"Updated caption"`
|
|
FolderID *uint `json:"folder_id,omitempty" jsonschema:"Move item to this virtual folder ID (never changes public URL)"`
|
|
ClearFolder bool `json:"clear_folder,omitempty" jsonschema:"Set true to move item to the root library (no folder)"`
|
|
}
|
|
|
|
type mediaDeleteMCPInput struct {
|
|
ID uint `json:"id" path:"id" jsonschema:"Media item ID to permanently delete"`
|
|
}
|
|
|
|
type mediaCreateFolderMCPInput struct {
|
|
Name string `json:"name" jsonschema:"Display name of the new virtual folder"`
|
|
ParentID *uint `json:"parent_id,omitempty" jsonschema:"Optional parent folder ID"`
|
|
}
|
|
|
|
func (p mediaMCPPlugin) MCP() pact.MCP {
|
|
destructive := true
|
|
return pact.MCP{
|
|
Tools: []pact.MCPTool{
|
|
{
|
|
Name: "prepare_upload",
|
|
Title: "Prepare media upload URL",
|
|
Description: "Create a single-use signed upload URL (valid for 5 minutes) for uploading binary media up to media.max_bytes via HTTP PUT without base64 overhead.",
|
|
Method: http.MethodPost,
|
|
Path: p.apiBase + "/media/uploads/prepare",
|
|
Guard: "backend",
|
|
Permissions: []string{PermissionManageMedia},
|
|
Input: mediaPrepareUploadMCPInput{},
|
|
},
|
|
{
|
|
Name: "upload",
|
|
Title: "Upload media (base64)",
|
|
Description: "Upload a file directly via base64 encoding (decoded size capped at media.base64_max_bytes, default 5 MB). Raster images automatically generate WebP srcset variants.",
|
|
Method: http.MethodPost,
|
|
Path: p.apiBase + "/media/items/base64",
|
|
Guard: "backend",
|
|
Permissions: []string{PermissionManageMedia},
|
|
Input: mediaUploadMCPInput{},
|
|
},
|
|
{
|
|
Name: "list",
|
|
Title: "List media items",
|
|
Description: "List, search, and filter items in the Media Library by folder, query string, type (image/file), or SHA-256 hash.",
|
|
Method: http.MethodGet,
|
|
Path: p.apiBase + "/media/items",
|
|
Guard: "backend",
|
|
Permissions: []string{PermissionViewMedia},
|
|
Input: mediaListMCPInput{},
|
|
},
|
|
{
|
|
Name: "get",
|
|
Title: "Get media item",
|
|
Description: "Fetch full metadata, public URL, WebP variants, and srcset for a single Media Library item by ID.",
|
|
Method: http.MethodGet,
|
|
Path: p.apiBase + "/media/items/{id}",
|
|
Guard: "backend",
|
|
Permissions: []string{PermissionViewMedia},
|
|
Input: mediaGetMCPInput{},
|
|
},
|
|
{
|
|
Name: "update",
|
|
Title: "Update media metadata",
|
|
Description: "Update alt, title, caption, or virtual folder on a Media Library item without changing its immutable public URL.",
|
|
Method: http.MethodPut,
|
|
Path: p.apiBase + "/media/items/{id}",
|
|
Guard: "backend",
|
|
Permissions: []string{PermissionManageMedia},
|
|
Input: mediaUpdateMCPInput{},
|
|
},
|
|
{
|
|
Name: "delete",
|
|
Title: "Delete media item",
|
|
Description: "Permanently delete a Media Library item and its stored original and WebP variant blobs. Published URLs referencing this item will stop working.",
|
|
Method: http.MethodDelete,
|
|
Path: p.apiBase + "/media/items/{id}",
|
|
Guard: "backend",
|
|
Permissions: []string{PermissionManageMedia},
|
|
Destructive: &destructive,
|
|
Input: mediaDeleteMCPInput{},
|
|
},
|
|
{
|
|
Name: "create_folder",
|
|
Title: "Create media folder",
|
|
Description: "Create a new virtual folder in the Media Library.",
|
|
Method: http.MethodPost,
|
|
Path: p.apiBase + "/media/folders",
|
|
Guard: "backend",
|
|
Permissions: []string{PermissionManageMedia},
|
|
Input: mediaCreateFolderMCPInput{},
|
|
},
|
|
},
|
|
}
|
|
}
|