mcp 1.11.0 → 1.12.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/lib/mcp/client/http.rb +3 -2
- data/lib/mcp/content.rb +12 -0
- data/lib/mcp/server/transports/streamable_http_transport.rb +408 -109
- data/lib/mcp/server.rb +214 -30
- data/lib/mcp/server_session.rb +52 -7
- data/lib/mcp/version.rb +1 -1
- metadata +2 -2
data/lib/mcp/server.rb
CHANGED
|
@@ -185,8 +185,8 @@ module MCP
|
|
|
185
185
|
Methods::SKILLS_GET,
|
|
186
186
|
].freeze
|
|
187
187
|
|
|
188
|
-
attr_accessor :description, :name, :title, :version, :website_url, :instructions, :tools, :prompts, :
|
|
189
|
-
attr_reader :icons, :resources, :page_size, :client_capabilities, :ttl_ms, :cache_scope, :request_state_security, :capabilities
|
|
188
|
+
attr_accessor :description, :name, :title, :version, :website_url, :instructions, :tools, :prompts, :server_context, :configuration, :transport, :logging_message_notification
|
|
189
|
+
attr_reader :icons, :resources, :resource_templates, :page_size, :client_capabilities, :ttl_ms, :cache_scope, :request_state_security, :capabilities
|
|
190
190
|
# The skills registered with `skills:` as a frozen Array (an empty one when only `skills_handlers` configured
|
|
191
191
|
# the extension), or nil when the server serves no skills.
|
|
192
192
|
attr_reader :skills
|
|
@@ -250,9 +250,12 @@ module MCP
|
|
|
250
250
|
@skills_list_handler = nil
|
|
251
251
|
@skills_get_handler = nil
|
|
252
252
|
@skills_methods_registered = false
|
|
253
|
+
ensure_required_members!(resources, :uri, :name)
|
|
254
|
+
ensure_required_members!(resource_templates, :uriTemplate, :name)
|
|
253
255
|
ensure_no_skill_file_collision!(resources.map(&:uri))
|
|
254
256
|
@resource_index = index_resources_by_uri(resources)
|
|
255
257
|
@resources_list_handler = nil
|
|
258
|
+
@tools_list_handler = nil
|
|
256
259
|
@server_context = server_context
|
|
257
260
|
self.page_size = page_size
|
|
258
261
|
self.ttl_ms = ttl_ms
|
|
@@ -311,10 +314,14 @@ module MCP
|
|
|
311
314
|
# @param auth_info [Server::OAuth::AccessToken, nil] The verified access token
|
|
312
315
|
# for the current request, passed by the transport layer and exposed to
|
|
313
316
|
# handlers as `server_context.auth_info`.
|
|
317
|
+
# @param tool_call_check [#call, nil] For a transport that has to compare a `tools/call` with the schema of
|
|
318
|
+
# its tool (the custom header check of the streamable HTTP transport): called with the tool the call resolved to
|
|
319
|
+
# and the request params, after the tool is found and before its arguments are examined. A message it returns
|
|
320
|
+
# refuses the call with `-32020` Header mismatch, and the tool does not run. Not part of the public API.
|
|
314
321
|
# @return [Hash, nil] The JSON-RPC response, or `nil` for notifications.
|
|
315
|
-
def handle(request, session: nil, auth_info: nil)
|
|
322
|
+
def handle(request, session: nil, auth_info: nil, tool_call_check: nil)
|
|
316
323
|
JsonRpcHandler.handle(request, id_validation_pattern: nil) do |method, request_id|
|
|
317
|
-
handle_request(request, method, session: session, related_request_id: request_id, auth_info: auth_info)
|
|
324
|
+
handle_request(request, method, session: session, related_request_id: request_id, auth_info: auth_info, tool_call_check: tool_call_check)
|
|
318
325
|
end
|
|
319
326
|
end
|
|
320
327
|
|
|
@@ -357,6 +364,7 @@ module MCP
|
|
|
357
364
|
mime_type: mime_type, annotations: annotations, size: size, meta: meta,
|
|
358
365
|
&block
|
|
359
366
|
)
|
|
367
|
+
ensure_required_members!([resource], :uri, :name)
|
|
360
368
|
ensure_no_skill_file_collision!([resource.uri])
|
|
361
369
|
@resources << resource
|
|
362
370
|
@resource_index[resource.uri] = resource
|
|
@@ -370,12 +378,19 @@ module MCP
|
|
|
370
378
|
mime_type: mime_type, annotations: annotations, meta: meta,
|
|
371
379
|
&block
|
|
372
380
|
)
|
|
381
|
+
ensure_required_members!([resource_template], :uriTemplate, :name)
|
|
373
382
|
@resource_templates << resource_template
|
|
374
383
|
|
|
375
384
|
validate!
|
|
376
385
|
end
|
|
377
386
|
|
|
387
|
+
def resource_templates=(resource_templates)
|
|
388
|
+
ensure_required_members!(resource_templates, :uriTemplate, :name)
|
|
389
|
+
@resource_templates = resource_templates
|
|
390
|
+
end
|
|
391
|
+
|
|
378
392
|
def resources=(resources)
|
|
393
|
+
ensure_required_members!(resources, :uri, :name)
|
|
379
394
|
ensure_no_skill_file_collision!(resources.map(&:uri))
|
|
380
395
|
@resources = resources
|
|
381
396
|
@resource_index = index_resources_by_uri(resources)
|
|
@@ -476,6 +491,50 @@ module MCP
|
|
|
476
491
|
@handlers[Methods::NOTIFICATIONS_ROOTS_LIST_CHANGED] = block
|
|
477
492
|
end
|
|
478
493
|
|
|
494
|
+
# Sets a custom handler for the tools visible to a request, letting them depend on request context such as
|
|
495
|
+
# the authenticated principal or granted scope. The block returns the tool collection for the request;
|
|
496
|
+
# the framework paginates it for `tools/list` and stamps the same cache hints it applies to the registered tools,
|
|
497
|
+
# so the block returns only the array, not the paginated result.
|
|
498
|
+
# A block that declares a `server_context:` keyword receives an `MCP::ServerContext`. When no handler is set,
|
|
499
|
+
# the registered tools are used unchanged.
|
|
500
|
+
#
|
|
501
|
+
# The returned collection is both what `tools/list` advertises and what `tools/call` resolves against:
|
|
502
|
+
# a tool the block leaves out is refused as not found, and a tool it returns is callable
|
|
503
|
+
# whether or not it is registered on the server.
|
|
504
|
+
#
|
|
505
|
+
# The block is invoked once per `tools/list` page and once per `tools/call`, so it should be cheap and
|
|
506
|
+
# must return a stable ordering across the pages of one logical query; the cursor is a positional offset
|
|
507
|
+
# into the returned collection.
|
|
508
|
+
#
|
|
509
|
+
# @yield [params, server_context:] The `tools/list` or `tools/call` request params,
|
|
510
|
+
# and an `MCP::ServerContext` when declared.
|
|
511
|
+
# @yieldreturn [Array<MCP::Tool>] The tools visible to the request.
|
|
512
|
+
def tools_list_handler(&block)
|
|
513
|
+
@tools_list_handler = block
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
# The tool a `tools/call` naming `name` resolves to when that can be told before the request is dispatched:
|
|
517
|
+
# `nil` for a name that is not registered, and always with a `tools_list_handler`, whose answer belongs to
|
|
518
|
+
# the dispatch that asks for it.
|
|
519
|
+
#
|
|
520
|
+
# @api private
|
|
521
|
+
def tool_known_ahead_of_dispatch(name)
|
|
522
|
+
tools[name] unless @tools_list_handler
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
# Whether a request for `method` reaches a handler, by the two checks the dispatch makes first: a handler is registered,
|
|
526
|
+
# and the capability the method belongs to is declared.
|
|
527
|
+
#
|
|
528
|
+
# @api private
|
|
529
|
+
def implements_method?(method)
|
|
530
|
+
return false unless @handlers[method]
|
|
531
|
+
|
|
532
|
+
Methods.ensure_capability!(method, effective_capabilities)
|
|
533
|
+
true
|
|
534
|
+
rescue Methods::MissingRequiredCapabilityError
|
|
535
|
+
false
|
|
536
|
+
end
|
|
537
|
+
|
|
479
538
|
# Sets a custom handler for `resources/list` requests, letting the visible list depend on request context such as
|
|
480
539
|
# the authenticated principal or granted scope. The block returns the resource collection to serve;
|
|
481
540
|
# the framework paginates it and stamps SEP-2549 cache hints exactly as it does for the constructor-provided resources,
|
|
@@ -717,7 +776,7 @@ module MCP
|
|
|
717
776
|
end
|
|
718
777
|
end
|
|
719
778
|
|
|
720
|
-
def handle_request(request, method, session: nil, related_request_id: nil, auth_info: nil)
|
|
779
|
+
def handle_request(request, method, session: nil, related_request_id: nil, auth_info: nil, tool_call_check: nil)
|
|
721
780
|
# A well-formed notification carries no JSON-RPC id and receives no response.
|
|
722
781
|
# If a client erroneously sends a notification-only method with an id, the message
|
|
723
782
|
# is framed as a request; since notification methods have no request handler,
|
|
@@ -799,13 +858,19 @@ module MCP
|
|
|
799
858
|
end
|
|
800
859
|
end
|
|
801
860
|
|
|
802
|
-
->(params)
|
|
861
|
+
->(params) do
|
|
803
862
|
reported_exception = nil
|
|
804
863
|
instrument_call(
|
|
805
864
|
method,
|
|
806
865
|
server_context: { request: request },
|
|
807
866
|
exception_already_reported: ->(e) { reported_exception.equal?(e) },
|
|
808
867
|
) do |instrumentation_data|
|
|
868
|
+
# Cancelled before its handler starts, as a request is when its session ended while it was being admitted.
|
|
869
|
+
if cancellation&.cancelled?
|
|
870
|
+
instrumentation_data.merge!(cancelled: true, cancellation_reason: cancellation.reason)
|
|
871
|
+
next JsonRpcHandler::NO_RESPONSE
|
|
872
|
+
end
|
|
873
|
+
|
|
809
874
|
refuse_non_object_meta!(params)
|
|
810
875
|
envelope = lift_request_envelope(params, method: method, session: session)
|
|
811
876
|
|
|
@@ -835,12 +900,15 @@ module MCP
|
|
|
835
900
|
|
|
836
901
|
subscription_result(handler_result)
|
|
837
902
|
when Methods::TOOLS_CALL
|
|
838
|
-
|
|
903
|
+
validate_object_params!(params)
|
|
904
|
+
call_tool(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation, envelope: envelope, auth_info: auth_info, tool_call_check: tool_call_check)
|
|
839
905
|
when Methods::PROMPTS_GET
|
|
906
|
+
validate_object_params!(params)
|
|
840
907
|
get_prompt(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation, envelope: envelope, auth_info: auth_info)
|
|
841
908
|
when Methods::COMPLETION_COMPLETE
|
|
842
909
|
complete(params, session: session, related_request_id: related_request_id, cancellation: cancellation, envelope: envelope, auth_info: auth_info)
|
|
843
910
|
when Methods::LOGGING_SET_LEVEL
|
|
911
|
+
validate_object_params!(params)
|
|
844
912
|
configure_logging_level(params, session: session)
|
|
845
913
|
else
|
|
846
914
|
dispatch_optional_context_handler(@handlers[method], params, session: session, related_request_id: related_request_id, cancellation: cancellation, envelope: envelope, auth_info: auth_info)
|
|
@@ -865,6 +933,7 @@ module MCP
|
|
|
865
933
|
session: session,
|
|
866
934
|
related_request_id: related_request_id,
|
|
867
935
|
cancellation: cancellation,
|
|
936
|
+
auth_info: auth_info,
|
|
868
937
|
)
|
|
869
938
|
else
|
|
870
939
|
serialize_input_required_result(result, instrumentation_data: instrumentation_data, envelope: envelope, request: params, method: method)
|
|
@@ -901,27 +970,35 @@ module MCP
|
|
|
901
970
|
# HTTP-level 403 challenges remain the transport `required_scopes` gate's job.
|
|
902
971
|
report_exception(e, { request: request })
|
|
903
972
|
instrumentation_data.merge!(error: :insufficient_scope)
|
|
973
|
+
next cancelled_response(cancellation, instrumentation_data) if cancellation&.cancelled?
|
|
974
|
+
|
|
904
975
|
converted = RequestHandlerError.new(e.message, request, error_type: :invalid_request, original_error: e)
|
|
905
976
|
reported_exception = converted
|
|
906
977
|
raise converted
|
|
907
978
|
rescue RequestHandlerError => e
|
|
908
979
|
report_exception(e.original_error || e, { request: request })
|
|
909
980
|
instrumentation_data.merge!(error: e.error_type)
|
|
981
|
+
next cancelled_response(cancellation, instrumentation_data) if cancellation&.cancelled?
|
|
982
|
+
|
|
910
983
|
reported_exception = e
|
|
911
984
|
raise e
|
|
912
985
|
rescue => e
|
|
913
986
|
report_exception(e, { request: request })
|
|
914
987
|
instrumentation_data.merge!(error: :internal_error)
|
|
988
|
+
next cancelled_response(cancellation, instrumentation_data) if cancellation&.cancelled?
|
|
989
|
+
|
|
915
990
|
wrapped = RequestHandlerError.new("Internal error handling #{method} request", request, original_error: e)
|
|
916
991
|
reported_exception = wrapped
|
|
917
992
|
raise wrapped
|
|
918
|
-
ensure
|
|
919
|
-
# `cancellation` is non-nil exactly when this request claimed the id above, so this also keeps `initialize`
|
|
920
|
-
# (which never registers) from evicting an in-flight registration under a reused id when the duplicate-`initialize`
|
|
921
|
-
# refusal raises out of the handler.
|
|
922
|
-
session&.unregister_in_flight(related_request_id, cancellation: cancellation) if related_request_id && cancellation
|
|
923
993
|
end
|
|
924
|
-
|
|
994
|
+
ensure
|
|
995
|
+
# Outside the instrumented block, which `configuration.around_request` may never run.
|
|
996
|
+
#
|
|
997
|
+
# `cancellation` is non-nil exactly when this request claimed the id above, so this also keeps `initialize`
|
|
998
|
+
# (which never registers) from evicting an in-flight registration under a reused id when the duplicate-`initialize`
|
|
999
|
+
# refusal raises out of the handler.
|
|
1000
|
+
session&.unregister_in_flight(related_request_id, cancellation: cancellation) if related_request_id && cancellation
|
|
1001
|
+
end
|
|
925
1002
|
end
|
|
926
1003
|
|
|
927
1004
|
# Whether this request belongs to the modern lifecycle, used to refuse the RPCs SEP-2575 removed from it.
|
|
@@ -1026,7 +1103,7 @@ module MCP
|
|
|
1026
1103
|
serialized = serialized.merge(requestState: @request_state_security.seal(
|
|
1027
1104
|
serialized[:requestState],
|
|
1028
1105
|
method: method,
|
|
1029
|
-
target: mrtr_target(request),
|
|
1106
|
+
target: mrtr_target(request, method: method),
|
|
1030
1107
|
arguments_digest: mrtr_arguments_digest(request),
|
|
1031
1108
|
))
|
|
1032
1109
|
end
|
|
@@ -1048,7 +1125,7 @@ module MCP
|
|
|
1048
1125
|
# re-runs with `inputResponses`/`requestState` merged into the original params - the same deterministic replay
|
|
1049
1126
|
# contract the modern client driver follows. The `requestState` round-trips in-process as the raw value
|
|
1050
1127
|
# the handler wrote; `RequestStateSecurity` sealing is wire hardening and does not apply.
|
|
1051
|
-
def run_legacy_input_required_shim(result, instrumentation_data:, method:, params:, session:, related_request_id:, cancellation:)
|
|
1128
|
+
def run_legacy_input_required_shim(result, instrumentation_data:, method:, params:, session:, related_request_id:, cancellation:, auth_info:)
|
|
1052
1129
|
rounds = 0
|
|
1053
1130
|
|
|
1054
1131
|
loop do
|
|
@@ -1065,7 +1142,12 @@ module MCP
|
|
|
1065
1142
|
)
|
|
1066
1143
|
end
|
|
1067
1144
|
|
|
1145
|
+
# `handle_request` checks for cancellation only before the shim starts, so the shim checks again before each leg,
|
|
1146
|
+
# before re-running the handler, and before returning its result: a client's `notifications/cancelled` can arrive
|
|
1147
|
+
# after it answered a leg or while the handler re-runs. `CancelledError` reaches the rescue a cancelled nested
|
|
1148
|
+
# request does, which suppresses the response.
|
|
1068
1149
|
responses = (result.input_requests || {}).each_with_object({}) do |(key, entry), collected|
|
|
1150
|
+
cancellation&.raise_if_cancelled!
|
|
1069
1151
|
collected[key] = session.fulfill_input_request(
|
|
1070
1152
|
entry[:method],
|
|
1071
1153
|
legacy_leg_params(entry),
|
|
@@ -1077,6 +1159,7 @@ module MCP
|
|
|
1077
1159
|
retry_params[:inputResponses] = responses unless responses.empty?
|
|
1078
1160
|
retry_params[:requestState] = result.request_state if result.request_state
|
|
1079
1161
|
|
|
1162
|
+
cancellation&.raise_if_cancelled!
|
|
1080
1163
|
result = redispatch_mrtr_method(
|
|
1081
1164
|
method,
|
|
1082
1165
|
retry_params,
|
|
@@ -1084,7 +1167,9 @@ module MCP
|
|
|
1084
1167
|
session: session,
|
|
1085
1168
|
related_request_id: related_request_id,
|
|
1086
1169
|
cancellation: cancellation,
|
|
1170
|
+
auth_info: auth_info,
|
|
1087
1171
|
)
|
|
1172
|
+
cancellation&.raise_if_cancelled!
|
|
1088
1173
|
return result unless result.is_a?(InputRequiredResult)
|
|
1089
1174
|
|
|
1090
1175
|
rounds += 1
|
|
@@ -1113,15 +1198,16 @@ module MCP
|
|
|
1113
1198
|
end
|
|
1114
1199
|
|
|
1115
1200
|
# Re-runs the handler of one of the three MRTR-capable methods for
|
|
1116
|
-
# the legacy shim. Legacy wire, so no envelope is threaded.
|
|
1117
|
-
|
|
1201
|
+
# the legacy shim. Legacy wire, so no envelope is threaded. The access token is threaded: every round belongs to
|
|
1202
|
+
# the one authenticated request, so the handler sees the same `auth_info` on each.
|
|
1203
|
+
def redispatch_mrtr_method(method, params, instrumentation_data:, session:, related_request_id:, cancellation:, auth_info:)
|
|
1118
1204
|
case method
|
|
1119
1205
|
when Methods::TOOLS_CALL
|
|
1120
|
-
call_tool(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation)
|
|
1206
|
+
call_tool(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation, auth_info: auth_info)
|
|
1121
1207
|
when Methods::PROMPTS_GET
|
|
1122
|
-
get_prompt(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation)
|
|
1208
|
+
get_prompt(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation, auth_info: auth_info)
|
|
1123
1209
|
when Methods::RESOURCES_READ
|
|
1124
|
-
contents = read_resource_contents(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation)
|
|
1210
|
+
contents = read_resource_contents(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation, auth_info: auth_info)
|
|
1125
1211
|
contents.is_a?(InputRequiredResult) ? contents : build_read_resource_result(contents)
|
|
1126
1212
|
else
|
|
1127
1213
|
raise RequestHandlerError.new(
|
|
@@ -1145,7 +1231,7 @@ module MCP
|
|
|
1145
1231
|
plaintext = @request_state_security.unseal(
|
|
1146
1232
|
sealed,
|
|
1147
1233
|
method: method,
|
|
1148
|
-
target: mrtr_target(params),
|
|
1234
|
+
target: mrtr_target(params, method: method),
|
|
1149
1235
|
arguments_digest: mrtr_arguments_digest(params),
|
|
1150
1236
|
)
|
|
1151
1237
|
key = params.key?("requestState") ? "requestState" : :requestState
|
|
@@ -1160,10 +1246,14 @@ module MCP
|
|
|
1160
1246
|
)
|
|
1161
1247
|
end
|
|
1162
1248
|
|
|
1163
|
-
|
|
1249
|
+
# What a sealed state is bound to besides the method and the arguments: the resource URI of a `resources/read`,
|
|
1250
|
+
# the tool or prompt name otherwise. The member is fixed per method, so a `name` added to the `params` of
|
|
1251
|
+
# a `resources/read` cannot stand in for the URI and carry a state over to another resource.
|
|
1252
|
+
def mrtr_target(params, method:)
|
|
1164
1253
|
return "" unless params.is_a?(Hash)
|
|
1165
1254
|
|
|
1166
|
-
|
|
1255
|
+
member = method == Methods::RESOURCES_READ ? "uri" : "name"
|
|
1256
|
+
params[member.to_sym] || params[member] || ""
|
|
1167
1257
|
end
|
|
1168
1258
|
|
|
1169
1259
|
# Digest of the originating arguments, binding a sealed state to retries of
|
|
@@ -1311,6 +1401,15 @@ module MCP
|
|
|
1311
1401
|
meta.is_a?(Hash) ? { _meta: meta } : {}
|
|
1312
1402
|
end
|
|
1313
1403
|
|
|
1404
|
+
# `tools/call`, `prompts/get` and `logging/setLevel` read named members of `params`. JSON-RPC also allows
|
|
1405
|
+
# an array there, and a request may leave `params` out, so neither is refused before it gets here;
|
|
1406
|
+
# without this check the first read raised and the client was answered Internal error for its own mistake.
|
|
1407
|
+
def validate_object_params!(params)
|
|
1408
|
+
return if params.is_a?(Hash)
|
|
1409
|
+
|
|
1410
|
+
raise RequestHandlerError.new("Invalid params", params, error_type: :invalid_params)
|
|
1411
|
+
end
|
|
1412
|
+
|
|
1314
1413
|
def validate_initialize_params!(params)
|
|
1315
1414
|
unless params.is_a?(Hash)
|
|
1316
1415
|
raise RequestHandlerError.new("Invalid params", params, error_type: :invalid_params)
|
|
@@ -1407,23 +1506,79 @@ module MCP
|
|
|
1407
1506
|
{}
|
|
1408
1507
|
end
|
|
1409
1508
|
|
|
1410
|
-
def list_tools(request)
|
|
1411
|
-
|
|
1509
|
+
def list_tools(request, server_context: nil)
|
|
1510
|
+
tools = if @tools_list_handler
|
|
1511
|
+
invoke_tools_list_handler(request, server_context)
|
|
1512
|
+
else
|
|
1513
|
+
@tools.values
|
|
1514
|
+
end
|
|
1515
|
+
|
|
1516
|
+
page = paginate(tools, cursor: cursor_from(request), page_size: @page_size, request: request, &:to_h)
|
|
1412
1517
|
|
|
1413
1518
|
apply_cache_metadata({ tools: page[:items], nextCursor: page[:next_cursor] }.compact)
|
|
1414
1519
|
end
|
|
1415
1520
|
|
|
1416
|
-
|
|
1521
|
+
# Calls the `tools_list_handler` block, forwarding `server_context:` only when the block opts in
|
|
1522
|
+
# by declaring the keyword (the same rule `dispatch_optional_context_handler` applies).
|
|
1523
|
+
def invoke_tools_list_handler(request, server_context)
|
|
1524
|
+
if handler_declares_server_context?(@tools_list_handler)
|
|
1525
|
+
@tools_list_handler.call(request, server_context: server_context)
|
|
1526
|
+
else
|
|
1527
|
+
@tools_list_handler.call(request)
|
|
1528
|
+
end
|
|
1529
|
+
end
|
|
1530
|
+
|
|
1531
|
+
def resolve_tool(request, session:, related_request_id:, cancellation:, envelope:, auth_info:)
|
|
1532
|
+
return tools[request[:name]] unless @tools_list_handler
|
|
1533
|
+
|
|
1534
|
+
server_context = if handler_declares_server_context?(@tools_list_handler)
|
|
1535
|
+
build_server_context(
|
|
1536
|
+
request: request,
|
|
1537
|
+
session: session,
|
|
1538
|
+
related_request_id: related_request_id,
|
|
1539
|
+
cancellation: cancellation,
|
|
1540
|
+
envelope: envelope,
|
|
1541
|
+
auth_info: auth_info,
|
|
1542
|
+
)
|
|
1543
|
+
end
|
|
1544
|
+
|
|
1545
|
+
invoke_tools_list_handler(request, server_context).find { |tool| tool.name_value == request[:name] }
|
|
1546
|
+
end
|
|
1547
|
+
|
|
1548
|
+
def call_tool(request, instrumentation_data:, session: nil, related_request_id: nil, cancellation: nil, envelope: nil, auth_info: nil, tool_call_check: nil)
|
|
1417
1549
|
tool_name = request[:name]
|
|
1418
1550
|
|
|
1419
|
-
tool =
|
|
1551
|
+
tool = resolve_tool(request, session: session, related_request_id: related_request_id, cancellation: cancellation, envelope: envelope, auth_info: auth_info)
|
|
1420
1552
|
unless tool
|
|
1421
1553
|
instrumentation_data.merge!(tool_name: tool_name, error: :tool_not_found)
|
|
1422
1554
|
|
|
1423
1555
|
raise RequestHandlerError.new("Tool not found: #{tool_name}", request, error_type: :invalid_params)
|
|
1424
1556
|
end
|
|
1425
1557
|
|
|
1426
|
-
|
|
1558
|
+
# The transport's check runs here, on the tool this very call resolved to, rather than on a lookup of its own:
|
|
1559
|
+
# with a `tools_list_handler`, a second resolution could see another context and answer for a schema the caller
|
|
1560
|
+
# was not served, or for a tool the caller cannot see. It comes after the lookup, so a tool that is not found
|
|
1561
|
+
# answers the same whatever the request carries, and before the arguments are examined or the tool runs.
|
|
1562
|
+
if tool_call_check && (mismatch = tool_call_check.call(tool, request))
|
|
1563
|
+
instrumentation_data.merge!(tool_name: tool_name)
|
|
1564
|
+
|
|
1565
|
+
raise RequestHandlerError.new(
|
|
1566
|
+
"Header mismatch: #{mismatch}",
|
|
1567
|
+
request,
|
|
1568
|
+
error_type: :invalid_request,
|
|
1569
|
+
error_code: ErrorCodes::HEADER_MISMATCH,
|
|
1570
|
+
)
|
|
1571
|
+
end
|
|
1572
|
+
|
|
1573
|
+
arguments = request[:arguments]
|
|
1574
|
+
unless arguments.nil? || arguments.is_a?(Hash)
|
|
1575
|
+
instrumentation_data.merge!(tool_name: tool_name, error: :invalid_params)
|
|
1576
|
+
|
|
1577
|
+
raise RequestHandlerError.new("Invalid arguments: expected an object", request, error_type: :invalid_params)
|
|
1578
|
+
end
|
|
1579
|
+
|
|
1580
|
+
arguments ||= {}
|
|
1581
|
+
|
|
1427
1582
|
instrumentation_data.merge!(tool_name: tool_name, tool_arguments: arguments)
|
|
1428
1583
|
|
|
1429
1584
|
if tool.input_schema&.missing_required_arguments?(arguments)
|
|
@@ -1512,6 +1667,12 @@ module MCP
|
|
|
1512
1667
|
instrumentation_data.merge!(prompt_name: prompt_name)
|
|
1513
1668
|
|
|
1514
1669
|
prompt_args = request[:arguments]
|
|
1670
|
+
unless prompt_args.nil? || prompt_args.is_a?(Hash)
|
|
1671
|
+
instrumentation_data.merge!(error: :invalid_params)
|
|
1672
|
+
|
|
1673
|
+
raise RequestHandlerError.new("Invalid arguments: expected an object", request, error_type: :invalid_params)
|
|
1674
|
+
end
|
|
1675
|
+
|
|
1515
1676
|
prompt.validate_arguments!(prompt_args)
|
|
1516
1677
|
|
|
1517
1678
|
server_context = build_server_context(
|
|
@@ -1988,10 +2149,33 @@ module MCP
|
|
|
1988
2149
|
configuration.exception_reporter.call(exception, server_context)
|
|
1989
2150
|
end
|
|
1990
2151
|
|
|
2152
|
+
# A cancelled request gets no response, so an error it raises once cancelled is reported but not answered.
|
|
2153
|
+
def cancelled_response(cancellation, instrumentation_data)
|
|
2154
|
+
instrumentation_data.merge!(cancelled: true, cancellation_reason: cancellation.reason)
|
|
2155
|
+
JsonRpcHandler::NO_RESPONSE
|
|
2156
|
+
end
|
|
2157
|
+
|
|
1991
2158
|
def index_resources_by_uri(resources)
|
|
1992
2159
|
resources.to_h { |resource| [resource.uri, resource] }
|
|
1993
2160
|
end
|
|
1994
2161
|
|
|
2162
|
+
# A resource or a resource template serializes without the members it was not given, so one registered without
|
|
2163
|
+
# a member the spec requires would be listed without it, in a result a client has every reason to reject whole.
|
|
2164
|
+
# Refused where it is registered instead, and named by what it does have.
|
|
2165
|
+
def ensure_required_members!(definitions, *members)
|
|
2166
|
+
definitions.each do |definition|
|
|
2167
|
+
serialized = definition.to_h
|
|
2168
|
+
missing = members.select { |member| serialized[member].nil? }
|
|
2169
|
+
next if missing.empty?
|
|
2170
|
+
|
|
2171
|
+
kind = members.include?(:uriTemplate) ? "resource template" : "resource"
|
|
2172
|
+
known = serialized.slice(*members).values.first
|
|
2173
|
+
verb = missing.size == 1 ? "is" : "are"
|
|
2174
|
+
|
|
2175
|
+
raise ArgumentError, "A #{kind} must have #{members.join(" and ")}#{" (#{known})" if known}: #{missing.join(", ")} #{verb} missing."
|
|
2176
|
+
end
|
|
2177
|
+
end
|
|
2178
|
+
|
|
1995
2179
|
def error_tool_response(text)
|
|
1996
2180
|
Tool::Response.new(
|
|
1997
2181
|
[{
|
|
@@ -2098,12 +2282,12 @@ module MCP
|
|
|
2098
2282
|
end
|
|
2099
2283
|
|
|
2100
2284
|
ref = params[:ref]
|
|
2101
|
-
if ref.
|
|
2285
|
+
if !ref.is_a?(Hash) || ref[:type].nil?
|
|
2102
2286
|
raise RequestHandlerError.new("Missing or invalid ref", params, error_type: :invalid_params)
|
|
2103
2287
|
end
|
|
2104
2288
|
|
|
2105
2289
|
argument = params[:argument]
|
|
2106
|
-
if argument.
|
|
2290
|
+
if !argument.is_a?(Hash) || argument[:name].nil? || !argument.key?(:value)
|
|
2107
2291
|
raise RequestHandlerError.new("Missing argument name or value", params, error_type: :invalid_params)
|
|
2108
2292
|
end
|
|
2109
2293
|
|
data/lib/mcp/server_session.rb
CHANGED
|
@@ -27,8 +27,11 @@ module MCP
|
|
|
27
27
|
@client_capabilities = nil
|
|
28
28
|
@logging_message_notification = nil
|
|
29
29
|
@protocol_version = nil
|
|
30
|
+
@incoming_cancelled = false
|
|
31
|
+
@incoming_cancel_reason = nil
|
|
30
32
|
@in_flight = {}
|
|
31
33
|
@in_flight_mutex = Mutex.new
|
|
34
|
+
@last_request_finished_at = nil
|
|
32
35
|
@initialized = false
|
|
33
36
|
end
|
|
34
37
|
|
|
@@ -63,18 +66,28 @@ module MCP
|
|
|
63
66
|
# and `notifications/cancelled` back to the request that caused them, so a second live request under the same id
|
|
64
67
|
# has no destination of its own. Rather than let the newcomer displace the registration, report the collision
|
|
65
68
|
# and leave the first request intact; the caller turns that into an Invalid Request.
|
|
69
|
+
#
|
|
70
|
+
# Once `cancel_all_incoming` has run, the token comes back already cancelled with its reason and is not tracked:
|
|
71
|
+
# a transport can hand a request to a session it is ending, and that request must not outlive the session.
|
|
66
72
|
def register_in_flight(request_id)
|
|
67
73
|
return if request_id.nil?
|
|
68
74
|
|
|
69
75
|
cancellation = Cancellation.new(request_id: request_id)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
cancel_reason = nil
|
|
77
|
+
admission = @in_flight_mutex.synchronize do
|
|
78
|
+
if @incoming_cancelled
|
|
79
|
+
cancel_reason = @incoming_cancel_reason
|
|
80
|
+
:cancelled
|
|
81
|
+
elsif @in_flight.key?(request_id)
|
|
82
|
+
nil
|
|
83
|
+
else
|
|
84
|
+
@in_flight[request_id] = cancellation
|
|
85
|
+
:registered
|
|
86
|
+
end
|
|
75
87
|
end
|
|
76
88
|
|
|
77
|
-
|
|
89
|
+
cancellation.cancel(reason: cancel_reason) if admission == :cancelled
|
|
90
|
+
cancellation if admission
|
|
78
91
|
end
|
|
79
92
|
|
|
80
93
|
# Removes an in-flight registration. Passing the `Cancellation` that `register_in_flight` returned removes
|
|
@@ -85,10 +98,19 @@ module MCP
|
|
|
85
98
|
@in_flight_mutex.synchronize do
|
|
86
99
|
next if cancellation && !@in_flight[request_id].equal?(cancellation)
|
|
87
100
|
|
|
88
|
-
@in_flight.delete(request_id)
|
|
101
|
+
@last_request_finished_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) if @in_flight.delete(request_id)
|
|
89
102
|
end
|
|
90
103
|
end
|
|
91
104
|
|
|
105
|
+
# What a transport needs to tell whether the session is idle, read in one step so the two cannot disagree:
|
|
106
|
+
# whether a request is still in flight, and the monotonic clock reading at which the last one finished
|
|
107
|
+
# (`nil` when none has).
|
|
108
|
+
#
|
|
109
|
+
# @api private
|
|
110
|
+
def in_flight_activity
|
|
111
|
+
@in_flight_mutex.synchronize { [!@in_flight.empty?, @last_request_finished_at] }
|
|
112
|
+
end
|
|
113
|
+
|
|
92
114
|
# Whether `request_id` is currently in flight, so a transport can refuse a colliding request
|
|
93
115
|
# before registering any state of its own for it.
|
|
94
116
|
def in_flight?(request_id)
|
|
@@ -106,6 +128,29 @@ module MCP
|
|
|
106
128
|
cancellation&.cancel(reason: reason)
|
|
107
129
|
end
|
|
108
130
|
|
|
131
|
+
# Flips the `Cancellation` of every request still in flight, for a transport ending the session they belong to:
|
|
132
|
+
# their handlers observe it as they would `notifications/cancelled`, and their responses are suppressed the same way.
|
|
133
|
+
# Requests registered afterwards are cancelled as they register. The cancellations run outside the registry lock,
|
|
134
|
+
# since each fires its `on_cancel` hooks synchronously. One that raises does not keep the rest from being cancelled:
|
|
135
|
+
# the first error is raised once all of them are.
|
|
136
|
+
def cancel_all_incoming(reason: nil)
|
|
137
|
+
cancellations = @in_flight_mutex.synchronize do
|
|
138
|
+
@incoming_cancelled = true
|
|
139
|
+
@incoming_cancel_reason = reason
|
|
140
|
+
@in_flight.values
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
first_error = nil
|
|
144
|
+
|
|
145
|
+
cancellations.each do |cancellation|
|
|
146
|
+
cancellation.cancel(reason: reason)
|
|
147
|
+
rescue StandardError => e
|
|
148
|
+
first_error ||= e
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
raise first_error if first_error
|
|
152
|
+
end
|
|
153
|
+
|
|
109
154
|
# Sends `notifications/cancelled` to the peer for a previously-issued request.
|
|
110
155
|
# Also unblocks any transport-level `send_request` waiting on a response for `request_id`.
|
|
111
156
|
def cancel_request(request_id:, reason: nil)
|
data/lib/mcp/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mcp
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.12.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Model Context Protocol
|
|
@@ -126,7 +126,7 @@ licenses:
|
|
|
126
126
|
- Apache-2.0
|
|
127
127
|
metadata:
|
|
128
128
|
allowed_push_host: https://rubygems.org
|
|
129
|
-
changelog_uri: https://github.com/modelcontextprotocol/ruby-sdk/releases/tag/v1.
|
|
129
|
+
changelog_uri: https://github.com/modelcontextprotocol/ruby-sdk/releases/tag/v1.12.0
|
|
130
130
|
homepage_uri: https://ruby.sdk.modelcontextprotocol.io
|
|
131
131
|
source_code_uri: https://github.com/modelcontextprotocol/ruby-sdk
|
|
132
132
|
bug_tracker_uri: https://github.com/modelcontextprotocol/ruby-sdk/issues
|