forked from modelcontextprotocol/ruby-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprompt.rb
More file actions
126 lines (108 loc) · 3.16 KB
/
prompt.rb
File metadata and controls
126 lines (108 loc) · 3.16 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
# frozen_string_literal: true
require_relative "prompt/argument"
require_relative "prompt/message"
require_relative "prompt/result"
module MCP
class Prompt
class << self
NOT_SET = Object.new
attr_reader :title_value
attr_reader :description_value
attr_reader :icons_value
attr_reader :arguments_value
attr_reader :meta_value
def template(args, server_context: nil)
raise NotImplementedError, "Subclasses must implement template"
end
def to_h
{
name: name_value,
title: title_value,
description: description_value,
icons: icons_value&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
arguments: arguments_value.empty? ? nil : arguments_value.map(&:to_h),
_meta: meta_value,
}.compact
end
def inherited(subclass)
super
subclass.instance_variable_set(:@name_value, nil)
subclass.instance_variable_set(:@title_value, nil)
subclass.instance_variable_set(:@description_value, nil)
subclass.instance_variable_set(:@icons_value, nil)
subclass.instance_variable_set(:@arguments_value, [])
subclass.instance_variable_set(:@meta_value, nil)
end
def prompt_name(value = NOT_SET)
if value == NOT_SET
@name_value
else
@name_value = value
end
end
def name_value
@name_value || StringUtils.handle_from_class_name(name)
end
def title(value = NOT_SET)
if value == NOT_SET
@title_value
else
@title_value = value
end
end
def description(value = NOT_SET)
if value == NOT_SET
@description_value
else
@description_value = value
end
end
def icons(value = NOT_SET)
if value == NOT_SET
@icons_value
else
@icons_value = value
end
end
def arguments(value = NOT_SET)
if value == NOT_SET
@arguments_value
else
@arguments_value = Array(value)
end
end
def meta(value = NOT_SET)
if value == NOT_SET
@meta_value
else
@meta_value = value
end
end
def define(name: nil, title: nil, description: nil, icons: [], arguments: [], meta: nil, &block)
Class.new(self) do
prompt_name name
title title
description description
icons icons
arguments arguments
define_singleton_method(:template) do |args, server_context: nil|
instance_exec(args, server_context: server_context, &block)
end
meta meta
end
end
def validate_arguments!(args)
args ||= {}
missing = required_args - args.keys
return if missing.empty?
raise MCP::Server::RequestHandlerError.new(
"Missing required arguments: #{missing.join(", ")}", nil, error_type: :missing_required_arguments
)
end
private
def required_args
arguments_value.filter_map { |arg| arg.name.to_sym if arg.required }
end
end
end
end