feat(files): enhance file handling with support for HTTP uploads and direct binary access
This commit is contained in:
@@ -23,7 +23,8 @@ type FilesTool struct {
|
||||
|
||||
type SaveFileInput struct {
|
||||
Name string `json:"name" jsonschema:"file name including extension, for example photo.png or note.pdf"`
|
||||
ContentBase64 string `json:"content_base64" jsonschema:"file contents encoded as base64"`
|
||||
ContentBase64 string `json:"content_base64,omitempty" jsonschema:"file contents encoded as base64; provide this or content_uri, not both"`
|
||||
ContentURI string `json:"content_uri,omitempty" jsonschema:"resource URI of an already-uploaded file, e.g. amcs://files/{id}; use this instead of content_base64 to avoid re-encoding binary content"`
|
||||
MediaType string `json:"media_type,omitempty" jsonschema:"optional MIME type such as image/png, application/pdf, or audio/mpeg"`
|
||||
Kind string `json:"kind,omitempty" jsonschema:"optional logical type such as image, document, audio, or file"`
|
||||
ThoughtID string `json:"thought_id,omitempty" jsonschema:"optional thought id to link this file to"`
|
||||
@@ -68,19 +69,48 @@ func NewFilesTool(db *store.DB, sessions *session.ActiveProjects) *FilesTool {
|
||||
}
|
||||
|
||||
func (t *FilesTool) Save(ctx context.Context, req *mcp.CallToolRequest, in SaveFileInput) (*mcp.CallToolResult, SaveFileOutput, error) {
|
||||
contentBase64, mediaTypeFromDataURL := splitDataURL(strings.TrimSpace(in.ContentBase64))
|
||||
if contentBase64 == "" {
|
||||
return nil, SaveFileOutput{}, errInvalidInput("content_base64 is required")
|
||||
uri := strings.TrimSpace(in.ContentURI)
|
||||
b64 := strings.TrimSpace(in.ContentBase64)
|
||||
|
||||
if uri != "" && b64 != "" {
|
||||
return nil, SaveFileOutput{}, errInvalidInput("provide content_uri or content_base64, not both")
|
||||
}
|
||||
|
||||
content, err := decodeBase64(contentBase64)
|
||||
if err != nil {
|
||||
return nil, SaveFileOutput{}, errInvalidInput("content_base64 must be valid base64")
|
||||
var content []byte
|
||||
var mediaTypeFromSource string
|
||||
|
||||
if uri != "" {
|
||||
if !strings.HasPrefix(uri, fileURIPrefix) {
|
||||
return nil, SaveFileOutput{}, errInvalidInput("content_uri must be an amcs://files/{id} URI")
|
||||
}
|
||||
rawID := strings.TrimPrefix(uri, fileURIPrefix)
|
||||
id, err := parseUUID(rawID)
|
||||
if err != nil {
|
||||
return nil, SaveFileOutput{}, errInvalidInput("content_uri contains an invalid file id")
|
||||
}
|
||||
file, err := t.store.GetStoredFile(ctx, id)
|
||||
if err != nil {
|
||||
return nil, SaveFileOutput{}, errInvalidInput("content_uri references a file that does not exist")
|
||||
}
|
||||
content = file.Content
|
||||
mediaTypeFromSource = file.MediaType
|
||||
} else {
|
||||
contentBase64, mediaTypeFromDataURL := splitDataURL(b64)
|
||||
if contentBase64 == "" {
|
||||
return nil, SaveFileOutput{}, errInvalidInput("content_base64 or content_uri is required")
|
||||
}
|
||||
var err error
|
||||
content, err = decodeBase64(contentBase64)
|
||||
if err != nil {
|
||||
return nil, SaveFileOutput{}, errInvalidInput("content_base64 must be valid base64")
|
||||
}
|
||||
mediaTypeFromSource = mediaTypeFromDataURL
|
||||
}
|
||||
|
||||
out, err := t.SaveDecoded(ctx, req, SaveFileDecodedInput{
|
||||
Name: in.Name,
|
||||
Content: content,
|
||||
MediaType: firstNonEmpty(strings.TrimSpace(in.MediaType), mediaTypeFromDataURL),
|
||||
MediaType: firstNonEmpty(strings.TrimSpace(in.MediaType), mediaTypeFromSource),
|
||||
Kind: in.Kind,
|
||||
ThoughtID: in.ThoughtID,
|
||||
Project: in.Project,
|
||||
@@ -91,6 +121,16 @@ func (t *FilesTool) Save(ctx context.Context, req *mcp.CallToolRequest, in SaveF
|
||||
return nil, out, nil
|
||||
}
|
||||
|
||||
const fileURIPrefix = "amcs://files/"
|
||||
|
||||
func (t *FilesTool) GetRaw(ctx context.Context, rawID string) (thoughttypes.StoredFile, error) {
|
||||
id, err := parseUUID(strings.TrimSpace(rawID))
|
||||
if err != nil {
|
||||
return thoughttypes.StoredFile{}, err
|
||||
}
|
||||
return t.store.GetStoredFile(ctx, id)
|
||||
}
|
||||
|
||||
func (t *FilesTool) Load(ctx context.Context, _ *mcp.CallToolRequest, in LoadFileInput) (*mcp.CallToolResult, LoadFileOutput, error) {
|
||||
id, err := parseUUID(in.ID)
|
||||
if err != nil {
|
||||
@@ -102,12 +142,48 @@ func (t *FilesTool) Load(ctx context.Context, _ *mcp.CallToolRequest, in LoadFil
|
||||
return nil, LoadFileOutput{}, err
|
||||
}
|
||||
|
||||
return nil, LoadFileOutput{
|
||||
uri := fileURIPrefix + file.ID.String()
|
||||
result := &mcp.CallToolResult{
|
||||
Content: []mcp.Content{
|
||||
&mcp.EmbeddedResource{
|
||||
Resource: &mcp.ResourceContents{
|
||||
URI: uri,
|
||||
MIMEType: file.MediaType,
|
||||
Blob: file.Content,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return result, LoadFileOutput{
|
||||
File: file,
|
||||
ContentBase64: base64.StdEncoding.EncodeToString(file.Content),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *FilesTool) ReadResource(ctx context.Context, req *mcp.ReadResourceRequest) (*mcp.ReadResourceResult, error) {
|
||||
rawID := strings.TrimPrefix(req.Params.URI, fileURIPrefix)
|
||||
id, err := parseUUID(strings.TrimSpace(rawID))
|
||||
if err != nil {
|
||||
return nil, mcp.ResourceNotFoundError(req.Params.URI)
|
||||
}
|
||||
|
||||
file, err := t.store.GetStoredFile(ctx, id)
|
||||
if err != nil {
|
||||
return nil, mcp.ResourceNotFoundError(req.Params.URI)
|
||||
}
|
||||
|
||||
return &mcp.ReadResourceResult{
|
||||
Contents: []*mcp.ResourceContents{
|
||||
{
|
||||
URI: req.Params.URI,
|
||||
MIMEType: file.MediaType,
|
||||
Blob: file.Content,
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *FilesTool) List(ctx context.Context, req *mcp.CallToolRequest, in ListFilesInput) (*mcp.CallToolResult, ListFilesOutput, error) {
|
||||
project, err := resolveProject(ctx, t.store, t.sessions, req, in.Project, false)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user