返回 DeepSeek-Reasonix
usecapability_mcp_arguments.go
根目录 / internal / agent / usecapability_mcp_arguments.go
1 package agent
2
3 import (
4 "encoding/json"
5 "fmt"
6 "strings"
7 )
8
9 // capabilityInputError distinguishes repairable envelope errors from target
10 // availability and authorization failures without changing their error text.
11 type capabilityInputError struct{ error }
12
13 func (e *capabilityInputError) Unwrap() error { return e.error }
14 func capabilityInputErrorf(format string, args ...any) error {
15 return &capabilityInputError{fmt.Errorf(format, args...)}
16 }
17
18 type useCapabilityArgs struct {
19 Action string `json:"action"`
20 CapabilityID string `json:"capability_id"`
21 Query string `json:"query"`
22 Limit int `json:"limit"`
23 Cursor string `json:"cursor"`
24 Arguments json.RawMessage `json:"arguments"`
25 Reason string `json:"reason"`
26 }
27
28 func parseUseCapabilityArgs(raw json.RawMessage) (useCapabilityArgs, string, string, error) {
29 var args useCapabilityArgs
30 if err := json.Unmarshal(raw, &args); err != nil {
31 return args, "", "", fmt.Errorf("invalid args: %w", err)
32 }
33 action := strings.ToLower(strings.TrimSpace(args.Action))
34 id := strings.TrimSpace(args.CapabilityID)
35 if args.Limit < 0 || action == "search" && args.Limit > 8 || action == "list" && args.Limit > 100 {
36 return args, "", "", fmt.Errorf("limit must be 1..8 for search or 1..100 for list when provided")
37 }
38 if action == "call" && strings.HasPrefix(id, "mcp-tool:") {
39 normalized, err := normalizeMCPToolArguments(args.Arguments)
40 if err != nil {
41 return args, "", "", err
42 }
43 args.Arguments = normalized
44 }
45 return args, action, id, nil
46 }
47
48 // normalizeMCPToolArguments accepts only an object. It deliberately does not
49 // unwrap JSON strings, rename fields, coerce values, or guess enums: schema
50 // mistakes must produce one precise repair contract instead of hidden behavior
51 // that differs between direct and proxied MCP calls.
52 func normalizeMCPToolArguments(raw json.RawMessage) (json.RawMessage, error) {
53 trimmed := strings.TrimSpace(string(raw))
54 if trimmed == "" || trimmed == "null" {
55 return json.RawMessage(`{}`), nil
56 }
57 var object map[string]any
58 if !strings.HasPrefix(trimmed, "{") || json.Unmarshal([]byte(trimmed), &object) != nil || object == nil {
59 return nil, fmt.Errorf("arguments for an MCP tool must be a JSON object; arrays, scalars, malformed JSON, and nested JSON strings are not supported")
60 }
61 return json.RawMessage(append([]byte(nil), trimmed...)), nil
62 }
63
63 lines GO