diff --git a/lib/mcp/prompt.rb b/lib/mcp/prompt.rb index 598a3d0d..c2ed2db0 100644 --- a/lib/mcp/prompt.rb +++ b/lib/mcp/prompt.rb @@ -111,8 +111,15 @@ def validate_arguments!(args) missing = required_args - args.keys return if missing.empty? + # The explicit `error_code` maps a missing prompt argument to Invalid Params (-32602) rather + # than the default Internal Error (-32603); a missing required argument is client input, not a + # server fault. `error_type: :missing_required_arguments` keeps the descriptive message and + # instrumentation label. Mirrors `MCP::Server::ResourceNotFoundError`. raise MCP::Server::RequestHandlerError.new( - "Missing required arguments: #{missing.join(", ")}", nil, error_type: :missing_required_arguments + "Missing required arguments: #{missing.join(", ")}", + nil, + error_type: :missing_required_arguments, + error_code: JsonRpcHandler::ErrorCode::INVALID_PARAMS, ) end diff --git a/lib/mcp/server.rb b/lib/mcp/server.rb index 5fa024be..14a0ec63 100644 --- a/lib/mcp/server.rb +++ b/lib/mcp/server.rb @@ -1260,7 +1260,16 @@ def get_prompt(request, session: nil, related_request_id: nil, cancellation: nil prompt = @prompts[prompt_name] unless prompt add_instrumentation_data(error: :prompt_not_found) - raise RequestHandlerError.new("Prompt not found #{prompt_name}", request, error_type: :prompt_not_found) + # The explicit `error_code` maps an unknown prompt to Invalid Params (-32602) rather than + # the default Internal Error (-32603), matching the `tools/call`, `resources/read`, and + # `completion/complete` siblings for the same not-found condition, while `error_type: + # :prompt_not_found` keeps the descriptive message and instrumentation label. + raise RequestHandlerError.new( + "Prompt not found #{prompt_name}", + request, + error_type: :prompt_not_found, + error_code: JsonRpcHandler::ErrorCode::INVALID_PARAMS, + ) end add_instrumentation_data(prompt_name: prompt_name) diff --git a/test/mcp/server_test.rb b/test/mcp/server_test.rb index 480827e9..3d897026 100644 --- a/test/mcp/server_test.rb +++ b/test/mcp/server_test.rb @@ -1395,6 +1395,9 @@ class Example < Tool } response = @server.handle(request) + # An unknown prompt is client input, so it maps to Invalid Params (-32602), not the + # default Internal Error (-32603); matches the tools/call and completion/complete siblings. + assert_equal JsonRpcHandler::ErrorCode::INVALID_PARAMS, response.dig(:error, :code) assert_equal("Prompt not found unknown_prompt", response[:error][:data]) assert_instrumentation_data({ method: "prompts/get", error: :prompt_not_found }) end @@ -1411,6 +1414,9 @@ class Example < Tool } response = @server.handle(request) + # A missing required argument is client input, so it maps to Invalid Params (-32602), + # not the default Internal Error (-32603). + assert_equal JsonRpcHandler::ErrorCode::INVALID_PARAMS, response.dig(:error, :code) assert_equal "Missing required arguments: test_argument", response[:error][:data] assert_instrumentation_data({ method: "prompts/get",