elelem-mcp 0.1.0 → 0.2.1
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 +4 -4
- data/.github/workflows/release.yml +61 -0
- data/.release-please-manifest.json +3 -0
- data/.rspec +1 -0
- data/.rubocop.yml +66 -0
- data/CHANGELOG.md +16 -0
- data/Rakefile +5 -1
- data/lib/elelem/mcp/client.rb +64 -29
- data/lib/elelem/mcp/oauth.rb +235 -0
- data/lib/elelem/mcp/version.rb +1 -1
- data/lib/elelem/mcp.rb +42 -14
- data/release-please-config.json +12 -0
- data/spec/elelem/mcp/client_spec.rb +61 -0
- data/spec/elelem/mcp/http_server_spec.rb +103 -0
- data/spec/elelem/mcp/oauth_spec.rb +122 -0
- data/spec/elelem/mcp/token_storage_spec.rb +66 -0
- data/spec/elelem/mcp_spec.rb +39 -0
- data/spec/fixtures/echo_server.rb +24 -0
- data/spec/fixtures/noisy_server.rb +5 -0
- data/spec/spec_helper.rb +11 -0
- metadata +78 -14
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0fa285432462756c2dc5cf154f06cd1de71d56e812cf5bfd4c9b1227bb0dec65
|
|
4
|
+
data.tar.gz: a56578d33d6c074448058fbde0eceb622809307142bd0b0be4f7afeb918da6bf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 67b4640e9b79c5c025f125173b6e9a8c6d4e1e6c2802d27d94b9f1e0327ddae81b84fcf9ece500aa8165525a9d1382903e2f0b6760e03512968350dcd2843bb0
|
|
7
|
+
data.tar.gz: 657b1a123830063c11671a17fd292ff756550d5ee6d6a6166fc3b04fff9b48811d8ecd4f578c51793cdebc315a2d95bb1105ab58cf52bb69074ffb9a8029bd34
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
name: Release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
permissions: {}
|
|
9
|
+
|
|
10
|
+
concurrency:
|
|
11
|
+
group: release
|
|
12
|
+
cancel-in-progress: false
|
|
13
|
+
|
|
14
|
+
jobs:
|
|
15
|
+
test:
|
|
16
|
+
runs-on: ubuntu-latest
|
|
17
|
+
permissions:
|
|
18
|
+
contents: read
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
|
|
21
|
+
with:
|
|
22
|
+
persist-credentials: false
|
|
23
|
+
- uses: ruby/setup-ruby@3ff19f5e2baf30647122352b96108b1fbe250c64 # v1.299.0
|
|
24
|
+
with:
|
|
25
|
+
ruby-version: "4.0"
|
|
26
|
+
bundler-cache: true
|
|
27
|
+
- run: bin/test
|
|
28
|
+
- run: bin/lint
|
|
29
|
+
|
|
30
|
+
release-please:
|
|
31
|
+
needs: test
|
|
32
|
+
runs-on: ubuntu-latest
|
|
33
|
+
permissions:
|
|
34
|
+
contents: write
|
|
35
|
+
issues: write
|
|
36
|
+
pull-requests: write
|
|
37
|
+
outputs:
|
|
38
|
+
release_created: ${{ steps.release.outputs.release_created }}
|
|
39
|
+
steps:
|
|
40
|
+
- id: release
|
|
41
|
+
uses: googleapis/release-please-action@45996ed1f6d02564a971a2fa1b5860e934307cf7 # v5.0.0
|
|
42
|
+
with:
|
|
43
|
+
config-file: release-please-config.json
|
|
44
|
+
manifest-file: .release-please-manifest.json
|
|
45
|
+
|
|
46
|
+
publish:
|
|
47
|
+
needs: release-please
|
|
48
|
+
if: needs.release-please.outputs.release_created == 'true'
|
|
49
|
+
runs-on: ubuntu-latest
|
|
50
|
+
permissions:
|
|
51
|
+
contents: write
|
|
52
|
+
id-token: write
|
|
53
|
+
steps:
|
|
54
|
+
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
|
|
55
|
+
with:
|
|
56
|
+
persist-credentials: false
|
|
57
|
+
- uses: ruby/setup-ruby@3ff19f5e2baf30647122352b96108b1fbe250c64 # v1.299.0
|
|
58
|
+
with:
|
|
59
|
+
ruby-version: "4.0"
|
|
60
|
+
bundler-cache: true
|
|
61
|
+
- uses: rubygems/release-gem@7f9650160c1a4e7989fdc9855807bdbd421d8b6b # v1.4.1
|
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--require spec_helper
|
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
NewCops: enable
|
|
3
|
+
TargetRubyVersion: 4.0
|
|
4
|
+
SuggestExtensions: false
|
|
5
|
+
Exclude:
|
|
6
|
+
- "pkg/**/*"
|
|
7
|
+
- "bin/**/*"
|
|
8
|
+
- "vendor/**/*"
|
|
9
|
+
- "fixtures/**/*"
|
|
10
|
+
|
|
11
|
+
Metrics:
|
|
12
|
+
Enabled: false
|
|
13
|
+
|
|
14
|
+
# exe/elelem runs with -W0, which silences Kernel#warn.
|
|
15
|
+
Style/StderrPuts:
|
|
16
|
+
Enabled: false
|
|
17
|
+
|
|
18
|
+
Naming/PredicateMethod:
|
|
19
|
+
AllowedMethods:
|
|
20
|
+
- call
|
|
21
|
+
- run
|
|
22
|
+
- summarize
|
|
23
|
+
- prompt
|
|
24
|
+
|
|
25
|
+
Naming/MethodParameterName:
|
|
26
|
+
AllowedNames:
|
|
27
|
+
- me
|
|
28
|
+
- id
|
|
29
|
+
- as
|
|
30
|
+
- io
|
|
31
|
+
|
|
32
|
+
Style/GlobalVars:
|
|
33
|
+
Exclude:
|
|
34
|
+
- "spec/**/*"
|
|
35
|
+
|
|
36
|
+
Lint/EmptyBlock:
|
|
37
|
+
Exclude:
|
|
38
|
+
- "spec/**/*"
|
|
39
|
+
|
|
40
|
+
Lint/UselessDefaultValueArgument:
|
|
41
|
+
Enabled: false
|
|
42
|
+
|
|
43
|
+
Lint/RedundantRequireStatement:
|
|
44
|
+
Enabled: false
|
|
45
|
+
|
|
46
|
+
Lint/UnusedBlockArgument:
|
|
47
|
+
IgnoreEmptyBlocks: true
|
|
48
|
+
AllowUnusedKeywordArguments: true
|
|
49
|
+
|
|
50
|
+
Style/Documentation:
|
|
51
|
+
Enabled: false
|
|
52
|
+
|
|
53
|
+
Style/FormatStringToken:
|
|
54
|
+
Enabled: false
|
|
55
|
+
|
|
56
|
+
Style/FrozenStringLiteralComment:
|
|
57
|
+
Enabled: true
|
|
58
|
+
|
|
59
|
+
Style/StringLiterals:
|
|
60
|
+
EnforcedStyle: double_quotes
|
|
61
|
+
|
|
62
|
+
Style/StringLiteralsInInterpolation:
|
|
63
|
+
EnforcedStyle: double_quotes
|
|
64
|
+
|
|
65
|
+
Layout/LineLength:
|
|
66
|
+
Max: 120
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.2.1](https://github.com/elelem-sh/mcp/compare/v0.2.0...v0.2.1) (2026-11-16)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* drain MCP server stderr, wait for servers in elelem ask, and accept type stdio (elelem-sh/elelem.sh[#45](https://github.com/elelem-sh/mcp/issues/45)) ([e8d2ed6](https://github.com/elelem-sh/mcp/commit/e8d2ed6ee292cf2c1274bc866f682317a6248ca8))
|
|
9
|
+
* drop the digest dependency, which ships with ruby and clashes with the publish job ([e8fd648](https://github.com/elelem-sh/mcp/commit/e8fd648ca0658399a93887d02d413fd68bea7255))
|
|
10
|
+
* load each MCP server on its own and print the ones that fail ([67b0939](https://github.com/elelem-sh/mcp/commit/67b0939646d6ddd3eb7c1846169de2fa334a605c))
|
|
11
|
+
* MCP asks for protocol 2025-11-25, keeps the server's answer, and sends it over HTTP (elelem-sh/elelem.sh[#33](https://github.com/elelem-sh/mcp/issues/33)) ([dbd9f96](https://github.com/elelem-sh/mcp/commit/dbd9f96b711c764ed329bfe09af82e2e45209f3e))
|
|
12
|
+
* name the unsupported MCP server type in the error ([cd5f8fc](https://github.com/elelem-sh/mcp/commit/cd5f8fc08f33252dbb6fa2f49723461fadb47629))
|
|
13
|
+
* OAuth discovery keeps the server's port (elelem-sh/elelem.sh[#33](https://github.com/elelem-sh/mcp/issues/33)) ([6f81560](https://github.com/elelem-sh/mcp/commit/6f81560c76f8a434e62e5589e59d4cc00a916cf1))
|
|
14
|
+
* relock elelem to 0.11.0, a real published version ([b0f0e6b](https://github.com/elelem-sh/mcp/commit/b0f0e6b68423df31a406434b236dd6eef5e15bd9))
|
|
15
|
+
* require elelem ~> 0.12, the floor builtins/skills actually need ([c26b32d](https://github.com/elelem-sh/mcp/commit/c26b32dcaba7d7952a02f2b79f29cb1d4fbd0d32))
|
|
16
|
+
* the OAuth callback listens on 127.0.0.1, and tool payloads log at debug (elelem-sh/elelem.sh[#5](https://github.com/elelem-sh/mcp/issues/5)) ([1d19539](https://github.com/elelem-sh/mcp/commit/1d19539e4b342c41e813923470265f6fc29b4d54))
|
data/Rakefile
CHANGED
data/lib/elelem/mcp/client.rb
CHANGED
|
@@ -18,20 +18,22 @@ module Elelem
|
|
|
18
18
|
@servers = {}
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def
|
|
22
|
-
@config.fetch("mcpServers", {}).
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
21
|
+
def server_names
|
|
22
|
+
@config.fetch("mcpServers", {}).keys
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def tools_for(name)
|
|
26
|
+
server(name).tools.to_h do |tool|
|
|
27
|
+
[
|
|
28
|
+
"#{name}_#{tool["name"]}",
|
|
29
|
+
{
|
|
30
|
+
description: tool["description"],
|
|
31
|
+
params: symbolize(tool.dig("inputSchema", "properties") || {}),
|
|
32
|
+
required: tool.dig("inputSchema", "required") || [],
|
|
33
|
+
fn: ->(a) { server(name).call(tool["name"], a) }
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
end
|
|
35
37
|
end
|
|
36
38
|
|
|
37
39
|
def close
|
|
@@ -40,12 +42,16 @@ module Elelem
|
|
|
40
42
|
|
|
41
43
|
private
|
|
42
44
|
|
|
45
|
+
def symbolize(hash)
|
|
46
|
+
JSON.parse(JSON.generate(hash), symbolize_names: true)
|
|
47
|
+
end
|
|
48
|
+
|
|
43
49
|
def load_config(configurations)
|
|
44
50
|
configurations.each_with_object({}) do |path, merged|
|
|
45
51
|
file = File.expand_path(path)
|
|
46
52
|
next unless File.exist?(file)
|
|
47
53
|
|
|
48
|
-
config = JSON.parse(
|
|
54
|
+
config = JSON.parse(File.read(file))
|
|
49
55
|
servers = config.fetch("mcpServers", {})
|
|
50
56
|
merged["mcpServers"] = (merged["mcpServers"] || {}).merge(servers)
|
|
51
57
|
end
|
|
@@ -56,22 +62,28 @@ module Elelem
|
|
|
56
62
|
end
|
|
57
63
|
|
|
58
64
|
def build_server(config)
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
65
|
+
config = config.dup
|
|
66
|
+
type = config.delete("type") || (config.key?("url") ? "http" : "stdio")
|
|
67
|
+
case type
|
|
68
|
+
when "http" then HttpServer.new(url: config["url"], headers: config["headers"] || {})
|
|
69
|
+
when "stdio" then Server.new(**config.transform_keys(&:to_sym))
|
|
70
|
+
else raise "unsupported type: #{type}"
|
|
63
71
|
end
|
|
64
72
|
end
|
|
65
73
|
end
|
|
66
74
|
|
|
75
|
+
PROTOCOL_VERSION = "2025-11-25"
|
|
76
|
+
|
|
67
77
|
module ServerInterface
|
|
78
|
+
attr_reader :protocol_version
|
|
79
|
+
|
|
68
80
|
def tools
|
|
69
81
|
request("tools/list")["tools"]
|
|
70
82
|
end
|
|
71
83
|
|
|
72
84
|
def call(name, args)
|
|
73
85
|
result = request("tools/call", { name: name, arguments: args })
|
|
74
|
-
Elelem.logger.
|
|
86
|
+
Elelem.logger.debug({ tool: name, args: args, result: result }.to_json)
|
|
75
87
|
content = extract_content(result)
|
|
76
88
|
result["isError"] ? { error: content } : { content: content }
|
|
77
89
|
end
|
|
@@ -86,12 +98,15 @@ module Elelem
|
|
|
86
98
|
|
|
87
99
|
private
|
|
88
100
|
|
|
101
|
+
# Asks for the version this client follows, then speaks whichever the
|
|
102
|
+
# server answers with.
|
|
89
103
|
def handshake!
|
|
90
|
-
request("initialize", {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
104
|
+
result = request("initialize", {
|
|
105
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
106
|
+
capabilities: {},
|
|
107
|
+
clientInfo: { name: "elelem", version: Elelem::MCP::VERSION }
|
|
108
|
+
})
|
|
109
|
+
@protocol_version = result&.dig("protocolVersion") || PROTOCOL_VERSION
|
|
95
110
|
notify("notifications/initialized")
|
|
96
111
|
end
|
|
97
112
|
end
|
|
@@ -101,9 +116,12 @@ module Elelem
|
|
|
101
116
|
|
|
102
117
|
def initialize(command:, args: [], env: {})
|
|
103
118
|
resolved_env = env.transform_values do |v|
|
|
104
|
-
v.gsub(/\$\{(\w+)\}/)
|
|
119
|
+
v.gsub(/\$\{(\w+)\}/) do
|
|
120
|
+
ENV[::Regexp.last_match(1)] || raise("Missing environment variable: #{::Regexp.last_match(1)}")
|
|
121
|
+
end
|
|
105
122
|
end
|
|
106
123
|
@stdin, @stdout, @stderr, @wait = Open3.popen3(resolved_env, command, *args)
|
|
124
|
+
@drain = drain(@stderr)
|
|
107
125
|
@id = 0
|
|
108
126
|
handshake!
|
|
109
127
|
end
|
|
@@ -115,12 +133,22 @@ module Elelem
|
|
|
115
133
|
nil
|
|
116
134
|
end
|
|
117
135
|
@wait.kill
|
|
136
|
+
@drain.join(1)
|
|
118
137
|
rescue StandardError
|
|
119
138
|
nil
|
|
120
139
|
end
|
|
121
140
|
|
|
122
141
|
private
|
|
123
142
|
|
|
143
|
+
# A server that writes to stderr would block once the pipe fills, so read it.
|
|
144
|
+
def drain(io)
|
|
145
|
+
Thread.new do
|
|
146
|
+
io.each_line { |line| Elelem.logger.debug("mcp stderr: #{line.chomp}") }
|
|
147
|
+
rescue IOError
|
|
148
|
+
nil
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
124
152
|
def request(method, params = {})
|
|
125
153
|
send_msg(id: @id += 1, method: method, params: params)
|
|
126
154
|
read_response(@id)
|
|
@@ -157,6 +185,7 @@ module Elelem
|
|
|
157
185
|
@open_timeout = open_timeout
|
|
158
186
|
@id = 0
|
|
159
187
|
@session_id = nil
|
|
188
|
+
@access_token = nil
|
|
160
189
|
handshake!
|
|
161
190
|
end
|
|
162
191
|
|
|
@@ -167,7 +196,7 @@ module Elelem
|
|
|
167
196
|
def resolve_headers(headers)
|
|
168
197
|
headers.transform_values do |v|
|
|
169
198
|
v.gsub(/\$\{(\w+)\}/) do
|
|
170
|
-
ENV[
|
|
199
|
+
ENV[::Regexp.last_match(1)] || raise("Missing environment variable: #{::Regexp.last_match(1)}")
|
|
171
200
|
end
|
|
172
201
|
end
|
|
173
202
|
end
|
|
@@ -185,7 +214,7 @@ module Elelem
|
|
|
185
214
|
post(msg)
|
|
186
215
|
end
|
|
187
216
|
|
|
188
|
-
def post(msg)
|
|
217
|
+
def post(msg, retry_auth: true)
|
|
189
218
|
request = Net::HTTP::Post.new(@uri)
|
|
190
219
|
request["content-type"] = "application/json"
|
|
191
220
|
request_headers.each { |k, v| request[k] = v }
|
|
@@ -204,7 +233,10 @@ module Elelem
|
|
|
204
233
|
return parse_response(response)
|
|
205
234
|
when Net::HTTPUnauthorized
|
|
206
235
|
response.body
|
|
207
|
-
raise "
|
|
236
|
+
raise "Authorization failed" unless retry_auth
|
|
237
|
+
|
|
238
|
+
@access_token = OAuth.new(@uri.to_s).token
|
|
239
|
+
return post(msg, retry_auth: false)
|
|
208
240
|
else
|
|
209
241
|
raise "HTTP #{response.code}: #{response.body}"
|
|
210
242
|
end
|
|
@@ -215,6 +247,9 @@ module Elelem
|
|
|
215
247
|
def request_headers
|
|
216
248
|
base = { "Accept" => "application/json, text/event-stream" }
|
|
217
249
|
base["Mcp-Session-Id"] = @session_id if @session_id
|
|
250
|
+
# Streamable HTTP needs the negotiated version on every request after initialize.
|
|
251
|
+
base["MCP-Protocol-Version"] = @protocol_version if @protocol_version
|
|
252
|
+
base["Authorization"] = "Bearer #{@access_token}" if @access_token
|
|
218
253
|
@headers.merge(base)
|
|
219
254
|
end
|
|
220
255
|
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Elelem
|
|
4
|
+
module MCP
|
|
5
|
+
class OAuth
|
|
6
|
+
CALLBACK_PORT = 18_273
|
|
7
|
+
REDIRECT_URI = "http://127.0.0.1:#{CALLBACK_PORT}/callback".freeze
|
|
8
|
+
|
|
9
|
+
def initialize(resource_url, http: HttpAdapter.new)
|
|
10
|
+
@resource_url = resource_url
|
|
11
|
+
@http = http
|
|
12
|
+
@storage = TokenStorage.new
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def token
|
|
16
|
+
stored = @storage.load(@resource_url)
|
|
17
|
+
return stored[:access_token] if stored && !expired?(stored)
|
|
18
|
+
return refresh(stored[:refresh_token]) if stored&.dig(:refresh_token)
|
|
19
|
+
|
|
20
|
+
authorize
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def expired?(stored)
|
|
26
|
+
return false unless stored[:expires_at]
|
|
27
|
+
|
|
28
|
+
Time.now.to_i >= stored[:expires_at] - 60
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def authorize
|
|
32
|
+
metadata = discover_auth_server
|
|
33
|
+
client = load_or_register_client(metadata)
|
|
34
|
+
verifier, challenge = generate_pkce
|
|
35
|
+
state = SecureRandom.hex(16)
|
|
36
|
+
|
|
37
|
+
auth_url = build_auth_url(metadata, client, challenge, state)
|
|
38
|
+
open_browser(auth_url)
|
|
39
|
+
|
|
40
|
+
code = wait_for_callback(state)
|
|
41
|
+
tokens = exchange_code(metadata, client, code, verifier)
|
|
42
|
+
|
|
43
|
+
@storage.save(
|
|
44
|
+
@resource_url,
|
|
45
|
+
access_token: tokens["access_token"],
|
|
46
|
+
refresh_token: tokens["refresh_token"],
|
|
47
|
+
expires_in: tokens["expires_in"]
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
tokens["access_token"]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def refresh(refresh_token)
|
|
54
|
+
metadata = discover_auth_server
|
|
55
|
+
client = load_or_register_client(metadata)
|
|
56
|
+
uri = URI.parse(metadata["token_endpoint"])
|
|
57
|
+
|
|
58
|
+
body = {
|
|
59
|
+
grant_type: "refresh_token",
|
|
60
|
+
refresh_token: refresh_token,
|
|
61
|
+
client_id: client[:client_id]
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
response = post_form(uri, body)
|
|
65
|
+
tokens = JSON.parse(response.body)
|
|
66
|
+
|
|
67
|
+
@storage.save(
|
|
68
|
+
@resource_url,
|
|
69
|
+
access_token: tokens["access_token"],
|
|
70
|
+
refresh_token: tokens["refresh_token"] || refresh_token,
|
|
71
|
+
expires_in: tokens["expires_in"]
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
tokens["access_token"]
|
|
75
|
+
rescue StandardError => e
|
|
76
|
+
$stderr.puts "elelem-mcp: token refresh failed: #{e.message}"
|
|
77
|
+
authorize
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def discover_auth_server
|
|
81
|
+
metadata_url = URI.join(@resource_url, "/.well-known/oauth-protected-resource").to_s
|
|
82
|
+
|
|
83
|
+
resource_metadata = fetch_json(metadata_url)
|
|
84
|
+
auth_server_url = resource_metadata["authorization_servers"]&.first
|
|
85
|
+
raise "No authorization server found" unless auth_server_url
|
|
86
|
+
|
|
87
|
+
auth_metadata_url = "#{auth_server_url}/.well-known/oauth-authorization-server"
|
|
88
|
+
fetch_json(auth_metadata_url)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def load_or_register_client(metadata)
|
|
92
|
+
stored = @storage.load_client(@resource_url)
|
|
93
|
+
return stored if stored
|
|
94
|
+
|
|
95
|
+
client = register_client(metadata)
|
|
96
|
+
@storage.save_client(@resource_url, client)
|
|
97
|
+
@storage.load_client(@resource_url)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def register_client(metadata)
|
|
101
|
+
endpoint = metadata["registration_endpoint"]
|
|
102
|
+
raise "Dynamic registration not supported" unless endpoint
|
|
103
|
+
|
|
104
|
+
body = {
|
|
105
|
+
client_name: "elelem",
|
|
106
|
+
redirect_uris: [REDIRECT_URI],
|
|
107
|
+
grant_types: %w[authorization_code refresh_token],
|
|
108
|
+
response_types: ["code"],
|
|
109
|
+
token_endpoint_auth_method: "none"
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
response = post_json(endpoint, body)
|
|
113
|
+
JSON.parse(response.body)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def generate_pkce
|
|
117
|
+
verifier = SecureRandom.urlsafe_base64(32)
|
|
118
|
+
challenge = Base64.urlsafe_encode64(
|
|
119
|
+
Digest::SHA256.digest(verifier),
|
|
120
|
+
padding: false
|
|
121
|
+
)
|
|
122
|
+
[verifier, challenge]
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def build_auth_url(metadata, client, challenge, state)
|
|
126
|
+
params = {
|
|
127
|
+
response_type: "code",
|
|
128
|
+
client_id: client[:client_id],
|
|
129
|
+
redirect_uri: REDIRECT_URI,
|
|
130
|
+
scope: metadata["scopes_supported"]&.join(" ") || "openid",
|
|
131
|
+
state: state,
|
|
132
|
+
code_challenge: challenge,
|
|
133
|
+
code_challenge_method: "S256"
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
"#{metadata["authorization_endpoint"]}?#{URI.encode_www_form(params)}"
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def open_browser(url)
|
|
140
|
+
commands = %w[xdg-open open start]
|
|
141
|
+
opened = commands.any? { |cmd| system(cmd, url, out: File::NULL, err: File::NULL) }
|
|
142
|
+
$stderr.puts "Open this URL in your browser: #{url}" unless opened
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def wait_for_callback(expected_state)
|
|
146
|
+
code = nil
|
|
147
|
+
@server = WEBrick::HTTPServer.new(
|
|
148
|
+
Port: CALLBACK_PORT,
|
|
149
|
+
BindAddress: "127.0.0.1",
|
|
150
|
+
Logger: WEBrick::Log.new(File::NULL),
|
|
151
|
+
AccessLog: []
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
at_exit { @server&.shutdown }
|
|
155
|
+
|
|
156
|
+
@server.mount_proc("/callback") do |req, res|
|
|
157
|
+
state = req.query["state"]
|
|
158
|
+
raise "State mismatch" unless state == expected_state
|
|
159
|
+
|
|
160
|
+
code = req.query["code"]
|
|
161
|
+
res.content_type = "text/html"
|
|
162
|
+
res.body = "<html><body><h1>Authorization complete</h1><p>You can close this window.</p></body></html>"
|
|
163
|
+
@server.shutdown
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
Timeout.timeout(120) { @server.start }
|
|
167
|
+
code
|
|
168
|
+
rescue Timeout::Error
|
|
169
|
+
@server.shutdown
|
|
170
|
+
raise "OAuth callback timed out"
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def exchange_code(metadata, client, code, verifier)
|
|
174
|
+
uri = URI.parse(metadata["token_endpoint"])
|
|
175
|
+
|
|
176
|
+
body = {
|
|
177
|
+
grant_type: "authorization_code",
|
|
178
|
+
code: code,
|
|
179
|
+
redirect_uri: REDIRECT_URI,
|
|
180
|
+
client_id: client[:client_id],
|
|
181
|
+
code_verifier: verifier
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
response = post_form(uri, body)
|
|
185
|
+
JSON.parse(response.body)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def fetch_json(url)
|
|
189
|
+
response = nil
|
|
190
|
+
@http.get(url) { |r| response = r }
|
|
191
|
+
JSON.parse(response.body)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def post_json(url, body)
|
|
195
|
+
response = nil
|
|
196
|
+
@http.post(
|
|
197
|
+
url,
|
|
198
|
+
headers: { "Content-Type" => "application/json" },
|
|
199
|
+
body: body.to_json
|
|
200
|
+
) { |r| response = r }
|
|
201
|
+
response
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def post_form(uri, body)
|
|
205
|
+
response = nil
|
|
206
|
+
@http.post(
|
|
207
|
+
uri.to_s,
|
|
208
|
+
headers: { "Content-Type" => "application/x-www-form-urlencoded" },
|
|
209
|
+
body: URI.encode_www_form(body)
|
|
210
|
+
) { |r| response = r }
|
|
211
|
+
response
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
class HttpAdapter
|
|
215
|
+
def get(url)
|
|
216
|
+
uri = URI.parse(url)
|
|
217
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |conn|
|
|
218
|
+
yield conn.get(uri.request_uri)
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def post(url, headers: {}, body: nil)
|
|
223
|
+
uri = URI.parse(url)
|
|
224
|
+
request = Net::HTTP::Post.new(uri)
|
|
225
|
+
headers.each { |k, v| request[k] = v }
|
|
226
|
+
request.body = body
|
|
227
|
+
|
|
228
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |conn|
|
|
229
|
+
yield conn.request(request)
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
end
|
data/lib/elelem/mcp/version.rb
CHANGED
data/lib/elelem/mcp.rb
CHANGED
|
@@ -1,33 +1,61 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "base64"
|
|
3
4
|
require "digest"
|
|
4
5
|
require "elelem"
|
|
5
6
|
require "fileutils"
|
|
6
7
|
require "json"
|
|
7
8
|
require "net/http"
|
|
8
9
|
require "open3"
|
|
10
|
+
require "securerandom"
|
|
11
|
+
require "timeout"
|
|
9
12
|
require "uri"
|
|
13
|
+
require "webrick"
|
|
10
14
|
|
|
11
15
|
require_relative "mcp/version"
|
|
12
16
|
require_relative "mcp/token_storage"
|
|
17
|
+
require_relative "mcp/oauth"
|
|
13
18
|
require_relative "mcp/client"
|
|
14
19
|
|
|
15
|
-
Elelem
|
|
16
|
-
|
|
20
|
+
module Elelem
|
|
21
|
+
module MCP
|
|
22
|
+
# Waits for every loader, but no more than timeout seconds in all.
|
|
23
|
+
def self.wait(threads, timeout: 30)
|
|
24
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
|
|
25
|
+
threads.each { |t| t.join([deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC), 0].max) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# A person at a terminal can start typing while servers load. A one-shot
|
|
29
|
+
# run, such as elelem ask, has to wait or its first turn has no MCP tools.
|
|
30
|
+
def self.interactive?(session)
|
|
31
|
+
!session.respond_to?(:input) || session.input.interactive?
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
Elelem.configure do |config|
|
|
37
|
+
config.setup(:mcp) do |session|
|
|
38
|
+
mcp = Elelem::MCP::Client.memoized
|
|
17
39
|
|
|
18
|
-
|
|
19
|
-
mcp.
|
|
20
|
-
|
|
40
|
+
# One thread per server, so a broken or slow server does not hold back the others.
|
|
41
|
+
threads = mcp.server_names.map do |server|
|
|
42
|
+
Thread.new do
|
|
43
|
+
mcp.tools_for(server).each do |name, tool|
|
|
44
|
+
next if session.toolbox.tools.key?(name)
|
|
21
45
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
46
|
+
session.toolbox.add(
|
|
47
|
+
name,
|
|
48
|
+
description: tool[:description],
|
|
49
|
+
params: tool[:params],
|
|
50
|
+
required: tool[:required],
|
|
51
|
+
&tool[:fn]
|
|
52
|
+
)
|
|
53
|
+
end
|
|
54
|
+
rescue StandardError => e
|
|
55
|
+
$stderr.puts "elelem-mcp: #{server}: #{e.message}"
|
|
56
|
+
end
|
|
29
57
|
end
|
|
30
|
-
|
|
31
|
-
|
|
58
|
+
Elelem::MCP.wait(threads) unless Elelem::MCP.interactive?(session)
|
|
59
|
+
threads
|
|
32
60
|
end
|
|
33
61
|
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"bootstrap-sha": "f51f7a671b8a01de32b68b83f9b6a92a0f91b206",
|
|
3
|
+
"packages": {
|
|
4
|
+
".": {
|
|
5
|
+
"release-type": "ruby",
|
|
6
|
+
"package-name": "elelem-mcp",
|
|
7
|
+
"version-file": "lib/elelem/mcp/version.rb",
|
|
8
|
+
"bump-minor-pre-major": true,
|
|
9
|
+
"include-component-in-tag": false
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
RSpec.describe Elelem::MCP::Client do
|
|
4
|
+
subject(:client) { described_class.new([config]) }
|
|
5
|
+
|
|
6
|
+
let(:dir) { Dir.mktmpdir }
|
|
7
|
+
let(:config) { File.join(dir, "mcp.json") }
|
|
8
|
+
let(:echo) { { "command" => RbConfig.ruby, "args" => [File.expand_path("../../fixtures/echo_server.rb", __dir__)] } }
|
|
9
|
+
|
|
10
|
+
before do
|
|
11
|
+
servers = { "echo" => echo, "broken" => { "command" => "/nonexistent/mcp" } }
|
|
12
|
+
File.write(config, JSON.generate("mcpServers" => servers))
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
after do
|
|
16
|
+
client.close
|
|
17
|
+
FileUtils.rm_rf(dir)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "lists every configured server" do
|
|
21
|
+
expect(client.server_names).to eq(%w[echo broken])
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "loads one server's tools when another fails" do
|
|
25
|
+
expect { client.tools_for("broken") }.to raise_error(SystemCallError)
|
|
26
|
+
expect(client.tools_for("echo").keys).to eq(["echo_say"])
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "answers when the server writes a lot to stderr" do
|
|
30
|
+
noisy = { "command" => RbConfig.ruby, "args" => [File.expand_path("../../fixtures/noisy_server.rb", __dir__)] }
|
|
31
|
+
File.write(config, JSON.generate("mcpServers" => { "noisy" => noisy }))
|
|
32
|
+
|
|
33
|
+
Timeout.timeout(10) { expect(client.tools_for("noisy").keys).to eq(["noisy_say"]) }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "accepts type stdio, and takes a url without a type as http" do
|
|
37
|
+
servers = { "s" => echo.merge("type" => "stdio"), "h" => { "url" => "http://127.0.0.1:1/mcp" } }
|
|
38
|
+
File.write(config, JSON.generate("mcpServers" => servers))
|
|
39
|
+
|
|
40
|
+
expect(client.tools_for("s").keys).to eq(["s_say"])
|
|
41
|
+
expect { client.tools_for("h") }.to raise_error(SystemCallError)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "names an unsupported type" do
|
|
45
|
+
File.write(config, JSON.generate("mcpServers" => { "x" => { "type" => "sse", "url" => "http://h/" } }))
|
|
46
|
+
|
|
47
|
+
expect { client.tools_for("x") }.to raise_error("unsupported type: sse")
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "asks for 2025-11-25 and keeps the version the server answers with" do
|
|
51
|
+
client.tools_for("echo")
|
|
52
|
+
server = client.send(:server, "echo")
|
|
53
|
+
|
|
54
|
+
expect(Elelem::MCP::PROTOCOL_VERSION).to eq("2025-11-25")
|
|
55
|
+
expect(server.protocol_version).to eq("2025-06-18")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "calls a loaded tool" do
|
|
59
|
+
expect(client.tools_for("echo")["echo_say"][:fn].call("text" => "hi")).to eq(content: "hi")
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
RSpec.describe Elelem::MCP::HttpServer do
|
|
4
|
+
let(:url) { "https://mcp.example.com/sse" }
|
|
5
|
+
let(:http) { double("http") }
|
|
6
|
+
|
|
7
|
+
describe "#parse_sse" do
|
|
8
|
+
subject { described_class.allocate }
|
|
9
|
+
|
|
10
|
+
it "parses single SSE event" do
|
|
11
|
+
response = double
|
|
12
|
+
allow(response).to receive(:read_body).and_yield("data: {\"result\": \"ok\"}\n\n")
|
|
13
|
+
|
|
14
|
+
result = subject.send(:parse_sse, response)
|
|
15
|
+
expect(result).to eq({ "result" => "ok" })
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "parses chunked SSE events" do
|
|
19
|
+
response = double
|
|
20
|
+
chunks = ["data: {\"id\"", ": 1}\n\ndata: {\"id\": 2}\n\n"]
|
|
21
|
+
allow(response).to receive(:read_body) do |&block|
|
|
22
|
+
chunks.each { |c| block.call(c) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
result = subject.send(:parse_sse, response)
|
|
26
|
+
expect(result).to eq({ "id" => 2 })
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "ignores non-data lines" do
|
|
30
|
+
response = double
|
|
31
|
+
allow(response).to receive(:read_body).and_yield("event: message\ndata: {\"value\": 42}\n\n")
|
|
32
|
+
|
|
33
|
+
result = subject.send(:parse_sse, response)
|
|
34
|
+
expect(result).to eq({ "value" => 42 })
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "returns last event when multiple present" do
|
|
38
|
+
response = double
|
|
39
|
+
allow(response).to receive(:read_body).and_yield("data: {\"n\": 1}\n\ndata: {\"n\": 2}\n\n")
|
|
40
|
+
|
|
41
|
+
result = subject.send(:parse_sse, response)
|
|
42
|
+
expect(result).to eq({ "n" => 2 })
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
describe "#parse_response" do
|
|
47
|
+
subject { described_class.allocate }
|
|
48
|
+
|
|
49
|
+
it "parses JSON response" do
|
|
50
|
+
response = double(content_type: "application/json", body: '{"tools": []}')
|
|
51
|
+
result = subject.send(:parse_response, response)
|
|
52
|
+
expect(result).to eq({ "tools" => [] })
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "parses SSE response" do
|
|
56
|
+
response = double(content_type: "text/event-stream")
|
|
57
|
+
allow(response).to receive(:read_body).and_yield("data: {\"ok\": true}\n\n")
|
|
58
|
+
|
|
59
|
+
result = subject.send(:parse_response, response)
|
|
60
|
+
expect(result).to eq({ "ok" => true })
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "returns nil for empty body" do
|
|
64
|
+
response = double(content_type: "application/json", body: "")
|
|
65
|
+
result = subject.send(:parse_response, response)
|
|
66
|
+
expect(result).to be_nil
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
describe "#request_headers" do
|
|
71
|
+
subject do
|
|
72
|
+
server = described_class.allocate
|
|
73
|
+
server.instance_variable_set(:@headers, {})
|
|
74
|
+
server.instance_variable_set(:@session_id, nil)
|
|
75
|
+
server.instance_variable_set(:@access_token, nil)
|
|
76
|
+
server
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "includes Accept header" do
|
|
80
|
+
headers = subject.send(:request_headers)
|
|
81
|
+
expect(headers["Accept"]).to eq("application/json, text/event-stream")
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it "includes the negotiated protocol version once there is one" do
|
|
85
|
+
expect(subject.send(:request_headers)).not_to have_key("MCP-Protocol-Version")
|
|
86
|
+
|
|
87
|
+
subject.instance_variable_set(:@protocol_version, "2025-11-25")
|
|
88
|
+
expect(subject.send(:request_headers)["MCP-Protocol-Version"]).to eq("2025-11-25")
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it "includes session ID when set" do
|
|
92
|
+
subject.instance_variable_set(:@session_id, "abc123")
|
|
93
|
+
headers = subject.send(:request_headers)
|
|
94
|
+
expect(headers["Mcp-Session-Id"]).to eq("abc123")
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it "includes authorization when access_token set" do
|
|
98
|
+
subject.instance_variable_set(:@access_token, "token123")
|
|
99
|
+
headers = subject.send(:request_headers)
|
|
100
|
+
expect(headers["Authorization"]).to eq("Bearer token123")
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
RSpec.describe Elelem::MCP::OAuth do
|
|
4
|
+
let(:resource_url) { "https://mcp.example.com/sse" }
|
|
5
|
+
let(:http) { double("http") }
|
|
6
|
+
let(:storage) { instance_double(Elelem::MCP::TokenStorage) }
|
|
7
|
+
|
|
8
|
+
subject { described_class.new(resource_url, http: http) }
|
|
9
|
+
|
|
10
|
+
before do
|
|
11
|
+
allow(Elelem::MCP::TokenStorage).to receive(:new).and_return(storage)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "#token" do
|
|
15
|
+
context "with valid cached token" do
|
|
16
|
+
it "returns cached access_token" do
|
|
17
|
+
allow(storage).to receive(:load).with(resource_url).and_return({
|
|
18
|
+
access_token: "cached_token",
|
|
19
|
+
expires_at: Time.now.to_i + 3600
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
expect(subject.token).to eq("cached_token")
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
context "with expired token and refresh_token" do
|
|
27
|
+
let(:auth_metadata) do
|
|
28
|
+
{
|
|
29
|
+
"authorization_endpoint" => "https://auth.example.com/authorize",
|
|
30
|
+
"token_endpoint" => "https://auth.example.com/token",
|
|
31
|
+
"registration_endpoint" => "https://auth.example.com/register"
|
|
32
|
+
}
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "refreshes using refresh_token" do
|
|
36
|
+
allow(storage).to receive(:load).with(resource_url).and_return({
|
|
37
|
+
access_token: "old_token",
|
|
38
|
+
refresh_token: "refresh_abc",
|
|
39
|
+
expires_at: Time.now.to_i - 100
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
allow(storage).to receive(:load_client).with(resource_url).and_return({
|
|
43
|
+
client_id: "elelem-123"
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
resource_response = double(body: { "authorization_servers" => ["https://auth.example.com"] }.to_json)
|
|
47
|
+
auth_response = double(body: auth_metadata.to_json)
|
|
48
|
+
token_response = double(body: { "access_token" => "new_token", "expires_in" => 3600 }.to_json)
|
|
49
|
+
|
|
50
|
+
allow(http).to receive(:get).with("https://mcp.example.com/.well-known/oauth-protected-resource").and_yield(resource_response)
|
|
51
|
+
allow(http).to receive(:get).with("https://auth.example.com/.well-known/oauth-authorization-server").and_yield(auth_response)
|
|
52
|
+
allow(http).to receive(:post).and_yield(token_response)
|
|
53
|
+
allow(storage).to receive(:save)
|
|
54
|
+
|
|
55
|
+
expect(subject.token).to eq("new_token")
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe "PKCE generation" do
|
|
61
|
+
it "generates valid verifier and challenge" do
|
|
62
|
+
verifier, challenge = subject.send(:generate_pkce)
|
|
63
|
+
|
|
64
|
+
expect(verifier.length).to be >= 43
|
|
65
|
+
expect(challenge.length).to be >= 43
|
|
66
|
+
expect(challenge).not_to include("+", "/", "=")
|
|
67
|
+
|
|
68
|
+
expected_challenge = Base64.urlsafe_encode64(
|
|
69
|
+
Digest::SHA256.digest(verifier),
|
|
70
|
+
padding: false
|
|
71
|
+
)
|
|
72
|
+
expect(challenge).to eq(expected_challenge)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
describe "expiration check" do
|
|
77
|
+
it "considers token expired when within 60 seconds of expiry" do
|
|
78
|
+
stored = { expires_at: Time.now.to_i + 30 }
|
|
79
|
+
expect(subject.send(:expired?, stored)).to be true
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
it "considers token valid when more than 60 seconds remain" do
|
|
83
|
+
stored = { expires_at: Time.now.to_i + 120 }
|
|
84
|
+
expect(subject.send(:expired?, stored)).to be false
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it "considers token valid when no expires_at" do
|
|
88
|
+
stored = { expires_at: nil }
|
|
89
|
+
expect(subject.send(:expired?, stored)).to be false
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
RSpec.describe Elelem::MCP::OAuth, "callback" do
|
|
95
|
+
it "listens on 127.0.0.1 only" do
|
|
96
|
+
server = instance_double(WEBrick::HTTPServer, mount_proc: nil, shutdown: nil)
|
|
97
|
+
allow(server).to receive(:start) { raise Timeout::Error }
|
|
98
|
+
allow(WEBrick::HTTPServer).to receive(:new).and_return(server)
|
|
99
|
+
|
|
100
|
+
oauth = described_class.new("https://mcp.example.com/mcp", http: double("http"))
|
|
101
|
+
expect { oauth.send(:wait_for_callback, "state") }.to raise_error("OAuth callback timed out")
|
|
102
|
+
expect(WEBrick::HTTPServer).to have_received(:new).with(hash_including(BindAddress: "127.0.0.1"))
|
|
103
|
+
ensure
|
|
104
|
+
oauth&.instance_variable_set(:@server, nil) # its at_exit shutdown must not reach the double
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
RSpec.describe Elelem::MCP::OAuth, "discovery" do
|
|
109
|
+
it "keeps the port of the server's URL" do
|
|
110
|
+
http = double("http")
|
|
111
|
+
asked = []
|
|
112
|
+
allow(http).to receive(:get) do |url, &block|
|
|
113
|
+
asked << url
|
|
114
|
+
body = url.include?("protected-resource") ? { authorization_servers: ["http://auth:9000"] } : {}
|
|
115
|
+
block.call(double(body: JSON.generate(body)))
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
described_class.new("http://localhost:8080/mcp", http: http).send(:discover_auth_server)
|
|
119
|
+
|
|
120
|
+
expect(asked.first).to eq("http://localhost:8080/.well-known/oauth-protected-resource")
|
|
121
|
+
end
|
|
122
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
RSpec.describe Elelem::MCP::TokenStorage do
|
|
4
|
+
subject { described_class.new }
|
|
5
|
+
|
|
6
|
+
let(:resource_url) { "https://example.com/mcp" }
|
|
7
|
+
let(:token_dir) { File.expand_path("~/.config/elelem/tokens") }
|
|
8
|
+
|
|
9
|
+
after do
|
|
10
|
+
Dir.glob(File.join(token_dir, "*.json")).each do |f|
|
|
11
|
+
File.delete(f)
|
|
12
|
+
rescue StandardError
|
|
13
|
+
nil
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe "#save and #load" do
|
|
18
|
+
it "stores and retrieves tokens" do
|
|
19
|
+
subject.save(resource_url, access_token: "abc123", refresh_token: "refresh456", expires_in: 3600)
|
|
20
|
+
|
|
21
|
+
stored = subject.load(resource_url)
|
|
22
|
+
expect(stored[:access_token]).to eq("abc123")
|
|
23
|
+
expect(stored[:refresh_token]).to eq("refresh456")
|
|
24
|
+
expect(stored[:expires_at]).to be > Time.now.to_i
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "returns nil for unknown resource" do
|
|
28
|
+
expect(subject.load("https://unknown.com")).to be_nil
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "handles missing refresh_token" do
|
|
32
|
+
subject.save(resource_url, access_token: "abc123")
|
|
33
|
+
|
|
34
|
+
stored = subject.load(resource_url)
|
|
35
|
+
expect(stored[:access_token]).to eq("abc123")
|
|
36
|
+
expect(stored[:refresh_token]).to be_nil
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
describe "#save_client and #load_client" do
|
|
41
|
+
it "stores and retrieves client registration" do
|
|
42
|
+
client_data = { "client_id" => "elelem-123", "client_secret" => nil }
|
|
43
|
+
subject.save_client(resource_url, client_data)
|
|
44
|
+
|
|
45
|
+
stored = subject.load_client(resource_url)
|
|
46
|
+
expect(stored[:client_id]).to eq("elelem-123")
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "returns nil for unknown client" do
|
|
50
|
+
expect(subject.load_client("https://unknown.com")).to be_nil
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
describe "file permissions" do
|
|
55
|
+
it "creates token files with 0600 permissions" do
|
|
56
|
+
subject.save(resource_url, access_token: "secret")
|
|
57
|
+
|
|
58
|
+
files = Dir.glob(File.join(token_dir, "*.json"))
|
|
59
|
+
expect(files).not_to be_empty
|
|
60
|
+
files.each do |f|
|
|
61
|
+
mode = File.stat(f).mode & 0o777
|
|
62
|
+
expect(mode).to eq(0o600)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "open3"
|
|
4
|
+
|
|
5
|
+
RSpec.describe "the mcp setup" do
|
|
6
|
+
let(:lib) { File.expand_path("../../lib", __dir__) }
|
|
7
|
+
let(:echo) { File.expand_path("../fixtures/echo_server.rb", __dir__) }
|
|
8
|
+
let(:script) do
|
|
9
|
+
'require "elelem/mcp"; ' \
|
|
10
|
+
"puts Elelem.build(input: Elelem::NullInput.new, output: Elelem::NullOutput.new).toolbox.tools.keys"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def build_session_in(dir)
|
|
14
|
+
servers = {
|
|
15
|
+
"broken" => { "command" => "/nonexistent/mcp" },
|
|
16
|
+
"echo" => { "command" => RbConfig.ruby, "args" => [echo] }
|
|
17
|
+
}
|
|
18
|
+
FileUtils.mkdir_p(File.join(dir, ".agents"))
|
|
19
|
+
File.write(File.join(dir, ".agents/mcp.json"), JSON.generate("mcpServers" => servers))
|
|
20
|
+
Open3.capture3({ "HOME" => dir }, RbConfig.ruby, "-I", lib, "-e", script, chdir: dir)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "waits for the servers when the input is not interactive, and loads the tools of the working one" do
|
|
24
|
+
Dir.mktmpdir do |dir|
|
|
25
|
+
tools, _, status = build_session_in(dir)
|
|
26
|
+
|
|
27
|
+
expect(status).to be_success
|
|
28
|
+
expect(tools.lines(chomp: true)).to include("echo_say")
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "names the server that failed" do
|
|
33
|
+
Dir.mktmpdir do |dir|
|
|
34
|
+
_, errors, = build_session_in(dir)
|
|
35
|
+
|
|
36
|
+
expect(errors).to eq("elelem-mcp: broken: No such file or directory - /nonexistent/mcp\n")
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# A stdio MCP server with one tool, "say", that returns its text.
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
6
|
+
SAY = {
|
|
7
|
+
name: "say",
|
|
8
|
+
description: "Echo text",
|
|
9
|
+
inputSchema: { type: "object", properties: { text: { type: "string" } }, required: ["text"] }
|
|
10
|
+
}.freeze
|
|
11
|
+
|
|
12
|
+
RESULTS = {
|
|
13
|
+
"initialize" => ->(_) { { protocolVersion: "2025-06-18", capabilities: {}, serverInfo: { name: "echo" } } },
|
|
14
|
+
"tools/list" => ->(_) { { tools: [SAY] } },
|
|
15
|
+
"tools/call" => ->(m) { { content: [{ type: "text", text: m.dig("params", "arguments", "text") }] } }
|
|
16
|
+
}.freeze
|
|
17
|
+
|
|
18
|
+
$stdout.sync = true
|
|
19
|
+
$stdin.each_line do |line|
|
|
20
|
+
message = JSON.parse(line)
|
|
21
|
+
next unless message["id"]
|
|
22
|
+
|
|
23
|
+
puts({ jsonrpc: "2.0", id: message["id"], result: RESULTS.fetch(message["method"]).call(message) }.to_json)
|
|
24
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: elelem-mcp
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1
|
|
4
|
+
version: 0.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- mo khan
|
|
@@ -10,33 +10,39 @@ cert_chain: []
|
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
|
-
name:
|
|
13
|
+
name: base64
|
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
|
15
15
|
requirements:
|
|
16
16
|
- - "~>"
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: '3
|
|
18
|
+
version: '0.3'
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - "~>"
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: '3
|
|
25
|
+
version: '0.3'
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: elelem
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
29
29
|
requirements:
|
|
30
|
-
- - "
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0.12'
|
|
33
|
+
- - "<"
|
|
31
34
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: '
|
|
35
|
+
version: '1'
|
|
33
36
|
type: :runtime
|
|
34
37
|
prerelease: false
|
|
35
38
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
39
|
requirements:
|
|
37
|
-
- - "
|
|
40
|
+
- - ">="
|
|
38
41
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: '0.
|
|
42
|
+
version: '0.12'
|
|
43
|
+
- - "<"
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '1'
|
|
40
46
|
- !ruby/object:Gem::Dependency
|
|
41
47
|
name: fileutils
|
|
42
48
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -57,14 +63,14 @@ dependencies:
|
|
|
57
63
|
requirements:
|
|
58
64
|
- - "~>"
|
|
59
65
|
- !ruby/object:Gem::Version
|
|
60
|
-
version: '
|
|
66
|
+
version: '3.0'
|
|
61
67
|
type: :runtime
|
|
62
68
|
prerelease: false
|
|
63
69
|
version_requirements: !ruby/object:Gem::Requirement
|
|
64
70
|
requirements:
|
|
65
71
|
- - "~>"
|
|
66
72
|
- !ruby/object:Gem::Version
|
|
67
|
-
version: '
|
|
73
|
+
version: '3.0'
|
|
68
74
|
- !ruby/object:Gem::Dependency
|
|
69
75
|
name: net-http
|
|
70
76
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -93,6 +99,34 @@ dependencies:
|
|
|
93
99
|
- - "~>"
|
|
94
100
|
- !ruby/object:Gem::Version
|
|
95
101
|
version: '0.2'
|
|
102
|
+
- !ruby/object:Gem::Dependency
|
|
103
|
+
name: securerandom
|
|
104
|
+
requirement: !ruby/object:Gem::Requirement
|
|
105
|
+
requirements:
|
|
106
|
+
- - "~>"
|
|
107
|
+
- !ruby/object:Gem::Version
|
|
108
|
+
version: '0.4'
|
|
109
|
+
type: :runtime
|
|
110
|
+
prerelease: false
|
|
111
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
112
|
+
requirements:
|
|
113
|
+
- - "~>"
|
|
114
|
+
- !ruby/object:Gem::Version
|
|
115
|
+
version: '0.4'
|
|
116
|
+
- !ruby/object:Gem::Dependency
|
|
117
|
+
name: timeout
|
|
118
|
+
requirement: !ruby/object:Gem::Requirement
|
|
119
|
+
requirements:
|
|
120
|
+
- - "~>"
|
|
121
|
+
- !ruby/object:Gem::Version
|
|
122
|
+
version: '0.6'
|
|
123
|
+
type: :runtime
|
|
124
|
+
prerelease: false
|
|
125
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
126
|
+
requirements:
|
|
127
|
+
- - "~>"
|
|
128
|
+
- !ruby/object:Gem::Version
|
|
129
|
+
version: '0.6'
|
|
96
130
|
- !ruby/object:Gem::Dependency
|
|
97
131
|
name: uri
|
|
98
132
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -107,6 +141,20 @@ dependencies:
|
|
|
107
141
|
- - "~>"
|
|
108
142
|
- !ruby/object:Gem::Version
|
|
109
143
|
version: '1.1'
|
|
144
|
+
- !ruby/object:Gem::Dependency
|
|
145
|
+
name: webrick
|
|
146
|
+
requirement: !ruby/object:Gem::Requirement
|
|
147
|
+
requirements:
|
|
148
|
+
- - "~>"
|
|
149
|
+
- !ruby/object:Gem::Version
|
|
150
|
+
version: '1.9'
|
|
151
|
+
type: :runtime
|
|
152
|
+
prerelease: false
|
|
153
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
154
|
+
requirements:
|
|
155
|
+
- - "~>"
|
|
156
|
+
- !ruby/object:Gem::Version
|
|
157
|
+
version: '1.9'
|
|
110
158
|
description: Adds MCP server tool discovery (stdio and HTTP transports) as elelem
|
|
111
159
|
tools.
|
|
112
160
|
email:
|
|
@@ -115,19 +163,35 @@ executables: []
|
|
|
115
163
|
extensions: []
|
|
116
164
|
extra_rdoc_files: []
|
|
117
165
|
files:
|
|
166
|
+
- ".github/workflows/release.yml"
|
|
167
|
+
- ".release-please-manifest.json"
|
|
168
|
+
- ".rspec"
|
|
169
|
+
- ".rubocop.yml"
|
|
170
|
+
- CHANGELOG.md
|
|
118
171
|
- LICENSE.txt
|
|
119
172
|
- Rakefile
|
|
120
173
|
- lib/elelem/mcp.rb
|
|
121
174
|
- lib/elelem/mcp/client.rb
|
|
175
|
+
- lib/elelem/mcp/oauth.rb
|
|
122
176
|
- lib/elelem/mcp/token_storage.rb
|
|
123
177
|
- lib/elelem/mcp/version.rb
|
|
124
|
-
|
|
178
|
+
- release-please-config.json
|
|
179
|
+
- spec/elelem/mcp/client_spec.rb
|
|
180
|
+
- spec/elelem/mcp/http_server_spec.rb
|
|
181
|
+
- spec/elelem/mcp/oauth_spec.rb
|
|
182
|
+
- spec/elelem/mcp/token_storage_spec.rb
|
|
183
|
+
- spec/elelem/mcp_spec.rb
|
|
184
|
+
- spec/fixtures/echo_server.rb
|
|
185
|
+
- spec/fixtures/noisy_server.rb
|
|
186
|
+
- spec/spec_helper.rb
|
|
187
|
+
homepage: https://elelem.sh
|
|
125
188
|
licenses:
|
|
126
189
|
- MIT
|
|
127
190
|
metadata:
|
|
128
191
|
allowed_push_host: https://rubygems.org
|
|
129
|
-
homepage_uri: https://
|
|
130
|
-
source_code_uri: https://
|
|
192
|
+
homepage_uri: https://elelem.sh
|
|
193
|
+
source_code_uri: https://github.com/elelem-sh/mcp
|
|
194
|
+
rubygems_mfa_required: 'true'
|
|
131
195
|
rdoc_options: []
|
|
132
196
|
require_paths:
|
|
133
197
|
- lib
|
|
@@ -142,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
142
206
|
- !ruby/object:Gem::Version
|
|
143
207
|
version: '0'
|
|
144
208
|
requirements: []
|
|
145
|
-
rubygems_version: 4.0.
|
|
209
|
+
rubygems_version: 4.0.6
|
|
146
210
|
specification_version: 4
|
|
147
211
|
summary: MCP (Model Context Protocol) client support for elelem.
|
|
148
212
|
test_files: []
|