ask-mcp 0.4.5 → 0.5.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/CHANGELOG.md +69 -0
- data/lib/ask/mcp/adapters/tool_server.rb +27 -0
- data/lib/ask/mcp/client.rb +46 -3
- data/lib/ask/mcp/server/core.rb +372 -0
- data/lib/ask/mcp/server/http.rb +344 -0
- data/lib/ask/mcp/server/stdio.rb +16 -292
- data/lib/ask/mcp/server.rb +18 -0
- data/lib/ask/mcp/transport/streamable_http.rb +27 -15
- data/lib/ask/mcp/version.rb +1 -1
- metadata +4 -2
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "base64"
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
6
|
+
module Ask
|
|
7
|
+
module MCP
|
|
8
|
+
class Server
|
|
9
|
+
# MCP server over the stateless Streamable HTTP transport (2026-07-28).
|
|
10
|
+
#
|
|
11
|
+
# A Rack application. Every JSON-RPC message is one HTTP POST to the
|
|
12
|
+
# endpoint and the server answers with a single application/json object.
|
|
13
|
+
# The 2026-07-28 revision removed protocol-level sessions, the GET
|
|
14
|
+
# stream endpoint and SSE resumability, so nothing here is stateful —
|
|
15
|
+
# each request is dispatched by a fresh Core.
|
|
16
|
+
#
|
|
17
|
+
# mount Ask::MCP::Server.rack_app(name: "anychat", tools: [...]) => "/mcp"
|
|
18
|
+
#
|
|
19
|
+
# Multi-tenant servers resolve tools per request. `authenticate`
|
|
20
|
+
# receives the Rack env on every POST and returns whatever the host uses
|
|
21
|
+
# to identify the caller; a nil return rejects the request with 401.
|
|
22
|
+
# `tools` then receives that value:
|
|
23
|
+
#
|
|
24
|
+
# HTTP.new(
|
|
25
|
+
# name: "anychat",
|
|
26
|
+
# authenticate: ->(env) { User.find_by_token(bearer_token(env)) },
|
|
27
|
+
# tools: ->(user) { user ? Tools.for(user) : [] }
|
|
28
|
+
# )
|
|
29
|
+
#
|
|
30
|
+
# Two spec obligations are deliberately left to the host or to a later
|
|
31
|
+
# revision, and are called out here so nobody assumes otherwise:
|
|
32
|
+
#
|
|
33
|
+
# * Requests that carry `Mcp-Param-{Name}` headers are checked for legal
|
|
34
|
+
# field values, but not matched against the call arguments. Matching
|
|
35
|
+
# needs the calling tool's `x-mcp-header` annotations; no tool in this
|
|
36
|
+
# ecosystem declares them yet, so the condition cannot arise.
|
|
37
|
+
# * Notifications raised while handling a request are dropped rather than
|
|
38
|
+
# streamed ahead of the response. This transport answers with
|
|
39
|
+
# application/json rather than text/event-stream, and the 2026-07-28
|
|
40
|
+
# channel for server-initiated changes is the opt-in
|
|
41
|
+
# subscriptions/listen stream, which is not implemented.
|
|
42
|
+
class HTTP
|
|
43
|
+
ERROR_CODES = Native::Messages::ErrorCodes
|
|
44
|
+
|
|
45
|
+
NAME_BEARING_METHODS = %w[tools/call resources/read prompts/get].freeze
|
|
46
|
+
JSON_CONTENT_TYPE = "application/json"
|
|
47
|
+
MAX_BODY_BYTES = 1_048_576
|
|
48
|
+
# Values that are not plain visible ASCII travel base64-encoded inside
|
|
49
|
+
# this sentinel form (2026-07-28, SEP-2243). The markers are
|
|
50
|
+
# case-sensitive.
|
|
51
|
+
BASE64_SENTINEL = "=?base64?"
|
|
52
|
+
BASE64_SENTINEL_END = "?="
|
|
53
|
+
PARAM_HEADER_PREFIX = "HTTP_MCP_PARAM_"
|
|
54
|
+
|
|
55
|
+
# @param allowed_origins [Array<String>, :any, nil] origins permitted to
|
|
56
|
+
# call this endpoint. nil — the default — trusts no browser origin,
|
|
57
|
+
# which still admits server-to-server clients because they send no
|
|
58
|
+
# Origin header at all.
|
|
59
|
+
def initialize(name:, version: nil, tools: [], capabilities: { tools: {} },
|
|
60
|
+
resources: {}, prompts: {}, resource_templates: {},
|
|
61
|
+
debug: false, tool_timeout: nil, cache_ttl_ms: 60_000,
|
|
62
|
+
cache_scope: "private", authenticate: nil, context: nil,
|
|
63
|
+
allowed_origins: nil)
|
|
64
|
+
@name = name
|
|
65
|
+
@tools = tools
|
|
66
|
+
@authenticate = authenticate
|
|
67
|
+
@context = context
|
|
68
|
+
@debug = debug
|
|
69
|
+
@allowed_origins = allowed_origins
|
|
70
|
+
@core_options = {
|
|
71
|
+
name: name,
|
|
72
|
+
version: version,
|
|
73
|
+
capabilities: capabilities,
|
|
74
|
+
resources: resources,
|
|
75
|
+
prompts: prompts,
|
|
76
|
+
resource_templates: resource_templates,
|
|
77
|
+
debug: debug,
|
|
78
|
+
tool_timeout: tool_timeout,
|
|
79
|
+
cache_ttl_ms: cache_ttl_ms,
|
|
80
|
+
cache_scope: cache_scope
|
|
81
|
+
}
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Rack interface.
|
|
85
|
+
def call(env)
|
|
86
|
+
return forbidden unless origin_allowed?(env)
|
|
87
|
+
return method_not_allowed(env) unless post?(env)
|
|
88
|
+
|
|
89
|
+
if @authenticate
|
|
90
|
+
context = @authenticate.call(env)
|
|
91
|
+
return unauthorized unless context
|
|
92
|
+
|
|
93
|
+
return dispatch(env, context)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
dispatch(env, context_for(env))
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
private
|
|
100
|
+
|
|
101
|
+
def dispatch(env, context)
|
|
102
|
+
raw = read_body(env)
|
|
103
|
+
return error_response(400, ERROR_CODES::PARSE_ERROR, "Empty request body") if raw.strip.empty?
|
|
104
|
+
|
|
105
|
+
message = JSON.parse(raw, symbolize_names: true)
|
|
106
|
+
unless message.is_a?(Hash)
|
|
107
|
+
return error_response(400, ERROR_CODES::INVALID_REQUEST,
|
|
108
|
+
"Expected a single JSON-RPC object (batching is not part of this revision)")
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# A revision this server cannot speak is a transport-level failure
|
|
112
|
+
# with its own error and status, so it is settled before validation.
|
|
113
|
+
declared_version = env["HTTP_MCP_PROTOCOL_VERSION"].to_s
|
|
114
|
+
if !declared_version.empty? && !Ask::MCP::SUPPORTED_PROTOCOL_VERSIONS.include?(declared_version)
|
|
115
|
+
return unsupported_version_response(declared_version, message[:id])
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# The header requirements are specified for requests; a notification
|
|
119
|
+
# is not one, and this revision defines no client-to-server
|
|
120
|
+
# notifications over this transport, so notifications are accepted
|
|
121
|
+
# without them.
|
|
122
|
+
if message.key?(:id)
|
|
123
|
+
violation = transport_violation(env, message)
|
|
124
|
+
return error_response(400, ERROR_CODES::HEADER_MISMATCH, violation, id: message[:id]) if violation
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
core = build_core(context)
|
|
128
|
+
core.handle_message(message)
|
|
129
|
+
render(core.outbox)
|
|
130
|
+
rescue JSON::ParserError => e
|
|
131
|
+
error_response(400, ERROR_CODES::PARSE_ERROR, "Parse error: #{e.message}")
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Servers MUST validate the Origin header and refuse an Origin they do
|
|
135
|
+
# not trust (2026-07-28): without this, a remote page can reach a
|
|
136
|
+
# local MCP endpoint through DNS rebinding.
|
|
137
|
+
def origin_allowed?(env)
|
|
138
|
+
origin = env["HTTP_ORIGIN"].to_s
|
|
139
|
+
return true if origin.empty?
|
|
140
|
+
return false if @allowed_origins.nil?
|
|
141
|
+
|
|
142
|
+
@allowed_origins == :any || Array(@allowed_origins).include?(origin)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def transport_violation(env, message)
|
|
146
|
+
protocol_violation(env, message) ||
|
|
147
|
+
method_violation(env, message) ||
|
|
148
|
+
name_violation(env, message) ||
|
|
149
|
+
param_value_violation(env)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# MCP-Protocol-Version is required on every POST and must agree with
|
|
153
|
+
# the version the body declares in `_meta`: the body is authoritative,
|
|
154
|
+
# the header exists so intermediaries can route without parsing it.
|
|
155
|
+
# A request with no `_meta` version is a client expecting the removed
|
|
156
|
+
# `initialize` handshake, which this stateless endpoint cannot honour.
|
|
157
|
+
def protocol_violation(env, message)
|
|
158
|
+
declared = env["HTTP_MCP_PROTOCOL_VERSION"].to_s
|
|
159
|
+
return "Missing required MCP-Protocol-Version header" if declared.empty?
|
|
160
|
+
|
|
161
|
+
body_version = Core.declared_protocol_version(message[:params] || {})
|
|
162
|
+
if body_version.nil?
|
|
163
|
+
return "MCP-Protocol-Version #{declared.inspect} has no matching " \
|
|
164
|
+
"#{Native::Messages::Meta::PROTOCOL_VERSION_KEY} in params._meta"
|
|
165
|
+
end
|
|
166
|
+
return nil if declared == body_version
|
|
167
|
+
|
|
168
|
+
"MCP-Protocol-Version #{declared.inspect} does not match body value #{body_version.inspect}"
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# Mcp-Method is required for all requests (2026-07-28, SEP-2243).
|
|
172
|
+
def method_violation(env, message)
|
|
173
|
+
method = message[:method].to_s
|
|
174
|
+
declared = env["HTTP_MCP_METHOD"].to_s
|
|
175
|
+
return "Missing required Mcp-Method header" if declared.empty?
|
|
176
|
+
return nil if declared == method
|
|
177
|
+
|
|
178
|
+
"Mcp-Method header #{declared.inspect} does not match method #{method.inspect}"
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Mcp-Name is required for the methods whose body carries a name.
|
|
182
|
+
def name_violation(env, message)
|
|
183
|
+
method = message[:method].to_s
|
|
184
|
+
return nil unless NAME_BEARING_METHODS.include?(method)
|
|
185
|
+
|
|
186
|
+
params = message[:params] || {}
|
|
187
|
+
expected = (params[:name] || params[:uri]).to_s
|
|
188
|
+
# A call missing its name is reported by the tool layer, with the
|
|
189
|
+
# error shape that layer already owns.
|
|
190
|
+
return nil if expected.empty?
|
|
191
|
+
|
|
192
|
+
declared = decode_header_value(env["HTTP_MCP_NAME"])
|
|
193
|
+
return "Missing required Mcp-Name header for #{method}" if declared.nil? || declared.empty?
|
|
194
|
+
return nil if declared == expected
|
|
195
|
+
|
|
196
|
+
"Mcp-Name header #{declared.inspect} does not match name #{expected.inspect}"
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# Mirrored tool-parameter headers must carry legal field values, in
|
|
200
|
+
# base64 sentinel form when they cannot (2026-07-28, SEP-2243).
|
|
201
|
+
def param_value_violation(env)
|
|
202
|
+
env.each do |key, value|
|
|
203
|
+
next unless key.start_with?(PARAM_HEADER_PREFIX)
|
|
204
|
+
next if header_value_legal?(value)
|
|
205
|
+
|
|
206
|
+
return "#{header_name_for(key)} contains characters that require base64 encoding"
|
|
207
|
+
end
|
|
208
|
+
nil
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def header_value_legal?(value)
|
|
212
|
+
return base64_encoded?(value) if value.start_with?(BASE64_SENTINEL)
|
|
213
|
+
|
|
214
|
+
value.each_char.all? do |char|
|
|
215
|
+
char.ord == 0x09 || char.ord.between?(0x20, 0x7e)
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def base64_encoded?(value)
|
|
220
|
+
return false unless value.end_with?(BASE64_SENTINEL_END)
|
|
221
|
+
|
|
222
|
+
Base64.strict_decode64(sentinel_payload(value))
|
|
223
|
+
true
|
|
224
|
+
rescue ArgumentError
|
|
225
|
+
false
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# Header values that are not plain visible ASCII travel base64-encoded
|
|
229
|
+
# in the =?base64?...?= sentinel form; servers MUST decode before
|
|
230
|
+
# comparing them with the body. Base64 carries bytes and knows nothing
|
|
231
|
+
# of encodings, and the spec defines the payload as the UTF-8
|
|
232
|
+
# representation, so the decoded string is tagged as such — otherwise
|
|
233
|
+
# it compares unequal to the same text from the body.
|
|
234
|
+
def decode_header_value(value)
|
|
235
|
+
return nil if value.nil?
|
|
236
|
+
return value unless value.start_with?(BASE64_SENTINEL)
|
|
237
|
+
|
|
238
|
+
Base64.strict_decode64(sentinel_payload(value)).force_encoding(Encoding::UTF_8)
|
|
239
|
+
rescue ArgumentError
|
|
240
|
+
value
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def sentinel_payload(value)
|
|
244
|
+
value[BASE64_SENTINEL.length..-(BASE64_SENTINEL_END.length + 1)]
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# Rack folds header names into HTTP_MCP_PARAM_REGION; fold it back for
|
|
248
|
+
# a message a human can act on.
|
|
249
|
+
def header_name_for(key)
|
|
250
|
+
key.delete_prefix("HTTP_").split("_").map(&:capitalize).join("-")
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def build_core(context)
|
|
254
|
+
Core.new(**@core_options, tools: resolve_tools(context))
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def resolve_tools(context)
|
|
258
|
+
return @tools unless @tools.respond_to?(:call)
|
|
259
|
+
|
|
260
|
+
@tools.call(context) || []
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def context_for(env)
|
|
264
|
+
@context ? @context.call(env) : env
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def render(outbox)
|
|
268
|
+
response = outbox.reverse.find { |message| message.key?(:id) }
|
|
269
|
+
dropped = outbox.count { |message| !message.key?(:id) }
|
|
270
|
+
debug_log "Dropped #{dropped} notification(s): no channel for them on this transport" if dropped.positive?
|
|
271
|
+
|
|
272
|
+
# A message with no id is a notification: accepted, nothing to say.
|
|
273
|
+
return [202, json_headers, []] if response.nil?
|
|
274
|
+
|
|
275
|
+
[status_for(response), json_headers, [JSON.generate(response)]]
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# An RPC the server does not implement is a transport-level 404 with a
|
|
279
|
+
# JSON-RPC -32601 body (2026-07-28); every other outcome is 200, with
|
|
280
|
+
# failures inside the JSON-RPC envelope where they belong.
|
|
281
|
+
def status_for(response)
|
|
282
|
+
response.dig(:error, :code) == ERROR_CODES::METHOD_NOT_FOUND ? 404 : 200
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def read_body(env)
|
|
286
|
+
input = env["rack.input"]
|
|
287
|
+
return "" unless input
|
|
288
|
+
|
|
289
|
+
input.read(MAX_BODY_BYTES).to_s
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def post?(env)
|
|
293
|
+
env["REQUEST_METHOD"] == "POST"
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def json_headers
|
|
297
|
+
{ "Content-Type" => JSON_CONTENT_TYPE }
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def method_not_allowed(env)
|
|
301
|
+
rpc_error(405, ERROR_CODES::METHOD_NOT_FOUND,
|
|
302
|
+
"Method not allowed: #{env['REQUEST_METHOD']} (this endpoint accepts POST)")
|
|
303
|
+
.tap { |triple| triple[1] = triple[1].merge("Allow" => "POST") }
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def unsupported_version_response(version, id)
|
|
307
|
+
rpc_error(400, ERROR_CODES::UNSUPPORTED_PROTOCOL_VERSION,
|
|
308
|
+
"Unsupported protocol version: #{version}",
|
|
309
|
+
id: id, data: { supported: Ask::MCP::SUPPORTED_PROTOCOL_VERSIONS })
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
def forbidden
|
|
313
|
+
rpc_error(403, ERROR_CODES::INVALID_REQUEST, "Origin not allowed")
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
def unauthorized
|
|
317
|
+
rpc_error(401, ERROR_CODES::AUTH_ERROR, "Unauthorized")
|
|
318
|
+
.tap { |triple| triple[1] = triple[1].merge("WWW-Authenticate" => "Bearer") }
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def error_response(status, code, message, id: nil)
|
|
322
|
+
rpc_error(status, code, message, id: id)
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
# A rejection that the server could tie to a request echoes that
|
|
326
|
+
# request's id, so a JSON-RPC client can correlate the failure with
|
|
327
|
+
# what it sent. Failures raised before the body is understood — and the
|
|
328
|
+
# 403 for an untrusted Origin — carry no id, which the spec allows.
|
|
329
|
+
def rpc_error(status, code, message, id: nil, data: nil)
|
|
330
|
+
error = { code: code, message: message }
|
|
331
|
+
error[:data] = data if data
|
|
332
|
+
body = { jsonrpc: "2.0", id: id, error: error }
|
|
333
|
+
[status, json_headers, [JSON.generate(body)]]
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
def debug_log(message)
|
|
337
|
+
return unless @debug
|
|
338
|
+
|
|
339
|
+
warn "[ask-mcp] [#{@name}] #{message}"
|
|
340
|
+
end
|
|
341
|
+
end
|
|
342
|
+
end
|
|
343
|
+
end
|
|
344
|
+
end
|
data/lib/ask/mcp/server/stdio.rb
CHANGED
|
@@ -1,42 +1,21 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "json"
|
|
4
|
-
require "timeout"
|
|
5
|
-
|
|
6
3
|
module Ask
|
|
7
4
|
module MCP
|
|
8
5
|
class Server
|
|
9
|
-
# MCP server over stdio transport.
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
# MCP server over the stdio transport.
|
|
7
|
+
#
|
|
8
|
+
# Reads newline-delimited JSON-RPC on stdin and writes replies to
|
|
9
|
+
# stdout as they are produced. All message handling lives in Core;
|
|
10
|
+
# this class adds the read loop and the stdout writer.
|
|
11
|
+
class Stdio < Core
|
|
12
12
|
# Deprecated: use Ask::MCP::PROTOCOL_VERSION (the canonical constant).
|
|
13
13
|
PROTOCOL_VERSION = Ask::MCP::PROTOCOL_VERSION
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def initialize(name:, tools: [], capabilities: {}, resources: {}, prompts: {},
|
|
18
|
-
resource_templates: {}, debug: false, tool_timeout: nil,
|
|
19
|
-
cache_ttl_ms: 60_000, cache_scope: "private", version: nil)
|
|
20
|
-
@name = name
|
|
21
|
-
@server_version = version || Ask::MCP::VERSION
|
|
22
|
-
@capabilities = capabilities
|
|
23
|
-
@resources = resources
|
|
24
|
-
@prompts = prompts
|
|
25
|
-
@resource_templates = resource_templates
|
|
26
|
-
@debug = debug
|
|
27
|
-
@tool_timeout = tool_timeout
|
|
28
|
-
@cache_ttl_ms = cache_ttl_ms
|
|
29
|
-
@cache_scope = cache_scope
|
|
30
|
-
|
|
31
|
-
@adapter = Adapters::ToolServer.new(tools || [])
|
|
32
|
-
@initialized = false
|
|
15
|
+
def initialize(**options)
|
|
16
|
+
super
|
|
33
17
|
@running = false
|
|
34
18
|
@shutdown_requested = false
|
|
35
|
-
@result_cache = {}
|
|
36
|
-
# Negotiated protocol version. nil until the client tells us which
|
|
37
|
-
# revision it speaks (legacy `initialize` or stateless `_meta`).
|
|
38
|
-
@protocol_version = nil
|
|
39
|
-
@stateless = false
|
|
40
19
|
end
|
|
41
20
|
|
|
42
21
|
def start
|
|
@@ -54,6 +33,7 @@ module Ask
|
|
|
54
33
|
while @running && !@shutdown_requested && (line = $stdin.gets)
|
|
55
34
|
line = line.strip
|
|
56
35
|
next if line.empty?
|
|
36
|
+
|
|
57
37
|
process_line(line)
|
|
58
38
|
end
|
|
59
39
|
|
|
@@ -95,6 +75,12 @@ module Ask
|
|
|
95
75
|
|
|
96
76
|
private
|
|
97
77
|
|
|
78
|
+
# Static stdio peers have no request/response framing, so each message
|
|
79
|
+
# is written the moment the Core produces it.
|
|
80
|
+
def deliver(message)
|
|
81
|
+
$stdout.puts(JSON.generate(message))
|
|
82
|
+
end
|
|
83
|
+
|
|
98
84
|
def graceful_shutdown
|
|
99
85
|
@shutdown_requested = true
|
|
100
86
|
# Close stdin to unblock $stdin.gets so the signal handler
|
|
@@ -106,269 +92,7 @@ module Ask
|
|
|
106
92
|
msg = JSON.parse(line, symbolize_names: true)
|
|
107
93
|
handle_message(msg)
|
|
108
94
|
rescue JSON::ParserError => e
|
|
109
|
-
send_error(nil,
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
def handle_message(msg)
|
|
113
|
-
method = msg[:method]
|
|
114
|
-
id = msg[:id]
|
|
115
|
-
params = msg[:params] || {}
|
|
116
|
-
has_id = msg.key?(:id)
|
|
117
|
-
|
|
118
|
-
# Stateless (2026-07-28) requests carry the protocol version in
|
|
119
|
-
# `_meta` instead of an `initialize` handshake. Detecting it here
|
|
120
|
-
# unlocks all handlers without the legacy @initialized gate.
|
|
121
|
-
if (meta_version = meta_protocol_version(params))
|
|
122
|
-
@protocol_version = meta_version
|
|
123
|
-
@stateless = true
|
|
124
|
-
@initialized = true
|
|
125
|
-
debug_log "Stateless request (protocol #{meta_version})"
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
case method
|
|
129
|
-
when "initialize"
|
|
130
|
-
handle_initialize(id, params)
|
|
131
|
-
when "server/discover"
|
|
132
|
-
handle_discover(id)
|
|
133
|
-
when "notifications/initialized"
|
|
134
|
-
@initialized = true
|
|
135
|
-
debug_log "Client initialized"
|
|
136
|
-
when "tools/list"
|
|
137
|
-
return send_error(id, -32000, "Server not initialized") unless @initialized
|
|
138
|
-
handle_tools_list(id)
|
|
139
|
-
when "tools/call"
|
|
140
|
-
return send_error(id, -32000, "Server not initialized") unless @initialized
|
|
141
|
-
handle_tool_call(id, params)
|
|
142
|
-
when "resources/list"
|
|
143
|
-
return send_error(id, -32000, "Server not initialized") unless @initialized
|
|
144
|
-
handle_resources_list(id)
|
|
145
|
-
when "resources/read"
|
|
146
|
-
return send_error(id, -32000, "Server not initialized") unless @initialized
|
|
147
|
-
handle_resource_read(id, params)
|
|
148
|
-
when "resources/templates/list"
|
|
149
|
-
return send_error(id, -32000, "Server not initialized") unless @initialized
|
|
150
|
-
handle_resources_templates_list(id)
|
|
151
|
-
when "prompts/list"
|
|
152
|
-
return send_error(id, -32000, "Server not initialized") unless @initialized
|
|
153
|
-
handle_prompts_list(id)
|
|
154
|
-
when "prompts/get"
|
|
155
|
-
return send_error(id, -32000, "Server not initialized") unless @initialized
|
|
156
|
-
handle_prompt_get(id, params)
|
|
157
|
-
when "ping"
|
|
158
|
-
# ping was removed in 2026-07-28; legacy clients still use it.
|
|
159
|
-
if stateless_mode?
|
|
160
|
-
send_error(id, -32601, "Method not found: ping") if has_id
|
|
161
|
-
else
|
|
162
|
-
send_result(id, {}) if has_id
|
|
163
|
-
end
|
|
164
|
-
else
|
|
165
|
-
debug_log "Unknown method: #{method}"
|
|
166
|
-
send_error(id, -32601, "Method not found: #{method}") if has_id
|
|
167
|
-
end
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
# server/discover (2026-07-28): advertise supported protocol versions,
|
|
171
|
-
# capabilities, and identity. Clients call it before anything else to
|
|
172
|
-
# select a version (or as a backward-compat probe on stdio).
|
|
173
|
-
def handle_discover(id)
|
|
174
|
-
send_result(id, {
|
|
175
|
-
protocolVersions: Ask::MCP::SUPPORTED_PROTOCOL_VERSIONS,
|
|
176
|
-
capabilities: @capabilities,
|
|
177
|
-
serverInfo: { name: @name, version: @server_version }
|
|
178
|
-
})
|
|
179
|
-
debug_log "server/discover answered"
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
def handle_initialize(id, params)
|
|
183
|
-
@initialized = true
|
|
184
|
-
@protocol_version = params[:protocolVersion] || Ask::MCP::PROTOCOL_VERSION
|
|
185
|
-
client_version = params[:protocolVersion] || Ask::MCP::PROTOCOL_VERSION
|
|
186
|
-
debug_log "Handling initialize (id=#{id.inspect}, version=#{client_version})"
|
|
187
|
-
send_result(id, {
|
|
188
|
-
protocolVersion: client_version,
|
|
189
|
-
capabilities: @capabilities,
|
|
190
|
-
serverInfo: {
|
|
191
|
-
name: @name,
|
|
192
|
-
version: @server_version
|
|
193
|
-
}
|
|
194
|
-
})
|
|
195
|
-
debug_log "Initialize complete"
|
|
196
|
-
end
|
|
197
|
-
|
|
198
|
-
def handle_tools_list(id)
|
|
199
|
-
defs = @adapter.definitions
|
|
200
|
-
debug_log "tools/list returning #{defs.length} tool definitions"
|
|
201
|
-
send_result(id, cacheable({ tools: defs }))
|
|
202
|
-
end
|
|
203
|
-
|
|
204
|
-
def handle_resources_list(id)
|
|
205
|
-
defs = @resources.values.map { |r| resource_to_h(r) }
|
|
206
|
-
debug_log "resources/list returning #{defs.length} resources"
|
|
207
|
-
send_result(id, cacheable({ resources: defs }))
|
|
208
|
-
end
|
|
209
|
-
|
|
210
|
-
def handle_resources_templates_list(id)
|
|
211
|
-
defs = @resource_templates.values.map { |t| template_to_h(t) }
|
|
212
|
-
debug_log "resources/templates/list returning #{defs.length} templates"
|
|
213
|
-
send_result(id, cacheable({ resourceTemplates: defs }))
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
def handle_resource_read(id, params)
|
|
217
|
-
uri = params[:uri].to_s
|
|
218
|
-
resource = @resources[uri]
|
|
219
|
-
if resource.nil?
|
|
220
|
-
code = stateless_mode? ? -32_602 : Native::Messages::ErrorCodes::RESOURCE_NOT_FOUND
|
|
221
|
-
return send_error(id, code, "Resource not found: #{uri}")
|
|
222
|
-
end
|
|
223
|
-
|
|
224
|
-
contents = if resource.respond_to?(:content)
|
|
225
|
-
resource.content
|
|
226
|
-
elsif resource.respond_to?(:read)
|
|
227
|
-
resource.read
|
|
228
|
-
else
|
|
229
|
-
[{ uri: uri, text: "" }]
|
|
230
|
-
end
|
|
231
|
-
send_result(id, cacheable({ contents: contents }))
|
|
232
|
-
end
|
|
233
|
-
|
|
234
|
-
def handle_prompts_list(id)
|
|
235
|
-
defs = @prompts.values.map { |p| prompt_to_h(p) }
|
|
236
|
-
debug_log "prompts/list returning #{defs.length} prompts"
|
|
237
|
-
send_result(id, cacheable({ prompts: defs }))
|
|
238
|
-
end
|
|
239
|
-
|
|
240
|
-
def handle_prompt_get(id, params)
|
|
241
|
-
name = params[:name].to_s
|
|
242
|
-
prompt = @prompts[name]
|
|
243
|
-
if prompt.nil?
|
|
244
|
-
return send_error(id, Native::Messages::ErrorCodes::PROMPT_NOT_FOUND, "Prompt not found: #{name}")
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
messages = prompt.respond_to?(:messages) ? prompt.messages : []
|
|
248
|
-
send_result(id, { messages: messages })
|
|
249
|
-
end
|
|
250
|
-
|
|
251
|
-
def handle_tool_call(id, params)
|
|
252
|
-
cache_key = id.to_s
|
|
253
|
-
|
|
254
|
-
# Return cached result for retried requests (same ID, already processed)
|
|
255
|
-
if @result_cache.key?(cache_key)
|
|
256
|
-
debug_log "Returning cached result for id=#{id}"
|
|
257
|
-
return send_result(id, @result_cache[cache_key])
|
|
258
|
-
end
|
|
259
|
-
|
|
260
|
-
tool_name = params[:name].to_s
|
|
261
|
-
arguments = params[:arguments] || {}
|
|
262
|
-
|
|
263
|
-
debug_log "Handling tools/call: #{tool_name} (id=#{id.inspect})"
|
|
264
|
-
|
|
265
|
-
result = if @tool_timeout
|
|
266
|
-
Timeout.timeout(@tool_timeout) { @adapter.call(tool_name, arguments) }
|
|
267
|
-
else
|
|
268
|
-
@adapter.call(tool_name, arguments)
|
|
269
|
-
end
|
|
270
|
-
|
|
271
|
-
@result_cache[cache_key] = result
|
|
272
|
-
trim_cache
|
|
273
|
-
|
|
274
|
-
send_result(id, result)
|
|
275
|
-
rescue Timeout::Error
|
|
276
|
-
debug_log "Tool call timed out: #{tool_name}"
|
|
277
|
-
send_result(id, {
|
|
278
|
-
content: [{ type: "text", text: "Tool call timed out: #{tool_name}" }],
|
|
279
|
-
isError: true
|
|
280
|
-
})
|
|
281
|
-
end
|
|
282
|
-
|
|
283
|
-
# Serialize a resource object for resources/list. Prefers to_h (the
|
|
284
|
-
# Resource value object emits title/icons/description/mimeType);
|
|
285
|
-
# otherwise builds the shape from duck-typed accessors.
|
|
286
|
-
def resource_to_h(resource)
|
|
287
|
-
return resource.to_h if resource.respond_to?(:to_h)
|
|
288
|
-
|
|
289
|
-
h = { uri: resource.uri, name: resource.name }
|
|
290
|
-
h[:title] = resource.title if resource.respond_to?(:title) && resource.title
|
|
291
|
-
h[:description] = resource.description if resource.respond_to?(:description) && resource.description
|
|
292
|
-
h[:mimeType] = resource.mime_type if resource.respond_to?(:mime_type) && resource.mime_type
|
|
293
|
-
h[:icons] = resource.icons if resource.respond_to?(:icons) && resource.icons&.any?
|
|
294
|
-
h
|
|
295
|
-
end
|
|
296
|
-
|
|
297
|
-
def template_to_h(template)
|
|
298
|
-
return template.to_h if template.respond_to?(:to_h)
|
|
299
|
-
|
|
300
|
-
h = { uriTemplate: template.uri_template, name: template.name }
|
|
301
|
-
h[:title] = template.title if template.respond_to?(:title) && template.title
|
|
302
|
-
h[:mimeType] = template.mime_type if template.respond_to?(:mime_type) && template.mime_type
|
|
303
|
-
h[:icons] = template.icons if template.respond_to?(:icons) && template.icons&.any?
|
|
304
|
-
h
|
|
305
|
-
end
|
|
306
|
-
|
|
307
|
-
def prompt_to_h(prompt)
|
|
308
|
-
return prompt.to_h if prompt.respond_to?(:to_h)
|
|
309
|
-
|
|
310
|
-
h = { name: prompt.name }
|
|
311
|
-
h[:title] = prompt.title if prompt.respond_to?(:title) && prompt.title
|
|
312
|
-
h[:description] = prompt.description if prompt.respond_to?(:description) && prompt.description
|
|
313
|
-
h[:arguments] = prompt.arguments if prompt.respond_to?(:arguments) && prompt.arguments&.any?
|
|
314
|
-
h[:icons] = prompt.icons if prompt.respond_to?(:icons) && prompt.icons&.any?
|
|
315
|
-
h
|
|
316
|
-
end
|
|
317
|
-
|
|
318
|
-
def send_result(id, result)
|
|
319
|
-
# 2026-07-28: all results carry `resultType`. Legacy peers tolerate
|
|
320
|
-
# the field, but we only add it for stateless peers to keep the
|
|
321
|
-
# legacy wire output unchanged.
|
|
322
|
-
result = result.merge(resultType: "complete") if stateless_mode?
|
|
323
|
-
$stdout.puts({ jsonrpc: "2.0", id: id, result: result }.to_json)
|
|
324
|
-
end
|
|
325
|
-
|
|
326
|
-
# Write a server→client notification (no id). stdout is sync'd in
|
|
327
|
-
# #start, so this is safe to call from any thread.
|
|
328
|
-
def send_notification(method, params = {})
|
|
329
|
-
msg = { jsonrpc: "2.0", method: method }
|
|
330
|
-
msg[:params] = params unless params.empty?
|
|
331
|
-
$stdout.puts(msg.to_json)
|
|
332
|
-
end
|
|
333
|
-
|
|
334
|
-
# 2026-07-28 CacheableResult: freshness hints (ttlMs) and scope
|
|
335
|
-
# (public/private) on list/read results so clients and shared
|
|
336
|
-
# intermediaries may cache them. Only emitted for stateless peers.
|
|
337
|
-
def cacheable(result)
|
|
338
|
-
return result unless stateless_mode?
|
|
339
|
-
result.merge(ttlMs: @cache_ttl_ms, cacheScope: @cache_scope)
|
|
340
|
-
end
|
|
341
|
-
|
|
342
|
-
# True once a 2026-07-28 stateless peer has been detected.
|
|
343
|
-
def stateless_mode?
|
|
344
|
-
@protocol_version == Ask::MCP::LATEST_PROTOCOL_VERSION
|
|
345
|
-
end
|
|
346
|
-
|
|
347
|
-
# Read the protocol version a stateless client advertises in params
|
|
348
|
-
# `_meta`. Returns nil for legacy requests. Handles both symbol and
|
|
349
|
-
# string key forms (the JSON parser symbolizes all keys).
|
|
350
|
-
def meta_protocol_version(params)
|
|
351
|
-
meta = params[:meta] || params[:_meta] || {}
|
|
352
|
-
meta_value(meta, Native::Messages::Meta::PROTOCOL_VERSION_KEY)
|
|
353
|
-
end
|
|
354
|
-
|
|
355
|
-
def meta_value(meta, key)
|
|
356
|
-
meta[key] || meta[key.to_sym] || meta[key.to_s]
|
|
357
|
-
end
|
|
358
|
-
|
|
359
|
-
def send_error(id, code, message)
|
|
360
|
-
$stdout.puts({ jsonrpc: "2.0", id: id, error: { code: code, message: message } }.to_json)
|
|
361
|
-
end
|
|
362
|
-
|
|
363
|
-
def debug_log(msg)
|
|
364
|
-
return unless @debug
|
|
365
|
-
ts = Time.now.strftime("%H:%M:%S.%L")
|
|
366
|
-
$stderr.puts "[#{ts}] [ask-mcp] #{msg}"
|
|
367
|
-
end
|
|
368
|
-
|
|
369
|
-
def trim_cache
|
|
370
|
-
return if @result_cache.size <= MAX_RESULT_CACHE
|
|
371
|
-
@result_cache.shift(@result_cache.size - MAX_RESULT_CACHE)
|
|
95
|
+
send_error(nil, Native::Messages::ErrorCodes::PARSE_ERROR, "Parse error: #{e.message}")
|
|
372
96
|
end
|
|
373
97
|
end
|
|
374
98
|
end
|