fix: mcp server media sending
This commit is contained in:
1 parent
63b671c6f3
commit
0bdcca8a90
2 files changed
+27
-1
No files matched your search
@@ -6,12 +6,15 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
)
|
||||
@@ -228,7 +231,14 @@ func (s *server) uploadMedia(ctx context.Context, _ *mcp.CallToolRequest, in upl
|
||||
}
|
||||
var form bytes.Buffer
|
||||
writer := multipart.NewWriter(&form)
|
||||
part, err := writer.CreateFormFile("file", name)
|
||||
contentType, err := uploadMediaType(name)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
header := make(textproto.MIMEHeader)
|
||||
header.Set("Content-Disposition", mime.FormatMediaType("form-data", map[string]string{"name": "file", "filename": name}))
|
||||
header.Set("Content-Type", contentType)
|
||||
part, err := writer.CreatePart(header)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -241,6 +251,21 @@ func (s *server) uploadMedia(ctx context.Context, _ *mcp.CallToolRequest, in upl
|
||||
return result(s.client.Do(ctx, http.MethodPost, "/v1/media/", nil, form.Bytes(), writer.FormDataContentType()))
|
||||
}
|
||||
|
||||
func uploadMediaType(filename string) (string, error) {
|
||||
switch strings.ToLower(filepath.Ext(filename)) {
|
||||
case ".png":
|
||||
return "image/png", nil
|
||||
case ".jpg", ".jpeg":
|
||||
return "image/jpeg", nil
|
||||
case ".webp":
|
||||
return "image/webp", nil
|
||||
case ".svg":
|
||||
return "image/svg+xml", nil
|
||||
default:
|
||||
return "", fmt.Errorf("filename must end in .png, .jpg, .jpeg, .webp or .svg")
|
||||
}
|
||||
}
|
||||
|
||||
func uploadContent(in uploadMediaInput) (string, []byte, error) {
|
||||
switch {
|
||||
case in.Path != "" && in.Base64 != "":
|
||||
|
||||
Reference in new issue
Block a user