Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ docker compose up -d # Run with PostgreSQL + Streamable HTTP (via supergatewa
- **Domain errors → tool result errors** (not transport errors) — MCP client gets structured messages
- **Collision resolution** — base word, then word2..word999, all within a transaction
- **Auth per-transport** — stdio validates WORDSTORE_AUTH_TOKEN once at startup; SSE uses HTTP middleware
- **S3 presigned URLs + optional proxy** — the `upload_file`/`download_file` MCP tools only return URLs, never file bytes. They always include presigned S3 PUT/GET URLs. When `s3.proxy` is configured they also include HMAC-signed `proxy_upload_url`/`proxy_url`, and then the server **does** carry file data: `GET /dl` streams S3 GetObject → client, `PUT /ul` streams client → S3 PutObject. Both must stay streaming (`io.Copy`, no `io.ReadAll`/whole-file buffers); the container runs with a 128Mi memory limit. `/dl` records completed downloads in `download_history` in the background with a detached context (`context.WithoutCancel` + timeout), because the request context is cancelled once the handler returns. File tools are registered only when S3 is configured
- **S3 presigned URLs + optional proxy** — the `upload_file`/`download_file` MCP tools only return URLs, never file bytes. They always include presigned S3 PUT/GET URLs. When `s3.proxy` is configured they also include HMAC-signed `proxy_upload_url`/`proxy_url`, and then the server **does** carry file data: `GET /dl` streams S3 GetObject → client, `PUT /ul` streams client → S3 PutObject. Both must stay streaming (`io.Copy`, no `io.ReadAll`/whole-file buffers); the container runs with a 128Mi memory limit. `/dl` records completed downloads in `download_history` in the background with a detached context (`context.WithoutCancel` + timeout), because the request context is cancelled once the handler returns. The presigned URLs are the primary path and the proxy URLs only a fallback for sandboxed clients that cannot reach S3: the `hint` and the tool descriptions put the presigned curl command first and the proxy command in a separate fallback sentence, because the proxy goes through Cloudflare (orders of magnitude slower, ~100 MB request-body limit on uploads) — note that `download_history` therefore only records proxied downloads. File tools are registered only when S3 is configured
- **Compact list/search responses** — `list_entries`, `search_entries`, `search_words` omit payload to reduce token usage; `get_entry`/`get_entry_by_word` return full content
- **delete_entry accepts UUID or word** — tries UUID parse first, falls back to word lookup
- **entry_type column** — `text` (default) or `file`; file entries store JSON metadata in payload field
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ var defaultToolsConfig = ToolsConfig{
},
},
UploadFile: ToolDesc{
Description: "Upload a file via S3 presigned URL. Returns a presigned PUT URL and a keyword. The file data is NOT sent through this tool — instead, use the returned URL with curl or fetch to PUT the file directly to S3. Example: curl -X PUT -T myfile.pdf '<presigned_url>'. Same collision resolution as store_entry. IMPORTANT: Always tell the user the RETURNED word.",
Description: "Upload a file via S3 presigned URL. Returns a presigned PUT URL (upload_url) and a keyword. The file data is NOT sent through this tool — instead, use the returned URL with curl or fetch to PUT the file directly to S3. upload_url is the primary, fast path: always try it first, with the Content-Type header from the hint. Example: curl -X PUT -H 'Content-Type: application/pdf' -T myfile.pdf '<upload_url>'. proxy_upload_url (present only when the server proxy is enabled) is a fallback for sandboxes that cannot reach S3 (amazonaws.com); it goes through Cloudflare and is limited to ~100 MB. Same collision resolution as store_entry. IMPORTANT: Always tell the user the RETURNED word.",
Properties: map[string]string{
"word": "A memorable keyword for this file.",
"filename": "Original filename (e.g., 'report.pdf'). Used as part of the S3 key and returned on download.",
Expand All @@ -146,7 +146,7 @@ var defaultToolsConfig = ToolsConfig{
},
},
DownloadFile: ToolDesc{
Description: "Get a presigned download URL for a file entry. Returns a presigned GET URL, filename, and content type. The file data is NOT sent through this tool — instead, use the returned URL with curl or fetch to GET the file directly from S3. Example: curl -o '<filename>' '<presigned_url>'.",
Description: "Get a presigned download URL for a file entry. Returns a presigned GET URL (download_url), filename, and content type. The file data is NOT sent through this tool — instead, use the returned URL with curl or fetch to GET the file directly from S3. download_url is the primary, fast path: always try it first. Example: curl -o '<filename>' '<download_url>'. proxy_url (present only when the server proxy is enabled) is a fallback for sandboxes that cannot reach S3 (amazonaws.com); it is much slower because it goes through Cloudflare.",
Properties: map[string]string{
"word": "The keyword of the file entry to download.",
},
Expand Down
4 changes: 2 additions & 2 deletions internal/service/file_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (s *FileService) UploadFile(ctx context.Context, word string, filename stri
}
if s.proxySigner != nil {
result.ProxyUploadURL = s.proxySigner.SignUpload(created.ID.String(), filename)
result.Hint = fmt.Sprintf("Upload your file with: curl -X PUT -H 'Content-Type: %s' -T '%s' '%s' (or use proxy_upload_url if the presigned URL is not accessible)", contentType, filename, result.ProxyUploadURL)
result.Hint += fmt.Sprintf(". If S3 (amazonaws.com) is not reachable from your environment, use proxy_upload_url instead (limited to ~100 MB, it goes through Cloudflare): curl -X PUT -H 'Content-Type: %s' -T '%s' '%s'", contentType, filename, result.ProxyUploadURL)
}
if expiresAt != nil {
result.ExpiresAt = expiresAt.Format(time.RFC3339)
Expand Down Expand Up @@ -226,7 +226,7 @@ func (s *FileService) DownloadFile(ctx context.Context, word string) (*DownloadR

if s.proxySigner != nil {
result.ProxyURL = s.proxySigner.SignDownload(entry.ID.String(), meta.Filename)
result.Hint = fmt.Sprintf("Download with: curl -o '%s' '%s' (or use the proxy_url if the presigned URL is not accessible)", meta.Filename, result.ProxyURL)
result.Hint += fmt.Sprintf(". If S3 (amazonaws.com) is not reachable from your environment, use proxy_url instead: curl -o '%s' '%s'", meta.Filename, result.ProxyURL)
}

return result, nil
Expand Down
107 changes: 107 additions & 0 deletions internal/service/file_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log/slog"
"os"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -218,3 +219,109 @@ func mustParseUUID(t *testing.T, s string) uuid.UUID {
}
return id
}

type fakeProxySigner struct{}

func (fakeProxySigner) SignDownload(entryID, filename string) string {
return "https://proxy.example/dl?id=" + entryID + "&f=" + filename
}

func (fakeProxySigner) SignUpload(entryID, filename string) string {
return "https://proxy.example/ul?id=" + entryID + "&f=" + filename
}

// fallbackMarker separates the primary command in a hint from the proxy fallback.
const fallbackMarker = "If S3 (amazonaws.com) is not reachable"

// splitHint returns the primary part of a hint and the fallback part ("" if none).
func splitHint(hint string) (primary, fallback string) {
if i := strings.Index(hint, fallbackMarker); i >= 0 {
return hint[:i], hint[i:]
}
return hint, ""
}

func TestUploadFile_Hint_PresignedFirst(t *testing.T) {
for _, withProxy := range []bool{false, true} {
t.Run(fmt.Sprintf("proxy=%v", withProxy), func(t *testing.T) {
svc, _, _ := newTestFileService(t)
if withProxy {
svc.SetProxySigner(fakeProxySigner{})
}
result, err := svc.UploadFile(context.Background(), "otter", "big.bin", "application/zip", nil)
if err != nil {
t.Fatalf("UploadFile: %v", err)
}
primary, fallback := splitHint(result.Hint)
wantPrimary := "curl -X PUT -H 'Content-Type: application/zip' -T 'big.bin' '" + result.UploadURL + "'"
if !strings.Contains(primary, wantPrimary) {
t.Errorf("primary hint %q does not contain %q", primary, wantPrimary)
}
if strings.Contains(primary, "proxy.example") {
t.Errorf("primary hint must not contain the proxy URL: %q", primary)
}
if !withProxy {
if result.ProxyUploadURL != "" || fallback != "" || strings.Contains(result.Hint, "proxy") {
t.Errorf("no proxy configured, but hint/result mention one: %q", result.Hint)
}
return
}
if result.ProxyUploadURL == "" {
t.Fatal("expected proxy_upload_url")
}
wantFallback := "curl -X PUT -H 'Content-Type: application/zip' -T 'big.bin' '" + result.ProxyUploadURL + "'"
if !strings.Contains(fallback, "proxy_upload_url") || !strings.Contains(fallback, wantFallback) {
t.Errorf("fallback hint %q should name proxy_upload_url and contain %q", fallback, wantFallback)
}
if !strings.Contains(fallback, "100 MB") {
t.Errorf("fallback hint should mention the ~100 MB limit: %q", fallback)
}
if strings.Contains(fallback, result.UploadURL) {
t.Errorf("fallback hint must not contain the presigned URL: %q", fallback)
}
})
}
}

func TestDownloadFile_Hint_PresignedFirst(t *testing.T) {
for _, withProxy := range []bool{false, true} {
t.Run(fmt.Sprintf("proxy=%v", withProxy), func(t *testing.T) {
svc, _, _ := newTestFileService(t)
if withProxy {
svc.SetProxySigner(fakeProxySigner{})
}
ctx := context.Background()
if _, err := svc.UploadFile(ctx, "heron", "video.mp4", "video/mp4", nil); err != nil {
t.Fatalf("UploadFile: %v", err)
}
result, err := svc.DownloadFile(ctx, "heron")
if err != nil {
t.Fatalf("DownloadFile: %v", err)
}
primary, fallback := splitHint(result.Hint)
wantPrimary := "curl -o 'video.mp4' '" + result.DownloadURL + "'"
if !strings.Contains(primary, wantPrimary) {
t.Errorf("primary hint %q does not contain %q", primary, wantPrimary)
}
if strings.Contains(primary, "proxy.example") {
t.Errorf("primary hint must not contain the proxy URL: %q", primary)
}
if !withProxy {
if result.ProxyURL != "" || fallback != "" || strings.Contains(result.Hint, "proxy") {
t.Errorf("no proxy configured, but hint/result mention one: %q", result.Hint)
}
return
}
if result.ProxyURL == "" {
t.Fatal("expected proxy_url")
}
wantFallback := "curl -o 'video.mp4' '" + result.ProxyURL + "'"
if !strings.Contains(fallback, "proxy_url") || !strings.Contains(fallback, wantFallback) {
t.Errorf("fallback hint %q should name proxy_url and contain %q", fallback, wantFallback)
}
if strings.Contains(fallback, result.DownloadURL) {
t.Errorf("fallback hint must not contain the presigned URL: %q", fallback)
}
})
}
}
Loading