mcp-rb 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28e6b1fc53252ecd7717e71d06f0c124c61a8fbb9e389fa9514f3249b056930e
4
- data.tar.gz: 454af20d33b8cbe80dad61214b0ca3680a8715bef024d4d26dd77a5b800e37ba
3
+ metadata.gz: 131c29522f8eb44f7f613f51d92db433370d99950dcfcd27340ab9917b306dd3
4
+ data.tar.gz: a676110b09851f7018205bc012b09a6f22d1c7730555ba569b9b7ae475c06009
5
5
  SHA512:
6
- metadata.gz: 6aca66d612f5851304248561e0eb73402ef30d30da6fd45c35aea89b43d6499c29da58a37f6deae55931a3ede9eb56de4cf447595adaee8f227836e8923f1823
7
- data.tar.gz: cde9c8f9b18b6945d5e5e478ea18e1592c372393b1263f329b35bc0510df58110feda73ae1fcd785d93af0e701fe6bfaab200296845d7b601ba33525337f654c
6
+ metadata.gz: ebe914ec0fa9ba0ef97650f83454d0fe5bf5c924717365d5cea3bdd73bfb06b5e0ede7d49c6da2caa8d38c5a337bae3c98c22b86d5f1c25740785c66ac195509
7
+ data.tar.gz: 719f1f95950ca24c0695316af978866e1b8834b5112c962701324d8279faa9e5705151157d23a66e7f9e6572313df18f7c4acfc247037965d059f12bdcacb883
data/CHANGELOG.md CHANGED
@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.3.0] - 2025-02-19
9
+
10
+ - Allow specifying the version via DSL keyword: https://github.com/funwarioisii/mcp-rb/pull/2
11
+ - Add MCP Client: https://github.com/funwarioisii/mcp-rb/pull/3
12
+
13
+ ### Breaking Changes
14
+ - `MCP::PROTOCOL_VERSION` is moved to `MCP::Constants::PROTOCOL_VERSION`
15
+ - https://github.com/funwarioisii/mcp-rb/pull/3/commits/caad65500935a8eebfe024dbd25de0d16868c44e
16
+
8
17
  ## [0.2.0] - 2025-02-14
9
18
 
10
19
  ### Breaking Changes
data/README.md CHANGED
@@ -36,6 +36,23 @@ tool "greet" do
36
36
  end
37
37
  ```
38
38
 
39
+ ## Supported specifications
40
+
41
+ Reference: [MCP 2024-11-05](https://spec.modelcontextprotocol.io/specification/2024-11-05/)
42
+
43
+ - [Base Protocol](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/)
44
+ - ping
45
+ - stdio transport
46
+ - [Server features](https://spec.modelcontextprotocol.io/specification/2024-11-05/server/)
47
+ - Resources
48
+ - resources/read
49
+ - resources/list
50
+ - Tools
51
+ - tools/list
52
+ - tools/call
53
+
54
+ Any capabilities are not supported yet.
55
+
39
56
  ## Testing
40
57
 
41
58
  ```bash
data/lib/mcp/client.rb ADDED
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "open3"
5
+ require "securerandom"
6
+ require_relative "constants"
7
+
8
+ module MCP
9
+ class Client
10
+ attr_reader :command, :args, :process, :stdin, :stdout, :stderr, :wait_thread
11
+
12
+ def initialize(command:, args: [], name: "mcp-client", version: VERSION)
13
+ @command = command
14
+ @args = args
15
+ @process = nil
16
+ @name = name
17
+ @version = version
18
+ end
19
+
20
+ def connect
21
+ return if @process
22
+
23
+ start_server
24
+ initialize_connection
25
+ self
26
+ end
27
+
28
+ def running? = !@process.nil?
29
+
30
+ def list_tools
31
+ ensure_running
32
+ send_request({
33
+ jsonrpc: Constants::JSON_RPC_VERSION,
34
+ method: Constants::RequestMethods::TOOLS_LIST,
35
+ params: {},
36
+ id: SecureRandom.uuid
37
+ })
38
+ end
39
+
40
+ def call_tool(name:, args: {})
41
+ ensure_running
42
+ send_request({
43
+ jsonrpc: Constants::JSON_RPC_VERSION,
44
+ method: Constants::RequestMethods::TOOLS_CALL,
45
+ params: {
46
+ name: name,
47
+ arguments: args
48
+ },
49
+ id: SecureRandom.uuid
50
+ })
51
+ end
52
+
53
+ def close
54
+ return unless @process
55
+
56
+ @stdin.close
57
+ @stdout.close
58
+ @stderr.close
59
+ Process.kill("TERM", @process)
60
+ @wait_thread.join
61
+ @process = nil
62
+ rescue IOError, Errno::ESRCH
63
+ # プロセスが既に終了している場合は無視
64
+ @process = nil
65
+ end
66
+
67
+ private
68
+
69
+ def ensure_running
70
+ raise "Server process not running. Call #start first." unless running?
71
+ end
72
+
73
+ def initialize_connection
74
+ response = send_request({
75
+ jsonrpc: Constants::JSON_RPC_VERSION,
76
+ method: "initialize",
77
+ params: {
78
+ protocolVersion: Constants::PROTOCOL_VERSION,
79
+ client: {
80
+ name: @name,
81
+ version: @version
82
+ }
83
+ },
84
+ id: SecureRandom.uuid
85
+ })
86
+
87
+ @stdin.puts(JSON.generate({
88
+ jsonrpc: Constants::JSON_RPC_VERSION,
89
+ method: "notifications/initialized"
90
+ }))
91
+
92
+ response
93
+ end
94
+
95
+ def start_server
96
+ @stdin, @stdout, @stderr, @wait_thread = Open3.popen3(@command, *@args)
97
+ @process = @wait_thread.pid
98
+
99
+ Thread.new do
100
+ while (line = @stderr.gets)
101
+ warn "[MCP Server] #{line}"
102
+ end
103
+ rescue IOError
104
+ # ignore when stream is closed
105
+ end
106
+ end
107
+
108
+ def send_request(request)
109
+ @stdin.puts(JSON.generate(request))
110
+ response = @stdout.gets
111
+ raise "No response from server" unless response
112
+
113
+ result = JSON.parse(response, symbolize_names: true)
114
+ if result[:error]
115
+ raise "Server error: #{result[:error][:message]} (#{result[:error][:code]})"
116
+ end
117
+
118
+ result[:result]
119
+ rescue JSON::ParserError => e
120
+ raise "Invalid JSON response: #{e.message}"
121
+ end
122
+ end
123
+ end
data/lib/mcp/constants.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  module MCP
4
4
  module Constants
5
5
  JSON_RPC_VERSION = "2.0"
6
+ PROTOCOL_VERSION = "2024-11-05"
6
7
 
7
8
  module ErrorCodes
8
9
  NOT_INITIALIZED = -32_002
data/lib/mcp/delegator.rb CHANGED
@@ -15,6 +15,6 @@ module MCP
15
15
  end
16
16
  end
17
17
 
18
- delegate :name, :resource, :tool
18
+ delegate :name, :version, :resource, :tool
19
19
  end
20
20
  end
data/lib/mcp/server.rb CHANGED
@@ -3,26 +3,33 @@
3
3
  require "json"
4
4
  require "English"
5
5
  require "uri"
6
+ require_relative "constants"
6
7
 
7
8
  module MCP
8
9
  class Server
9
- attr_accessor :name
10
+ attr_writer :name, :version
10
11
  attr_reader :initialized
11
12
 
12
- def initialize(name:, version: VERSION)
13
+ def initialize(name:, version: "0.1.0")
13
14
  @name = name
14
15
  @version = version
15
16
  @app = App.new
16
17
  @initialized = false
17
- @supported_protocol_versions = [PROTOCOL_VERSION]
18
+ @supported_protocol_versions = [Constants::PROTOCOL_VERSION]
18
19
  end
19
20
 
20
- def name(value = nil) # standard:disable Lint/DuplicateMethods
21
+ def name(value = nil)
21
22
  return @name if value.nil?
22
23
 
23
24
  @name = value
24
25
  end
25
26
 
27
+ def version(value = nil)
28
+ return @version if value.nil?
29
+
30
+ @version = value
31
+ end
32
+
26
33
  def tool(name, &block)
27
34
  @app.register_tool(name, &block)
28
35
  end
@@ -113,7 +120,7 @@ module MCP
113
120
  jsonrpc: MCP::Constants::JSON_RPC_VERSION,
114
121
  id: request[:id],
115
122
  result: {
116
- protocolVersion: PROTOCOL_VERSION,
123
+ protocolVersion: Constants::PROTOCOL_VERSION,
117
124
  capabilities: {
118
125
  logging: {},
119
126
  prompts: {
data/lib/mcp/version.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MCP
4
- PROTOCOL_VERSION = "2024-11-05"
5
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
6
5
  end
data/lib/mcp.rb CHANGED
@@ -8,6 +8,7 @@ require_relative "mcp/constants"
8
8
  require_relative "mcp/app"
9
9
  require_relative "mcp/server"
10
10
  require_relative "mcp/delegator"
11
+ require_relative "mcp/client"
11
12
 
12
13
  module MCP
13
14
  class << self
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mcp-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - funwarioisii
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-14 00:00:00.000000000 Z
10
+ date: 2025-02-19 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: MCP-RB is a Ruby framework that provides a Sinatra-like DSL for implementing
13
13
  Model Context Protocol servers.
@@ -24,6 +24,7 @@ files:
24
24
  - lib/mcp/app.rb
25
25
  - lib/mcp/app/resource.rb
26
26
  - lib/mcp/app/tool.rb
27
+ - lib/mcp/client.rb
27
28
  - lib/mcp/constants.rb
28
29
  - lib/mcp/delegator.rb
29
30
  - lib/mcp/server.rb
@@ -49,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
50
  - !ruby/object:Gem::Version
50
51
  version: '0'
51
52
  requirements: []
52
- rubygems_version: 3.6.2
53
+ rubygems_version: 3.6.3
53
54
  specification_version: 4
54
55
  summary: A lightweight Ruby framework for implementing MCP (Model Context Protocol)
55
56
  servers