feat(files): update save_file tool description and enforce size limit for base64 payloads
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user