-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgateway.go
More file actions
219 lines (202 loc) · 5.6 KB
/
Copy pathgateway.go
File metadata and controls
219 lines (202 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Package gateway is the MCP gateway core. It terminates streamable-HTTP on
// the front, runs every message through the inspection pipeline, then routes
// to one or more backends (aggregating with tool prefixes when several).
package gateway
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"log/slog"
"net/http"
"sync"
"time"
"github.com/hoophq/mcpproxy/audit"
"github.com/hoophq/mcpproxy/backend"
"github.com/hoophq/mcpproxy/inspect"
"github.com/hoophq/mcpproxy/session"
)
// IdentityResolver authenticates an HTTP request (inbound auth). It returns
// the caller identity or an error that terminates the request. The gateway
// stays auth-agnostic: the daemon (or hoop) supplies the resolver.
type IdentityResolver func(r *http.Request) (inspect.Identity, error)
// Options assemble a Gateway.
type Options struct {
// Backends: name → factory. Names are tool prefixes in aggregation.
Backends map[string]backend.Factory
// Pipeline is the ordered check chain applied to every message.
Pipeline []inspect.Check
// Resolver authenticates requests. nil = anonymous.
Resolver IdentityResolver
// Sink receives audit events. nil = discard.
Sink audit.Sink
// SessionIdleTimeout reaps sessions with no traffic. 0 = 30m.
SessionIdleTimeout time.Duration
// RequestTimeout bounds one round-trip to a backend. 0 = 5m, since
// slow tool calls are normal. Approvals hold longer via ctx.
RequestTimeout time.Duration
// Held is the approval parking lot; nil disables Hold verdicts
// (they become Deny).
Held HeldStore
// Observer receives per-message timing/decision telemetry. Optional.
Observer Observer
Logger *slog.Logger
}
// Observer is the telemetry seam: one call per gateway-processed message.
type Observer interface {
ObserveMessage(dir inspect.Direction, backendName, method, tool, decision string, dur time.Duration)
}
// HeldStore parks messages awaiting human approval.
type HeldStore interface {
// Hold parks a call and returns a resolution channel. The channel
// yields true (approved) or false (rejected) exactly once.
Hold(ctx context.Context, h HeldCall) (id string, resolved <-chan bool, err error)
}
// HeldCall describes one parked tool call.
type HeldCall struct {
Session string
User string
Backend string
Tool string
Args []byte
Rule string
}
// Gateway is the shared front for all sessions.
type Gateway struct {
opts Options
log *slog.Logger
sink audit.Sink
mu sync.Mutex
sess map[string]*conn
closed bool
}
// New builds a Gateway.
func New(opts Options) (*Gateway, error) {
if len(opts.Backends) == 0 {
return nil, fmt.Errorf("gateway requires at least one backend")
}
if opts.Sink == nil {
opts.Sink = audit.Discard
}
if opts.Logger == nil {
opts.Logger = slog.Default()
}
if opts.SessionIdleTimeout == 0 {
opts.SessionIdleTimeout = 30 * time.Minute
}
if opts.RequestTimeout == 0 {
opts.RequestTimeout = 5 * time.Minute
}
g := &Gateway{
opts: opts,
log: opts.Logger,
sink: opts.Sink,
sess: map[string]*conn{},
}
go g.reaper()
return g, nil
}
// aggregated reports whether tool-prefix aggregation is active.
func (g *Gateway) aggregated() bool { return len(g.opts.Backends) > 1 }
func newSessionID() string {
b := make([]byte, 16)
_, _ = rand.Read(b)
return hex.EncodeToString(b)
}
func (g *Gateway) getSession(id string) *conn {
g.mu.Lock()
defer g.mu.Unlock()
return g.sess[id]
}
func (g *Gateway) putSession(c *conn) {
g.mu.Lock()
defer g.mu.Unlock()
g.sess[c.state.ID()] = c
}
func (g *Gateway) dropSession(id string) {
g.mu.Lock()
c := g.sess[id]
delete(g.sess, id)
g.mu.Unlock()
if c != nil {
c.shutdown(nil)
}
}
// Close terminates all sessions.
func (g *Gateway) Close() {
g.mu.Lock()
if g.closed {
g.mu.Unlock()
return
}
g.closed = true
conns := make([]*conn, 0, len(g.sess))
for _, c := range g.sess {
conns = append(conns, c)
}
g.sess = map[string]*conn{}
g.mu.Unlock()
for _, c := range conns {
c.shutdown(nil)
}
}
func (g *Gateway) reaper() {
t := time.NewTicker(time.Minute)
defer t.Stop()
for range t.C {
g.mu.Lock()
if g.closed {
g.mu.Unlock()
return
}
var stale []*conn
for id, c := range g.sess {
if time.Since(c.lastSeen()) > g.opts.SessionIdleTimeout {
stale = append(stale, c)
delete(g.sess, id)
}
}
g.mu.Unlock()
for _, c := range stale {
g.log.Info("reaping idle session", "sid", c.state.ID())
c.shutdown(nil)
}
}
}
// startSession spawns backends and pumps for a new session.
func (g *Gateway) startSession(ctx context.Context, identity inspect.Identity) (*conn, error) {
st := session.New(newSessionID(), identity)
c := &conn{
g: g,
state: st,
backends: map[string]*backendConn{},
s2c: make(chan []byte, 64),
done: make(chan struct{}),
}
c.touch()
// Backends live as long as the session, not the initialize request.
// ctx here is the POST's request context and cancels the moment the
// handler writes the response, which would kill stdio children
// mid-session. conn.shutdown tears the session down instead.
bctx := context.WithoutCancel(ctx)
for name, factory := range g.opts.Backends {
b, err := factory(bctx)
if err != nil {
c.shutdown(nil)
return nil, fmt.Errorf("backend %s: %w", name, err)
}
if err := b.Start(bctx); err != nil {
c.shutdown(nil)
return nil, fmt.Errorf("starting backend %s: %w", name, err)
}
bc := &backendConn{name: name, b: b, waiters: map[string]chan []byte{}}
c.backends[name] = bc
go c.pump(bc)
}
g.putSession(c)
g.sink.Emit(ctx, audit.Event{
Time: time.Now(), Type: audit.EventSessionStart,
Session: st.ID(), User: identity.Subject,
})
return c, nil
}