fix: mcp server media sending

This commit is contained in:
Florian Sylvain committed 2026-10-31 16:54:34 +01:00
1 parent 63b671c6f3
commit 0bdcca8a90
2 files changed
+27 -1

No files matched your search

+1
View File
@@ -29,6 +29,7 @@ CLAUDE.md
AGENTS.md AGENTS.md
codex-workflow.md codex-workflow.md
CODING_STANDARDS.md CODING_STANDARDS.md
tools/product-videos/
# Generated LLM docs # Generated LLM docs
internal/infrastructure/llmdocs/generated/* internal/infrastructure/llmdocs/generated/*
+26 -1
View File
@@ -6,12 +6,15 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"mime"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"net/textproto"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
"github.com/modelcontextprotocol/go-sdk/mcp" "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 var form bytes.Buffer
writer := multipart.NewWriter(&form) 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 { if err != nil {
return nil, nil, err 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())) 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) { func uploadContent(in uploadMediaInput) (string, []byte, error) {
switch { switch {
case in.Path != "" && in.Base64 != "": case in.Path != "" && in.Base64 != "":