| 1 | package plugin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "sync" |
| 7 | |
| 8 | "reasonix/internal/mcpinteraction" |
| 9 | ) |
| 10 | |
| 11 | type legacyElicitationCall struct { |
| 12 | ctx context.Context |
| 13 | broker mcpinteraction.Broker |
| 14 | } |
| 15 | |
| 16 | func (t *sdkSessionTransport) invokeManaged(ctx context.Context, managed *managedMCPSession, method string, params any) (json.RawMessage, error) { |
| 17 | unregister := t.registerLegacyElicitationCall(ctx, managed.protocol, method) |
| 18 | defer unregister() |
| 19 | return invokeSDKMethod(ctx, managed.session, method, params) |
| 20 | } |
| 21 | |
| 22 | // registerLegacyElicitationCall covers the push-style elicitation/create flow |
| 23 | // used before 2026-07-28. Legacy JSON-RPC requests arrive on the session |
| 24 | // context, not the originating tools/call context, so every active legacy call |
| 25 | // is tracked. The handler only routes a decision when exactly one call is in |
| 26 | // flight; ambiguity cancels instead of crossing tabs or headless callers. |
| 27 | func (t *sdkSessionTransport) registerLegacyElicitationCall(ctx context.Context, protocol, method string) func() { |
| 28 | if method != "tools/call" || protocol == "" || protocol >= "2026-07-28" { |
| 29 | return func() {} |
| 30 | } |
| 31 | if ctx == nil { |
| 32 | ctx = context.Background() |
| 33 | } |
| 34 | t.legacyElicitationMu.Lock() |
| 35 | t.legacyElicitationNext++ |
| 36 | id := t.legacyElicitationNext |
| 37 | if t.legacyElicitation == nil { |
| 38 | t.legacyElicitation = map[uint64]legacyElicitationCall{} |
| 39 | } |
| 40 | t.legacyElicitation[id] = legacyElicitationCall{ctx: ctx, broker: mcpinteraction.FromContext(ctx)} |
| 41 | t.legacyElicitationMu.Unlock() |
| 42 | var once sync.Once |
| 43 | return func() { |
| 44 | once.Do(func() { |
| 45 | t.legacyElicitationMu.Lock() |
| 46 | delete(t.legacyElicitation, id) |
| 47 | t.legacyElicitationMu.Unlock() |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func (t *sdkSessionTransport) unambiguousLegacyElicitation() (mcpinteraction.Broker, context.Context, bool) { |
| 53 | t.legacyElicitationMu.Lock() |
| 54 | defer t.legacyElicitationMu.Unlock() |
| 55 | if len(t.legacyElicitation) != 1 { |
| 56 | return nil, nil, false |
| 57 | } |
| 58 | for _, call := range t.legacyElicitation { |
| 59 | if call.broker == nil || call.ctx == nil || call.ctx.Err() != nil { |
| 60 | return nil, nil, false |
| 61 | } |
| 62 | return call.broker, call.ctx, true |
| 63 | } |
| 64 | return nil, nil, false |
| 65 | } |
| 66 |