Skip to content

Refactor towards simple data/config classes #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions lib/mcp.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative "mcp/annotations"
require_relative "mcp/configuration"
require_relative "mcp/content"
require_relative "mcp/instrumentation"
Expand All @@ -14,7 +15,6 @@
require_relative "mcp/resource_template"
require_relative "mcp/server"
require_relative "mcp/server/transports/stdio_transport"
require_relative "mcp/string_utils"
require_relative "mcp/tool"
require_relative "mcp/tool/input_schema"
require_relative "mcp/tool/response"
Expand All @@ -32,13 +32,4 @@ def configuration
@configuration ||= Configuration.new
end
end

class Annotations
attr_reader :audience, :priority

def initialize(audience: nil, priority: nil)
@audience = audience
@priority = priority
end
end
end
18 changes: 18 additions & 0 deletions lib/mcp/annotations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module MCP
class Annotations
attr_reader :audience, :priority

def initialize(audience: nil, priority: nil)
@audience = audience
@priority = priority

freeze
end

def to_h
{ audience:, priority: }.compact.freeze
end
end
end
66 changes: 33 additions & 33 deletions lib/mcp/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,41 @@ def initialize(method, capability)
end
end

extend self
class << self
def ensure_capability!(method, capabilities)
case method
when PROMPTS_GET, PROMPTS_LIST
unless capabilities[:prompts]
raise MissingRequiredCapabilityError.new(method, :prompts)
end
when RESOURCES_LIST, RESOURCES_TEMPLATES_LIST, RESOURCES_READ, RESOURCES_SUBSCRIBE, RESOURCES_UNSUBSCRIBE
unless capabilities[:resources]
raise MissingRequiredCapabilityError.new(method, :resources)
end

def ensure_capability!(method, capabilities)
case method
when PROMPTS_GET, PROMPTS_LIST
unless capabilities[:prompts]
raise MissingRequiredCapabilityError.new(method, :prompts)
if method == RESOURCES_SUBSCRIBE && !capabilities[:resources][:subscribe]
raise MissingRequiredCapabilityError.new(method, :resources_subscribe)
end
when TOOLS_CALL, TOOLS_LIST
unless capabilities[:tools]
raise MissingRequiredCapabilityError.new(method, :tools)
end
when SAMPLING_CREATE_MESSAGE
unless capabilities[:sampling]
raise MissingRequiredCapabilityError.new(method, :sampling)
end
when COMPLETION_COMPLETE
unless capabilities[:completions]
raise MissingRequiredCapabilityError.new(method, :completions)
end
when LOGGING_SET_LEVEL
# Logging is unsupported by the Server
unless capabilities[:logging]
raise MissingRequiredCapabilityError.new(method, :logging)
end
when INITIALIZE, PING
# No specific capability required for initialize or ping
end
when RESOURCES_LIST, RESOURCES_TEMPLATES_LIST, RESOURCES_READ, RESOURCES_SUBSCRIBE, RESOURCES_UNSUBSCRIBE
unless capabilities[:resources]
raise MissingRequiredCapabilityError.new(method, :resources)
end

if method == RESOURCES_SUBSCRIBE && !capabilities[:resources][:subscribe]
raise MissingRequiredCapabilityError.new(method, :resources_subscribe)
end
when TOOLS_CALL, TOOLS_LIST
unless capabilities[:tools]
raise MissingRequiredCapabilityError.new(method, :tools)
end
when SAMPLING_CREATE_MESSAGE
unless capabilities[:sampling]
raise MissingRequiredCapabilityError.new(method, :sampling)
end
when COMPLETION_COMPLETE
unless capabilities[:completions]
raise MissingRequiredCapabilityError.new(method, :completions)
end
when LOGGING_SET_LEVEL
# Logging is unsupported by the Server
unless capabilities[:logging]
raise MissingRequiredCapabilityError.new(method, :logging)
end
when INITIALIZE, PING
# No specific capability required for initialize or ping
end
end
end
Expand Down
81 changes: 18 additions & 63 deletions lib/mcp/prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,35 @@

module MCP
class Prompt
class << self
NOT_SET = Object.new

attr_reader :description_value
attr_reader :arguments_value
attr_reader :name, :description, :arguments, :to_h

def template(args, server_context:)
raise NotImplementedError, "Subclasses must implement template"
end
class << self
def define(...) = new(...)
private :new

def to_h
{ name: name_value, description: description_value, arguments: arguments_value.map(&:to_h) }.compact
end
private

def inherited(subclass)
super
subclass.instance_variable_set(:@name_value, nil)
subclass.instance_variable_set(:@description_value, nil)
subclass.instance_variable_set(:@arguments_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 description(value = NOT_SET)
if value == NOT_SET
@description_value
else
@description_value = value
end
end

def arguments(value = NOT_SET)
if value == NOT_SET
@arguments_value
else
@arguments_value = value
end
raise TypeError, "#{self} should no longer be subclassed. Use #{self}.define factory method instead."
end
end

def define(name: nil, description: nil, arguments: [], &block)
Class.new(self) do
prompt_name name
description description
arguments arguments
define_singleton_method(:template) do |args, server_context:|
instance_exec(args, server_context:, &block)
end
end
end
def initialize(name:, description:, arguments:, &block)
arguments = arguments.map { |arg| Hash === arg ? Argument.new(**arg) : arg }

def validate_arguments!(args)
missing = required_args - args.keys
return if missing.empty?
@name = name
@description = description
@arguments = arguments
@block = block

raise MCP::Server::RequestHandlerError.new(
"Missing required arguments: #{missing.join(", ")}", nil, error_type: :missing_required_arguments
)
end
@to_h = { name:, description:, arguments: arguments.map(&:to_h) }.compact.freeze

private
freeze
end

def required_args
arguments_value.filter_map { |arg| arg.name.to_sym if arg.required }
end
def call(args, server_context:)
@block.call(args, server_context:)
end
end
end
13 changes: 8 additions & 5 deletions lib/mcp/prompt/argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
module MCP
class Prompt
class Argument
attr_reader :name, :description, :required, :arguments
attr_reader :name, :description, :required, :to_h

def initialize(name:, description: nil, required: false)
@name = name
@description = description
@required = required
@arguments = arguments
end

def to_h
{ name:, description:, required: }.compact
@to_h = {
name:,
description:,
required:,
}.compact.freeze

freeze
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/mcp/resource/contents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ class TextContents < Contents
attr_reader :text

def initialize(text:, uri:, mime_type:)
super(uri: uri, mime_type: mime_type)
super(uri:, mime_type:)
@text = text
end

def to_h
super.merge(text: text)
super.merge(text:)
end
end

class BlobContents < Contents
attr_reader :data

def initialize(data:, uri:, mime_type:)
super(uri: uri, mime_type: mime_type)
super(uri:, mime_type:)
@data = data
end

def to_h
super.merge(data: data)
super.merge(data:)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/mcp/resource/embedded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Embedded
attr_reader :resource, :annotations

def initialize(resource:, annotations: nil)
@resource = resource
@annotations = annotations
end

Expand Down
10 changes: 5 additions & 5 deletions lib/mcp/resource_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@

module MCP
class ResourceTemplate
attr_reader :uri_template, :name, :description, :mime_type
attr_reader :uri_template, :name, :description, :mime_type, :to_h

def initialize(uri_template:, name:, description: nil, mime_type: nil)
@uri_template = uri_template
@name = name
@description = description
@mime_type = mime_type
end

def to_h
{
@to_h = {
uriTemplate: @uri_template,
name: @name,
description: @description,
mimeType: @mime_type,
}.compact
}.compact.freeze

freeze
end
end
end
Loading