Class: MCP::Server

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/mcp/server.rb,
lib/mcp/server/capabilities.rb,
lib/mcp/server/transports/stdio_transport.rb,
lib/mcp/server/transports/streamable_http_transport.rb

Defined Under Namespace

Modules: Transports Classes: Capabilities, MethodAlreadyDefinedError, RequestHandlerError

Constant Summary collapse

DEFAULT_VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

#add_instrumentation_data, #instrument_call

Constructor Details

#initialize(name: "model_context_protocol", title: nil, version: DEFAULT_VERSION, instructions: nil, tools: [], prompts: [], resources: [], resource_templates: [], server_context: nil, configuration: nil, capabilities: nil, transport: nil) ⇒ Server

Returns a new instance of Server.



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

def initialize(
  name: "model_context_protocol",
  title: nil,
  version: DEFAULT_VERSION,
  instructions: nil,
  tools: [],
  prompts: [],
  resources: [],
  resource_templates: [],
  server_context: nil,
  configuration: nil,
  capabilities: nil,
  transport: nil
)
  @name = name
  @title = title
  @version = version
  @instructions = instructions
  @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 = MCP.configuration.merge(configuration)

  validate!

  @capabilities = capabilities || default_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 => ->(_) { {} },
    Methods::NOTIFICATIONS_INITIALIZED => ->(_) {},

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

Instance Attribute Details

#capabilitiesObject

Returns the value of attribute capabilities.



34
35
36
# File 'lib/mcp/server.rb', line 34

def capabilities
  @capabilities
end

#configurationObject

Returns the value of attribute configuration.



34
35
36
# File 'lib/mcp/server.rb', line 34

def configuration
  @configuration
end

#instructionsObject

Returns the value of attribute instructions.



34
35
36
# File 'lib/mcp/server.rb', line 34

def instructions
  @instructions
end

#nameObject

Returns the value of attribute name.



34
35
36
# File 'lib/mcp/server.rb', line 34

def name
  @name
end

#promptsObject

Returns the value of attribute prompts.



34
35
36
# File 'lib/mcp/server.rb', line 34

def prompts
  @prompts
end

#resourcesObject

Returns the value of attribute resources.



34
35
36
# File 'lib/mcp/server.rb', line 34

def resources
  @resources
end

#server_contextObject

Returns the value of attribute server_context.



34
35
36
# File 'lib/mcp/server.rb', line 34

def server_context
  @server_context
end

#titleObject

Returns the value of attribute title.



34
35
36
# File 'lib/mcp/server.rb', line 34

def title
  @title
end

#toolsObject

Returns the value of attribute tools.



34
35
36
# File 'lib/mcp/server.rb', line 34

def tools
  @tools
end

#transportObject

Returns the value of attribute transport.



34
35
36
# File 'lib/mcp/server.rb', line 34

def transport
  @transport
end

#versionObject

Returns the value of attribute version.



34
35
36
# File 'lib/mcp/server.rb', line 34

def version
  @version
end

Instance Method Details

#define_custom_method(method_name:, &block) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/mcp/server.rb', line 111

def define_custom_method(method_name:, &block)
  if @handlers.key?(method_name)
    raise MethodAlreadyDefinedError, method_name
  end

  @handlers[method_name] = block
end

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



106
107
108
109
# File 'lib/mcp/server.rb', line 106

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

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



99
100
101
102
103
104
# File 'lib/mcp/server.rb', line 99

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

  validate!
end

#handle(request) ⇒ Object



87
88
89
90
91
# File 'lib/mcp/server.rb', line 87

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

#handle_json(request) ⇒ Object



93
94
95
96
97
# File 'lib/mcp/server.rb', line 93

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

#notify_prompts_list_changedObject



127
128
129
130
131
132
133
# File 'lib/mcp/server.rb', line 127

def notify_prompts_list_changed
  return unless @transport

  @transport.send_notification(Methods::NOTIFICATIONS_PROMPTS_LIST_CHANGED)
rescue => e
  report_exception(e, { notification: "prompts_list_changed" })
end

#notify_resources_list_changedObject



135
136
137
138
139
140
141
# File 'lib/mcp/server.rb', line 135

def notify_resources_list_changed
  return unless @transport

  @transport.send_notification(Methods::NOTIFICATIONS_RESOURCES_LIST_CHANGED)
rescue => e
  report_exception(e, { notification: "resources_list_changed" })
end

#notify_tools_list_changedObject



119
120
121
122
123
124
125
# File 'lib/mcp/server.rb', line 119

def notify_tools_list_changed
  return unless @transport

  @transport.send_notification(Methods::NOTIFICATIONS_TOOLS_LIST_CHANGED)
rescue => e
  report_exception(e, { notification: "tools_list_changed" })
end

#prompts_get_handler(&block) ⇒ Object



167
168
169
# File 'lib/mcp/server.rb', line 167

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

#prompts_list_handler(&block) ⇒ Object



163
164
165
# File 'lib/mcp/server.rb', line 163

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

#resources_list_handler(&block) ⇒ Object



143
144
145
# File 'lib/mcp/server.rb', line 143

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

#resources_read_handler(&block) ⇒ Object



147
148
149
# File 'lib/mcp/server.rb', line 147

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

#resources_templates_list_handler(&block) ⇒ Object



151
152
153
# File 'lib/mcp/server.rb', line 151

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

#tools_call_handler(&block) ⇒ Object



159
160
161
# File 'lib/mcp/server.rb', line 159

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

#tools_list_handler(&block) ⇒ Object



155
156
157
# File 'lib/mcp/server.rb', line 155

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