Class: MCP::Server
- Inherits:
-
Object
- Object
- MCP::Server
- Includes:
- Instrumentation
- Defined in:
- lib/mcp/server.rb
Defined Under Namespace
Classes: RequestHandlerError
Constant Summary collapse
- DEFAULT_VERSION =
"0.1.0"
Instance Attribute Summary collapse
- #capabilities ⇒ Object
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#name ⇒ Object
Returns the value of attribute name.
-
#prompts ⇒ Object
Returns the value of attribute prompts.
-
#resources ⇒ Object
Returns the value of attribute resources.
-
#server_context ⇒ Object
Returns the value of attribute server_context.
-
#tools ⇒ Object
Returns the value of attribute tools.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #define_prompt(name: nil, description: nil, arguments: [], &block) ⇒ Object
- #define_tool(name: nil, description: nil, input_schema: nil, annotations: nil, &block) ⇒ Object
- #handle(request) ⇒ Object
- #handle_json(request) ⇒ Object
-
#initialize(name: "model_context_protocol", version: DEFAULT_VERSION, tools: [], prompts: [], resources: [], resource_templates: [], server_context: nil, configuration: nil, capabilities: nil) ⇒ Server
constructor
A new instance of Server.
- #prompts_get_handler(&block) ⇒ Object
- #prompts_list_handler(&block) ⇒ Object
- #resources_list_handler(&block) ⇒ Object
- #resources_read_handler(&block) ⇒ Object
- #resources_templates_list_handler(&block) ⇒ Object
- #tools_call_handler(&block) ⇒ Object
- #tools_list_handler(&block) ⇒ Object
Methods included from Instrumentation
#add_instrumentation_data, #instrument_call
Constructor Details
#initialize(name: "model_context_protocol", version: DEFAULT_VERSION, tools: [], prompts: [], resources: [], resource_templates: [], server_context: nil, configuration: nil, capabilities: nil) ⇒ Server
Returns a new instance of Server.
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 |
# File 'lib/mcp/server.rb', line 28 def initialize( name: "model_context_protocol", version: DEFAULT_VERSION, tools: [], prompts: [], resources: [], resource_templates: [], server_context: nil, configuration: nil, capabilities: nil ) @name = name @version = version @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) @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::COMPLETION_COMPLETE => ->(_) {}, Methods::LOGGING_SET_LEVEL => ->(_) {}, } end |
Instance Attribute Details
#capabilities ⇒ Object
68 69 70 |
# File 'lib/mcp/server.rb', line 68 def capabilities @capabilities ||= determine_capabilities end |
#configuration ⇒ Object
Returns the value of attribute configuration.
26 27 28 |
# File 'lib/mcp/server.rb', line 26 def configuration @configuration end |
#name ⇒ Object
Returns the value of attribute name.
26 27 28 |
# File 'lib/mcp/server.rb', line 26 def name @name end |
#prompts ⇒ Object
Returns the value of attribute prompts.
26 27 28 |
# File 'lib/mcp/server.rb', line 26 def prompts @prompts end |
#resources ⇒ Object
Returns the value of attribute resources.
26 27 28 |
# File 'lib/mcp/server.rb', line 26 def resources @resources end |
#server_context ⇒ Object
Returns the value of attribute server_context.
26 27 28 |
# File 'lib/mcp/server.rb', line 26 def server_context @server_context end |
#tools ⇒ Object
Returns the value of attribute tools.
26 27 28 |
# File 'lib/mcp/server.rb', line 26 def tools @tools end |
#version ⇒ Object
Returns the value of attribute version.
26 27 28 |
# File 'lib/mcp/server.rb', line 26 def version @version end |
Instance Method Details
#define_prompt(name: nil, description: nil, arguments: [], &block) ⇒ Object
89 90 91 92 |
# File 'lib/mcp/server.rb', line 89 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
84 85 86 87 |
# File 'lib/mcp/server.rb', line 84 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
72 73 74 75 76 |
# File 'lib/mcp/server.rb', line 72 def handle(request) JsonRpcHandler.handle(request) do |method| handle_request(request, method) end end |
#handle_json(request) ⇒ Object
78 79 80 81 82 |
# File 'lib/mcp/server.rb', line 78 def handle_json(request) JsonRpcHandler.handle_json(request) do |method| handle_request(request, method) end end |
#prompts_get_handler(&block) ⇒ Object
118 119 120 |
# File 'lib/mcp/server.rb', line 118 def prompts_get_handler(&block) @handlers[Methods::PROMPTS_GET] = block end |
#prompts_list_handler(&block) ⇒ Object
114 115 116 |
# File 'lib/mcp/server.rb', line 114 def prompts_list_handler(&block) @handlers[Methods::PROMPTS_LIST] = block end |
#resources_list_handler(&block) ⇒ Object
94 95 96 |
# File 'lib/mcp/server.rb', line 94 def resources_list_handler(&block) @handlers[Methods::RESOURCES_LIST] = block end |
#resources_read_handler(&block) ⇒ Object
98 99 100 |
# File 'lib/mcp/server.rb', line 98 def resources_read_handler(&block) @handlers[Methods::RESOURCES_READ] = block end |
#resources_templates_list_handler(&block) ⇒ Object
102 103 104 |
# File 'lib/mcp/server.rb', line 102 def resources_templates_list_handler(&block) @handlers[Methods::RESOURCES_TEMPLATES_LIST] = block end |
#tools_call_handler(&block) ⇒ Object
110 111 112 |
# File 'lib/mcp/server.rb', line 110 def tools_call_handler(&block) @handlers[Methods::TOOLS_CALL] = block end |
#tools_list_handler(&block) ⇒ Object
106 107 108 |
# File 'lib/mcp/server.rb', line 106 def tools_list_handler(&block) @handlers[Methods::TOOLS_LIST] = block end |