Class: ModelContextProtocol::Server

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/model_context_protocol/server.rb

Defined Under Namespace

Classes: RequestHandlerError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

#add_instrumentation_data, #instrument_call

Constructor Details

#initialize(name: "model_context_protocol", tools: [], prompts: [], resources: [], resource_templates: [], server_context: nil, configuration: nil, capabilities: { prompts: {}, resources: {}, tools: {} }) ⇒ Server

Returns a new instance of Server.



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
# File 'lib/model_context_protocol/server.rb', line 25

def initialize(
  name: "model_context_protocol",
  tools: [],
  prompts: [],
  resources: [],
  resource_templates: [],
  server_context: nil,
  configuration: nil,
  capabilities: { prompts: {}, resources: {}, tools: {} }
)
  @name = name
  @tools = tools.to_h { |t| [t.name_value, t] }
  @prompts = prompts.to_h { |p| [p.name_value, p] }
  @resources = resources
  @resource_templates = resource_templates
  @resource_index = index_resources_by_uri(resources)
  @server_context = server_context
  @configuration = ModelContextProtocol.configuration.merge(configuration)
  @capabilities = capabilities

  @handlers = {
    Methods::RESOURCES_LIST => method(:list_resources),
    Methods::RESOURCES_READ => method(:read_resource_no_content),
    Methods::RESOURCES_TEMPLATES_LIST => method(:list_resource_templates),
    Methods::TOOLS_LIST => method(:list_tools),
    Methods::TOOLS_CALL => method(:call_tool),
    Methods::PROMPTS_LIST => method(:list_prompts),
    Methods::PROMPTS_GET => method(:get_prompt),
    Methods::INITIALIZE => method(:init),
    Methods::PING => ->(_) { {} },

    # No op handlers for currently unsupported methods
    Methods::RESOURCES_SUBSCRIBE => ->(_) {},
    Methods::RESOURCES_UNSUBSCRIBE => ->(_) {},
    Methods::LOGGING_SET_LEVEL => ->(_) {},
  }
end

Instance Attribute Details

#capabilitiesObject

Returns the value of attribute capabilities.



23
24
25
# File 'lib/model_context_protocol/server.rb', line 23

def capabilities
  @capabilities
end

#configurationObject

Returns the value of attribute configuration.



23
24
25
# File 'lib/model_context_protocol/server.rb', line 23

def configuration
  @configuration
end

#nameObject

Returns the value of attribute name.



23
24
25
# File 'lib/model_context_protocol/server.rb', line 23

def name
  @name
end

#promptsObject

Returns the value of attribute prompts.



23
24
25
# File 'lib/model_context_protocol/server.rb', line 23

def prompts
  @prompts
end

#resourcesObject

Returns the value of attribute resources.



23
24
25
# File 'lib/model_context_protocol/server.rb', line 23

def resources
  @resources
end

#server_contextObject

Returns the value of attribute server_context.



23
24
25
# File 'lib/model_context_protocol/server.rb', line 23

def server_context
  @server_context
end

#toolsObject

Returns the value of attribute tools.



23
24
25
# File 'lib/model_context_protocol/server.rb', line 23

def tools
  @tools
end

Instance Method Details

#define_prompt(name: nil, description: nil, arguments: [], &block) ⇒ Object



80
81
82
83
# File 'lib/model_context_protocol/server.rb', line 80

def define_prompt(name: nil, description: nil, arguments: [], &block)
  prompt = Prompt.define(name:, description:, arguments:, &block)
  @prompts[prompt.name_value] = prompt
end

#define_tool(name: nil, description: nil, input_schema: nil, annotations: nil, &block) ⇒ Object



75
76
77
78
# File 'lib/model_context_protocol/server.rb', line 75

def define_tool(name: nil, description: nil, input_schema: nil, annotations: nil, &block)
  tool = Tool.define(name:, description:, input_schema:, annotations:, &block)
  @tools[tool.name_value] = tool
end

#handle(request) ⇒ Object



63
64
65
66
67
# File 'lib/model_context_protocol/server.rb', line 63

def handle(request)
  JsonRpcHandler.handle(request) do |method|
    handle_request(request, method)
  end
end

#handle_json(request) ⇒ Object



69
70
71
72
73
# File 'lib/model_context_protocol/server.rb', line 69

def handle_json(request)
  JsonRpcHandler.handle_json(request) do |method|
    handle_request(request, method)
  end
end

#prompts_get_handler(&block) ⇒ Object



109
110
111
# File 'lib/model_context_protocol/server.rb', line 109

def prompts_get_handler(&block)
  @handlers[Methods::PROMPTS_GET] = block
end

#prompts_list_handler(&block) ⇒ Object



105
106
107
# File 'lib/model_context_protocol/server.rb', line 105

def prompts_list_handler(&block)
  @handlers[Methods::PROMPTS_LIST] = block
end

#resources_list_handler(&block) ⇒ Object



85
86
87
# File 'lib/model_context_protocol/server.rb', line 85

def resources_list_handler(&block)
  @handlers[Methods::RESOURCES_LIST] = block
end

#resources_read_handler(&block) ⇒ Object



89
90
91
# File 'lib/model_context_protocol/server.rb', line 89

def resources_read_handler(&block)
  @handlers[Methods::RESOURCES_READ] = block
end

#resources_templates_list_handler(&block) ⇒ Object



93
94
95
# File 'lib/model_context_protocol/server.rb', line 93

def resources_templates_list_handler(&block)
  @handlers[Methods::RESOURCES_TEMPLATES_LIST] = block
end

#tools_call_handler(&block) ⇒ Object



101
102
103
# File 'lib/model_context_protocol/server.rb', line 101

def tools_call_handler(&block)
  @handlers[Methods::TOOLS_CALL] = block
end

#tools_list_handler(&block) ⇒ Object



97
98
99
# File 'lib/model_context_protocol/server.rb', line 97

def tools_list_handler(&block)
  @handlers[Methods::TOOLS_LIST] = block
end