feat(files): update save_file tool description and enforce size limit for base64 payloads

This commit is contained in:
2026-03-31 00:20:36 +02:00
parent 4bd3c4e0ba
commit 3819eb4fee
3 changed files with 26 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ package mcpserver
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"reflect"
@@ -12,6 +13,8 @@ import (
"github.com/modelcontextprotocol/go-sdk/mcp"
)
const maxLoggedArgBytes = 512
var toolSchemaOptions = &jsonschema.ForOptions{
TypeSchemas: map[reflect.Type]*jsonschema.Schema{
reflect.TypeFor[uuid.UUID](): {
@@ -37,7 +40,7 @@ func logToolCall[In any, Out any](logger *slog.Logger, toolName string, handler
start := time.Now()
attrs := []any{slog.String("tool", toolName)}
if req != nil && req.Params != nil {
attrs = append(attrs, slog.Any("arguments", req.Params.Arguments))
attrs = append(attrs, slog.String("arguments", truncateArgs(req.Params.Arguments)))
}
logger.Info("mcp tool started", attrs...)
@@ -56,6 +59,17 @@ func logToolCall[In any, Out any](logger *slog.Logger, toolName string, handler
}
}
func truncateArgs(args any) string {
b, err := json.Marshal(args)
if err != nil {
return "<unserializable>"
}
if len(b) <= maxLoggedArgBytes {
return string(b)
}
return string(b[:maxLoggedArgBytes]) + fmt.Sprintf("… (%d bytes total)", len(b))
}
func setToolSchemas[In any, Out any](tool *mcp.Tool) error {
if tool.InputSchema == nil {
inputSchema, err := jsonschema.For[In](toolSchemaOptions)

View File

@@ -136,7 +136,7 @@ func New(cfg config.MCPConfig, logger *slog.Logger, toolSet ToolSet) http.Handle
addTool(server, logger, &mcp.Tool{
Name: "save_file",
Description: "Store a base64-encoded file such as an image, document, or audio clip, optionally linking it to a thought.",
Description: "Store a file and optionally link it to a thought. Supply either content_base64 (≤10 MB) or content_uri (amcs://files/{id} from a prior POST /files upload). For files larger than 10 MB, upload via POST /files first and pass the returned URI as content_uri.",
}, toolSet.Files.Save)
addTool(server, logger, &mcp.Tool{