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
-
.configuration ⇒ Object
Returns the value of attribute configuration.
-
.logger ⇒ Logger
Get the logger.
Class Method Summary collapse
-
.broadcast_resource_update(uri, event_type = 'resource_updated') ⇒ Object
Broadcast resource update to connected SSE clients.
-
.configure {|Configuration| ... } ⇒ Configuration
Configure the library.
-
.mount_in_rails(app, **options) {|Server| ... } ⇒ Server
Mount MCP server in Rails application.
-
.resource(uri, **options, &block) ⇒ Resource
Create a resource with DSL.
-
.server(options = {}) {|Server| ... } ⇒ Server
Create a new MCP server.
-
.tool(name, description = '', input_schema = {}, **options, &block) ⇒ Tool
Create a tool with DSL.
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
26 27 28 |
# File 'lib/mcp_on_ruby.rb', line 26 def configuration @configuration end |
.logger ⇒ Logger
Get 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
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
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
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, **, &block) server = self.server(, &block) # Mount the transport middleware app.config.middleware.use( Transport::RackMiddleware, server: server, ** ) server end |
.resource(uri, **options, &block) ⇒ Resource
Create a resource with DSL
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, **, &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: [:name], description: [:description] || '', mime_type: [:mime_type] || 'application/json', metadata: [:metadata] || {}, tags: [:tags] || [] ) end |
.server(options = {}) {|Server| ... } ⇒ Server
Create a new MCP server
50 51 52 |
# File 'lib/mcp_on_ruby.rb', line 50 def server( = {}, &block) Server.new(, &block) end |
.tool(name, description = '', input_schema = {}, **options, &block) ⇒ Tool
Create a tool with DSL
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 = {}, **, &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: [:metadata] || {}, tags: [:tags] || [] ) end |