返回 DeepSeek-Reasonix
mcpinteraction.go
根目录 / internal / mcpinteraction / mcpinteraction.go
1 // Package mcpinteraction carries server-initiated MCP elicitation requests
2 // from the plugin transport to whichever frontend is driving the call, and the
3 // user's decision back. It has no UI dependency: the plugin layer hands a
4 // Broker through the per-call context, the controller layer implements it.
5 package mcpinteraction
6
7 import (
8 "context"
9 "encoding/json"
10 )
11
12 // Elicitation modes defined by MCP 2026-07-28.
13 const (
14 ModeForm = "form"
15 ModeURL = "url"
16 )
17
18 // User actions in answer to an elicitation request.
19 const (
20 ActionAccept = "accept"
21 ActionDecline = "decline"
22 ActionCancel = "cancel"
23 )
24
25 // Request is one server-initiated elicitation: a typed form (flat primitive
26 // schema) or a URL the server asks the user to visit.
27 type Request struct {
28 // ID is the host-assigned stable identifier for the pending decision,
29 // echoed by the frontend in the resolve call.
30 ID string
31 // Server is the MCP server asking. Surfaced so the user can see who is
32 // requesting before answering.
33 Server string
34 // Mode is "form" or "url".
35 Mode string
36 // Message is the server's human-readable explanation.
37 Message string
38 // RequestedSchema is the raw JSON schema for form mode. Flat primitives
39 // only (string/number/integer/boolean/enum, defaults, required, bounds).
40 RequestedSchema json.RawMessage
41 // URL is the credential-free HTTP/HTTPS target for url mode.
42 URL string
43 // ElicitationID is the server-assigned id for url mode completion.
44 ElicitationID string
45 }
46
47 // Result is the user's decision. Action is accept/decline/cancel; Content is
48 // the submitted form values, present only for accept in form mode.
49 type Result struct {
50 Action string
51 Content map[string]any
52 }
53
54 // Broker delivers one elicitation to the user and blocks until they answer.
55 // Implementations must respect ctx cancellation. Privacy: brokers must keep
56 // form values, URL query strings, and free-text answers out of logs and
57 // telemetry; only the request kind, action, and error class may be recorded.
58 type Broker interface {
59 Interact(ctx context.Context, req Request) (Result, error)
60 }
61
62 type brokerKey struct{}
63
64 // WithBroker attaches b to ctx for the duration of one MCP call. The broker
65 // travels with the call context — never with the shared plugin.Host — so
66 // concurrent tabs and turns cannot cross wires.
67 func WithBroker(ctx context.Context, b Broker) context.Context {
68 return context.WithValue(ctx, brokerKey{}, b)
69 }
70
71 // FromContext returns the call's broker, or nil when the entry point has no
72 // decision channel (headless/bot). A nil broker means the request must be
73 // answered cancel — the model never guesses.
74 func FromContext(ctx context.Context) Broker {
75 if ctx == nil {
76 return nil
77 }
78 b, _ := ctx.Value(brokerKey{}).(Broker)
79 return b
80 }
81
82 // SanitizeURLMode validates a url-mode request target. Only credential-free
83 // HTTP(S) URLs are presentable; anything else is refused before any UI sees it.
84 func SanitizeURLMode(req Request) bool {
85 if req.Mode != ModeURL {
86 return true
87 }
88 return allowedURL(req.URL)
89 }
90
90 lines GO