mcp-rb 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 610a73e9025b6400f091beeec7e0e0b9b51e917ba57b068f7132fd2dad3fa207
4
+ data.tar.gz: 8a5533dd1abdb1fbdde067facfe0ceeeb50d9fac332ba4ec4c4815a9b8f1a199
5
+ SHA512:
6
+ metadata.gz: be76a1399e5121d0e0886489630fe7695b9fba3cea55835d6da2d67d5b74d3a714f68c354dbf447e8953f965c0c65e416e12417ee109fe4695f47cc4c940bdc6
7
+ data.tar.gz: d0ec9f439c4bc497c264241000269b7c5928167f0623a3a34335e6c28b597c95967b38819051a954f96323e861ea6553d218bc639df7b52697185463da1ac158
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - 2025-02-12
9
+
10
+ ### Added
11
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 funwarioisii
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # MCP-RB
2
+
3
+ A lightweight Ruby framework for implementing MCP (Model Context Protocol) servers with a Sinatra-like DSL.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mcp-rb'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Here's a simple example of how to create an MCP server:
16
+
17
+ ```ruby
18
+ require 'mcp'
19
+
20
+ name "hello-world"
21
+
22
+ # リソースの定義
23
+ resource "hello://world",
24
+ name: "Hello World",
25
+ description: "A simple hello world message" do
26
+ "Hello, World!"
27
+ end
28
+
29
+ tool "greet",
30
+ description: "Greet someone by name",
31
+ input_schema: {
32
+ type: :object,
33
+ properties: {
34
+ name: {
35
+ type: :string,
36
+ description: "Name to greet"
37
+ }
38
+ },
39
+ required: [:name]
40
+ } do |args|
41
+ "Hello, #{args[:name]}!"
42
+ end
43
+ ```
44
+
45
+ ## Testing
46
+
47
+ ```bash
48
+ ruby -Ilib:test -e "Dir.glob('./test/**/*_test.rb').each { |f| require f }"
49
+ ```
50
+
51
+ Test with MCP Inspector
52
+
53
+ ```bash
54
+ bunx @modelcontextprotocol/inspector $(pwd)/examples/hello_world.rb
55
+ ```
56
+
57
+ Find broken using `hello_world.rb`
58
+
59
+ ```bash
60
+ ./test/test_requests.sh
61
+ ```
62
+
63
+ ## Formatting
64
+
65
+ ```bash
66
+ bundle exec standardrb --fix
67
+ ```
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MCP
4
+ class App
5
+ module Resource
6
+ def register_resource(uri, name:, mime_type: "text/plain", description: "", &block)
7
+ raise ArgumentError, "Resource name cannot be nil or empty" if uri.nil? || uri.empty?
8
+ raise ArgumentError, "Block must be provided" unless block_given?
9
+
10
+ resources[uri] = {
11
+ uri:, name:, mime_type:, description:,
12
+ handler: block
13
+ }
14
+ end
15
+
16
+ def list_resources(cursor: nil, page_size: nil)
17
+ start_index = cursor&.to_i || 0
18
+ values = resources.values
19
+
20
+ if page_size.nil?
21
+ paginated = values[start_index..]
22
+ next_cursor = ""
23
+ else
24
+ paginated = values[start_index, page_size]
25
+ has_next = start_index + page_size < values.length
26
+ next_cursor = has_next ? (start_index + page_size).to_s : ""
27
+ end
28
+
29
+ {
30
+ resources: paginated.map { |r| format_resource(r) },
31
+ nextCursor: next_cursor
32
+ }
33
+ end
34
+
35
+ def read_resource(uri)
36
+ resource = resources[uri]
37
+ raise ArgumentError, "Resource not found: #{uri}" unless resource
38
+
39
+ begin
40
+ content = resource[:handler].call
41
+ {
42
+ contents: [{
43
+ uri: resource[:uri],
44
+ mimeType: resource[:mime_type],
45
+ text: content
46
+ }]
47
+ }
48
+ rescue => e
49
+ raise ArgumentError, "Error reading resource: #{e.message}"
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def resources
56
+ @resources ||= {}
57
+ end
58
+
59
+ def format_resource(resource)
60
+ {
61
+ uri: resource[:uri],
62
+ name: resource[:name],
63
+ description: resource[:description],
64
+ mimeType: resource[:mime_type]
65
+ }
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MCP
4
+ class App
5
+ module Tool
6
+ def register_tool(name, description: "", input_schema: {}, &block)
7
+ raise ArgumentError, "Tool name cannot be nil or empty" if name.nil? || name.empty?
8
+ raise ArgumentError, "Block must be provided" unless block_given?
9
+
10
+ tools[name] = {
11
+ name: name,
12
+ description: description,
13
+ input_schema: input_schema,
14
+ handler: block
15
+ }
16
+ end
17
+
18
+ def list_tools(cursor: nil, page_size: 10)
19
+ tool_values = tools.values
20
+ start_index = cursor ? cursor.to_i : 0
21
+ paginated = tool_values[start_index, page_size]
22
+ next_cursor = (start_index + page_size < tool_values.length) ? (start_index + page_size).to_s : ""
23
+
24
+ {
25
+ tools: paginated.map { |t| format_tool(t) },
26
+ nextCursor: next_cursor
27
+ }
28
+ end
29
+
30
+ def call_tool(name, **arguments)
31
+ tool = tools[name]
32
+ raise ArgumentError, "Tool not found: #{name}" unless tool
33
+
34
+ begin
35
+ result = tool[:handler].call(arguments)
36
+ {
37
+ content: [
38
+ {
39
+ type: "text",
40
+ text: result.to_s
41
+ }
42
+ ],
43
+ isError: false
44
+ }
45
+ rescue => e
46
+ {
47
+ content: [
48
+ {
49
+ type: "text",
50
+ text: "Error: #{e.message}"
51
+ }
52
+ ],
53
+ isError: true
54
+ }
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def tools
61
+ @tools ||= {}
62
+ end
63
+
64
+ def format_tool(tool)
65
+ {
66
+ name: tool[:name],
67
+ description: tool[:description],
68
+ inputSchema: tool[:input_schema]
69
+ }
70
+ end
71
+ end
72
+ end
73
+ end
data/lib/mcp/app.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "app/resource"
4
+ require_relative "app/tool"
5
+
6
+ module MCP
7
+ class App
8
+ include Resource
9
+ include Tool
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MCP
4
+ module Constants
5
+ JSON_RPC_VERSION = "2.0"
6
+
7
+ module ErrorCodes
8
+ NOT_INITIALIZED = -32_002
9
+ ALREADY_INITIALIZED = -32_002
10
+
11
+ INVALID_REQUEST = -32_600
12
+ METHOD_NOT_FOUND = -32_601
13
+ UNSUPPORTED_PROTOCOL_VERSION = -32_602
14
+ INTERNAL_ERROR = -32_603
15
+ end
16
+
17
+ module RequestMethods
18
+ INITIALIZE = "initialize"
19
+ INITIALIZED = "notifications/initialized"
20
+ PING = "ping"
21
+ TOOLS_LIST = "tools/list"
22
+ TOOLS_CALL = "tools/call"
23
+ RESOURCES_LIST = "resources/list"
24
+ RESOURCES_READ = "resources/read"
25
+ RESOURCES_TEMPLATES_LIST = "resources/templates/list"
26
+ end
27
+ end
28
+ end.freeze
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MCP
4
+ module Delegator
5
+ def self.delegate(*methods)
6
+ methods.each do |method_name|
7
+ define_method(method_name) do |*args, **kwargs, &block|
8
+ # name が呼ばれたら Server インスタンスを生成
9
+ # もうすこしいい感じにしたい
10
+ if method_name == :name && !MCP.server
11
+ MCP.initialize_server(name: args.first || "default")
12
+ end
13
+ MCP.server.send(method_name, *args, **kwargs, &block)
14
+ end
15
+ end
16
+ end
17
+
18
+ delegate :name, :resource, :tool
19
+ end
20
+ end
data/lib/mcp/server.rb ADDED
@@ -0,0 +1,204 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "English"
5
+ require "uri"
6
+
7
+ module MCP
8
+ class Server
9
+ attr_accessor :name
10
+ attr_reader :initialized
11
+
12
+ def initialize(name:, version: VERSION)
13
+ @name = name
14
+ @version = version
15
+ @app = App.new
16
+ @initialized = false
17
+ @supported_protocol_versions = [PROTOCOL_VERSION]
18
+ end
19
+
20
+ def name(value = nil) # standard:disable Lint/DuplicateMethods
21
+ return @name if value.nil?
22
+
23
+ @name = value
24
+ end
25
+
26
+ def tool(name, description: "", input_schema: {}, &block)
27
+ @app.register_tool(name, description: description, input_schema: input_schema, &block)
28
+ end
29
+
30
+ def resource(uri, name:, mime_type: "text/plain", description: "", &block)
31
+ @app.register_resource(uri, name: name, mime_type: mime_type, description: description, &block)
32
+ end
33
+
34
+ def run
35
+ while (input = $stdin.gets)
36
+ process_input(input)
37
+ end
38
+ end
39
+
40
+ def list_tools
41
+ @app.list_tools[:tools]
42
+ end
43
+
44
+ def call_tool(name, **args)
45
+ @app.call_tool(name, **args).dig(:content, 0, :text)
46
+ end
47
+
48
+ def list_resources
49
+ @app.list_resources[:resources]
50
+ end
51
+
52
+ def read_resource(uri)
53
+ @app.read_resource(uri).dig(:contents, 0, :text)
54
+ end
55
+
56
+ private
57
+
58
+ def process_input(line)
59
+ request = JSON.parse(line, symbolize_names: true)
60
+ response = handle_request(request)
61
+ return unless response # 通知の場合はnilが返されるので、何も出力しない
62
+
63
+ response_json = JSON.generate(response)
64
+ $stdout.puts(response_json)
65
+ $stdout.flush
66
+ rescue JSON::ParserError => e
67
+ error_response(nil, Constants::ErrorCodes::INVALID_REQUEST, "Invalid JSON: #{e.message}")
68
+ rescue => e
69
+ error_response(nil, Constants::ErrorCodes::INTERNAL_ERROR, e.message)
70
+ end
71
+
72
+ def handle_request(request)
73
+ allowed_methods = [
74
+ Constants::RequestMethods::INITIALIZE,
75
+ Constants::RequestMethods::INITIALIZED,
76
+ Constants::RequestMethods::PING
77
+ ]
78
+ if !@initialized && !allowed_methods.include?(request[:method])
79
+ return error_response(request[:id], Constants::ErrorCodes::NOT_INITIALIZED, "Server not initialized")
80
+ end
81
+
82
+ case request[:method]
83
+ when Constants::RequestMethods::INITIALIZE then handle_initialize(request)
84
+ when Constants::RequestMethods::INITIALIZED then handle_initialized(request)
85
+ when Constants::RequestMethods::PING then handle_ping(request)
86
+ when Constants::RequestMethods::TOOLS_LIST then handle_list_tools(request)
87
+ when Constants::RequestMethods::TOOLS_CALL then handle_call_tool(request)
88
+ when Constants::RequestMethods::RESOURCES_LIST then handle_list_resources(request)
89
+ when Constants::RequestMethods::RESOURCES_READ then handle_read_resource(request)
90
+ when Constants::RequestMethods::RESOURCES_TEMPLATES_LIST then handle_list_resources_templates(request)
91
+ else
92
+ error_response(request[:id], Constants::ErrorCodes::METHOD_NOT_FOUND, "Unknown method: #{request[:method]}")
93
+ end
94
+ end
95
+
96
+ def handle_initialize(request)
97
+ return error_response(request[:id], Constants::ErrorCodes::ALREADY_INITIALIZED, "Server already initialized") if @initialized
98
+
99
+ client_version = request.dig(:params, :protocolVersion)
100
+ unless @supported_protocol_versions.include?(client_version)
101
+ return error_response(
102
+ request[:id],
103
+ Constants::ErrorCodes::UNSUPPORTED_PROTOCOL_VERSION,
104
+ "Unsupported protocol version",
105
+ {
106
+ supported: @supported_protocol_versions,
107
+ requested: client_version
108
+ }
109
+ )
110
+ end
111
+
112
+ {
113
+ jsonrpc: MCP::Constants::JSON_RPC_VERSION,
114
+ id: request[:id],
115
+ result: {
116
+ protocolVersion: PROTOCOL_VERSION,
117
+ capabilities: {
118
+ logging: {},
119
+ prompts: {
120
+ listChanged: false
121
+ },
122
+ resources: {
123
+ subscribe: false,
124
+ listChanged: false
125
+ },
126
+ tools: {
127
+ listChanged: false
128
+ }
129
+ },
130
+ serverInfo: {
131
+ name: @name,
132
+ version: @version
133
+ }
134
+ }
135
+ }
136
+ end
137
+
138
+ def handle_initialized(request)
139
+ return error_response(request[:id], Constants::ErrorCodes::ALREADY_INITIALIZED, "Server already initialized") if @initialized
140
+
141
+ @initialized = true
142
+ nil # 通知に対しては応答を返さない
143
+ end
144
+
145
+ def handle_list_tools(request)
146
+ cursor = request.dig(:params, :cursor)
147
+ result = @app.list_tools(cursor: cursor)
148
+ success_response(request[:id], result)
149
+ end
150
+
151
+ def handle_call_tool(request)
152
+ name = request.dig(:params, :name)
153
+ arguments = request.dig(:params, :arguments)
154
+ begin
155
+ result = @app.call_tool(name, **arguments.transform_keys(&:to_sym))
156
+ success_response(request[:id], result)
157
+ rescue ArgumentError => e
158
+ error_response(request[:id], Constants::ErrorCodes::INVALID_REQUEST, e.message)
159
+ end
160
+ end
161
+
162
+ def handle_list_resources(request)
163
+ cursor = request.dig(:params, :cursor)
164
+ result = @app.list_resources(cursor:)
165
+ success_response(request[:id], result)
166
+ end
167
+
168
+ def handle_read_resource(request)
169
+ uri = request.dig(:params, :uri)
170
+ result = @app.read_resource(uri)
171
+
172
+ if result
173
+ success_response(request[:id], result)
174
+ else
175
+ error_response(request[:id], Constants::ErrorCodes::INVALID_REQUEST, "Resource not found", {uri: uri})
176
+ end
177
+ end
178
+
179
+ def handle_ping(request)
180
+ success_response(request[:id], {})
181
+ end
182
+
183
+ def success_response(id, result)
184
+ {
185
+ jsonrpc: MCP::Constants::JSON_RPC_VERSION,
186
+ id: id,
187
+ result: result
188
+ }
189
+ end
190
+
191
+ def error_response(id, code, message, data = nil)
192
+ response = {
193
+ jsonrpc: MCP::Constants::JSON_RPC_VERSION,
194
+ id: id,
195
+ error: {
196
+ code: code,
197
+ message: message
198
+ }
199
+ }
200
+ response[:error][:data] = data if data
201
+ response
202
+ end
203
+ end
204
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MCP
4
+ PROTOCOL_VERSION = "2024-11-05"
5
+ VERSION = "0.1.0"
6
+ end
data/lib/mcp.rb ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "English"
4
+ require "json"
5
+
6
+ require_relative "mcp/version"
7
+ require_relative "mcp/constants"
8
+ require_relative "mcp/app"
9
+ require_relative "mcp/server"
10
+ require_relative "mcp/delegator"
11
+
12
+ module MCP
13
+ class << self
14
+ attr_reader :server
15
+
16
+ def initialize_server(name:, **options)
17
+ @server ||= Server.new(name: name, **options)
18
+ end
19
+ end
20
+
21
+ # require 'mcp' したファイルで最後に到達したら実行されるようにするため
22
+ # https://docs.ruby-lang.org/ja/latest/method/Kernel/m/at_exit.html
23
+ at_exit { server.run if $ERROR_INFO.nil? && server }
24
+
25
+ def self.new(**options, &block)
26
+ @server = Server.new(**options)
27
+ return @server if block.nil?
28
+
29
+ if block.arity.zero?
30
+ @server.instance_eval(&block)
31
+ else
32
+ (block.arity == 1) ? yield(@server) : yield
33
+ end
34
+
35
+ @server
36
+ end
37
+ end
38
+
39
+ extend MCP::Delegator # standard:disable Style/MixinUsage
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mcp-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - funwarioisii
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-02-12 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: MCP-RB is a Ruby framework that provides a Sinatra-like DSL for implementing
13
+ Model Context Protocol servers.
14
+ email:
15
+ - kazuyukihashimoto2006@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - LICENSE.txt
22
+ - README.md
23
+ - lib/mcp.rb
24
+ - lib/mcp/app.rb
25
+ - lib/mcp/app/resource.rb
26
+ - lib/mcp/app/tool.rb
27
+ - lib/mcp/constants.rb
28
+ - lib/mcp/delegator.rb
29
+ - lib/mcp/server.rb
30
+ - lib/mcp/version.rb
31
+ homepage: https://github.com/funwarioisii/mcp-rb
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://github.com/funwarioisii/mcp-rb
36
+ source_code_uri: https://github.com/funwarioisii/mcp-rb
37
+ changelog_uri: https://github.com/funwarioisii/mcp-rb/blob/main/CHANGELOG.md
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 3.0.0
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 3.6.2
53
+ specification_version: 4
54
+ summary: A lightweight Ruby framework for implementing MCP (Model Context Protocol)
55
+ servers
56
+ test_files: []