-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
65 lines (58 loc) · 1.34 KB
/
utils.go
File metadata and controls
65 lines (58 loc) · 1.34 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
package mcp
import (
"github.com/tinywasm/fmt"
"github.com/tinywasm/json"
)
func JSON(data fmt.Fielder) (*Result, error) {
var s string
if err := json.Encode(data, &s); err != nil {
return nil, err
}
return &Result{Content: s}, nil
}
func ParseResult(raw []byte) (*Result, error) {
var result Result
if err := json.Decode(raw, &result); err != nil {
return nil, err
}
return &result, nil
}
func GetText(r *Result) (string, error) {
var c TextContent
if err := json.Decode([]byte(r.Content), &c); err != nil {
return "", err
}
return c.Text, nil
}
func newResultResponse(id RequestId, result any) JSONRPCMessage {
var resJSON string
if f, ok := result.(fmt.Fielder); ok {
json.Encode(f, &resJSON)
}
return &JSONRPCResponseStruct{
JSONRPC: JSONRPC_VERSION,
ID: id,
Result: resJSON,
}
}
func newErrorDetails(code int, message string, data any) *JSONRPCErrorDetails {
var dataJSON string
if f, ok := data.(fmt.Fielder); ok {
json.Encode(f, &dataJSON)
}
return &JSONRPCErrorDetails{
Code: int64(code),
Message: message,
Data: dataJSON,
}
}
func newErrorResponse(id RequestId, code int, message string, data any) JSONRPCMessage {
det := newErrorDetails(code, message, data)
var detJSON string
json.Encode(det, &detJSON)
return &JSONRPCError{
JSONRPC: JSONRPC_VERSION,
ID: id,
Error: detJSON,
}
}