File tree Expand file tree Collapse file tree 5 files changed +83
-10
lines changed Expand file tree Collapse file tree 5 files changed +83
-10
lines changed Original file line number Diff line number Diff line change
1
+ module RubyMCP ::Capabilities ::Prompts
2
+ def add_prompt ( ...)
3
+ @prompts . add ( ...)
4
+
5
+ RubyMCP . logger . info ( @transport )
6
+ send_prompts_list_changed if @transport
7
+ end
8
+
9
+ private
10
+
11
+ def send_prompts_list_changed
12
+ @transport . enqueue ( jsonrpc : "2.0" , method : "notifications/prompts/list_changed" )
13
+ end
14
+ end
Original file line number Diff line number Diff line change 1
1
module RubyMCP
2
2
class Server
3
3
include Capabilities ::Logging
4
+ include Capabilities ::Prompts
4
5
5
6
attr_reader :lifecycle , :prompts , :resources
6
7
@@ -21,17 +22,13 @@ def connect(transport)
21
22
start_transport
22
23
end
23
24
24
- def add_prompt ( ...)
25
- @prompts . add ( ...)
26
- end
27
-
28
25
def add_resource ( ...)
29
26
@resources . add ( ...)
30
27
end
31
28
32
29
def send_message ( message )
33
30
RubyMCP . logger . debug "S -> C : #{ message } "
34
- @transport . send ( message )
31
+ @transport . enqueue ( message )
35
32
end
36
33
37
34
def answer ( request , result )
Original file line number Diff line number Diff line change 1
1
module RubyMCP
2
2
class Transport
3
3
class Stdio < Transport
4
+ def initialize
5
+ @queue = Queue . new
6
+ end
7
+
4
8
def start
5
9
@running = true
10
+ start_message_worker
6
11
7
12
while @running
8
13
begin
9
14
line = $stdin. gets
10
15
11
16
break if line . nil?
12
17
13
- @on_message . call ( line . strip )
18
+ @queue << [ :incoming , line . strip ]
14
19
rescue StandardError => e
15
20
RubyMCP . logger . error ( "Exception: #{ e } " )
16
21
end
@@ -19,15 +24,33 @@ def start
19
24
@on_close . call
20
25
end
21
26
22
- def send ( message )
23
- $stdout. puts ( JSON . generate ( message ) )
24
- $stdout. flush
27
+ def enqueue ( message )
28
+ @queue << [ :outgoing , JSON . generate ( message ) ]
25
29
end
26
30
27
31
28
32
def on_close ( &block )
29
33
@on_close = block
30
34
end
35
+
36
+ private
37
+
38
+ def start_message_worker
39
+ sleep 0.2
40
+ RubyMCP . logger . info ( "Starting worker thread" )
41
+ @worker = Thread . new do
42
+ while @running
43
+ type , message = @queue . pop
44
+
45
+ if type == :incoming
46
+ @on_message . call ( message )
47
+ else
48
+ $stdout. puts ( message )
49
+ $stdout. flush
50
+ end
51
+ end
52
+ end
53
+ end
31
54
end
32
55
end
33
56
end
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ def start
11
11
@running = true
12
12
end
13
13
14
- def send ( message )
14
+ def enqueue ( message )
15
15
@responses << JSON . generate ( message )
16
16
end
17
17
Original file line number Diff line number Diff line change @@ -329,4 +329,43 @@ def test_prompt_get_missing_required_argument_with_multiple_required_args
329
329
}
330
330
)
331
331
end
332
+
333
+ def test_adding_prompt_sends_prompts_list_changed
334
+ @server . add_prompt (
335
+ name : "refactor" ,
336
+ description : "Review this code" ,
337
+ arguments : [
338
+ {
339
+ name : "code" ,
340
+ description : "code to review" ,
341
+ required : true ,
342
+ completions : -> ( *) { [ "some" , "completion" , "value" ] }
343
+ } ,
344
+ {
345
+ name : "language" ,
346
+ description : "Programming language" ,
347
+ required : true ,
348
+ completions : -> ( *) { [ "some" , "completion" , "value" ] }
349
+ }
350
+ ] ,
351
+ result : -> ( ) {
352
+ {
353
+ description : "demo" ,
354
+ messages : [
355
+ {
356
+ role : "user" ,
357
+ content : {
358
+ type : "text" ,
359
+ text : "demo"
360
+ }
361
+ }
362
+ ]
363
+ }
364
+ } ,
365
+ )
366
+
367
+ assert_last_response (
368
+ jsonrpc : "2.0" , method : "notifications/prompts/list_changed"
369
+ )
370
+ end
332
371
end
You can’t perform that action at this time.
0 commit comments