-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmcp_client_features.lua
More file actions
745 lines (627 loc) · 19.4 KB
/
Copy pathmcp_client_features.lua
File metadata and controls
745 lines (627 loc) · 19.4 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
--[[
MCP Client Features
Implements server-initiated requests to the client:
- Sampling: Request LLM completions from the client
- Elicitation: Request user input through the client
- Logging: Send log messages to the client
These features require support from the MCP client (host application).
The server stores client capabilities during initialization and only
uses features the client has declared support for.
--]]
local logger = require("logger")
local rapidjson = require("rapidjson")
local MCPClientFeatures = {
-- Client capabilities (set during initialization)
client_capabilities = {},
-- Callback for sending requests/notifications to the client
-- Set by the relay or server module
sendToClient = nil,
-- Request ID counter for JSON-RPC
_request_id = 0,
-- Pending requests awaiting response
_pending_requests = {},
-- Log level filter (client can set this)
log_level = "debug",
}
-- Log level severity (RFC 5424)
local LOG_LEVELS = {
debug = 1,
info = 2,
notice = 3,
warning = 4,
error = 5,
critical = 6,
alert = 7,
emergency = 8,
}
function MCPClientFeatures:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
o.client_capabilities = {}
o._pending_requests = {}
o._request_id = 0
o.log_level = "debug"
return o
end
--------------------------------------------------------------------------------
-- Capability Management
--------------------------------------------------------------------------------
-- Store client capabilities from initialization
function MCPClientFeatures:setClientCapabilities(capabilities)
self.client_capabilities = capabilities or {}
logger.dbg("MCP: Client capabilities set:", rapidjson.encode(self.client_capabilities))
end
-- Check if client supports sampling
function MCPClientFeatures:supportsSampling()
return self.client_capabilities.sampling ~= nil
end
-- Check if client supports sampling with tools
function MCPClientFeatures:supportsSamplingWithTools()
return self.client_capabilities.sampling
and self.client_capabilities.sampling.tools ~= nil
end
-- Check if client supports elicitation
function MCPClientFeatures:supportsElicitation()
return self.client_capabilities.elicitation ~= nil
end
-- Check if client supports form mode elicitation
function MCPClientFeatures:supportsFormElicitation()
if not self.client_capabilities.elicitation then
return false
end
-- Empty elicitation capability means form mode only (backward compat)
local elicit = self.client_capabilities.elicitation
return elicit.form ~= nil or (elicit.form == nil and elicit.url == nil)
end
-- Check if client supports URL mode elicitation
function MCPClientFeatures:supportsUrlElicitation()
return self.client_capabilities.elicitation
and self.client_capabilities.elicitation.url ~= nil
end
-- Check if client supports roots
function MCPClientFeatures:supportsRoots()
return self.client_capabilities.roots ~= nil
end
-- Set the callback function for sending messages to client
function MCPClientFeatures:setSendCallback(callback)
self.sendToClient = callback
end
--------------------------------------------------------------------------------
-- Logging (Server → Client notifications)
--------------------------------------------------------------------------------
-- Set minimum log level (called when client sends logging/setLevel)
function MCPClientFeatures:setLogLevel(level)
if LOG_LEVELS[level] then
self.log_level = level
logger.info("MCP: Log level set to", level)
return true
end
return false, "Invalid log level"
end
-- Send a log message notification to the client
-- level: debug, info, notice, warning, error, critical, alert, emergency
-- loggerName: optional logger name for filtering
-- data: any JSON-serializable data
function MCPClientFeatures:log(level, loggerName, data)
-- Check if level passes filter
local level_num = LOG_LEVELS[level] or 2
local filter_num = LOG_LEVELS[self.log_level] or 1
if level_num < filter_num then
return -- Filtered out
end
if not self.sendToClient then
return -- No way to send
end
local notification = {
jsonrpc = "2.0",
method = "notifications/message",
params = {
level = level,
logger = loggerName or "koreader-mcp",
data = data,
},
}
self.sendToClient(notification)
end
-- Convenience logging methods
function MCPClientFeatures:logDebug(loggerName, data)
self:log("debug", loggerName, data)
end
function MCPClientFeatures:logInfo(loggerName, data)
self:log("info", loggerName, data)
end
function MCPClientFeatures:logWarning(loggerName, data)
self:log("warning", loggerName, data)
end
function MCPClientFeatures:logError(loggerName, data)
self:log("error", loggerName, data)
end
--------------------------------------------------------------------------------
-- Resource Notifications (Server → Client)
--------------------------------------------------------------------------------
-- Notify client that a subscribed resource has been updated
function MCPClientFeatures:notifyResourceUpdated(uri)
if not self.sendToClient then
return
end
local notification = {
jsonrpc = "2.0",
method = "notifications/resources/updated",
params = {
uri = uri,
},
}
self.sendToClient(notification)
self:logDebug("resources", { event = "resource_updated", uri = uri })
end
-- Notify client that the resource list has changed
function MCPClientFeatures:notifyResourceListChanged()
if not self.sendToClient then
return
end
local notification = {
jsonrpc = "2.0",
method = "notifications/resources/list_changed",
}
self.sendToClient(notification)
self:logDebug("resources", { event = "list_changed" })
end
--------------------------------------------------------------------------------
-- Progress Notifications (Server → Client)
--------------------------------------------------------------------------------
-- Send progress notification for a long-running operation
-- progressToken: the token provided by the client in the request
-- progress: current progress value (must increase with each call)
-- total: optional total value (omit if unknown)
-- message: optional human-readable progress message
function MCPClientFeatures:notifyProgress(progressToken, progress, total, message)
if not self.sendToClient then
return
end
if not progressToken then
return -- No token means client doesn't want progress updates
end
local params = {
progressToken = progressToken,
progress = progress,
}
if total then
params.total = total
end
if message then
params.message = message
end
local notification = {
jsonrpc = "2.0",
method = "notifications/progress",
params = params,
}
self.sendToClient(notification)
end
--------------------------------------------------------------------------------
-- Sampling (Server → Client requests)
--------------------------------------------------------------------------------
-- Get the next request ID
function MCPClientFeatures:_nextRequestId()
self._request_id = self._request_id + 1
return self._request_id
end
-- Request LLM sampling from the client
-- This is an async operation - the response will be returned via callback
--
-- options:
-- messages: array of {role: "user"|"assistant", content: {type, text/data/...}}
-- systemPrompt: optional system prompt string
-- maxTokens: maximum tokens to generate
-- modelPreferences: optional {hints: [{name: string}], costPriority, speedPriority, intelligencePriority}
-- stopSequences: optional array of strings
-- temperature: optional 0-1
-- tools: optional array of tool definitions (if client supports sampling.tools)
-- toolChoice: optional {mode: "auto"|"required"|"none"}
--
-- callback: function(result, error) called when response received
-- result: {role, content, model, stopReason} or nil on error
-- error: error message string or nil on success
function MCPClientFeatures:requestSampling(options, callback)
if not self:supportsSampling() then
if callback then
callback(nil, "Client does not support sampling")
end
return
end
if not self.sendToClient then
if callback then
callback(nil, "No connection to client")
end
return
end
local requestId = self:_nextRequestId()
local params = {
messages = options.messages or {},
maxTokens = options.maxTokens or 1000,
}
if options.systemPrompt then
params.systemPrompt = options.systemPrompt
end
if options.modelPreferences then
params.modelPreferences = options.modelPreferences
end
if options.stopSequences then
params.stopSequences = options.stopSequences
end
if options.temperature then
params.temperature = options.temperature
end
-- Only include tools if client supports it
if options.tools and self:supportsSamplingWithTools() then
params.tools = options.tools
if options.toolChoice then
params.toolChoice = options.toolChoice
end
end
local request = {
jsonrpc = "2.0",
id = requestId,
method = "sampling/createMessage",
params = params,
}
-- Store callback for when response arrives
self._pending_requests[requestId] = {
callback = callback,
created_at = os.time(),
method = "sampling/createMessage",
}
self:logDebug("sampling", {
event = "request_sent",
id = requestId,
message_count = #params.messages,
max_tokens = params.maxTokens,
})
self.sendToClient(request)
return requestId
end
-- Simple sampling helper - send a text prompt and get text response
-- prompt: string to send to the LLM
-- callback: function(response_text, error)
function MCPClientFeatures:ask(prompt, callback)
local options = {
messages = {
{
role = "user",
content = {
type = "text",
text = prompt,
},
},
},
maxTokens = 1000,
}
self:requestSampling(options, function(result, error)
if error then
if callback then callback(nil, error) end
return
end
-- Extract text from response
local text = nil
if result and result.content then
if result.content.type == "text" then
text = result.content.text
elseif type(result.content) == "table" then
-- Array of content blocks
local parts = {}
for _, block in ipairs(result.content) do
if block.type == "text" then
table.insert(parts, block.text)
end
end
text = table.concat(parts, "\n")
end
end
if callback then callback(text, nil) end
end)
end
--------------------------------------------------------------------------------
-- Elicitation (Server → Client requests)
--------------------------------------------------------------------------------
-- Request user input via form mode elicitation
--
-- options:
-- message: human-readable message explaining why input is needed
-- requestedSchema: JSON Schema for the expected response (flat objects only)
-- Properties can be: string, number, integer, boolean, or enum
--
-- callback: function(result, error)
-- result: {action: "accept"|"decline"|"cancel", content: {property: value, ...}}
-- error: error message string or nil
function MCPClientFeatures:requestFormElicitation(options, callback)
if not self:supportsFormElicitation() then
if callback then
callback(nil, "Client does not support form elicitation")
end
return
end
if not self.sendToClient then
if callback then
callback(nil, "No connection to client")
end
return
end
local requestId = self:_nextRequestId()
local params = {
mode = "form",
message = options.message or "Please provide the requested information",
requestedSchema = options.requestedSchema or {
type = "object",
properties = {},
},
}
local request = {
jsonrpc = "2.0",
id = requestId,
method = "elicitation/create",
params = params,
}
self._pending_requests[requestId] = {
callback = callback,
created_at = os.time(),
method = "elicitation/create",
}
self:logDebug("elicitation", {
event = "form_request_sent",
id = requestId,
message = options.message,
})
self.sendToClient(request)
return requestId
end
-- Request user confirmation (simple yes/no elicitation)
-- message: the question to ask
-- callback: function(confirmed, error)
-- confirmed: true if user accepted, false if declined/cancelled
function MCPClientFeatures:confirm(message, callback)
local schema = {
type = "object",
properties = {
confirm = {
type = "boolean",
title = "Confirm",
description = message,
default = false,
},
},
}
self:requestFormElicitation({
message = message,
requestedSchema = schema,
}, function(result, error)
if error then
if callback then callback(false, error) end
return
end
local confirmed = result
and result.action == "accept"
and result.content
and result.content.confirm == true
if callback then callback(confirmed, nil) end
end)
end
-- Request text input from user
-- message: prompt to show
-- options: {title, description, default, minLength, maxLength}
-- callback: function(text, error)
function MCPClientFeatures:requestText(message, options, callback)
options = options or {}
local schema = {
type = "object",
properties = {
input = {
type = "string",
title = options.title or "Input",
description = options.description,
default = options.default,
minLength = options.minLength,
maxLength = options.maxLength,
},
},
required = { "input" },
}
self:requestFormElicitation({
message = message,
requestedSchema = schema,
}, function(result, error)
if error then
if callback then callback(nil, error) end
return
end
if result and result.action == "accept" and result.content then
if callback then callback(result.content.input, nil) end
else
if callback then callback(nil, "User cancelled") end
end
end)
end
-- Request choice from enum options
-- message: prompt to show
-- options: {title, choices: {value1, value2, ...} or {{value, title}, ...}}
-- callback: function(choice, error)
function MCPClientFeatures:requestChoice(message, options, callback)
options = options or {}
local enumValues = {}
local hasTitle = false
for _, choice in ipairs(options.choices or {}) do
if type(choice) == "table" then
hasTitle = true
break
end
end
local schema = {
type = "object",
properties = {
choice = {
type = "string",
title = options.title or "Choice",
},
},
required = { "choice" },
}
if hasTitle then
local oneOf = {}
for _, choice in ipairs(options.choices or {}) do
if type(choice) == "table" then
table.insert(oneOf, { const = choice[1], title = choice[2] })
else
table.insert(oneOf, { const = choice, title = choice })
end
end
schema.properties.choice.oneOf = oneOf
else
schema.properties.choice.enum = options.choices or {}
end
if options.default then
schema.properties.choice.default = options.default
end
self:requestFormElicitation({
message = message,
requestedSchema = schema,
}, function(result, error)
if error then
if callback then callback(nil, error) end
return
end
if result and result.action == "accept" and result.content then
if callback then callback(result.content.choice, nil) end
else
if callback then callback(nil, "User cancelled") end
end
end)
end
-- Request URL mode elicitation (for OAuth, sensitive data, etc.)
--
-- options:
-- message: human-readable message explaining the interaction
-- url: the URL the user should navigate to
-- elicitationId: unique identifier for this elicitation
--
-- callback: function(result, error)
-- result: {action: "accept"|"decline"|"cancel"}
function MCPClientFeatures:requestUrlElicitation(options, callback)
if not self:supportsUrlElicitation() then
if callback then
callback(nil, "Client does not support URL elicitation")
end
return
end
if not self.sendToClient then
if callback then
callback(nil, "No connection to client")
end
return
end
local requestId = self:_nextRequestId()
local params = {
mode = "url",
message = options.message or "Please complete the interaction",
url = options.url,
elicitationId = options.elicitationId or tostring(os.time()) .. "_" .. tostring(math.random(1000, 9999)),
}
local request = {
jsonrpc = "2.0",
id = requestId,
method = "elicitation/create",
params = params,
}
self._pending_requests[requestId] = {
callback = callback,
created_at = os.time(),
method = "elicitation/create",
elicitationId = params.elicitationId,
}
self:logDebug("elicitation", {
event = "url_request_sent",
id = requestId,
url = options.url,
elicitationId = params.elicitationId,
})
self.sendToClient(request)
return requestId, params.elicitationId
end
--------------------------------------------------------------------------------
-- Response Handling
--------------------------------------------------------------------------------
-- Handle a response from the client for a pending request
-- Returns true if this was a response to a pending request, false otherwise
function MCPClientFeatures:handleResponse(response)
local id = response.id
if not id then
return false
end
local pending = self._pending_requests[id]
if not pending then
return false
end
-- Remove from pending
self._pending_requests[id] = nil
-- Handle error response
if response.error then
self:logWarning(pending.method, {
event = "request_failed",
id = id,
error_code = response.error.code,
error_message = response.error.message,
})
if pending.callback then
pending.callback(nil, response.error.message or "Request failed")
end
return true
end
-- Handle success response
self:logDebug(pending.method, {
event = "request_succeeded",
id = id,
})
if pending.callback then
pending.callback(response.result, nil)
end
return true
end
-- Handle a notification from the client
function MCPClientFeatures:handleNotification(notification)
local method = notification.method
local params = notification.params or {}
if method == "notifications/elicitation/complete" then
-- URL mode elicitation completed
self:logDebug("elicitation", {
event = "url_elicitation_complete",
elicitationId = params.elicitationId,
})
-- Note: The actual callback is triggered by the response to the request,
-- this notification is just informational
return true
end
return false
end
-- Clean up old pending requests (call periodically)
function MCPClientFeatures:cleanupPendingRequests(max_age_seconds)
max_age_seconds = max_age_seconds or 300 -- 5 minutes default
local now = os.time()
local expired = {}
for id, pending in pairs(self._pending_requests) do
if now - pending.created_at > max_age_seconds then
table.insert(expired, id)
end
end
for _, id in ipairs(expired) do
local pending = self._pending_requests[id]
self._pending_requests[id] = nil
self:logWarning(pending.method, {
event = "request_timeout",
id = id,
})
if pending.callback then
pending.callback(nil, "Request timed out")
end
end
return #expired
end
return MCPClientFeatures