-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotify.go
48 lines (39 loc) · 955 Bytes
/
notify.go
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
package mcp
import (
"context"
"encoding/json"
)
func notify[P any](ctx context.Context, c *base, method string, req *Request[P]) error {
var interceptor Interceptor
if len(c.interceptors) > 0 {
interceptor = newStack(c.interceptors)
} else {
interceptor = UnaryInterceptorFunc(
func(next UnaryFunc) UnaryFunc {
return UnaryFunc(func(ctx context.Context, request AnyRequest) (AnyResponse, error) {
return next(ctx, request)
})
},
)
}
inner := UnaryFunc(func(ctx context.Context, request AnyRequest) (AnyResponse, error) {
rawmsg, err := json.Marshal(req.Params)
if err != nil {
return nil, err
}
msgVersion := "2.0"
msgParams := json.RawMessage(rawmsg)
msg := &Message{
JsonRPC: &msgVersion,
Method: &method,
Params: &msgParams,
}
return nil, c.stream.Send(msg)
})
req.method = method
_, err := interceptor.WrapUnary(inner)(ctx, req)
if err != nil {
return err
}
return nil
}