Module: McpOnRuby

Defined in:
lib/mcp_on_ruby.rb,
lib/mcp_on_ruby/tool.rb,
lib/mcp_on_ruby/errors.rb,
lib/mcp_on_ruby/server.rb,
lib/mcp_on_ruby/railtie.rb,
lib/mcp_on_ruby/version.rb,
lib/mcp_on_ruby/resource.rb,
lib/mcp_on_ruby/transport.rb,
lib/mcp_on_ruby/configuration.rb,
lib/mcp_on_ruby/generators/tool_generator.rb,
lib/mcp_on_ruby/generators/install_generator.rb,
lib/mcp_on_ruby/generators/resource_generator.rb

Overview

Production-ready Model Context Protocol implementation for Rails

Defined Under Namespace

Modules: Generators, Transport Classes: AuthorizationError, Configuration, ConfigurationError, Error, InvalidParamsError, InvalidRequestError, MethodNotFoundError, NotFoundError, ParseError, Railtie, RateLimitError, Resource, ResourceReadError, Server, Tool, ToolExecutionError, TransportError, ValidationError

Constant Summary collapse

VERSION =
"1.0.0"
PROTOCOL_VERSION =
"2024-11-05"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



26
27
28
# File 'lib/mcp_on_ruby.rb', line 26

def configuration
  @configuration
end

.loggerLogger

Get the logger

Returns:

  • (Logger)

    The logger



39
40
41
42
43
44
# File 'lib/mcp_on_ruby.rb', line 39

def logger
  @logger ||= Logger.new($stdout).tap do |log|
    log.progname = 'McpOnRuby'
    log.level = configuration&.log_level || Logger::INFO
  end
end

Class Method Details

.broadcast_resource_update(uri, event_type = 'resource_updated') ⇒ Object

Broadcast resource update to connected SSE clients

Parameters:

  • uri (String)

    Resource URI that was updated

  • event_type (String) (defaults to: 'resource_updated')

    Type of event



116
117
118
119
# File 'lib/mcp_on_ruby.rb', line 116

def broadcast_resource_update(uri, event_type = 'resource_updated')
  # This would be implemented when SSE is fully integrated
  logger.info("Resource update broadcasted: #{uri} (#{event_type})")
end

.configure {|Configuration| ... } ⇒ Configuration

Configure the library

Yields:

Returns:



31
32
33
34
35
# File 'lib/mcp_on_ruby.rb', line 31

def configure
  self.configuration ||= Configuration.new
  yield(configuration) if block_given?
  configuration
end

.mount_in_rails(app, **options) {|Server| ... } ⇒ Server

Mount MCP server in Rails application

Parameters:

  • app (Rails::Application)

    The Rails application

  • options (Hash)

    Mounting options

Yields:

  • (Server)

    The server instance for configuration

Returns:

  • (Server)

    The mounted server



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mcp_on_ruby.rb', line 126

def mount_in_rails(app, **options, &block)
  server = self.server(options, &block)
  
  # Mount the transport middleware
  app.config.middleware.use(
    Transport::RackMiddleware,
    server: server,
    **options
  )
  
  server
end

.resource(uri, **options, &block) ⇒ Resource

Create a resource with DSL

Parameters:

  • uri (String)

    Resource URI

  • options (Hash)

    Resource options (name, description, etc.)

  • block (Proc)

    Resource implementation

Returns:

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mcp_on_ruby.rb', line 89

def resource(uri, **options, &block)
  raise ArgumentError, 'Resource implementation block is required' unless block_given?

  Class.new(Resource) do
    define_method :fetch_content do |params, context|
      case block.arity
      when 0
        instance_exec(&block)
      when 1
        instance_exec(params, &block)
      else
        instance_exec(params, context, &block)
      end
    end
  end.new(
    uri: uri,
    name: options[:name],
    description: options[:description] || '',
    mime_type: options[:mime_type] || 'application/json',
    metadata: options[:metadata] || {},
    tags: options[:tags] || []
  )
end

.server(options = {}) {|Server| ... } ⇒ Server

Create a new MCP server

Parameters:

  • options (Hash) (defaults to: {})

    Server configuration options

Yields:

  • (Server)

    The server instance for configuration

Returns:

  • (Server)

    The configured server



50
51
52
# File 'lib/mcp_on_ruby.rb', line 50

def server(options = {}, &block)
  Server.new(options, &block)
end

.tool(name, description = '', input_schema = {}, **options, &block) ⇒ Tool

Create a tool with DSL

Parameters:

  • name (String)

    Tool name

  • description (String) (defaults to: '')

    Tool description

  • input_schema (Hash) (defaults to: {})

    JSON Schema for validation

  • options (Hash)

    Additional options (metadata, tags)

  • block (Proc)

    Tool implementation

Returns:

  • (Tool)

    The created tool

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mcp_on_ruby.rb', line 61

def tool(name, description = '', input_schema = {}, **options, &block)
  raise ArgumentError, 'Tool implementation block is required' unless block_given?

  Class.new(Tool) do
    define_method :execute do |arguments, context|
      case block.arity
      when 0
        instance_exec(&block)
      when 1
        instance_exec(arguments, &block)
      else
        instance_exec(arguments, context, &block)
      end
    end
  end.new(
    name: name,
    description: description,
    input_schema: input_schema,
    metadata: options[:metadata] || {},
    tags: options[:tags] || []
  )
end