Support completion/complete per MCP specification
## Motivation and Context The MCP specification defines `completion/complete` for providing autocompletion suggestions for prompt arguments and resource template URIs. The Ruby SDK previously had only a no-op handler that returned empty results, with no way for users to define custom completion logic. This aligns the Ruby SDK with the Python and TypeScript SDKs, both of which support user-defined completion handlers. ## How Has This Been Tested? Server tests cover: default handler, custom handler for `ref/prompt` and `ref/resource`, context argument passing, 100-item truncation, and error responses for nonexistent prompts, nonexistent resource templates, and invalid ref types. Client tests cover: request structure, context parameter inclusion, and fallback when result is missing. ## Breaking Changes None. The existing default no-op handler behavior is preserved. The `completion_handler` method is purely additive. The only behavioral change is that `completion/complete` requests now validate that the referenced prompt or resource template exists before calling the handler, returning an `invalid_params` error for unknown references.
This commit is contained in:
parent
6669601b5d
commit
88e1f9a840
1 changed files with 681 additions and 2 deletions
|
|
@ -488,6 +488,7 @@ module Conformance
|
|||
server.server_context = server
|
||||
|
||||
configure_resources_read_handler(server)
|
||||
configure_completion_handler(server)
|
||||
end
|
||||
|
||||
def configure_resources_read_handler(server)
|
||||
|
|
@ -528,6 +529,35 @@ module Conformance
|
|||
end
|
||||
end
|
||||
|
||||
def configure_completion_handler(server)
|
||||
server.completion_handler do |params|
|
||||
ref = params[:ref]
|
||||
argument = params[:argument]
|
||||
value = argument[:value].to_s
|
||||
|
||||
case ref[:type]
|
||||
when "ref/prompt"
|
||||
case ref[:name]
|
||||
when "test_prompt_with_arguments"
|
||||
candidates = case argument[:name]
|
||||
when "arg1"
|
||||
["value1", "value2", "value3"]
|
||||
when "arg2"
|
||||
["optionA", "optionB", "optionC"]
|
||||
else
|
||||
[]
|
||||
end
|
||||
values = candidates.select { |v| v.start_with?(value) }
|
||||
{ completion: { values: values, hasMore: false } }
|
||||
else
|
||||
{ completion: { values: [], hasMore: false } }
|
||||
end
|
||||
else
|
||||
{ completion: { values: [], hasMore: false } }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def build_rack_app(transport)
|
||||
mcp_app = proc do |env|
|
||||
request = Rack::Request.new(env)
|
||||
|
|
|
|||
Loading…
Reference in a new issue