mcp 1.8.2 → 1.10.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 +4 -4
- data/README.md +5 -0
- data/lib/json_rpc_handler.rb +1 -1
- data/lib/mcp/client/http.rb +29 -5
- data/lib/mcp/client/oauth/client_credentials_provider.rb +16 -0
- data/lib/mcp/client/oauth/cross_app_access_provider.rb +19 -2
- data/lib/mcp/client/oauth/flow.rb +11 -3
- data/lib/mcp/client/oauth/jwt_client_assertion.rb +3 -2
- data/lib/mcp/client/oauth/pkce.rb +3 -2
- data/lib/mcp/client/skill_errors.rb +45 -0
- data/lib/mcp/client/skills.rb +430 -0
- data/lib/mcp/client/stdio.rb +15 -3
- data/lib/mcp/client/tool.rb +8 -2
- data/lib/mcp/client.rb +40 -3
- data/lib/mcp/instrumentation.rb +12 -7
- data/lib/mcp/mcp_param_headers.rb +332 -0
- data/lib/mcp/methods.rb +5 -0
- data/lib/mcp/server/oauth/jwt_verifier.rb +34 -5
- data/lib/mcp/server/transports/stdio_transport.rb +12 -0
- data/lib/mcp/server/transports/streamable_http_transport.rb +193 -31
- data/lib/mcp/server.rb +431 -51
- data/lib/mcp/server_session.rb +23 -1
- data/lib/mcp/skill/directory.rb +316 -0
- data/lib/mcp/skill/frontmatter.rb +262 -0
- data/lib/mcp/skill/resource_entry.rb +109 -0
- data/lib/mcp/skill.rb +522 -0
- data/lib/mcp/tool/output_schema.rb +4 -3
- data/lib/mcp/version.rb +1 -1
- data/lib/mcp.rb +3 -0
- metadata +9 -3
- data/lib/mcp/client/mcp_param_headers.rb +0 -242
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
require "digest"
|
|
5
|
+
|
|
6
|
+
module MCP
|
|
7
|
+
class Client
|
|
8
|
+
# One page of `skills/list`: `skills` is a frozen Array of `MCP::Skill`; `next_cursor`, `meta`, `ttl_ms`, and
|
|
9
|
+
# `cache_scope` mirror the response members. Frozen on construction.
|
|
10
|
+
ListSkillsResult = Struct.new(:skills, :next_cursor, :meta, :ttl_ms, :cache_scope, keyword_init: true) do
|
|
11
|
+
def initialize(**kwargs)
|
|
12
|
+
super(**kwargs)
|
|
13
|
+
freeze
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# The entry `skills/get` returned for exactly the requested URI. Frozen on construction.
|
|
18
|
+
GetSkillResult = Struct.new(:skill, :meta, :ttl_ms, :cache_scope, keyword_init: true) do
|
|
19
|
+
def initialize(**kwargs)
|
|
20
|
+
super(**kwargs)
|
|
21
|
+
freeze
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# A skill file read and verified by `read_skill_resource`: `bytes` is the verified content as a frozen
|
|
26
|
+
# binary-encoded String (the decoded `blob`, or the bytes of the `text`), `mime_type` is the server's
|
|
27
|
+
# unverified display metadata (never a reason to pick an interpreter), `contents` is the raw frozen
|
|
28
|
+
# `contents` Array, `skill` is the entry object the content was checked against, and `verified` is true:
|
|
29
|
+
# the digest, size, and (for a `SKILL.md`) frontmatter matched. Every value is independent of the objects
|
|
30
|
+
# it was built from, so mutating a source cannot change the result.
|
|
31
|
+
SkillReadResult = Struct.new(:uri, :bytes, :mime_type, :contents, :skill, :verified, keyword_init: true) do
|
|
32
|
+
def initialize(**kwargs)
|
|
33
|
+
super(**kwargs)
|
|
34
|
+
freeze
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Client side of the Skills extension (SEP-2640), mixed into `MCP::Client`: `list_skills`, `get_skill`,
|
|
39
|
+
# `read_skill_resource`, and `skills_capability`.
|
|
40
|
+
#
|
|
41
|
+
# Every call is admitted only after a handshake of the current connection advertised both the extension and
|
|
42
|
+
# `resources`, as the SEP asks of clients. Entries the server returned are remembered per client instance,
|
|
43
|
+
# bound to the connection: `connect` starts a new generation and forgets them. `read_skill_resource` checks
|
|
44
|
+
# the content against the entry it resolved (size, SHA-256, and the frontmatter of a `SKILL.md`), withholds
|
|
45
|
+
# content that does not match, and latches the skill so that no later read of it is sent on this connection,
|
|
46
|
+
# which is what keeps a host from loading part of a tampered skill. `read_resource` stays the raw,
|
|
47
|
+
# unverified way to fetch a skill file; `MCP::Skill.verify_resource` and `MCP::Skill.verify_frontmatter`
|
|
48
|
+
# exist for hosts that go that way and then own the latching and ordering obligations themselves.
|
|
49
|
+
module Skills
|
|
50
|
+
# Fetches one page of the skills the server serves.
|
|
51
|
+
#
|
|
52
|
+
# @raise [SkillReadRefusedError] `:not_advertised` before any request is sent, `:connection_changed` when
|
|
53
|
+
# `connect` ran while the request was in flight (the page is discarded).
|
|
54
|
+
# @raise [SkillEntryError] When the page is not a conformant `skills/list` result.
|
|
55
|
+
def list_skills(cursor: nil, meta: nil, cancellation: nil)
|
|
56
|
+
generation = @skills_state.admit(connected: connected?)
|
|
57
|
+
params = cursor ? { cursor: cursor } : nil
|
|
58
|
+
response = request(method: Methods::SKILLS_LIST, params: params, meta: meta, cancellation: cancellation)
|
|
59
|
+
@skills_state.ensure_current!(generation)
|
|
60
|
+
|
|
61
|
+
result = response.is_a?(Hash) ? response["result"] : nil
|
|
62
|
+
raise SkillEntryError, "Response validation failed: missing or invalid `result`" unless result.is_a?(Hash)
|
|
63
|
+
|
|
64
|
+
entries = result["skills"]
|
|
65
|
+
raise SkillEntryError, "Response validation failed: `skills` is not an Array" unless entries.is_a?(Array)
|
|
66
|
+
|
|
67
|
+
skills = entries.map { |entry| parse_skill_entry(entry) }
|
|
68
|
+
uris = skills.map(&:uri)
|
|
69
|
+
raise SkillEntryError, "Response validation failed: a skill URI is listed twice in one page" unless uris.uniq.size == uris.size
|
|
70
|
+
|
|
71
|
+
@skills_state.install(generation, skills)
|
|
72
|
+
ListSkillsResult.new(
|
|
73
|
+
skills: skills.freeze,
|
|
74
|
+
next_cursor: result["nextCursor"],
|
|
75
|
+
meta: result["_meta"],
|
|
76
|
+
ttl_ms: result["ttlMs"],
|
|
77
|
+
cache_scope: result["cacheScope"],
|
|
78
|
+
)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Fetches the entry of one skill by the URI of its `SKILL.md`, listed or not.
|
|
82
|
+
#
|
|
83
|
+
# @raise [SkillReadRefusedError] `:not_advertised` or `:connection_changed`, as for `list_skills`.
|
|
84
|
+
# @raise [SkillEntryError] When the result is not a conformant entry for exactly `uri`.
|
|
85
|
+
def get_skill(uri:, meta: nil, cancellation: nil)
|
|
86
|
+
check_skill_uri!(uri)
|
|
87
|
+
|
|
88
|
+
generation = @skills_state.admit(connected: connected?)
|
|
89
|
+
response = request(method: Methods::SKILLS_GET, params: { uri: uri }, meta: meta, cancellation: cancellation)
|
|
90
|
+
@skills_state.ensure_current!(generation)
|
|
91
|
+
|
|
92
|
+
result = response.is_a?(Hash) ? response["result"] : nil
|
|
93
|
+
raise SkillEntryError, "Response validation failed: missing or invalid `result`" unless result.is_a?(Hash)
|
|
94
|
+
|
|
95
|
+
skill = parse_skill_entry(result["skill"])
|
|
96
|
+
raise SkillEntryError, "Response validation failed: skills/get answered for #{skill.uri.dump} instead of #{uri.dump}" unless skill.uri == uri
|
|
97
|
+
|
|
98
|
+
@skills_state.install(generation, [skill])
|
|
99
|
+
GetSkillResult.new(skill: skill, meta: result["_meta"], ttl_ms: result["ttlMs"], cache_scope: result["cacheScope"])
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Reads one file of a skill through `resources/read` and verifies it against the skill's entry before
|
|
103
|
+
# returning it. The entry is `skill:` when given (for this read only), else the most specific remembered
|
|
104
|
+
# skill that contains `uri`, never an enclosing one: when an enclosing skill lists `uri` as well the read is
|
|
105
|
+
# refused as `:ambiguous`, since only the host knows which skill it is acting on, and a `SKILL.md` URI
|
|
106
|
+
# resolves only to its own entry. A supporting file is read only after the skill's `SKILL.md` verified on
|
|
107
|
+
# this instance.
|
|
108
|
+
#
|
|
109
|
+
# @raise [SkillReadRefusedError] Before anything is sent: `:not_advertised`, `:latched`, `:unknown_skill` (also
|
|
110
|
+
# for a URI longer than 8 KiB or deeper than 128 segments, an SDK limit), `:ambiguous` (an enclosing
|
|
111
|
+
# remembered skill lists `uri` too; pass `skill:` for the skill being acted on), `:dynamic` (no digests; use
|
|
112
|
+
# `read_resource` with `MCP::Skill.verify_frontmatter`), `:not_in_manifest`, `:skill_md_unverified`; on
|
|
113
|
+
# completion, `:connection_changed` or `:latched`.
|
|
114
|
+
# @raise [SkillVerificationError] When the content does not match; the skill is latched for the connection.
|
|
115
|
+
def read_skill_resource(uri:, skill: nil, meta: nil, cancellation: nil)
|
|
116
|
+
check_skill_uri!(uri)
|
|
117
|
+
raise ArgumentError, "The value of skill must be an MCP::Skill or nil (got #{skill.class})." unless skill.nil? || skill.is_a?(Skill)
|
|
118
|
+
|
|
119
|
+
# A frozen copy, so a caller mutating its String during the read cannot change the URI that was verified.
|
|
120
|
+
uri = -uri
|
|
121
|
+
unless Skill::Uri.resolvable?(uri)
|
|
122
|
+
# Refused before the ancestor roots are computed, which would cost time and memory quadratic in
|
|
123
|
+
# the length of a URI that may come from model output; the message leaves the URI out for that reason.
|
|
124
|
+
@skills_state.admit(connected: connected?)
|
|
125
|
+
raise SkillReadRefusedError.new(
|
|
126
|
+
"Skills call refused (unknown_skill): the URI is longer than #{Skill::Uri::MAX_RESOLVED_BYTES} bytes or deeper " \
|
|
127
|
+
"than #{Skill::Uri::MAX_RESOLVED_SEGMENTS} segments, beyond what the client resolves",
|
|
128
|
+
reason: :unknown_skill,
|
|
129
|
+
uri: uri,
|
|
130
|
+
)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
candidates = Skill::Uri.candidate_skill_uris(uri)
|
|
134
|
+
generation, entry, resource_entry = @skills_state.admit_read(uri, candidates, skill, connected: connected?)
|
|
135
|
+
response = request(method: Methods::RESOURCES_READ, params: { uri: uri }, meta: meta, cancellation: cancellation)
|
|
136
|
+
outcome = verify_skill_contents(entry, resource_entry, uri, response)
|
|
137
|
+
bytes, mime_type, contents = @skills_state.complete_read(generation, uri, candidates, entry, explicit: !skill.nil?, outcome: outcome)
|
|
138
|
+
|
|
139
|
+
SkillReadResult.new(uri: uri, bytes: bytes, mime_type: mime_type, contents: contents, skill: entry, verified: true)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# The Skills settings the current connection's handshake advertised, as a frozen Hash, or nil when
|
|
143
|
+
# a Skills call would be refused with `:not_advertised`: not ready, not connected, or not advertised.
|
|
144
|
+
# nil never means "read skill files raw instead".
|
|
145
|
+
def skills_capability
|
|
146
|
+
@skills_state.skills_capability(connected: connected?)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
private
|
|
150
|
+
|
|
151
|
+
def check_skill_uri!(uri)
|
|
152
|
+
raise ArgumentError, "The value of uri must be a String (got #{uri.class})." unless uri.is_a?(String)
|
|
153
|
+
return if uri.valid_encoding? && uri.encoding.ascii_compatible?
|
|
154
|
+
|
|
155
|
+
raise ArgumentError, "The value of uri must be validly encoded in an ASCII-compatible encoding."
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def parse_skill_entry(entry)
|
|
159
|
+
Skill.from_entry(entry)
|
|
160
|
+
rescue ArgumentError => e
|
|
161
|
+
raise SkillEntryError, "Skill entry rejected: #{e.message}"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Pure verification, run outside the state lock: exactly one content item for `uri` with exactly one of
|
|
165
|
+
# `text` or `blob`, the size before decoding, the digest, and for the skill's own `SKILL.md` the frontmatter.
|
|
166
|
+
# Returns `[:ok, bytes, mime_type, contents]` or `[:failed, error]`.
|
|
167
|
+
def verify_skill_contents(entry, resource_entry, uri, response)
|
|
168
|
+
result = response.is_a?(Hash) ? response["result"] : nil
|
|
169
|
+
contents = result.is_a?(Hash) ? result["contents"] : nil
|
|
170
|
+
item = contents.is_a?(Array) && contents.size == 1 ? contents.first : nil
|
|
171
|
+
unless item.is_a?(Hash) && item["uri"] == uri
|
|
172
|
+
return skill_verification_failure(:contents, uri, entry, "did not come back as exactly one content item")
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
text = item["text"]
|
|
176
|
+
blob = item["blob"]
|
|
177
|
+
unless (text.is_a?(String) && blob.nil?) || (blob.is_a?(String) && text.nil?)
|
|
178
|
+
return skill_verification_failure(:contents, uri, entry, "did not carry exactly one of text or blob")
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
expected_size = resource_entry.size
|
|
182
|
+
if text
|
|
183
|
+
bytes = text.b
|
|
184
|
+
return skill_verification_failure(:size, uri, entry, "has a different size than listed") unless bytes.bytesize == expected_size
|
|
185
|
+
else
|
|
186
|
+
# Checked before decoding so the manifest's size bounds the work: this is the base64 length of exactly `size` bytes.
|
|
187
|
+
return skill_verification_failure(:size, uri, entry, "has a different size than listed") unless blob.length <= ((expected_size + 2) / 3) * 4
|
|
188
|
+
|
|
189
|
+
bytes = begin
|
|
190
|
+
blob.unpack1("m0")
|
|
191
|
+
rescue ArgumentError
|
|
192
|
+
return skill_verification_failure(:contents, uri, entry, "did not carry valid base64")
|
|
193
|
+
end
|
|
194
|
+
return skill_verification_failure(:size, uri, entry, "has a different size than listed") unless bytes.bytesize == expected_size
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
unless "sha256:#{Digest::SHA256.hexdigest(bytes)}" == resource_entry.digest
|
|
198
|
+
return skill_verification_failure(:digest, uri, entry, "has a different digest than listed")
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
if uri == entry.uri
|
|
202
|
+
begin
|
|
203
|
+
Skill.verify_frontmatter(entry, bytes.dup.force_encoding(Encoding::UTF_8))
|
|
204
|
+
rescue SkillVerificationError => e
|
|
205
|
+
return [:failed, e]
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
copy = begin
|
|
210
|
+
Skill::Json.deep_freeze(Skill::Json.deep_dup(contents))
|
|
211
|
+
rescue ArgumentError
|
|
212
|
+
return skill_verification_failure(:contents, uri, entry, "came back nested too deeply to be the file listed")
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
mime_type = item["mimeType"]
|
|
216
|
+
[:ok, bytes.freeze, mime_type.is_a?(String) ? mime_type.dup.freeze : nil, copy]
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def skill_verification_failure(reason, uri, entry, detail)
|
|
220
|
+
# URIs a server chose are shown with `dump`, which escapes everything outside printable ASCII (`inspect`
|
|
221
|
+
# leaves bidirectional overrides as they are), so they cannot forge or reorder lines in a host's log.
|
|
222
|
+
message = "Resource #{uri.dump} #{detail} by #{entry.uri.dump}"
|
|
223
|
+
[:failed, SkillVerificationError.new(message, uri: uri, skill_uri: entry.uri, reason: reason)]
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# Per-instance Skills state, bound to the connection. Every public method takes the one Mutex and none of
|
|
227
|
+
# them calls another inside it (Ruby's Mutex is not reentrant); hashing, decoding, and parsing happen
|
|
228
|
+
# outside, in the client methods above. No lock is ever held across I/O.
|
|
229
|
+
class State
|
|
230
|
+
def initialize
|
|
231
|
+
@mutex = Mutex.new
|
|
232
|
+
@generation = 0
|
|
233
|
+
@ready = false
|
|
234
|
+
@recorded = nil
|
|
235
|
+
@index = {}
|
|
236
|
+
@verified = {}.compare_by_identity
|
|
237
|
+
@latched = Set.new
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# A new connection is about to be established: forget everything and return the new generation.
|
|
241
|
+
def begin_handshake
|
|
242
|
+
@mutex.synchronize do
|
|
243
|
+
@generation += 1
|
|
244
|
+
@ready = false
|
|
245
|
+
@recorded = nil
|
|
246
|
+
@index = {}
|
|
247
|
+
@verified = {}.compare_by_identity
|
|
248
|
+
@latched = Set.new
|
|
249
|
+
@generation
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# The handshake that started `generation` completed with `result`, the wire Hash `transport.connect`
|
|
254
|
+
# returned; records a frozen copy of its capabilities, unless a newer handshake started meanwhile.
|
|
255
|
+
# Capabilities nested too deeply to copy are recorded as none, so `connect` succeeds as it does without
|
|
256
|
+
# Skills and every Skills call is refused.
|
|
257
|
+
def handshake_completed(generation, result)
|
|
258
|
+
capabilities = read_key(result, "capabilities")
|
|
259
|
+
copy = begin
|
|
260
|
+
capabilities.is_a?(Hash) ? Skill::Json.deep_freeze(Skill::Json.deep_dup(capabilities)) : nil
|
|
261
|
+
rescue ArgumentError
|
|
262
|
+
nil
|
|
263
|
+
end
|
|
264
|
+
@mutex.synchronize do
|
|
265
|
+
next unless generation == @generation
|
|
266
|
+
|
|
267
|
+
@ready = true
|
|
268
|
+
@recorded = copy
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# The handshake that started `generation` raised; a stale failure changes nothing.
|
|
273
|
+
def handshake_failed(generation)
|
|
274
|
+
@mutex.synchronize do
|
|
275
|
+
next unless generation == @generation
|
|
276
|
+
|
|
277
|
+
@ready = false
|
|
278
|
+
@recorded = nil
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def ready?
|
|
283
|
+
@mutex.synchronize { @ready }
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
def admit(connected:)
|
|
287
|
+
@mutex.synchronize do
|
|
288
|
+
admit_or_refuse!(connected)
|
|
289
|
+
@generation
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
def ensure_current!(generation)
|
|
294
|
+
@mutex.synchronize do
|
|
295
|
+
refuse!(:connection_changed, nil, nil, "the connection changed while the request was in flight") unless generation == @generation
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
# Resolves and checks a read before it is sent; returns the generation, the entry, and its resource entry.
|
|
300
|
+
def admit_read(uri, candidates, explicit, connected:)
|
|
301
|
+
@mutex.synchronize do
|
|
302
|
+
admit_or_refuse!(connected)
|
|
303
|
+
latched = candidates.find { |candidate| @latched.include?(candidate) }
|
|
304
|
+
refuse!(:latched, uri, latched, "#{latched.dump} failed verification on this connection") if latched
|
|
305
|
+
|
|
306
|
+
entry = resolve_entry(uri, candidates, explicit)
|
|
307
|
+
refuse!(:dynamic, uri, entry.uri, "#{entry.uri.dump} is dynamic and lists no digests") if entry.dynamic?
|
|
308
|
+
|
|
309
|
+
resource_entry = entry.resources.find { |candidate| candidate.uri == uri }
|
|
310
|
+
refuse!(:not_in_manifest, uri, entry.uri, "#{uri.dump} is not listed by #{entry.uri.dump}") unless resource_entry
|
|
311
|
+
if uri != entry.uri && !@verified.key?(entry)
|
|
312
|
+
refuse!(:skill_md_unverified, uri, entry.uri, "#{entry.uri.dump} has not been read and verified on this connection yet")
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
[@generation, entry, resource_entry]
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# Remembers entries a catalog call returned; a replaced entry object loses its verified status.
|
|
320
|
+
def install(generation, skills)
|
|
321
|
+
@mutex.synchronize do
|
|
322
|
+
refuse!(:connection_changed, nil, nil, "the connection changed while the request was in flight") unless generation == @generation
|
|
323
|
+
|
|
324
|
+
skills.each do |skill|
|
|
325
|
+
previous = @index[skill.uri]
|
|
326
|
+
@verified.delete(previous) if previous && !previous.equal?(skill)
|
|
327
|
+
@index[skill.uri] = skill
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
# Settles a read: discards a stale or newly latched completion, latches the skill on a failure, and
|
|
333
|
+
# grants the entry object verified status after its own `SKILL.md` verified (an automatic read only
|
|
334
|
+
# while the object is still current; an explicit read for the object the caller supplied).
|
|
335
|
+
def complete_read(generation, uri, candidates, entry, explicit:, outcome:)
|
|
336
|
+
@mutex.synchronize do
|
|
337
|
+
refuse!(:connection_changed, uri, entry.uri, "the connection changed while the request was in flight") unless generation == @generation
|
|
338
|
+
latched = candidates.find { |candidate| @latched.include?(candidate) }
|
|
339
|
+
refuse!(:latched, uri, latched, "#{latched.dump} failed verification on this connection") if latched
|
|
340
|
+
|
|
341
|
+
status, *values = outcome
|
|
342
|
+
if status == :failed
|
|
343
|
+
@latched << entry.uri
|
|
344
|
+
raise values.first
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
if uri == entry.uri && (explicit || @index[entry.uri].equal?(entry))
|
|
348
|
+
@verified[entry] = true
|
|
349
|
+
end
|
|
350
|
+
values
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def skills_capability(connected:)
|
|
355
|
+
@mutex.synchronize do
|
|
356
|
+
next unless admitted?(connected)
|
|
357
|
+
|
|
358
|
+
read_key(read_key(@recorded, "extensions"), Skill::EXTENSION_ID)
|
|
359
|
+
end
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
private
|
|
363
|
+
|
|
364
|
+
def admit_or_refuse!(connected)
|
|
365
|
+
return if admitted?(connected)
|
|
366
|
+
|
|
367
|
+
refuse!(:not_advertised, nil, nil, "no handshake of this connection advertised the skills extension and resources")
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
def admitted?(connected)
|
|
371
|
+
return false unless @ready && connected && @recorded.is_a?(Hash)
|
|
372
|
+
|
|
373
|
+
settings = read_key(read_key(@recorded, "extensions"), Skill::EXTENSION_ID)
|
|
374
|
+
settings.is_a?(Hash) && read_key(@recorded, "resources").is_a?(Hash)
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
def resolve_entry(uri, candidates, explicit)
|
|
378
|
+
if explicit
|
|
379
|
+
if Skill::Uri.manifest_uri?(uri) && uri != explicit.uri
|
|
380
|
+
refuse!(:unknown_skill, uri, explicit.uri, "a SKILL.md URI resolves only to its own skill")
|
|
381
|
+
end
|
|
382
|
+
return explicit
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
if Skill::Uri.manifest_uri?(uri)
|
|
386
|
+
entry = @index[uri]
|
|
387
|
+
refuse!(:unknown_skill, uri, nil, "#{uri.dump} has not been listed or fetched on this connection; pass skill: or call get_skill") unless entry
|
|
388
|
+
return entry
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
remembered = candidates.reverse_each.map { |candidate| @index[candidate] }.compact
|
|
392
|
+
refuse!(:unknown_skill, uri, nil, "no remembered skill contains #{uri.dump}; pass skill: or call get_skill") if remembered.empty?
|
|
393
|
+
|
|
394
|
+
# Only the most specific remembered skill containing `uri` is resolved automatically. The specification
|
|
395
|
+
# resolves a read against the entry the host loaded the skill from, and only the host knows which skill
|
|
396
|
+
# it is acting on, so an enclosing skill that lists the file (it lists its nested skills' files too) is
|
|
397
|
+
# reached with `skill:` alone: never because the nested entry is dynamic or leaves the file out.
|
|
398
|
+
entry = remembered.first
|
|
399
|
+
return entry if entry.dynamic?
|
|
400
|
+
|
|
401
|
+
enclosing = remembered.drop(1).select do |candidate|
|
|
402
|
+
!candidate.dynamic? && candidate.resources.any? { |resource| resource.uri == uri }
|
|
403
|
+
end
|
|
404
|
+
unless enclosing.empty?
|
|
405
|
+
refuse!(
|
|
406
|
+
:ambiguous,
|
|
407
|
+
uri,
|
|
408
|
+
entry.uri,
|
|
409
|
+
"#{uri.dump} is also listed by #{enclosing.map { |candidate| candidate.uri.dump }.join(" and ")}; " \
|
|
410
|
+
"pass skill: for the skill being acted on",
|
|
411
|
+
)
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
entry
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
def read_key(hash, key)
|
|
418
|
+
return unless hash.is_a?(Hash)
|
|
419
|
+
|
|
420
|
+
value = hash[key]
|
|
421
|
+
value.nil? ? hash[key.to_sym] : value
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
def refuse!(reason, uri, skill_uri, detail)
|
|
425
|
+
raise SkillReadRefusedError.new("Skills call refused (#{reason}): #{detail}", reason: reason, uri: uri, skill_uri: skill_uri)
|
|
426
|
+
end
|
|
427
|
+
end
|
|
428
|
+
end
|
|
429
|
+
end
|
|
430
|
+
end
|
data/lib/mcp/client/stdio.rb
CHANGED
|
@@ -34,9 +34,20 @@ module MCP
|
|
|
34
34
|
# Matches the C# SDK's `DiscoverProbeTimeout` default.
|
|
35
35
|
DEFAULT_DISCOVER_PROBE_TIMEOUT = 5
|
|
36
36
|
|
|
37
|
+
# Inherited Windows environment, including RubyGems paths.
|
|
38
|
+
WINDOWS_INHERITED_ENV_VARS = %w[
|
|
39
|
+
APPDATA HOMEDRIVE HOMEPATH LOCALAPPDATA PATH PATHEXT PROCESSOR_ARCHITECTURE
|
|
40
|
+
SYSTEMDRIVE SYSTEMROOT TEMP USERNAME USERPROFILE GEM_HOME GEM_PATH
|
|
41
|
+
].freeze
|
|
42
|
+
|
|
43
|
+
# Inherited Unix environment, including RubyGems paths.
|
|
44
|
+
UNIX_INHERITED_ENV_VARS = %w[HOME LOGNAME PATH SHELL TERM USER GEM_HOME GEM_PATH].freeze
|
|
45
|
+
|
|
46
|
+
DEFAULT_INHERITED_ENV_VARS = Gem.win_platform? ? WINDOWS_INHERITED_ENV_VARS : UNIX_INHERITED_ENV_VARS
|
|
47
|
+
|
|
37
48
|
attr_reader :command, :args, :env, :server_info
|
|
38
49
|
|
|
39
|
-
def initialize(command:, args: [], env: nil, read_timeout: nil, max_line_bytes: MAX_LINE_BYTES)
|
|
50
|
+
def initialize(command:, args: [], env: nil, inherit_env: false, read_timeout: nil, max_line_bytes: MAX_LINE_BYTES)
|
|
40
51
|
# Reject `nil` or non-positive values: `IO#gets("\n", nil)` and a negative
|
|
41
52
|
# limit read without an upper bound, which would silently disable the
|
|
42
53
|
# protection this option exists to provide.
|
|
@@ -47,6 +58,7 @@ module MCP
|
|
|
47
58
|
@command = command
|
|
48
59
|
@args = args
|
|
49
60
|
@env = env
|
|
61
|
+
@inherit_env = inherit_env
|
|
50
62
|
@read_timeout = read_timeout
|
|
51
63
|
@max_line_bytes = max_line_bytes
|
|
52
64
|
@stdin = nil
|
|
@@ -166,8 +178,8 @@ module MCP
|
|
|
166
178
|
def start
|
|
167
179
|
raise "MCP::Client::Stdio already started" if @started
|
|
168
180
|
|
|
169
|
-
spawn_env = @env || {}
|
|
170
|
-
@stdin, @stdout, @stderr, @wait_thread = Open3.popen3(spawn_env, @command, *@args)
|
|
181
|
+
spawn_env = DEFAULT_INHERITED_ENV_VARS.to_h { |key| [key, ENV[key]] }.compact.merge(@env || {})
|
|
182
|
+
@stdin, @stdout, @stderr, @wait_thread = Open3.popen3(spawn_env, @command, *@args, unsetenv_others: !@inherit_env)
|
|
171
183
|
@stdout.set_encoding("UTF-8")
|
|
172
184
|
@stdin.set_encoding("UTF-8")
|
|
173
185
|
|
data/lib/mcp/client/tool.rb
CHANGED
|
@@ -2,15 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
module MCP
|
|
4
4
|
class Client
|
|
5
|
+
# A tool as `tools/list` described it. Every member is kept as the server sent it, so what an extension
|
|
6
|
+
# puts under `_meta` (MCP Apps names a tool's app under `_meta.ui`) reaches the host unchanged.
|
|
5
7
|
class Tool
|
|
6
|
-
attr_reader :name, :description, :input_schema, :output_schema, :annotations
|
|
8
|
+
attr_reader :name, :title, :description, :input_schema, :output_schema, :annotations, :icons, :execution, :meta
|
|
7
9
|
|
|
8
|
-
def initialize(name:, description:, input_schema:, output_schema: nil, annotations: nil)
|
|
10
|
+
def initialize(name:, description:, input_schema:, output_schema: nil, annotations: nil, title: nil, icons: nil, execution: nil, meta: nil)
|
|
9
11
|
@name = name
|
|
12
|
+
@title = title
|
|
10
13
|
@description = description
|
|
11
14
|
@input_schema = input_schema
|
|
12
15
|
@output_schema = output_schema
|
|
13
16
|
@annotations = annotations
|
|
17
|
+
@icons = icons
|
|
18
|
+
@execution = execution
|
|
19
|
+
@meta = meta
|
|
14
20
|
end
|
|
15
21
|
end
|
|
16
22
|
end
|
data/lib/mcp/client.rb
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "client/elicitation"
|
|
4
|
-
require_relative "client/mcp_param_headers"
|
|
5
4
|
require_relative "client/modern_envelope"
|
|
6
5
|
require_relative "client/oauth"
|
|
7
6
|
require_relative "client/stdio"
|
|
8
7
|
require_relative "client/http"
|
|
9
8
|
require_relative "client/paginated_result"
|
|
9
|
+
require_relative "client/skill_errors"
|
|
10
|
+
require_relative "client/skills"
|
|
10
11
|
require_relative "client/tool"
|
|
11
12
|
require_relative "result_type"
|
|
12
13
|
|
|
@@ -18,6 +19,11 @@ module MCP
|
|
|
18
19
|
# every response would otherwise be followed indefinitely, growing the retained pages with it.
|
|
19
20
|
MAX_PAGES = 1_000
|
|
20
21
|
|
|
22
|
+
# The custom-header codec lived under this name until the server transport came to share it;
|
|
23
|
+
# the name stays for code that referenced it.
|
|
24
|
+
# @api private
|
|
25
|
+
McpParamHeaders = MCP::McpParamHeaders
|
|
26
|
+
|
|
21
27
|
class ServerError < StandardError
|
|
22
28
|
attr_reader :code, :data
|
|
23
29
|
|
|
@@ -114,6 +120,9 @@ module MCP
|
|
|
114
120
|
STATE_ONLY_BACKOFF_INITIAL_SECONDS = 0.05
|
|
115
121
|
STATE_ONLY_BACKOFF_CAP_SECONDS = 0.25
|
|
116
122
|
|
|
123
|
+
# The Skills extension methods (SEP-2640): `list_skills`, `get_skill`, `read_skill_resource`, `skills_capability`.
|
|
124
|
+
include Skills
|
|
125
|
+
|
|
117
126
|
# Initializes a new MCP::Client instance.
|
|
118
127
|
#
|
|
119
128
|
# @param transport [Object] The transport object to use for communication with the server.
|
|
@@ -144,6 +153,8 @@ module MCP
|
|
|
144
153
|
# a SEP-2322 `input_required` result.
|
|
145
154
|
@input_required_handlers = {}
|
|
146
155
|
@input_required_max_rounds = input_required_max_rounds
|
|
156
|
+
# Skills extension state (SEP-2640), bound to the connection by `connect`; see `Client::Skills`.
|
|
157
|
+
@skills_state = Skills::State.new
|
|
147
158
|
end
|
|
148
159
|
|
|
149
160
|
# The user may want to access additional transport-specific methods/attributes
|
|
@@ -217,7 +228,15 @@ module MCP
|
|
|
217
228
|
|
|
218
229
|
effective_mode = resolve_connect_mode(mode, protocol_version)
|
|
219
230
|
|
|
220
|
-
|
|
231
|
+
# Skills state (SEP-2640) follows the connection: a handshake about to run starts a new generation, its
|
|
232
|
+
# result is recorded only if no newer handshake started meanwhile, and a failure clears readiness under
|
|
233
|
+
# the same condition, so a stale completion or failure changes nothing. The bundled transports answer
|
|
234
|
+
# a repeated `connect` with their cached handshake result without any I/O, so when the transport is
|
|
235
|
+
# connected and Skills is ready nothing is reset: the latches of a live connection stay put.
|
|
236
|
+
generation = @skills_state.begin_handshake unless skills_ready_on_connected_transport?
|
|
237
|
+
completed = false
|
|
238
|
+
|
|
239
|
+
result = if effective_mode == :legacy
|
|
221
240
|
transport.connect(
|
|
222
241
|
client_info: client_info,
|
|
223
242
|
protocol_version: protocol_version,
|
|
@@ -231,6 +250,11 @@ module MCP
|
|
|
231
250
|
mode: effective_mode,
|
|
232
251
|
)
|
|
233
252
|
end
|
|
253
|
+
completed = true
|
|
254
|
+
@skills_state.handshake_completed(generation, result) if generation
|
|
255
|
+
result
|
|
256
|
+
ensure
|
|
257
|
+
@skills_state.handshake_failed(generation) if generation && !completed
|
|
234
258
|
end
|
|
235
259
|
|
|
236
260
|
# Sends `server/discover` (MCP 2026-07-28, SEP-2575): sessionless capability discovery
|
|
@@ -295,10 +319,14 @@ module MCP
|
|
|
295
319
|
|
|
296
320
|
Tool.new(
|
|
297
321
|
name: tool["name"],
|
|
322
|
+
title: tool["title"],
|
|
298
323
|
description: tool["description"],
|
|
299
324
|
input_schema: tool["inputSchema"],
|
|
300
325
|
output_schema: tool["outputSchema"],
|
|
301
326
|
annotations: tool["annotations"],
|
|
327
|
+
icons: tool["icons"],
|
|
328
|
+
execution: tool["execution"],
|
|
329
|
+
meta: tool["_meta"],
|
|
302
330
|
)
|
|
303
331
|
end
|
|
304
332
|
|
|
@@ -673,10 +701,19 @@ module MCP
|
|
|
673
701
|
scan = McpParamHeaders.scan(tool["inputSchema"])
|
|
674
702
|
return false if scan[:valid]
|
|
675
703
|
|
|
676
|
-
|
|
704
|
+
name = tool["name"]
|
|
705
|
+
# Both pieces are the server's: `dump` keeps control characters and bidirectional overrides out of the log line.
|
|
706
|
+
warn("MCP::Client: excluding tool #{(name.is_a?(String) ? name : name.inspect).dump} from tools/list: #{scan[:reason].dump}")
|
|
677
707
|
true
|
|
678
708
|
end
|
|
679
709
|
|
|
710
|
+
# Whether a `connect` would only hand back the transport's cached handshake: the transport reports itself
|
|
711
|
+
# connected and Skills already recorded a handshake of this connection. A transport without connection
|
|
712
|
+
# state always starts a new generation.
|
|
713
|
+
def skills_ready_on_connected_transport?
|
|
714
|
+
transport.respond_to?(:connected?) && transport.connected? && @skills_state.ready?
|
|
715
|
+
end
|
|
716
|
+
|
|
680
717
|
# Resolves the effective SEP-2575 lifecycle mode for `connect`:
|
|
681
718
|
#
|
|
682
719
|
# - An explicit `protocol_version` from a legacy generation pins the legacy handshake without a probe,
|
data/lib/mcp/instrumentation.rb
CHANGED
|
@@ -5,16 +5,17 @@ module MCP
|
|
|
5
5
|
def instrument_call(method, server_context: {}, exception_already_reported: nil, &block)
|
|
6
6
|
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
7
7
|
begin
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
instrumentation_data = {}
|
|
9
|
+
@instrumentation_data = instrumentation_data
|
|
10
|
+
instrumentation_data.merge!(method: method)
|
|
10
11
|
# `self.` is required: the `server_context:` keyword above shadows the
|
|
11
12
|
# reader, and the value we want here is the user-defined hash passed to
|
|
12
13
|
# `Server.new`, not the per-call reporter context.
|
|
13
14
|
if configuration.instrument_server_context? && respond_to?(:server_context)
|
|
14
|
-
|
|
15
|
+
instrumentation_data.merge!(server_context: self.server_context)
|
|
15
16
|
end
|
|
16
17
|
|
|
17
|
-
result = configuration.around_request.call(
|
|
18
|
+
result = configuration.around_request.call(instrumentation_data) { block.call(instrumentation_data) }
|
|
18
19
|
|
|
19
20
|
result
|
|
20
21
|
rescue => e
|
|
@@ -28,22 +29,26 @@ module MCP
|
|
|
28
29
|
end
|
|
29
30
|
|
|
30
31
|
unless already_reported
|
|
31
|
-
|
|
32
|
+
instrumentation_data.merge!(error: :internal_error) unless instrumentation_data.key?(:error)
|
|
32
33
|
configuration.exception_reporter.call(e, server_context)
|
|
33
34
|
end
|
|
34
35
|
|
|
35
36
|
raise
|
|
36
37
|
ensure
|
|
37
38
|
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
38
|
-
|
|
39
|
+
instrumentation_data.merge!(duration: end_time - start_time)
|
|
39
40
|
|
|
40
41
|
# Backward compatibility: `instrumentation_callback` is soft-deprecated
|
|
41
42
|
# in favor of `around_request`, but existing callers still expect it
|
|
42
43
|
# to fire after every request until it is removed in a future version.
|
|
43
|
-
configuration.instrumentation_callback.call(
|
|
44
|
+
configuration.instrumentation_callback.call(instrumentation_data)
|
|
44
45
|
end
|
|
45
46
|
end
|
|
46
47
|
|
|
48
|
+
# @deprecated Use the data hash passed to the {#instrument_call} block or
|
|
49
|
+
# `around_request` instead. `add_instrumentation_data` uses a shared record
|
|
50
|
+
# and is not safe for concurrent calls.
|
|
51
|
+
# @see #instrument_call
|
|
47
52
|
def add_instrumentation_data(**kwargs)
|
|
48
53
|
@instrumentation_data.merge!(kwargs)
|
|
49
54
|
end
|