MCPcopy Create free account
hub / github.com/GongShichen/LuminaCode / SerializeMessage

Function SerializeMessage

mcp/protocol.go:46–80  ·  view source on GitHub ↗
(msg any)

Source from the content-addressed store, hash-verified

44}
45
46func SerializeMessage(msg any) string {
47 pairs := []jsonPair{{Key: "jsonrpc", Value: "2.0"}}
48 setJSONRPC := func(value string) {
49 pairs[0].Value = firstNonEmptyString(value, "2.0")
50 }
51 switch m := msg.(type) {
52 case JSONRPCRequest:
53 setJSONRPC(m.JSONRPC)
54 pairs = append(pairs, jsonPair{Key: "id", Value: m.ID}, jsonPair{Key: "method", Value: m.Method})
55 if m.Params != nil {
56 pairs = append(pairs, jsonPair{Key: "params", Value: m.Params})
57 }
58 case JSONRPCResponse:
59 setJSONRPC(m.JSONRPC)
60 pairs = append(pairs, jsonPair{Key: "id", Value: m.ID}, jsonPair{Key: "result", Value: m.Result})
61 case JSONRPCError:
62 setJSONRPC(m.JSONRPC)
63 pairs = append(pairs, jsonPair{Key: "id", Value: m.ID}, jsonPair{Key: "error", Value: m.Error})
64 case JSONRPCNotification:
65 setJSONRPC(m.JSONRPC)
66 pairs = append(pairs, jsonPair{Key: "method", Value: m.Method})
67 if m.Params != nil {
68 pairs = append(pairs, jsonPair{Key: "params", Value: m.Params})
69 }
70 case *JSONRPCRequest:
71 return SerializeMessage(*m)
72 case *JSONRPCResponse:
73 return SerializeMessage(*m)
74 case *JSONRPCError:
75 return SerializeMessage(*m)
76 case *JSONRPCNotification:
77 return SerializeMessage(*m)
78 }
79 return marshalJSONPairs(pairs)
80}
81
82func ParseMessage(data string) any {
83 var obj map[string]any

Calls 2

marshalJSONPairsFunction · 0.85
firstNonEmptyStringFunction · 0.70