-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonrpc.lua
More file actions
188 lines (159 loc) · 5.08 KB
/
jsonrpc.lua
File metadata and controls
188 lines (159 loc) · 5.08 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
-- JSON-RPC 2.0 codec for MCP server
-- Pure protocol plumbing: encode/decode JSON-RPC envelopes
-- Entry kind: library.lua
local json = require("json")
-- JSON-RPC 2.0 version constant
local JSONRPC_VERSION = "2.0"
-- Standard JSON-RPC error codes
local PARSE_ERROR = -32700
local INVALID_REQUEST = -32600
local METHOD_NOT_FOUND = -32601
local INVALID_PARAMS = -32602
local INTERNAL_ERROR = -32603
local SERVER_ERROR = -32000
---------------------------------------------------------------------------
-- Encoding: table → JSON string
---------------------------------------------------------------------------
--- Encode a JSON-RPC success response
local function encode_response(id, result)
local msg = {
jsonrpc = JSONRPC_VERSION,
id = id,
result = result
}
local encoded = json.encode(msg)
-- Workaround: Wippy's json.encode turns empty tables into [].
-- MCP requires {} for empty objects (ping result, capabilities, inputSchema properties, etc.).
-- Replace all empty arrays that follow a key — these are always intended as empty objects.
encoded = string.gsub(encoded, ":%[%]", ":{}")
return encoded
end
--- Encode a JSON-RPC error response
local function encode_error(id, code, message, data)
local err_obj = {
code = code,
message = message
}
if data ~= nil then
err_obj.data = data
end
local msg = {
jsonrpc = JSONRPC_VERSION,
id = id,
error = err_obj
}
return json.encode(msg)
end
--- Encode a JSON-RPC notification (no id, no response expected)
local function encode_notification(method, params)
local msg = {
jsonrpc = JSONRPC_VERSION,
method = method
}
if params ~= nil then
msg.params = params
end
return json.encode(msg)
end
---------------------------------------------------------------------------
-- Decoding: JSON string → classified table
---------------------------------------------------------------------------
--- Classify a pre-parsed JSON-RPC table (validates and tags as request/notification/invalid)
local function classify(data)
if data.jsonrpc ~= JSONRPC_VERSION then
return {
kind = "invalid",
error = "Missing or invalid jsonrpc version"
}
end
if type(data.method) ~= "string" then
return {
kind = "invalid",
error = "Missing or invalid method field"
}
end
local params = data.params
if params == nil then
params = {}
elseif type(params) ~= "table" then
return {
kind = "invalid",
error = "params must be an object"
}
end
if data.id ~= nil then
return {
kind = "request",
id = data.id,
method = data.method,
params = params
}
else
return {
kind = "notification",
method = data.method,
params = params
}
end
end
--- Decode a JSON-RPC line and classify it
local function decode(line)
local data, err = json.decode(line)
if err then
return {
kind = "invalid",
error = "Parse error: " .. tostring(err)
}
end
if type(data) ~= "table" then
return {
kind = "invalid",
error = "Expected JSON object, got " .. type(data)
}
end
return classify(data)
end
---------------------------------------------------------------------------
-- Error helper constructors
---------------------------------------------------------------------------
local function parse_error(id, message)
return encode_error(id, PARSE_ERROR, message or "Parse error")
end
local function invalid_request(id, message)
return encode_error(id, INVALID_REQUEST, message or "Invalid request")
end
local function method_not_found(id, message)
return encode_error(id, METHOD_NOT_FOUND, message or "Method not found")
end
local function invalid_params(id, message)
return encode_error(id, INVALID_PARAMS, message or "Invalid params")
end
local function internal_error(id, message)
return encode_error(id, INTERNAL_ERROR, message or "Internal error")
end
local function server_error(id, message)
return encode_error(id, SERVER_ERROR, message or "Server error")
end
---------------------------------------------------------------------------
-- Module exports
---------------------------------------------------------------------------
return {
JSONRPC_VERSION = JSONRPC_VERSION,
PARSE_ERROR = PARSE_ERROR,
INVALID_REQUEST = INVALID_REQUEST,
METHOD_NOT_FOUND = METHOD_NOT_FOUND,
INVALID_PARAMS = INVALID_PARAMS,
INTERNAL_ERROR = INTERNAL_ERROR,
SERVER_ERROR = SERVER_ERROR,
encode_response = encode_response,
encode_error = encode_error,
encode_notification = encode_notification,
decode = decode,
classify = classify,
parse_error = parse_error,
invalid_request = invalid_request,
method_not_found = method_not_found,
invalid_params = invalid_params,
internal_error = internal_error,
server_error = server_error
}